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

    <resultMap type="QmsCheckResultDetail" id="QmsCheckResultDetailResult">
        <result property="checkResultDetailId"    column="check_result_detail_id"    />
        <result property="checkResultId"    column="check_result_id"    />
        <result property="materialBarcode"    column="material_barcode"    />
        <result property="checkStatus"    column="check_status"    />
        <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>

    <resultMap id="QmsCheckResultDetailQmsCheckResultDetailProjectResult" type="QmsCheckResultDetail" extends="QmsCheckResultDetailResult">
        <collection property="qmsCheckResultDetailProjectList" notNullColumn="sub_result_detail_project_id" javaType="java.util.List" resultMap="QmsCheckResultDetailProjectResult" />
    </resultMap>

    <resultMap type="QmsCheckResultDetailProject" id="QmsCheckResultDetailProjectResult">
        <result property="resultDetailProjectId"    column="sub_result_detail_project_id"    />
        <result property="checkResultDetailId"    column="sub_check_result_detail_id"    />
        <result property="checkProjectId"    column="sub_check_project_id"    />
        <result property="checkProjectStatus"    column="sub_check_project_status"    />
        <result property="checkProjectResult"    column="sub_check_project_result"    />
        <result property="standardValue"    column="sub_standard_value"    />
        <result property="upperDiff"    column="sub_upper_diff"    />
        <result property="downDiff"    column="sub_down_diff"    />
        <result property="createBy"    column="sub_create_by"    />
        <result property="createTime"    column="sub_create_time"    />
        <result property="updateBy"    column="sub_update_by"    />
        <result property="updateTime"    column="sub_update_time"    />
    </resultMap>

    <sql id="selectQmsCheckResultDetailVo">
        select check_result_detail_id, check_result_id, material_barcode, check_status, remark, create_by, create_time, update_by, update_time from qms_check_result_detail
    </sql>

    <select id="selectQmsCheckResultDetailList" parameterType="QmsCheckResultDetail" resultMap="QmsCheckResultDetailResult">
        <include refid="selectQmsCheckResultDetailVo"/>
        <where>
            <if test="checkResultId != null "> and check_result_id = #{checkResultId}</if>
            <if test="materialBarcode != null  and materialBarcode != ''"> and material_barcode = #{materialBarcode}</if>
            <if test="checkStatus != null  and checkStatus != ''"> and check_status = #{checkStatus}</if>
        </where>
    </select>

    <select id="selectQmsCheckResultDetailByCheckResultDetailId" parameterType="Long" resultMap="QmsCheckResultDetailQmsCheckResultDetailProjectResult">
        select a.check_result_detail_id, a.check_result_id, a.material_barcode, a.check_status, a.remark, a.create_by, a.create_time, a.update_by, a.update_time,
 b.result_detail_project_id as sub_result_detail_project_id, b.check_result_detail_id as sub_check_result_detail_id, b.check_project_id as sub_check_project_id, b.check_project_status as sub_check_project_status, b.check_project_result as sub_check_project_result, b.standard_value as sub_standard_value, b.upper_diff as sub_upper_diff, b.down_diff as sub_down_diff, b.create_by as sub_create_by, b.create_time as sub_create_time, b.update_by as sub_update_by, b.update_time as sub_update_time
        from qms_check_result_detail a
        left join qms_check_result_detail_project b on b.check_result_detail_id = a.check_result_detail_id
        where a.check_result_detail_id = #{checkResultDetailId}
    </select>

    <insert id="insertQmsCheckResultDetail" parameterType="QmsCheckResultDetail" useGeneratedKeys="true" keyProperty="checkResultDetailId">
        insert into qms_check_result_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="checkResultId != null">check_result_id,</if>
            <if test="materialBarcode != null and materialBarcode != ''">material_barcode,</if>
            <if test="checkStatus != null and checkStatus != ''">check_status,</if>
            <if test="remark != null">remark,</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="checkResultId != null">#{checkResultId},</if>
            <if test="materialBarcode != null and materialBarcode != ''">#{materialBarcode},</if>
            <if test="checkStatus != null and checkStatus != ''">#{checkStatus},</if>
            <if test="remark != null">#{remark},</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="updateQmsCheckResultDetail" parameterType="QmsCheckResultDetail">
        update qms_check_result_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="checkResultId != null">check_result_id = #{checkResultId},</if>
            <if test="materialBarcode != null and materialBarcode != ''">material_barcode = #{materialBarcode},</if>
            <if test="checkStatus != null and checkStatus != ''">check_status = #{checkStatus},</if>
            <if test="remark != null">remark = #{remark},</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 check_result_detail_id = #{checkResultDetailId}
    </update>

    <delete id="deleteQmsCheckResultDetailByCheckResultDetailId" parameterType="Long">
        delete from qms_check_result_detail where check_result_detail_id = #{checkResultDetailId}
    </delete>

    <delete id="deleteQmsCheckResultDetailByCheckResultDetailIds" parameterType="String">
        delete from qms_check_result_detail where check_result_detail_id in
        <foreach item="checkResultDetailId" collection="array" open="(" separator="," close=")">
            #{checkResultDetailId}
        </foreach>
    </delete>

    <delete id="deleteQmsCheckResultDetailProjectByCheckResultDetailIds" parameterType="String">
        delete from qms_check_result_detail_project where check_result_detail_id in
        <foreach item="checkResultDetailId" collection="array" open="(" separator="," close=")">
            #{checkResultDetailId}
        </foreach>
    </delete>

    <delete id="deleteQmsCheckResultDetailProjectByCheckResultDetailId" parameterType="Long">
        delete from qms_check_result_detail_project where check_result_detail_id = #{checkResultDetailId}
    </delete>

    <insert id="batchQmsCheckResultDetailProject">
        insert into qms_check_result_detail_project( result_detail_project_id, check_result_detail_id, check_project_id, check_project_status, check_project_result, standard_value, upper_diff, down_diff, create_by, create_time, update_by, update_time) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.resultDetailProjectId}, #{item.checkResultDetailId}, #{item.checkProjectId}, #{item.checkProjectStatus}, #{item.checkProjectResult}, #{item.standardValue}, #{item.upperDiff}, #{item.downDiff}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>





    <select id="selectQmsCheckResultDetailByMaterialBarcode" parameterType="String" resultMap="QmsCheckResultDetailQmsCheckResultDetailProjectResult">
        select a.check_result_detail_id, a.check_result_id, a.material_barcode, a.check_status, a.remark, a.create_by, a.create_time, a.update_by, a.update_time,
               b.result_detail_project_id as sub_result_detail_project_id, b.check_result_detail_id as sub_check_result_detail_id, b.check_project_id as sub_check_project_id, b.check_project_status as sub_check_project_status, b.check_project_result as sub_check_project_result, b.standard_value as sub_standard_value, b.upper_diff as sub_upper_diff, b.down_diff as sub_down_diff, b.create_by as sub_create_by, b.create_time as sub_create_time, b.update_by as sub_update_by, b.update_time as sub_update_time
        from qms_check_result_detail a
                 left join qms_check_result_detail_project b on b.check_result_detail_id = a.check_result_detail_id
        where a.material_barcode = #{materialBarcode} limit 1
    </select>

</mapper>
