<?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="materialId"    column="material_id"    />
        <result property="checkRuleId"    column="check_rule_id"    />
        <result property="checkRuleType"    column="check_rule_type"    />
        <result property="checkRuleName"    column="check_rule_name"    />
        <result property="checkMode"    column="check_mode"    />
        <result property="checkSample"    column="check_sample"    />
        <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="projectStepOrder"    column="sub_project_step_order"    />
        <result property="standardValue"    column="sub_standard_value"    />
        <result property="checkProjectName"    column="sub_check_project_name"    />
        <result property="checkProjectProperty"    column="sub_check_project_property"    />
        <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>



    <resultMap type="QmsCheckResultDetailVo" id="QmsCheckResultDetailVoResult">
        <result property="passAmount"    column="update_by"    />
        <result property="failAmount"    column="update_time"    />
    </resultMap>

    <sql id="selectQmsCheckResultDetailVo">
        select check_result_detail_id, check_result_id, material_barcode, material_id, check_rule_id, check_rule_type, check_rule_name, check_mode, check_sample, check_status, remark, create_by, create_time, update_by, update_time from qms_check_result_detail
    </sql>

    <sql id="selectQmsCheckResultDetailJoinVo">
        select a.check_result_detail_id, a.check_result_id, a.material_barcode, a.material_id, a.check_rule_id, a.check_rule_type, a.check_rule_name, a.check_mode, a.check_sample, 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.project_step_order as sub_project_step_order,b.standard_value as sub_standard_value,
               b.check_project_name as sub_check_project_name, b.check_project_property as sub_check_project_property,
               b.upper_diff as sub_upper_diff, b.down_diff as sub_down_diff
        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
    </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="materialId != null "> and material_id = #{materialId}</if>
            <if test="checkRuleId != null "> and check_rule_id = #{checkRuleId}</if>
            <if test="checkRuleType != null  and checkRuleType != ''"> and check_rule_type = #{checkRuleType}</if>
            <if test="checkRuleName != null  and checkRuleName != ''"> and check_rule_name like concat('%', #{checkRuleName}, '%')</if>
            <if test="checkMode != null  and checkMode != ''"> and check_mode = #{checkMode}</if>
            <if test="checkSample != null "> and check_sample = #{checkSample}</if>
            <if test="checkStatus != null  and checkStatus != ''"> and check_status = #{checkStatus}</if>
        </where>
    </select>

    <select id="selectQmsCheckResultDetailByCheckResultDetailId" parameterType="Long" resultMap="QmsCheckResultDetailQmsCheckResultDetailProjectResult">
        <include refid="selectQmsCheckResultDetailJoinVo"/>
        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="materialId != null">material_id,</if>
            <if test="checkRuleId != null">check_rule_id,</if>
            <if test="checkRuleType != null and checkRuleType != ''">check_rule_type,</if>
            <if test="checkRuleName != null and checkRuleName != ''">check_rule_name,</if>
            <if test="checkMode != null and checkMode != ''">check_mode,</if>
            <if test="checkSample != null">check_sample,</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="materialId != null">#{materialId},</if>
            <if test="checkRuleId != null">#{checkRuleId},</if>
            <if test="checkRuleType != null and checkRuleType != ''">#{checkRuleType},</if>
            <if test="checkRuleName != null and checkRuleName != ''">#{checkRuleName},</if>
            <if test="checkMode != null and checkMode != ''">#{checkMode},</if>
            <if test="checkSample != null">#{checkSample},</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="materialId != null">material_id = #{materialId},</if>
            <if test="checkRuleId != null">check_rule_id = #{checkRuleId},</if>
            <if test="checkRuleType != null and checkRuleType != ''">check_rule_type = #{checkRuleType},</if>
            <if test="checkRuleName != null and checkRuleName != ''">check_rule_name = #{checkRuleName},</if>
            <if test="checkMode != null and checkMode != ''">check_mode = #{checkMode},</if>
            <if test="checkSample != null">check_sample = #{checkSample},</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,project_step_order, standard_value, check_project_name, check_project_property, 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.projectStepOrder},#{item.standardValue}, #{item.checkProjectName}, #{item.checkProjectProperty}, #{item.upperDiff}, #{item.downDiff}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>




    <select id="selectQmsCheckResultDetailByMaterialBarcode" parameterType="String" resultMap="QmsCheckResultDetailQmsCheckResultDetailProjectResult">
        <include refid="selectQmsCheckResultDetailJoinVo"/>
        where a.material_barcode = #{materialBarcode}
    </select>


    <select id="selectResultDetailQualityCountByResultDetailId" parameterType="QmsCheckResultDetail">
        SELECT count(1) as pass_amount FROM `qms_check_result_detail` where check_status=#{checkStatus} and check_result_id = #{checkResultId}
    </select>

</mapper>
