<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.zg.project.wisdom.mapper.DdTaskMapper">

    <!-- ======================= ResultMap ======================= -->
    <resultMap id="DdTaskResult" type="com.zg.project.wisdom.domain.DdTask">
        <result property="id" column="id"/>
        <result property="taskNo" column="task_no"/>
        <result property="taskDtl" column="task_dtl"/>
        <result property="taskType" column="task_type"/>
        <result property="taskStatus" column="task_status"/>
        <result property="midStatus" column="mid_status"/>
        <result property="mid" column="mid"/>
        <result property="num" column="num"/>
        <result property="midType" column="mid_type"/>
        <result property="sourceName" column="source_name"/>
        <result property="targetName" column="target_name"/>
        <result property="operator" column="operator"/>
        <result property="approver" column="approver"/>
        <result property="rcptim" column="rcptim"/>
        <result property="rid" column="rid"/>
        <result property="doCount" column="do_count"/>
        <result property="prf" column="prf"/>
        <result property="dispatchMode" column="dispatch_mode"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="isDelete" column="is_delete"/>
    </resultMap>

    <!-- ======================= 查询列表 ======================= -->
    <select id="selectDdTaskList" resultMap="DdTaskResult">
        SELECT *
        FROM dd_task
        <where>
            <if test="taskNo != null and taskNo != ''">
                AND task_no = #{taskNo}
            </if>
            <if test="taskType != null and taskType != ''">
                AND task_type = #{taskType}
            </if>
            <if test="taskStatus != null">
                AND task_status = #{taskStatus}
            </if>
            <if test="dispatchMode != null">
                AND dispatch_mode = #{dispatchMode}
            </if>
            <if test="isDelete != null">
                AND is_delete = #{isDelete}
            </if>
        </where>
        ORDER BY id DESC
    </select>

    <!-- ======================= 根据ID查询 ======================= -->
    <select id="selectDdTaskById" resultMap="DdTaskResult">
        SELECT *
        FROM dd_task
        WHERE id = #{id}
          AND is_delete = '0'
    </select>

    <!-- ======================= 根据任务号查询 ======================= -->
    <select id="selectByTaskNo" resultMap="DdTaskResult" parameterType="String">
        SELECT *
        FROM dd_task
        WHERE task_no = #{taskNo}
          AND is_delete = '0'
            LIMIT 1
    </select>

    <!-- ======================= 新增调度任务 ======================= -->
    <insert id="insertDdTask" parameterType="com.zg.project.wisdom.domain.DdTask">
        INSERT INTO dd_task (
            task_no,
            task_dtl,
            task_type,
            task_status,
            mid_status,
            mid,
            num,
            mid_type,
            source_name,
            target_name,
            operator,
            approver,
            rcptim,
            rid,
            do_count,
            prf,
            dispatch_mode,
            create_by,
            create_time,
            update_by,
            update_time,
            is_delete
        ) VALUES (
                     #{taskNo},
                     #{taskDtl},
                     #{taskType},
                     #{taskStatus},
                     #{midStatus},
                     #{mid},
                     #{num},
                     #{midType},
                     #{sourceName},
                     #{targetName},
                     #{operator},
                     #{approver},
                     #{rcptim},
                     #{rid},
                     #{doCount},
                     #{prf},
                     #{dispatchMode},
                     #{createBy},
                     #{createTime},
                     #{updateBy},
                     #{updateTime},
                     #{isDelete}
                 )
    </insert>

    <!-- ======================= 批量新增调度任务 ======================= -->
    <insert id="batchInsertDdTask">
        INSERT INTO dd_task (
        task_no,
        task_dtl,
        task_type,
        task_status,
        mid_status,
        mid,
        num,
        mid_type,
        source_name,
        target_name,
        operator,
        approver,
        rcptim,
        rid,
        do_count,
        prf,
        dispatch_mode,
        create_by,
        create_time,
        update_by,
        update_time,
        is_delete
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.taskNo},
            #{item.taskDtl},
            #{item.taskType},
            #{item.taskStatus},
            #{item.midStatus},
            #{item.mid},
            #{item.num},
            #{item.midType},
            #{item.sourceName},
            #{item.targetName},
            #{item.operator},
            #{item.approver},
            #{item.rcptim},
            #{item.rid},
            #{item.doCount},
            #{item.prf},
            #{item.dispatchMode},
            #{item.createBy},
            #{item.createTime},
            #{item.updateBy},
            #{item.updateTime},
            #{item.isDelete}
            )
        </foreach>
    </insert>

    <!-- ======================= 修改调度任务 ======================= -->
    <update id="updateDdTask" parameterType="com.zg.project.wisdom.domain.DdTask">
        UPDATE dd_task
        SET
            task_dtl = #{taskDtl},
            task_type = #{taskType},
            task_status = #{taskStatus},
            mid_status = #{midStatus},
            mid = #{mid},
            num = #{num},
            mid_type = #{midType},
            source_name = #{sourceName},
            target_name = #{targetName},
            operator = #{operator},
            approver = #{approver},
            rcptim = #{rcptim},
            rid = #{rid},
            do_count = #{doCount},
            prf = #{prf},
            dispatch_mode = #{dispatchMode},
            update_by = #{updateBy},
            update_time = #{updateTime}
        WHERE id = #{id}
          AND is_delete = '0'
    </update>

    <!-- ======================= 根据任务号更新状态 ======================= -->
    <update id="updateTaskStatusByTaskNo">
        UPDATE dd_task
        SET task_status = #{status},
            update_time = NOW()
        WHERE task_no = #{taskNo}
          AND is_delete = '0'
    </update>

    <!-- ======================= 批量删除 ======================= -->
    <delete id="deleteDdTaskByIds">
        DELETE FROM dd_task
        WHERE id IN
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- ======================= 单条删除 ======================= -->
    <delete id="deleteDdTaskById">
        DELETE FROM dd_task
        WHERE id = #{id}
    </delete>

</mapper>
