<?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.aucma.base.mapper.QualityTraceMapper">

    <resultMap type="QualityTrace" id="QualityTraceResult">
        <result property="objId" column="obj_id" />
        <result property="traceNo" column="trace_no" />
        <result property="productCode" column="product_code" />
        <result property="productName" column="product_name" />
        <result property="workOrderNo" column="work_order_no" />
        <result property="batchNo" column="batch_no" />
        <result property="productionDate" column="production_date" />
        <result property="productionLine" column="production_line" />
        <result property="stationCode" column="station_code" />
        <result property="stationName" column="station_name" />
        <result property="inspectionNo" column="inspection_no" />
        <result property="inspectionType" column="inspection_type" />
        <result property="inspectionResult" column="inspection_result" />
        <result property="defectCode" column="defect_code" />
        <result property="defectName" column="defect_name" />
        <result property="defectQty" column="defect_qty" />
        <result property="handlingMeasure" column="handling_measure" />
        <result property="responsiblePerson" column="responsible_person" />
        <result property="remark" column="remark" />
        <result property="createdTime" column="created_time" />
        <result property="updatedTime" column="updated_time" />
    </resultMap>

    <sql id="selectQualityTraceVo">
        select "obj_id", "trace_no", "product_code", "product_name", "work_order_no", "batch_no",
               "production_date", "production_line", "station_code", "station_name", "inspection_no",
               "inspection_type", "inspection_result", "defect_code", "defect_name", "defect_qty",
               "handling_measure", "responsible_person", "remark", "created_time", "updated_time"
        from "quality_trace"
    </sql>

    <select id="selectQualityTraceList" parameterType="QualityTrace" resultMap="QualityTraceResult">
        <include refid="selectQualityTraceVo"/>
        <where>
            <if test="traceNo != null and traceNo != ''"> and "trace_no" = #{traceNo}</if>
            <if test="productCode != null and productCode != ''"> and "product_code" = #{productCode}</if>
            <if test="productName != null and productName != ''"> and "product_name" like concat(concat('%', #{productName}), '%')</if>
            <if test="workOrderNo != null and workOrderNo != ''"> and "work_order_no" = #{workOrderNo}</if>
            <if test="batchNo != null and batchNo != ''"> and "batch_no" = #{batchNo}</if>
            <if test="productionLine != null and productionLine != ''"> and "production_line" = #{productionLine}</if>
            <if test="stationCode != null and stationCode != ''"> and "station_code" = #{stationCode}</if>
            <if test="inspectionNo != null and inspectionNo != ''"> and "inspection_no" = #{inspectionNo}</if>
            <if test="inspectionType != null and inspectionType != ''"> and "inspection_type" = #{inspectionType}</if>
            <if test="inspectionResult != null and inspectionResult != ''"> and "inspection_result" = #{inspectionResult}</if>
            <if test="defectCode != null and defectCode != ''"> and "defect_code" = #{defectCode}</if>
        </where>
        order by "obj_id" desc
    </select>

    <select id="selectQualityTraceByObjId" parameterType="Long" resultMap="QualityTraceResult">
        <include refid="selectQualityTraceVo"/>
        where "obj_id" = #{objId}
    </select>

    <insert id="insertQualityTrace" parameterType="QualityTrace">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_quality_trace.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into "quality_trace"
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">"obj_id",</if>
            <if test="traceNo != null">"trace_no",</if>
            <if test="productCode != null">"product_code",</if>
            <if test="productName != null">"product_name",</if>
            <if test="workOrderNo != null">"work_order_no",</if>
            <if test="batchNo != null">"batch_no",</if>
            <if test="productionDate != null">"production_date",</if>
            <if test="productionLine != null">"production_line",</if>
            <if test="stationCode != null">"station_code",</if>
            <if test="stationName != null">"station_name",</if>
            <if test="inspectionNo != null">"inspection_no",</if>
            <if test="inspectionType != null">"inspection_type",</if>
            <if test="inspectionResult != null">"inspection_result",</if>
            <if test="defectCode != null">"defect_code",</if>
            <if test="defectName != null">"defect_name",</if>
            <if test="defectQty != null">"defect_qty",</if>
            <if test="handlingMeasure != null">"handling_measure",</if>
            <if test="responsiblePerson != null">"responsible_person",</if>
            <if test="remark != null">"remark",</if>
            <if test="createdTime != null">"created_time",</if>
            <if test="updatedTime != null">"updated_time",</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="traceNo != null">#{traceNo},</if>
            <if test="productCode != null">#{productCode},</if>
            <if test="productName != null">#{productName},</if>
            <if test="workOrderNo != null">#{workOrderNo},</if>
            <if test="batchNo != null">#{batchNo},</if>
            <if test="productionDate != null">#{productionDate},</if>
            <if test="productionLine != null">#{productionLine},</if>
            <if test="stationCode != null">#{stationCode},</if>
            <if test="stationName != null">#{stationName},</if>
            <if test="inspectionNo != null">#{inspectionNo},</if>
            <if test="inspectionType != null">#{inspectionType},</if>
            <if test="inspectionResult != null">#{inspectionResult},</if>
            <if test="defectCode != null">#{defectCode},</if>
            <if test="defectName != null">#{defectName},</if>
            <if test="defectQty != null">#{defectQty},</if>
            <if test="handlingMeasure != null">#{handlingMeasure},</if>
            <if test="responsiblePerson != null">#{responsiblePerson},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
        </trim>
    </insert>

    <update id="updateQualityTrace" parameterType="QualityTrace">
        update "quality_trace"
        <trim prefix="SET" suffixOverrides=",">
            <if test="traceNo != null">"trace_no" = #{traceNo},</if>
            <if test="productCode != null">"product_code" = #{productCode},</if>
            <if test="productName != null">"product_name" = #{productName},</if>
            <if test="workOrderNo != null">"work_order_no" = #{workOrderNo},</if>
            <if test="batchNo != null">"batch_no" = #{batchNo},</if>
            <if test="productionDate != null">"production_date" = #{productionDate},</if>
            <if test="productionLine != null">"production_line" = #{productionLine},</if>
            <if test="stationCode != null">"station_code" = #{stationCode},</if>
            <if test="stationName != null">"station_name" = #{stationName},</if>
            <if test="inspectionNo != null">"inspection_no" = #{inspectionNo},</if>
            <if test="inspectionType != null">"inspection_type" = #{inspectionType},</if>
            <if test="inspectionResult != null">"inspection_result" = #{inspectionResult},</if>
            <if test="defectCode != null">"defect_code" = #{defectCode},</if>
            <if test="defectName != null">"defect_name" = #{defectName},</if>
            <if test="defectQty != null">"defect_qty" = #{defectQty},</if>
            <if test="handlingMeasure != null">"handling_measure" = #{handlingMeasure},</if>
            <if test="responsiblePerson != null">"responsible_person" = #{responsiblePerson},</if>
            <if test="remark != null">"remark" = #{remark},</if>
            <if test="updatedTime != null">"updated_time" = #{updatedTime},</if>
        </trim>
        where "obj_id" = #{objId}
    </update>

    <delete id="deleteQualityTraceByObjId" parameterType="Long">
        delete from "quality_trace" where "obj_id" = #{objId}
    </delete>

    <delete id="deleteQualityTraceByObjIds" parameterType="String">
        delete from "quality_trace" where "obj_id" in
        <foreach item="objId" collection="array" open="(" separator="," close=")">
            #{objId}
        </foreach>
    </delete>
</mapper>
