<?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.ximai.mes.qc.mapper.QcOqcMapper">

    <resultMap type="QcOqc" id="QcOqcResult">
        <result property="oqcId"    column="oqc_id"    />
        <result property="oqcCode"    column="oqc_code"    />
        <result property="oqcName"    column="oqc_name"    />
        <result property="templateId"    column="template_id"    />
        <result property="sourceDocId" column="source_doc_id" ></result>
        <result property="sourceDocType" column="source_doc_type"></result>
        <result property="sourceDocCode" column="source_doc_code"></result>
        <result property="sourceLineId" column="source_line_id"></result>
        <result property="clientId"    column="client_id"    />
        <result property="clientCode"    column="client_code"    />
        <result property="clientName"    column="client_name"    />
        <result property="batchCode"    column="batch_code"    />
        <result property="itemId"    column="item_id"    />
        <result property="itemCode"    column="item_code"    />
        <result property="itemName"    column="item_name"    />
        <result property="specification"    column="specification"    />
        <result property="unitOfMeasure"    column="unit_of_measure"    />
        <result property="quantityMinCheck"    column="quantity_min_check"    />
        <result property="quantityMaxUnqualified"    column="quantity_max_unqualified"    />
        <result property="quantityOut"    column="quantity_out"    />
        <result property="quantityCheck"    column="quantity_check"    />
        <result property="quantityUnqualified"    column="quantity_unqualified"    />
        <result property="quantityQuanlified"    column="quantity_quanlified"    />
        <result property="crRate"    column="cr_rate"    />
        <result property="majRate"    column="maj_rate"    />
        <result property="minRate"    column="min_rate"    />
        <result property="crQuantity"    column="cr_quantity"    />
        <result property="majQuantity"    column="maj_quantity"    />
        <result property="minQuantity"    column="min_quantity"    />
        <result property="checkResult"    column="check_result"    />
        <result property="outDate"    column="out_date"    />
        <result property="inspectDate"    column="inspect_date"    />
        <result property="inspector"    column="inspector"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <result property="attr1"    column="attr1"    />
        <result property="attr2"    column="attr2"    />
        <result property="attr3"    column="attr3"    />
        <result property="attr4"    column="attr4"    />
        <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>

    <sql id="selectQcOqcVo">
        select oqc_id, oqc_code, oqc_name, template_id, source_doc_id,source_doc_type, source_doc_code, source_line_id, client_id, client_code, client_name, batch_code, item_id, item_code, item_name, specification, unit_of_measure, quantity_min_check, quantity_max_unqualified, quantity_out, quantity_check, quantity_unqualified, quantity_quanlified, cr_rate, maj_rate, min_rate, cr_quantity, maj_quantity, min_quantity, check_result, out_date, inspect_date, inspector, status, remark, attr1, attr2, attr3, attr4, create_by, create_time, update_by, update_time from qc_oqc
    </sql>

    <select id="selectQcOqcList" parameterType="QcOqc" resultMap="QcOqcResult">
        <include refid="selectQcOqcVo"/>
        <where>
            <if test="oqcCode != null  and oqcCode != ''"> and oqc_code = #{oqcCode}</if>
            <if test="oqcName != null  and oqcName != ''"> and oqc_name like concat('%', #{oqcName}, '%')</if>
            <if test="templateId != null "> and template_id = #{templateId}</if>
            <if test="sourceDocId != null"> and source_doc_id = #{sourceDocId}</if>
            <if test="sourceDocType !=null and sourceDocType !=''"> and source_doc_type = #{sourceDocType}</if>
            <if test="sourceDocCode !=null and sourceDocCode !=''"> and source_doc_code = #{sourceDocCode}</if>
            <if test="sourceLineId !=null"> and source_line_id = #{sourceLineId}</if>
            <if test="clientId != null "> and client_id = #{clientId}</if>
            <if test="clientCode != null  and clientCode != ''"> and client_code = #{clientCode}</if>
            <if test="clientName != null  and clientName != ''"> and client_name like concat('%', #{clientName}, '%')</if>
            <if test="batchCode != null  and batchCode != ''"> and batch_code = #{batchCode}</if>
            <if test="itemId != null "> and item_id = #{itemId}</if>
            <if test="itemCode != null  and itemCode != ''"> and item_code = #{itemCode}</if>
            <if test="itemName != null  and itemName != ''"> and item_name like concat('%', #{itemName}, '%')</if>
            <if test="specification != null  and specification != ''"> and specification = #{specification}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''"> and unit_of_measure = #{unitOfMeasure}</if>
            <if test="quantityMinCheck != null "> and quantity_min_check = #{quantityMinCheck}</if>
            <if test="quantityMaxUnqualified != null "> and quantity_max_unqualified = #{quantityMaxUnqualified}</if>
            <if test="quantityOut != null "> and quantity_out = #{quantityOut}</if>
            <if test="quantityCheck != null "> and quantity_check = #{quantityCheck}</if>
            <if test="quantityUnqualified != null "> and quantity_unqualified = #{quantityUnqualified}</if>
            <if test="quantityQuanlified != null "> and quantity_quanlified = #{quantityQuanlified}</if>
            <if test="crRate != null "> and cr_rate = #{crRate}</if>
            <if test="majRate != null "> and maj_rate = #{majRate}</if>
            <if test="minRate != null "> and min_rate = #{minRate}</if>
            <if test="crQuantity != null "> and cr_quantity = #{crQuantity}</if>
            <if test="majQuantity != null "> and maj_quantity = #{majQuantity}</if>
            <if test="minQuantity != null "> and min_quantity = #{minQuantity}</if>
            <if test="checkResult != null  and checkResult != ''"> and check_result = #{checkResult}</if>
            <if test="outDate != null "> and out_date = #{outDate}</if>
            <if test="inspectDate != null "> and inspect_date = #{inspectDate}</if>
            <if test="inspector != null  and inspector != ''"> and inspector = #{inspector}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectQcOqcByOqcId" parameterType="Long" resultMap="QcOqcResult">
        <include refid="selectQcOqcVo"/>
        where oqc_id = #{oqcId}
    </select>

    <select id="checkOqcCodeUnique" parameterType="QcOqc" resultMap="QcOqcResult">
        <include refid="selectQcOqcVo"/>
        where oqc_code = #{oqcCode}
    </select>

    <insert id="insertQcOqc" parameterType="QcOqc" useGeneratedKeys="true" keyProperty="oqcId">
        insert into qc_oqc
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="oqcCode != null and oqcCode != ''">oqc_code,</if>
            <if test="oqcName != null">oqc_name,</if>
            <if test="templateId != null">template_id,</if>
            <if test="sourceDocId != null">source_doc_id,</if>
            <if test="sourceDocType !=null and sourceDocType !=''">source_doc_type,</if>
            <if test="sourceDocCode !=null and sourceDocCode !=''">source_doc_code,</if>
            <if test="sourceLineId !=null">source_line_id,</if>
            <if test="clientId != null">client_id,</if>
            <if test="clientCode != null and clientCode != ''">client_code,</if>
            <if test="clientName != null and clientName != ''">client_name,</if>
            <if test="batchCode != null">batch_code,</if>
            <if test="itemId != null">item_id,</if>
            <if test="itemCode != null">item_code,</if>
            <if test="itemName != null">item_name,</if>
            <if test="specification != null">specification,</if>
            <if test="unitOfMeasure != null">unit_of_measure,</if>
            <if test="quantityMinCheck != null">quantity_min_check,</if>
            <if test="quantityMaxUnqualified != null">quantity_max_unqualified,</if>
            <if test="quantityOut != null">quantity_out,</if>
            <if test="quantityCheck != null">quantity_check,</if>
            <if test="quantityUnqualified != null">quantity_unqualified,</if>
            <if test="quantityQuanlified != null">quantity_quanlified,</if>
            <if test="crRate != null">cr_rate,</if>
            <if test="majRate != null">maj_rate,</if>
            <if test="minRate != null">min_rate,</if>
            <if test="crQuantity != null">cr_quantity,</if>
            <if test="majQuantity != null">maj_quantity,</if>
            <if test="minQuantity != null">min_quantity,</if>
            <if test="checkResult != null">check_result,</if>
            <if test="outDate != null">out_date,</if>
            <if test="inspectDate != null">inspect_date,</if>
            <if test="inspector != null">inspector,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="attr1 != null">attr1,</if>
            <if test="attr2 != null">attr2,</if>
            <if test="attr3 != null">attr3,</if>
            <if test="attr4 != null">attr4,</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="oqcCode != null and oqcCode != ''">#{oqcCode},</if>
            <if test="oqcName != null">#{oqcName},</if>
            <if test="templateId != null">#{templateId},</if>
            <if test="sourceDocId != null">#{sourceDocId},</if>
            <if test="sourceDocType !=null and sourceDocType !=''">#{sourceDocType},</if>
            <if test="sourceDocCode !=null and sourceDocCode !=''">#{sourceDocCode},</if>
            <if test="sourceLineId !=null">#{sourceLineId},</if>
            <if test="clientId != null">#{clientId},</if>
            <if test="clientCode != null and clientCode != ''">#{clientCode},</if>
            <if test="clientName != null and clientName != ''">#{clientName},</if>
            <if test="batchCode != null">#{batchCode},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="itemCode != null">#{itemCode},</if>
            <if test="itemName != null">#{itemName},</if>
            <if test="specification != null">#{specification},</if>
            <if test="unitOfMeasure != null">#{unitOfMeasure},</if>
            <if test="quantityMinCheck != null">#{quantityMinCheck},</if>
            <if test="quantityMaxUnqualified != null">#{quantityMaxUnqualified},</if>
            <if test="quantityOut != null">#{quantityOut},</if>
            <if test="quantityCheck != null">#{quantityCheck},</if>
            <if test="quantityUnqualified != null">#{quantityUnqualified},</if>
            <if test="quantityQuanlified != null">#{quantityQuanlified},</if>
            <if test="crRate != null">#{crRate},</if>
            <if test="majRate != null">#{majRate},</if>
            <if test="minRate != null">#{minRate},</if>
            <if test="crQuantity != null">#{crQuantity},</if>
            <if test="majQuantity != null">#{majQuantity},</if>
            <if test="minQuantity != null">#{minQuantity},</if>
            <if test="checkResult != null">#{checkResult},</if>
            <if test="outDate != null">#{outDate},</if>
            <if test="inspectDate != null">#{inspectDate},</if>
            <if test="inspector != null">#{inspector},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="attr1 != null">#{attr1},</if>
            <if test="attr2 != null">#{attr2},</if>
            <if test="attr3 != null">#{attr3},</if>
            <if test="attr4 != null">#{attr4},</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="updateQcOqc" parameterType="QcOqc">
        update qc_oqc
        <trim prefix="SET" suffixOverrides=",">
            <if test="oqcCode != null and oqcCode != ''">oqc_code = #{oqcCode},</if>
            <if test="oqcName != null">oqc_name = #{oqcName},</if>
            <if test="templateId != null">template_id = #{templateId},</if>
            <if test="sourceDocId !=null">source_doc_id = #{sourceDocId},</if>
            <if test="sourceDocType !=null and sourceDocType !=''">source_doc_type = #{sourceDocType},</if>
            <if test="sourceDocCode !=null and sourceDocCode !=''">source_doc_code = #{sourceDocCode},</if>
            <if test="sourceLineId !=null">source_line_id = #{sourceLineId},</if>
            <if test="clientId != null">client_id = #{clientId},</if>
            <if test="clientCode != null and clientCode != ''">client_code = #{clientCode},</if>
            <if test="clientName != null and clientName != ''">client_name = #{clientName},</if>
            <if test="batchCode != null">batch_code = #{batchCode},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="itemCode != null">item_code = #{itemCode},</if>
            <if test="itemName != null">item_name = #{itemName},</if>
            <if test="specification != null">specification = #{specification},</if>
            <if test="unitOfMeasure != null">unit_of_measure = #{unitOfMeasure},</if>
            <if test="quantityMinCheck != null">quantity_min_check = #{quantityMinCheck},</if>
            <if test="quantityMaxUnqualified != null">quantity_max_unqualified = #{quantityMaxUnqualified},</if>
            <if test="quantityOut != null">quantity_out = #{quantityOut},</if>
            <if test="quantityCheck != null">quantity_check = #{quantityCheck},</if>
            <if test="quantityUnqualified != null">quantity_unqualified = #{quantityUnqualified},</if>
            <if test="quantityQuanlified != null">quantity_quanlified = #{quantityQuanlified},</if>
            <if test="crRate != null">cr_rate = #{crRate},</if>
            <if test="majRate != null">maj_rate = #{majRate},</if>
            <if test="minRate != null">min_rate = #{minRate},</if>
            <if test="crQuantity != null">cr_quantity = #{crQuantity},</if>
            <if test="majQuantity != null">maj_quantity = #{majQuantity},</if>
            <if test="minQuantity != null">min_quantity = #{minQuantity},</if>
            <if test="checkResult != null">check_result = #{checkResult},</if>
            <if test="outDate != null">out_date = #{outDate},</if>
            <if test="inspectDate != null">inspect_date = #{inspectDate},</if>
            <if test="inspector != null">inspector = #{inspector},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="attr1 != null">attr1 = #{attr1},</if>
            <if test="attr2 != null">attr2 = #{attr2},</if>
            <if test="attr3 != null">attr3 = #{attr3},</if>
            <if test="attr4 != null">attr4 = #{attr4},</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 oqc_id = #{oqcId}
    </update>


    <update id="updateCrMajMinQuaAndRate" parameterType="Long">
        UPDATE qc_oqc a
            INNER JOIN (
            SELECT SUM(CASE WHEN defect_level = 'CR' THEN defect_quantity ELSE 0 END ) AS cr_quantity,
            SUM(CASE WHEN defect_level = 'MAJ' THEN defect_quantity ELSE 0 END) AS maj_quantity,
            SUM(CASE WHEN defect_level = 'MIN' THEN defect_quantity ELSE 0 END) AS min_quantity,
            qid.`qc_id` as 'oqc_id'
            FROM qc_defect_record qid
            WHERE qid.qc_id = #{oqcId}
            and qc_type = 'OQC'
            GROUP BY qid.qc_id
            ) b
        ON a.`oqc_id` = b.oqc_id
            SET a.cr_quantity=b.cr_quantity,a.maj_quantity=b.maj_quantity,a.min_quantity=b.min_quantity,
                a.`cr_rate`= ROUND(b.cr_quantity/a.`quantity_check`*100,2),
                a.`maj_rate`= ROUND(b.maj_quantity/a.`quantity_check`*100,2),
                a.`min_rate`= ROUND(b.min_quantity/a.`quantity_check`*100,2)
        WHERE a.oqc_id = #{oqcId}
    </update>

    <delete id="deleteQcOqcByOqcId" parameterType="Long">
        delete from qc_oqc where oqc_id = #{oqcId}
    </delete>

    <delete id="deleteQcOqcByOqcIds" parameterType="String">
        delete from qc_oqc where oqc_id in
        <foreach item="oqcId" collection="array" open="(" separator="," close=")">
            #{oqcId}
        </foreach>
    </delete>
</mapper>
