<?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.aucma.dms.mapper.DmsDispatchPlanMapper">
    
    <resultMap type="DmsDispatchPlan" id="DmsDispatchPlanResult">
        <result property="planId"    column="plan_id"    />
        <result property="planCode"    column="plan_code"    />
        <result property="workOrderId"    column="work_order_id"    />
        <result property="workOrderCode"    column="work_order_code"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="dispatchType"    column="dispatch_type"    />
        <result property="executorId"    column="executor_id"    />
        <result property="executorName"    column="executor_name"    />
        <result property="teamId"    column="team_id"    />
        <result property="teamName"    column="team_name"    />
        <result property="dispatchTime"    column="dispatch_time"    />
        <result property="requireStartTime"    column="require_start_time"    />
        <result property="requireEndTime"    column="require_end_time"    />
        <result property="receiveStatus"    column="receive_status"    />
        <result property="receiveTime"    column="receive_time"    />
        <result property="rejectReason"    column="reject_reason"    />
        <result property="executionStatus"    column="execution_status"    />
        <result property="priority"    column="priority"    />
        <result property="dispatcherId"    column="dispatcher_id"    />
        <result property="dispatcherName"    column="dispatcher_name"    />
        <result property="dispatchNotes"    column="dispatch_notes"    />
        <result property="isFlag"    column="is_flag"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectDmsDispatchPlanVo">
        select plan_id, plan_code, work_order_id, work_order_code, device_id, device_code, device_name,
               dispatch_type, executor_id, executor_name, team_id, team_name, dispatch_time,
               require_start_time, require_end_time, receive_status, receive_time, reject_reason,
               execution_status, priority, dispatcher_id, dispatcher_name, dispatch_notes,
               is_flag, remark, create_by, create_time, update_by, update_time
        from dms_dispatch_plan
    </sql>

    <select id="selectDmsDispatchPlanList" parameterType="DmsDispatchPlan" resultMap="DmsDispatchPlanResult">
        <include refid="selectDmsDispatchPlanVo"/>
        <where>
            <if test="planCode != null and planCode != ''">
                and plan_code like '%' || #{planCode} || '%'
            </if>
            <if test="workOrderId != null">
                and work_order_id = #{workOrderId}
            </if>
            <if test="workOrderCode != null and workOrderCode != ''">
                and work_order_code like '%' || #{workOrderCode} || '%'
            </if>
            <if test="deviceName != null and deviceName != ''">
                and device_name like '%' || #{deviceName} || '%'
            </if>
            <if test="dispatchType != null and dispatchType != ''">
                and dispatch_type = #{dispatchType}
            </if>
            <if test="executorId != null">
                and executor_id = #{executorId}
            </if>
            <if test="receiveStatus != null and receiveStatus != ''">
                and receive_status = #{receiveStatus}
            </if>
            <if test="executionStatus != null and executionStatus != ''">
                and execution_status = #{executionStatus}
            </if>
            and is_flag = '1'
        </where>
        order by create_time desc
    </select>
    
    <select id="selectDmsDispatchPlanByPlanId" parameterType="Long" resultMap="DmsDispatchPlanResult">
        <include refid="selectDmsDispatchPlanVo"/>
        where plan_id = #{planId}
    </select>
        
    <insert id="insertDmsDispatchPlan" parameterType="DmsDispatchPlan">
        <selectKey keyProperty="planId" resultType="long" order="BEFORE">
            select SEQ_DMS_DISPATCH_PLAN.nextval as planId from DUAL
        </selectKey>
        insert into dms_dispatch_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planId != null">plan_id,</if>
            <if test="planCode != null and planCode != ''">plan_code,</if>
            <if test="workOrderId != null">work_order_id,</if>
            <if test="workOrderCode != null">work_order_code,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="deviceCode != null">device_code,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="dispatchType != null">dispatch_type,</if>
            <if test="executorId != null">executor_id,</if>
            <if test="executorName != null">executor_name,</if>
            <if test="teamId != null">team_id,</if>
            <if test="teamName != null">team_name,</if>
            <if test="dispatchTime != null">dispatch_time,</if>
            <if test="requireStartTime != null">require_start_time,</if>
            <if test="requireEndTime != null">require_end_time,</if>
            <if test="receiveStatus != null">receive_status,</if>
            <if test="receiveTime != null">receive_time,</if>
            <if test="rejectReason != null">reject_reason,</if>
            <if test="executionStatus != null">execution_status,</if>
            <if test="priority != null">priority,</if>
            <if test="dispatcherId != null">dispatcher_id,</if>
            <if test="dispatcherName != null">dispatcher_name,</if>
            <if test="dispatchNotes != null">dispatch_notes,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planId != null">#{planId},</if>
            <if test="planCode != null and planCode != ''">#{planCode},</if>
            <if test="workOrderId != null">#{workOrderId},</if>
            <if test="workOrderCode != null">#{workOrderCode},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="dispatchType != null">#{dispatchType},</if>
            <if test="executorId != null">#{executorId},</if>
            <if test="executorName != null">#{executorName},</if>
            <if test="teamId != null">#{teamId},</if>
            <if test="teamName != null">#{teamName},</if>
            <if test="dispatchTime != null">#{dispatchTime},</if>
            <if test="requireStartTime != null">#{requireStartTime},</if>
            <if test="requireEndTime != null">#{requireEndTime},</if>
            <if test="receiveStatus != null">#{receiveStatus},</if>
            <if test="receiveTime != null">#{receiveTime},</if>
            <if test="rejectReason != null">#{rejectReason},</if>
            <if test="executionStatus != null">#{executionStatus},</if>
            <if test="priority != null">#{priority},</if>
            <if test="dispatcherId != null">#{dispatcherId},</if>
            <if test="dispatcherName != null">#{dispatcherName},</if>
            <if test="dispatchNotes != null">#{dispatchNotes},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate
        </trim>
    </insert>

    <update id="updateDmsDispatchPlan" parameterType="DmsDispatchPlan">
        update dms_dispatch_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="planCode != null and planCode != ''">plan_code = #{planCode},</if>
            <if test="workOrderId != null">work_order_id = #{workOrderId},</if>
            <if test="workOrderCode != null">work_order_code = #{workOrderCode},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="deviceCode != null">device_code = #{deviceCode},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="dispatchType != null">dispatch_type = #{dispatchType},</if>
            <if test="executorId != null">executor_id = #{executorId},</if>
            <if test="executorName != null">executor_name = #{executorName},</if>
            <if test="teamId != null">team_id = #{teamId},</if>
            <if test="teamName != null">team_name = #{teamName},</if>
            <if test="dispatchTime != null">dispatch_time = #{dispatchTime},</if>
            <if test="requireStartTime != null">require_start_time = #{requireStartTime},</if>
            <if test="requireEndTime != null">require_end_time = #{requireEndTime},</if>
            <if test="receiveStatus != null">receive_status = #{receiveStatus},</if>
            <if test="receiveTime != null">receive_time = #{receiveTime},</if>
            <if test="rejectReason != null">reject_reason = #{rejectReason},</if>
            <if test="executionStatus != null">execution_status = #{executionStatus},</if>
            <if test="priority != null">priority = #{priority},</if>
            <if test="dispatcherId != null">dispatcher_id = #{dispatcherId},</if>
            <if test="dispatcherName != null">dispatcher_name = #{dispatcherName},</if>
            <if test="dispatchNotes != null">dispatch_notes = #{dispatchNotes},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = sysdate
        </trim>
        where plan_id = #{planId}
    </update>

    <delete id="deleteDmsDispatchPlanByPlanId" parameterType="Long">
        update dms_dispatch_plan set is_flag = '0' where plan_id = #{planId}
    </delete>

    <delete id="deleteDmsDispatchPlanByPlanIds" parameterType="String">
        update dms_dispatch_plan set is_flag = '0' where plan_id in 
        <foreach item="planId" collection="array" open="(" separator="," close=")">
            #{planId}
        </foreach>
    </delete>
</mapper>
