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

    <resultMap type="QmsCheckResult" id="QmsCheckResultResult">
        <result property="checkResultId"    column="check_result_id"    />
        <result property="materialType"    column="material_type"    />
        <result property="materialId"    column="material_id"    />
        <result property="materialBatch"    column="material_batch"    />
        <result property="checkMode"    column="check_mode"    />
        <result property="checkSample"    column="check_sample"    />
        <result property="planCode"    column="plan_code"    />
        <result property="planDetailCode"    column="plan_detail_code"    />
        <result property="saleorderCode"    column="saleorder_code"    />
        <result property="poNo"    column="po_no"    />
        <result property="poLine"    column="po_line"    />
        <result property="projectNo"    column="project_no"    />
        <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"    />
        <result property="attr1"    column="attr1"    />
    </resultMap>

    <resultMap id="QmsCheckResultQmsCheckResultDetailResult" type="QmsCheckResult" extends="QmsCheckResultResult">
        <collection property="qmsCheckResultDetailList" notNullColumn="sub_check_result_detail_id" javaType="java.util.List" resultMap="QmsCheckResultDetailResult" />
    </resultMap>

    <resultMap type="QmsCheckResultDetail" id="QmsCheckResultDetailResult">
        <result property="checkResultDetailId"    column="sub_check_result_detail_id"    />
        <result property="checkResultId"    column="sub_check_result_id"    />
        <result property="materialBarcode"    column="sub_material_barcode"    />
        <result property="checkStatus"    column="sub_check_status"    />
        <result property="remark"    column="sub_remark"    />
        <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="selectQmsCheckResultVo">
        select check_result_id, material_type, material_id, material_batch, check_mode, check_sample, plan_code, plan_detail_code, saleorder_code, po_no, po_line, project_no, check_status, remark, create_by, create_time, update_by, update_time, attr1 from qms_check_result
    </sql>

    <select id="selectQmsCheckResultList" parameterType="QmsCheckResult" resultMap="QmsCheckResultResult">
        <include refid="selectQmsCheckResultVo"/>
        <where>
            <if test="materialType != null  and materialType != ''"> and material_type = #{materialType}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="materialBatch != null  and materialBatch != ''"> and material_batch = #{materialBatch}</if>
            <if test="checkMode != null  and checkMode != ''"> and check_mode = #{checkMode}</if>
            <if test="checkSample != null "> and check_sample = #{checkSample}</if>
            <if test="planCode != null  and planCode != ''"> and plan_code = #{planCode}</if>
            <if test="planDetailCode != null  and planDetailCode != ''"> and plan_detail_code = #{planDetailCode}</if>
            <if test="saleorderCode != null  and saleorderCode != ''"> and saleorder_code = #{saleorderCode}</if>
            <if test="poNo != null  and poNo != ''"> and po_no = #{poNo}</if>
            <if test="poLine != null  and poLine != ''"> and po_line = #{poLine}</if>
            <if test="projectNo != null  and projectNo != ''"> and project_no = #{projectNo}</if>
            <if test="checkStatus != null  and checkStatus != ''"> and check_status = #{checkStatus}</if>
            <if test="attr1 != null  and attr1 != ''"> and attr1 = #{attr1}</if>
        </where>
    </select>

    <select id="selectQmsCheckResultByCheckResultId" parameterType="Long" resultMap="QmsCheckResultQmsCheckResultDetailResult">
        select a.check_result_id, a.material_type, a.material_id, a.material_batch, a.check_mode, a.check_sample, a.plan_code, a.plan_detail_code, a.saleorder_code, a.po_no, a.po_line, a.project_no, a.check_status, a.remark, a.create_by, a.create_time, a.update_by, a.update_time, a.attr1,
 b.check_result_detail_id as sub_check_result_detail_id, b.check_result_id as sub_check_result_id, b.material_barcode as sub_material_barcode, b.check_status as sub_check_status, b.remark as sub_remark, 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 a
        left join qms_check_result_detail b on b.check_result_id = a.check_result_id
        where a.check_result_id = #{checkResultId}
    </select>

    <insert id="insertQmsCheckResult" parameterType="QmsCheckResult" useGeneratedKeys="true" keyProperty="checkResultId">
        insert into qms_check_result
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialType != null">material_type,</if>
            <if test="materialId != null">material_id,</if>
            <if test="materialBatch != null and materialBatch != ''">material_batch,</if>
            <if test="checkMode != null">check_mode,</if>
            <if test="checkSample != null">check_sample,</if>
            <if test="planCode != null">plan_code,</if>
            <if test="planDetailCode != null">plan_detail_code,</if>
            <if test="saleorderCode != null">saleorder_code,</if>
            <if test="poNo != null">po_no,</if>
            <if test="poLine != null">po_line,</if>
            <if test="projectNo != null">project_no,</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>
            <if test="attr1 != null">attr1,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialType != null">#{materialType},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="materialBatch != null and materialBatch != ''">#{materialBatch},</if>
            <if test="checkMode != null">#{checkMode},</if>
            <if test="checkSample != null">#{checkSample},</if>
            <if test="planCode != null">#{planCode},</if>
            <if test="planDetailCode != null">#{planDetailCode},</if>
            <if test="saleorderCode != null">#{saleorderCode},</if>
            <if test="poNo != null">#{poNo},</if>
            <if test="poLine != null">#{poLine},</if>
            <if test="projectNo != null">#{projectNo},</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>
            <if test="attr1 != null">#{attr1},</if>
         </trim>
    </insert>

    <update id="updateQmsCheckResult" parameterType="QmsCheckResult">
        update qms_check_result
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialType != null">material_type = #{materialType},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="materialBatch != null and materialBatch != ''">material_batch = #{materialBatch},</if>
            <if test="checkMode != null">check_mode = #{checkMode},</if>
            <if test="checkSample != null">check_sample = #{checkSample},</if>
            <if test="planCode != null">plan_code = #{planCode},</if>
            <if test="planDetailCode != null">plan_detail_code = #{planDetailCode},</if>
            <if test="saleorderCode != null">saleorder_code = #{saleorderCode},</if>
            <if test="poNo != null">po_no = #{poNo},</if>
            <if test="poLine != null">po_line = #{poLine},</if>
            <if test="projectNo != null">project_no = #{projectNo},</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>
            <if test="attr1 != null">attr1 = #{attr1},</if>
        </trim>
        where check_result_id = #{checkResultId}
    </update>

    <delete id="deleteQmsCheckResultByCheckResultId" parameterType="Long">
        delete from qms_check_result where check_result_id = #{checkResultId}
    </delete>

    <delete id="deleteQmsCheckResultByCheckResultIds" parameterType="String">
        delete from qms_check_result where check_result_id in
        <foreach item="checkResultId" collection="array" open="(" separator="," close=")">
            #{checkResultId}
        </foreach>
    </delete>

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

    <delete id="deleteQmsCheckResultDetailByCheckResultId" parameterType="Long">
        delete from qms_check_result_detail where check_result_id = #{checkResultId}
    </delete>

    <insert id="batchQmsCheckResultDetail">
        insert into qms_check_result_detail( check_result_detail_id, check_result_id, material_barcode, check_status, remark, create_by, create_time, update_by, update_time) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.checkResultDetailId}, #{item.checkResultId}, #{item.materialBarcode}, #{item.checkStatus}, #{item.remark}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>
</mapper>
