<?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.DmsRepairWorkOrderMapper">
    
    <resultMap type="DmsRepairWorkOrder" id="DmsRepairWorkOrderResult">
        <result property="workOrderId"    column="work_order_id"    />
        <result property="workOrderCode"    column="work_order_code"    />
        <result property="faultInstanceId"    column="fault_instance_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="faultType"    column="fault_type"    />
        <result property="faultDescription"    column="fault_description"    />
        <result property="repairType"    column="repair_type"    />
        <result property="orderStatus"    column="order_status"    />
        <result property="priority"    column="priority"    />
        <result property="planStartTime"    column="plan_start_time"    />
        <result property="planEndTime"    column="plan_end_time"    />
        <result property="actualStartTime"    column="actual_start_time"    />
        <result property="actualEndTime"    column="actual_end_time"    />
        <result property="executorId"    column="executor_id"    />
        <result property="executorName"    column="executor_name"    />
        <result property="repairHours"    column="repair_hours"    />
        <result property="budgetCost"    column="budget_cost"    />
        <result property="actualCost"    column="actual_cost"    />
        <result property="repairResult"    column="repair_result"    />
        <result property="repairNotes"    column="repair_notes"    />
        <result property="approveStatus"    column="approve_status"    />
        <result property="approver"    column="approver"    />
        <result property="approveTime"    column="approve_time"    />
        <result property="approveRemark"    column="approve_remark"    />
        <result property="needShutdown"    column="need_shutdown"    />
        <result property="shutdownDuration"    column="shutdown_duration"    />
        <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="selectDmsRepairWorkOrderVo">
        select work_order_id, work_order_code, fault_instance_id, device_id, device_code, device_name, 
               fault_type, fault_description, repair_type, order_status, priority, 
               plan_start_time, plan_end_time, actual_start_time, actual_end_time, 
               executor_id, executor_name, repair_hours, budget_cost, actual_cost, 
               repair_result, repair_notes, approve_status, approver, approve_time, approve_remark,
               need_shutdown, shutdown_duration, is_flag, remark, 
               create_by, create_time, update_by, update_time
        from dms_repair_work_order
    </sql>

    <select id="selectDmsRepairWorkOrderList" parameterType="DmsRepairWorkOrder" resultMap="DmsRepairWorkOrderResult">
        <include refid="selectDmsRepairWorkOrderVo"/>
        <where>
            <if test="workOrderCode != null and workOrderCode != ''">
                and work_order_code like '%' || #{workOrderCode} || '%'
            </if>
            <if test="deviceId != null">
                and device_id = #{deviceId}
            </if>
            <if test="deviceName != null and deviceName != ''">
                and device_name like '%' || #{deviceName} || '%'
            </if>
            <if test="faultType != null and faultType != ''">
                and fault_type = #{faultType}
            </if>
            <if test="repairType != null and repairType != ''">
                and repair_type = #{repairType}
            </if>
            <if test="orderStatus != null and orderStatus != ''">
                and order_status = #{orderStatus}
            </if>
            <if test="priority != null and priority != ''">
                and priority = #{priority}
            </if>
            <if test="executorId != null">
                and executor_id = #{executorId}
            </if>
            <if test="executorName != null and executorName != ''">
                and executor_name like '%' || #{executorName} || '%'
            </if>
            <if test="approveStatus != null and approveStatus != ''">
                and approve_status = #{approveStatus}
            </if>
            <if test="params != null and params.beginTime != null and params.beginTime != ''">
                and plan_start_time &gt;= to_date(#{params.beginTime}, 'yyyy-mm-dd')
            </if>
            <if test="params != null and params.endTime != null and params.endTime != ''">
                and plan_end_time &lt;= to_date(#{params.endTime}, 'yyyy-mm-dd')
            </if>
            and is_flag = '1'
        </where>
        order by create_time desc
    </select>
    
    <select id="selectDmsRepairWorkOrderByWorkOrderId" parameterType="Long" resultMap="DmsRepairWorkOrderResult">
        <include refid="selectDmsRepairWorkOrderVo"/>
        where work_order_id = #{workOrderId}
    </select>

    <select id="selectByFaultInstanceId" parameterType="Long" resultMap="DmsRepairWorkOrderResult">
        <include refid="selectDmsRepairWorkOrderVo"/>
        where fault_instance_id = #{faultInstanceId}
        and is_flag = '1'
    </select>
        
    <insert id="insertDmsRepairWorkOrder" parameterType="DmsRepairWorkOrder">
        <selectKey keyProperty="workOrderId" resultType="long" order="BEFORE">
            select HAIWEI.SEQ_DMS_REPAIR_WORK_ORDER.nextval as workOrderId from DUAL
        </selectKey>
        insert into dms_repair_work_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workOrderId != null">work_order_id,</if>
            <if test="workOrderCode != null and workOrderCode != ''">work_order_code,</if>
            <if test="faultInstanceId != null">fault_instance_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="deviceCode != null">device_code,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="faultType != null">fault_type,</if>
            <if test="faultDescription != null">fault_description,</if>
            <if test="repairType != null">repair_type,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="priority != null">priority,</if>
            <if test="planStartTime != null">plan_start_time,</if>
            <if test="planEndTime != null">plan_end_time,</if>
            <if test="actualStartTime != null">actual_start_time,</if>
            <if test="actualEndTime != null">actual_end_time,</if>
            <if test="executorId != null">executor_id,</if>
            <if test="executorName != null">executor_name,</if>
            <if test="repairHours != null">repair_hours,</if>
            <if test="budgetCost != null">budget_cost,</if>
            <if test="actualCost != null">actual_cost,</if>
            <if test="repairResult != null">repair_result,</if>
            <if test="repairNotes != null">repair_notes,</if>
            <if test="approveStatus != null">approve_status,</if>
            <if test="approver != null">approver,</if>
            <if test="approveTime != null">approve_time,</if>
            <if test="approveRemark != null">approve_remark,</if>
            <if test="needShutdown != null">need_shutdown,</if>
            <if test="shutdownDuration != null">shutdown_duration,</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="workOrderId != null">#{workOrderId},</if>
            <if test="workOrderCode != null and workOrderCode != ''">#{workOrderCode},</if>
            <if test="faultInstanceId != null">#{faultInstanceId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="faultType != null">#{faultType},</if>
            <if test="faultDescription != null">#{faultDescription},</if>
            <if test="repairType != null">#{repairType},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="priority != null">#{priority},</if>
            <if test="planStartTime != null">#{planStartTime},</if>
            <if test="planEndTime != null">#{planEndTime},</if>
            <if test="actualStartTime != null">#{actualStartTime},</if>
            <if test="actualEndTime != null">#{actualEndTime},</if>
            <if test="executorId != null">#{executorId},</if>
            <if test="executorName != null">#{executorName},</if>
            <if test="repairHours != null">#{repairHours},</if>
            <if test="budgetCost != null">#{budgetCost},</if>
            <if test="actualCost != null">#{actualCost},</if>
            <if test="repairResult != null">#{repairResult},</if>
            <if test="repairNotes != null">#{repairNotes},</if>
            <if test="approveStatus != null">#{approveStatus},</if>
            <if test="approver != null">#{approver},</if>
            <if test="approveTime != null">#{approveTime},</if>
            <if test="approveRemark != null">#{approveRemark},</if>
            <if test="needShutdown != null">#{needShutdown},</if>
            <if test="shutdownDuration != null">#{shutdownDuration},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate
        </trim>
    </insert>

    <update id="updateDmsRepairWorkOrder" parameterType="DmsRepairWorkOrder">
        update dms_repair_work_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="workOrderCode != null and workOrderCode != ''">work_order_code = #{workOrderCode},</if>
            <if test="faultInstanceId != null">fault_instance_id = #{faultInstanceId},</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="faultType != null">fault_type = #{faultType},</if>
            <if test="faultDescription != null">fault_description = #{faultDescription},</if>
            <if test="repairType != null">repair_type = #{repairType},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="priority != null">priority = #{priority},</if>
            <if test="planStartTime != null">plan_start_time = #{planStartTime},</if>
            <if test="planEndTime != null">plan_end_time = #{planEndTime},</if>
            <if test="actualStartTime != null">actual_start_time = #{actualStartTime},</if>
            <if test="actualEndTime != null">actual_end_time = #{actualEndTime},</if>
            <if test="executorId != null">executor_id = #{executorId},</if>
            <if test="executorName != null">executor_name = #{executorName},</if>
            <if test="repairHours != null">repair_hours = #{repairHours},</if>
            <if test="budgetCost != null">budget_cost = #{budgetCost},</if>
            <if test="actualCost != null">actual_cost = #{actualCost},</if>
            <if test="repairResult != null">repair_result = #{repairResult},</if>
            <if test="repairNotes != null">repair_notes = #{repairNotes},</if>
            <if test="approveStatus != null">approve_status = #{approveStatus},</if>
            <if test="approver != null">approver = #{approver},</if>
            <if test="approveTime != null">approve_time = #{approveTime},</if>
            <if test="approveRemark != null">approve_remark = #{approveRemark},</if>
            <if test="needShutdown != null">need_shutdown = #{needShutdown},</if>
            <if test="shutdownDuration != null">shutdown_duration = #{shutdownDuration},</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 work_order_id = #{workOrderId}
    </update>

    <delete id="deleteDmsRepairWorkOrderByWorkOrderId" parameterType="Long">
        update dms_repair_work_order set is_flag = '0' where work_order_id = #{workOrderId}
    </delete>

    <delete id="deleteDmsRepairWorkOrderByWorkOrderIds" parameterType="String">
        update dms_repair_work_order set is_flag = '0' where work_order_id in 
        <foreach item="workOrderId" collection="array" open="(" separator="," close=")">
            #{workOrderId}
        </foreach>
    </delete>
</mapper>
