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

    <resultMap type="QmsQualityInstance" id="QmsQualityInstanceResult">
        <result property="qualityInstanceId"    column="quality_instance_id"    />
        <result property="wfProcessId"    column="wf_process_id"    />
        <result property="wfInstanceStatus"    column="wf_instance_status"    />
        <result property="checkResultId"    column="check_result_id"    />
        <result property="qualityDescription"    column="quality_description"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <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>

    <resultMap id="QmsQualityInstanceQmsQualityInstanceActivityResult" type="QmsQualityInstance" extends="QmsQualityInstanceResult">
        <collection property="qmsQualityInstanceActivityList" notNullColumn="sub_instance_activity_id" javaType="java.util.List" resultMap="QmsQualityInstanceActivityResult" />
    </resultMap>

    <resultMap type="QmsQualityInstanceActivity" id="QmsQualityInstanceActivityResult">
        <result property="instanceActivityId"    column="sub_instance_activity_id"    />
        <result property="qualityInstanceId"    column="sub_quality_instance_id"    />
        <result property="processActivityId"    column="sub_process_activity_id"    />
        <result property="processHandleResolution"    column="sub_process_handle_resolution"    />
        <result property="processHandleDesc"    column="sub_process_handle_desc"    />
        <result property="processHandleStatus"    column="sub_process_handle_status"    />
        <result property="processStepOrder"    column="sub_process_step_order"    />
        <result property="startTime"    column="sub_start_time"    />
        <result property="endTime"    column="sub_end_time"    />
        <result property="handleUserId"    column="sub_handle_user_id"    />
        <result property="handleBy"    column="sub_handle_by"    />
        <result property="handleTime"    column="sub_handle_time"    />
        <result property="createBy"    column="sub_create_by"    />
        <result property="createTime"    column="sub_create_time"    />
        <result property="transferUserId"    column="sub_transfer_user_id"    />
    </resultMap>

    <sql id="selectQmsQualityInstanceVo">
        select quality_instance_id, wf_process_id, wf_instance_status, check_result_id, quality_description, start_time, end_time, create_by, create_time, update_by, update_time from qms_quality_instance
    </sql>

    <select id="selectQmsQualityInstanceList" parameterType="QmsQualityInstance" resultMap="QmsQualityInstanceResult">
        <include refid="selectQmsQualityInstanceVo"/>
        <where>
            <if test="wfProcessId != null "> and wf_process_id = #{wfProcessId}</if>
            <if test="wfInstanceStatus != null  and wfInstanceStatus != ''"> and wf_instance_status = #{wfInstanceStatus}</if>
            <if test="checkResultId != null "> and check_result_id = #{checkResultId}</if>
            <if test="qualityDescription != null  and qualityDescription != ''"> and quality_description = #{qualityDescription}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
        </where>
    </select>

    <select id="selectQmsQualityInstanceByQualityInstanceId" parameterType="Long" resultMap="QmsQualityInstanceQmsQualityInstanceActivityResult">
        select a.quality_instance_id, a.wf_process_id, a.wf_instance_status, a.check_result_id, a.quality_description, a.start_time, a.end_time, a.create_by, a.create_time, a.update_by, a.update_time,
 b.instance_activity_id as sub_instance_activity_id, b.quality_instance_id as sub_quality_instance_id, b.process_activity_id as sub_process_activity_id, b.process_handle_resolution as sub_process_handle_resolution, b.process_handle_desc as sub_process_handle_desc, b.process_handle_status as sub_process_handle_status, b.process_step_order as sub_process_step_order, b.start_time as sub_start_time, b.end_time as sub_end_time, b.handle_user_id as sub_handle_user_id, b.handle_by as sub_handle_by, b.handle_time as sub_handle_time, b.create_by as sub_create_by, b.create_time as sub_create_time, b.transfer_user_id as sub_transfer_user_id
        from qms_quality_instance a
        left join qms_quality_instance_activity b on b.quality_instance_id = a.quality_instance_id
        where a.quality_instance_id = #{qualityInstanceId}
    </select>

    <insert id="insertQmsQualityInstance" parameterType="QmsQualityInstance" useGeneratedKeys="true" keyProperty="qualityInstanceId">
        insert into qms_quality_instance
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="wfProcessId != null">wf_process_id,</if>
            <if test="wfInstanceStatus != null and wfInstanceStatus != ''">wf_instance_status,</if>
            <if test="checkResultId != null">check_result_id,</if>
            <if test="qualityDescription != null">quality_description,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="wfProcessId != null">#{wfProcessId},</if>
            <if test="wfInstanceStatus != null and wfInstanceStatus != ''">#{wfInstanceStatus},</if>
            <if test="checkResultId != null">#{checkResultId},</if>
            <if test="qualityDescription != null">#{qualityDescription},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateQmsQualityInstance" parameterType="QmsQualityInstance">
        update qms_quality_instance
        <trim prefix="SET" suffixOverrides=",">
            <if test="wfProcessId != null">wf_process_id = #{wfProcessId},</if>
            <if test="wfInstanceStatus != null and wfInstanceStatus != ''">wf_instance_status = #{wfInstanceStatus},</if>
            <if test="checkResultId != null">check_result_id = #{checkResultId},</if>
            <if test="qualityDescription != null">quality_description = #{qualityDescription},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where quality_instance_id = #{qualityInstanceId}
    </update>

    <delete id="deleteQmsQualityInstanceByQualityInstanceId" parameterType="Long">
        delete from qms_quality_instance where quality_instance_id = #{qualityInstanceId}
    </delete>

    <delete id="deleteQmsQualityInstanceByQualityInstanceIds" parameterType="String">
        delete from qms_quality_instance where quality_instance_id in
        <foreach item="qualityInstanceId" collection="array" open="(" separator="," close=")">
            #{qualityInstanceId}
        </foreach>
    </delete>

    <delete id="deleteQmsQualityInstanceActivityByQualityInstanceIds" parameterType="String">
        delete from qms_quality_instance_activity where quality_instance_id in
        <foreach item="qualityInstanceId" collection="array" open="(" separator="," close=")">
            #{qualityInstanceId}
        </foreach>
    </delete>

    <delete id="deleteQmsQualityInstanceActivityByQualityInstanceId" parameterType="Long">
        delete from qms_quality_instance_activity where quality_instance_id = #{qualityInstanceId}
    </delete>

    <insert id="batchQmsQualityInstanceActivity">
        insert into qms_quality_instance_activity( 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) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.instanceActivityId}, #{item.qualityInstanceId}, #{item.processActivityId}, #{item.processHandleResolution}, #{item.processHandleDesc}, #{item.processHandleStatus}, #{item.processStepOrder}, #{item.startTime}, #{item.endTime}, #{item.handleUserId}, #{item.handleBy}, #{item.handleTime}, #{item.createBy}, #{item.createTime}, #{item.transferUserId})
        </foreach>
    </insert>









    <select id="selectQmsQualityInstanceByCheckResultId" parameterType="Long" resultMap="QmsQualityInstanceQmsQualityInstanceActivityResult">
        select a.quality_instance_id, a.wf_process_id, a.wf_instance_status, a.check_result_id, a.quality_description, a.start_time, a.end_time, a.create_by, a.create_time, a.update_by, a.update_time,
               b.instance_activity_id as sub_instance_activity_id, b.quality_instance_id as sub_quality_instance_id, b.process_activity_id as sub_process_activity_id, b.process_handle_resolution as sub_process_handle_resolution, b.process_handle_desc as sub_process_handle_desc, b.process_handle_status as sub_process_handle_status, b.process_step_order as sub_process_step_order, b.start_time as sub_start_time, b.end_time as sub_end_time, b.handle_user_id as sub_handle_user_id, b.handle_by as sub_handle_by, b.handle_time as sub_handle_time, b.create_by as sub_create_by, b.create_time as sub_create_time, b.transfer_user_id as sub_transfer_user_id
        from qms_quality_instance a
                 left join qms_quality_instance_activity b on b.quality_instance_id = a.quality_instance_id
        where a.check_result_id = #{checkResultId}
    </select>

</mapper>
