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

    <resultMap type="FirstLastInspection" id="FirstLastInspectionResult">
        <result property="objId" column="obj_id" />
        <result property="inspectionNo" column="inspection_no" />
        <result property="inspectionType" column="inspection_type" />
        <result property="productCode" column="product_code" />
        <result property="productName" column="product_name" />
        <result property="workOrderNo" column="work_order_no" />
        <result property="stationCode" column="station_code" />
        <result property="stationName" column="station_name" />
        <result property="standardCode" column="standard_code" />
        <result property="inspector" column="inspector" />
        <result property="inspectionTime" column="inspection_time" />
        <result property="inspectionResult" column="inspection_result" />
        <result property="qualifiedQty" column="qualified_qty" />
        <result property="unqualifiedQty" column="unqualified_qty" />
        <result property="defectDescription" column="defect_description" />
        <result property="remark" column="remark" />
        <result property="status" column="status" />
        <result property="createdTime" column="created_time" />
        <result property="updatedTime" column="updated_time" />
    </resultMap>

    <sql id="selectFirstLastInspectionVo">
        select "obj_id", "inspection_no", "inspection_type", "product_code", "product_name", "work_order_no",
               "station_code", "station_name", "standard_code", "inspector", "inspection_time", "inspection_result",
               "qualified_qty", "unqualified_qty", "defect_description", "remark", "status", "created_time", "updated_time"
        from "first_last_inspection"
    </sql>

    <select id="selectFirstLastInspectionList" parameterType="FirstLastInspection" resultMap="FirstLastInspectionResult">
        <include refid="selectFirstLastInspectionVo"/>
        <where>
            <if test="inspectionNo != null and inspectionNo != ''"> and "inspection_no" = #{inspectionNo}</if>
            <if test="inspectionType != null and inspectionType != ''"> and "inspection_type" = #{inspectionType}</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="stationCode != null and stationCode != ''"> and "station_code" = #{stationCode}</if>
            <if test="inspector != null and inspector != ''"> and "inspector" = #{inspector}</if>
            <if test="inspectionResult != null and inspectionResult != ''"> and "inspection_result" = #{inspectionResult}</if>
            <if test="status != null and status != ''"> and "status" = #{status}</if>
        </where>
        order by "obj_id" desc
    </select>

    <select id="selectFirstLastInspectionByObjId" parameterType="Long" resultMap="FirstLastInspectionResult">
        <include refid="selectFirstLastInspectionVo"/>
        where "obj_id" = #{objId}
    </select>

    <insert id="insertFirstLastInspection" parameterType="FirstLastInspection">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_first_last_inspection.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into "first_last_inspection"
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">"obj_id",</if>
            <if test="inspectionNo != null">"inspection_no",</if>
            <if test="inspectionType != null">"inspection_type",</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="stationCode != null">"station_code",</if>
            <if test="stationName != null">"station_name",</if>
            <if test="standardCode != null">"standard_code",</if>
            <if test="inspector != null">"inspector",</if>
            <if test="inspectionTime != null">"inspection_time",</if>
            <if test="inspectionResult != null">"inspection_result",</if>
            <if test="qualifiedQty != null">"qualified_qty",</if>
            <if test="unqualifiedQty != null">"unqualified_qty",</if>
            <if test="defectDescription != null">"defect_description",</if>
            <if test="remark != null">"remark",</if>
            <if test="status != null">"status",</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="inspectionNo != null">#{inspectionNo},</if>
            <if test="inspectionType != null">#{inspectionType},</if>
            <if test="productCode != null">#{productCode},</if>
            <if test="productName != null">#{productName},</if>
            <if test="workOrderNo != null">#{workOrderNo},</if>
            <if test="stationCode != null">#{stationCode},</if>
            <if test="stationName != null">#{stationName},</if>
            <if test="standardCode != null">#{standardCode},</if>
            <if test="inspector != null">#{inspector},</if>
            <if test="inspectionTime != null">#{inspectionTime},</if>
            <if test="inspectionResult != null">#{inspectionResult},</if>
            <if test="qualifiedQty != null">#{qualifiedQty},</if>
            <if test="unqualifiedQty != null">#{unqualifiedQty},</if>
            <if test="defectDescription != null">#{defectDescription},</if>
            <if test="remark != null">#{remark},</if>
            <if test="status != null">#{status},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
        </trim>
    </insert>

    <update id="updateFirstLastInspection" parameterType="FirstLastInspection">
        update "first_last_inspection"
        <trim prefix="SET" suffixOverrides=",">
            <if test="inspectionNo != null">"inspection_no" = #{inspectionNo},</if>
            <if test="inspectionType != null">"inspection_type" = #{inspectionType},</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="stationCode != null">"station_code" = #{stationCode},</if>
            <if test="stationName != null">"station_name" = #{stationName},</if>
            <if test="standardCode != null">"standard_code" = #{standardCode},</if>
            <if test="inspector != null">"inspector" = #{inspector},</if>
            <if test="inspectionTime != null">"inspection_time" = #{inspectionTime},</if>
            <if test="inspectionResult != null">"inspection_result" = #{inspectionResult},</if>
            <if test="qualifiedQty != null">"qualified_qty" = #{qualifiedQty},</if>
            <if test="unqualifiedQty != null">"unqualified_qty" = #{unqualifiedQty},</if>
            <if test="defectDescription != null">"defect_description" = #{defectDescription},</if>
            <if test="remark != null">"remark" = #{remark},</if>
            <if test="status != null">"status" = #{status},</if>
            <if test="updatedTime != null">"updated_time" = #{updatedTime},</if>
        </trim>
        where "obj_id" = #{objId}
    </update>

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

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