<?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.hw.qms.mapper.QmsQualityInstanceActivityMapper">
    
    <resultMap type="QmsQualityInstanceActivity" id="QmsQualityInstanceActivityResult">
        <result property="instanceActivityId"    column="instance_activity_id"    />
        <result property="qualityInstanceId"    column="quality_instance_id"    />
        <result property="processActivityId"    column="process_activity_id"    />
        <result property="processHandleResolution"    column="process_handle_resolution"    />
        <result property="processHandleDesc"    column="process_handle_desc"    />
        <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"    />
    </resultMap>

    <resultMap id="QmsQualityInstanceActivityQmsCheckInstanceFileResult" type="QmsQualityInstanceActivity" extends="QmsQualityInstanceActivityResult">
        <collection property="qmsCheckInstanceFileList" notNullColumn="sub_check_instance_file_id" javaType="java.util.List" resultMap="QmsCheckInstanceFileResult" />
    </resultMap>

    <resultMap type="QmsCheckInstanceFile" id="QmsCheckInstanceFileResult">
        <result property="checkInstanceFileId"    column="sub_check_instance_file_id"    />
        <result property="targetType"    column="sub_target_type"    />
        <result property="targetId"    column="sub_target_id"    />
        <result property="faultFile"    column="sub_fault_file"    />
    </resultMap>

    <sql id="selectQmsQualityInstanceActivityVo">
        select instance_activity_id, quality_instance_id, process_activity_id, process_handle_resolution, process_handle_desc, process_handle_status, process_step_order, start_time, end_time, handle_user_id, handle_by, handle_time, create_by, create_time, transfer_user_id from qms_quality_instance_activity
    </sql>

    <select id="selectQmsQualityInstanceActivityList" parameterType="QmsQualityInstanceActivity" resultMap="QmsQualityInstanceActivityResult">
        <include refid="selectQmsQualityInstanceActivityVo"/>
        <where>  
            <if test="qualityInstanceId != null "> and quality_instance_id = #{qualityInstanceId}</if>
            <if test="processActivityId != null "> and process_activity_id = #{processActivityId}</if>
            <if test="processHandleResolution != null  and processHandleResolution != ''"> and process_handle_resolution = #{processHandleResolution}</if>
            <if test="processHandleDesc != null  and processHandleDesc != ''"> and process_handle_desc = #{processHandleDesc}</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>
        </where>
    </select>
    
    <select id="selectQmsQualityInstanceActivityByInstanceActivityId" parameterType="Long" resultMap="QmsQualityInstanceActivityQmsCheckInstanceFileResult">
        select a.instance_activity_id, a.quality_instance_id, a.process_activity_id, a.process_handle_resolution, a.process_handle_desc, a.process_handle_status, a.process_step_order, a.start_time, a.end_time, a.handle_user_id, a.handle_by, a.handle_time, a.create_by, a.create_time, a.transfer_user_id,
 b.check_instance_file_id as sub_check_instance_file_id, b.target_type as sub_target_type, b.target_id as sub_target_id, b.fault_file as sub_fault_file
        from qms_quality_instance_activity a
        left join qms_check_instance_file b on b.target_id = a.instance_activity_id
        where a.instance_activity_id = #{instanceActivityId}
    </select>
        
    <insert id="insertQmsQualityInstanceActivity" parameterType="QmsQualityInstanceActivity" useGeneratedKeys="true" keyProperty="instanceActivityId">
        insert into qms_quality_instance_activity
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="qualityInstanceId != null">quality_instance_id,</if>
            <if test="processActivityId != null">process_activity_id,</if>
            <if test="processHandleResolution != null and processHandleResolution != ''">process_handle_resolution,</if>
            <if test="processHandleDesc != null">process_handle_desc,</if>
            <if test="processHandleStatus != null">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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="qualityInstanceId != null">#{qualityInstanceId},</if>
            <if test="processActivityId != null">#{processActivityId},</if>
            <if test="processHandleResolution != null and processHandleResolution != ''">#{processHandleResolution},</if>
            <if test="processHandleDesc != null">#{processHandleDesc},</if>
            <if test="processHandleStatus != null">#{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>
         </trim>
    </insert>

    <update id="updateQmsQualityInstanceActivity" parameterType="QmsQualityInstanceActivity">
        update qms_quality_instance_activity
        <trim prefix="SET" suffixOverrides=",">
            <if test="qualityInstanceId != null">quality_instance_id = #{qualityInstanceId},</if>
            <if test="processActivityId != null">process_activity_id = #{processActivityId},</if>
            <if test="processHandleResolution != null and processHandleResolution != ''">process_handle_resolution = #{processHandleResolution},</if>
            <if test="processHandleDesc != null">process_handle_desc = #{processHandleDesc},</if>
            <if test="processHandleStatus != null">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>
        </trim>
        where instance_activity_id = #{instanceActivityId}
    </update>

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

    <delete id="deleteQmsQualityInstanceActivityByInstanceActivityIds" parameterType="String">
        delete from qms_quality_instance_activity where instance_activity_id in 
        <foreach item="instanceActivityId" collection="array" open="(" separator="," close=")">
            #{instanceActivityId}
        </foreach>
    </delete>
    
    <delete id="deleteQmsCheckInstanceFileByTargetIds" parameterType="String">
        delete from qms_check_instance_file where target_id in 
        <foreach item="targetId" collection="array" open="(" separator="," close=")">
            #{targetId}
        </foreach>
    </delete>

    <delete id="deleteQmsCheckInstanceFileByTargetId" parameterType="Long">
        delete from qms_check_instance_file where target_id = #{targetId}
    </delete>

    <insert id="batchQmsCheckInstanceFile">
        insert into qms_check_instance_file( check_instance_file_id, target_type, target_id, fault_file) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.checkInstanceFileId}, #{item.targetType}, #{item.targetId}, #{item.faultFile})
        </foreach>
    </insert>
</mapper>