<?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.DmsFaultInstanceActivityMapper">

    <resultMap type="DmsFaultInstanceActivity" id="DmsFaultInstanceActivityResult">
        <result property="instanceActivityId"    column="instance_activity_id"    />
        <result property="repairInstanceId"    column="repair_instance_id"    />
        <result property="processActivityId"    column="process_activity_id"    />
        <result property="faultType"    column="fault_type"    />
        <result property="faultDescription"    column="fault_description"    />
        <result property="designOperations"    column="design_operations"    />
        <result property="deviceId"    column="device_id"    />
        <result property="repairType"    column="repair_type"    />
        <result property="outsrcId"    column="outsrc_id"    />
        <result property="processHandleResolution"    column="process_handle_resolution"    />
        <result property="remark"    column="remark"    />
        <result property="processHandleStatus"    column="process_handle_status"    />
        <result property="processStepOrder"    column="process_step_order"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="handleUserId"    column="handle_user_id"    />
        <result property="handleBy"    column="handle_by"    />
        <result property="handleTime"    column="handle_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="transferUserId"    column="transfer_user_id"    />
        <result property="attr1"    column="attr1"    />
        <result property="checkedFault"    column="checked_fault"    />
        <result property="repairContent"    column="repair_content"    />
        <result property="protectedMethod"    column="protected_method"    />
        <result property="repairer"    column="repairer"    />
        <result property="repairConfirm"    column="repair_confirm"    />
        <result property="confirmTime"    column="confirm_time"    />
        <result property="componentsPartsId"    column="components_parts_id"    />
            <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectDmsFaultInstanceActivityVo">
        select instance_activity_id, repair_instance_id, process_activity_id, fault_type, fault_description, design_operations, device_id, repair_type, outsrc_id, process_handle_resolution, remark, process_handle_status, process_step_order, start_time, end_time, handle_user_id, handle_by, handle_time, create_by, create_time, transfer_user_id, attr1 from dms_fault_instance_activity
    </sql>

    <select id="selectDmsFaultInstanceActivityList" parameterType="DmsFaultInstanceActivity" resultMap="DmsFaultInstanceActivityResult">
        <include refid="selectDmsFaultInstanceActivityVo"/>
        <where>
            <if test="repairInstanceId != null "> and repair_instance_id = #{repairInstanceId}</if>
            <if test="processActivityId != null "> and process_activity_id = #{processActivityId}</if>
            <if test="faultType != null  and faultType != ''"> and fault_type = #{faultType}</if>
            <if test="faultDescription != null  and faultDescription != ''"> and fault_description = #{faultDescription}</if>
            <if test="designOperations != null  and designOperations != ''"> and design_operations = #{designOperations}</if>
            <if test="deviceId != null "> and device_id = #{deviceId}</if>
            <if test="repairType != null  and repairType != ''"> and repair_type = #{repairType}</if>
            <if test="outsrcId != null "> and outsrc_id = #{outsrcId}</if>
            <if test="processHandleResolution != null  and processHandleResolution != ''"> and process_handle_resolution = #{processHandleResolution}</if>
            <if test="processHandleStatus != null  and processHandleStatus != ''"> and process_handle_status = #{processHandleStatus}</if>
            <if test="processStepOrder != null "> and process_step_order = #{processStepOrder}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="handleUserId != null "> and handle_user_id = #{handleUserId}</if>
            <if test="handleBy != null  and handleBy != ''"> and handle_by = #{handleBy}</if>
            <if test="handleTime != null "> and handle_time = #{handleTime}</if>
            <if test="transferUserId != null "> and transfer_user_id = #{transferUserId}</if>
            <if test="attr1 != null  and attr1 != ''"> and attr1 = #{attr1}</if>
        </where>
    </select>

    <select id="selectDmsFaultInstanceActivityByInstanceActivityId" parameterType="Long" resultMap="DmsFaultInstanceActivityResult">
        select instance_activity_id, repair_instance_id, process_activity_id, fault_type, fault_description, design_operations, device_id, repair_type, outsrc_id, process_handle_resolution, remark, process_handle_status, process_step_order, start_time, end_time, handle_user_id, handle_by, handle_time, create_by, create_time, transfer_user_id, attr1,checked_fault,
        repair_content,
        protected_method,
        repairer,
        repair_confirm,
        confirm_time, components_parts_id  from dms_fault_instance_activity
        where instance_activity_id = #{instanceActivityId}
    </select>

    <insert id="insertDmsFaultInstanceActivity" parameterType="DmsFaultInstanceActivity">
        <selectKey keyProperty="instanceActivityId" resultType="long" order="BEFORE">
            SELECT seq_fault_instance_activity.NEXTVAL FROM DUAL
        </selectKey>
        insert into dms_fault_instance_activity
        <trim prefix="(" suffix=")" suffixOverrides=",">
            instance_activity_id,
            <if test="repairInstanceId != null">repair_instance_id,</if>
            <if test="processActivityId != null">process_activity_id,</if>
            <if test="faultType != null">fault_type,</if>
            <if test="faultDescription != null">fault_description,</if>
            <if test="designOperations != null">design_operations,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="repairType != null">repair_type,</if>
            <if test="outsrcId != null">outsrc_id,</if>
            <if test="processHandleResolution != null">process_handle_resolution,</if>
            <if test="remark != null">remark,</if>
            <if test="processHandleStatus != null and processHandleStatus != ''">process_handle_status,</if>
            <if test="processStepOrder != null">process_step_order,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="handleUserId != null">handle_user_id,</if>
            <if test="handleBy != null">handle_by,</if>
            <if test="handleTime != null">handle_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="transferUserId != null">transfer_user_id,</if>
            <if test="attr1 != null">attr1,</if>
            <if test="checkedFault != null">checked_fault,</if>
            <if test="repairContent != null">repair_content,</if>
            <if test="protectedMethod != null">protected_method,</if>
            <if test="repairer != null">repairer,</if>
            <if test="repairConfirm != null">repair_confirm,</if>
            <if test="confirmTime != null">confirm_time,</if>
            <if test="componentsPartsId != null">components_parts_id,</if>
                        <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{instanceActivityId},
            <if test="repairInstanceId != null">#{repairInstanceId},</if>
            <if test="processActivityId != null">#{processActivityId},</if>
            <if test="faultType != null">#{faultType},</if>
            <if test="faultDescription != null">#{faultDescription},</if>
            <if test="designOperations != null">#{designOperations},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="repairType != null">#{repairType},</if>
            <if test="outsrcId != null">#{outsrcId},</if>
            <if test="processHandleResolution != null">#{processHandleResolution},</if>
            <if test="remark != null">#{remark},</if>
            <if test="processHandleStatus != null and processHandleStatus != ''">#{processHandleStatus},</if>
            <if test="processStepOrder != null">#{processStepOrder},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="handleUserId != null">#{handleUserId},</if>
            <if test="handleBy != null">#{handleBy},</if>
            <if test="handleTime != null">#{handleTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="transferUserId != null">#{transferUserId},</if>
            <if test="attr1 != null">#{attr1},</if>
            <if test="checkedFault != null">#{checkedFault},</if>
            <if test="repairContent != null">#{repairContent},</if>
            <if test="protectedMethod != null">#{protectedMethod},</if>
            <if test="repairer != null">#{repairer},</if>
            <if test="repairConfirm != null">#{repairConfirm},</if>
            <if test="confirmTime != null">#{confirmTime},</if>
            <if test="componentsPartsId != null">#{componentsPartsId},</if>
                        <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>
    <insert id="insertParts" parameterType="java.util.List">
        insert into dms_fault_compents_parts(fault_id,part_name,part_specifications,amount)
        values
        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
            (#{item.faultId},#{item.partName},#{item.partSpecifications},#{item.amount})
        </foreach>
    </insert>

    <update id="updateDmsFaultInstanceActivity" parameterType="DmsFaultInstanceActivity">
        update dms_fault_instance_activity
        <trim prefix="SET" suffixOverrides=",">
            <if test="repairInstanceId != null">repair_instance_id = #{repairInstanceId},</if>
            <if test="processActivityId != null">process_activity_id = #{processActivityId},</if>
            <if test="faultType != null">fault_type = #{faultType},</if>
            <if test="faultDescription != null">fault_description = #{faultDescription},</if>
            <if test="designOperations != null">design_operations = #{designOperations},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="repairType != null">repair_type = #{repairType},</if>
            <if test="outsrcId != null">outsrc_id = #{outsrcId},</if>
            <if test="processHandleResolution != null">process_handle_resolution = #{processHandleResolution},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="processHandleStatus != null and processHandleStatus != ''">process_handle_status = #{processHandleStatus},</if>
            <if test="processStepOrder != null">process_step_order = #{processStepOrder},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="handleUserId != null">handle_user_id = #{handleUserId},</if>
            <if test="handleBy != null">handle_by = #{handleBy},</if>
            <if test="handleTime != null">handle_time = #{handleTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="transferUserId != null">transfer_user_id = #{transferUserId},</if>
            <if test="attr1 != null">attr1 = #{attr1},</if>
            <if test="checkedFault != null">checked_fault = #{checkedFault},</if>
            <if test="repairContent != null">repair_content = #{repairContent},</if>
            <if test="protectedMethod != null">protected_method = #{protectedMethod},</if>
            <if test="repairer != null">repairer = #{repairer},</if>
            <if test="repairConfirm != null">repair_confirm = #{repairConfirm},</if>
            <if test="confirmTime != null">confirm_time = #{confirmTime},</if>
            <if test="componentsPartsId != null">components_parts_id = #{componentsPartsId},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime}</if>
        </trim>
        where instance_activity_id = #{instanceActivityId}
    </update>

    <delete id="deleteDmsFaultInstanceActivityByInstanceActivityId" parameterType="Long">
        delete from dms_fault_instance_activity where instance_activity_id = #{instanceActivityId}
    </delete>

    <delete id="deleteDmsFaultInstanceActivityByInstanceActivityIds" parameterType="String">
        delete from dms_fault_instance_activity where instance_activity_id in
        <foreach item="instanceActivityId" collection="array" open="(" separator="," close=")">
            #{instanceActivityId}
        </foreach>
    </delete>
    <delete id="deleteByTargetId" parameterType="java.lang.Long">
        delete FROM dms_instance_file where target_id =#{activityId}
    </delete>
    <select id="selectDmsFaultInstanceActivityByRepairIdMin" parameterType="Long" resultMap="DmsFaultInstanceActivityResult">
        select * from dms_fault_instance_activity where instance_activity_id =  (select min(instance_activity_id) FROM dms_fault_instance_activity where repair_instance_id = #{repairInstanceId})
    </select>
    <select id="selectPartsById" resultType="com.aucma.dms.domain.DmsFaultCompentsParts"
            parameterType="java.lang.Long">
        SELECT x.* FROM dms_fault_compents_parts x where x.fault_id = #{repairInstanceId}
    </select>
    <select id="selectRepairInstanceId" resultType="java.lang.Long" parameterType="java.lang.Long">
        SELECT x.repair_instance_id FROM dms_fault_instance_activity x where x.instance_activity_id =#{activityId}
    </select>
</mapper>
