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

    <resultMap type="InspectionReport" id="InspectionReportResult">
        <result property="objId" column="obj_id" />
        <result property="reportNo" column="report_no" />
        <result property="reportName" column="report_name" />
        <result property="productCode" column="product_code" />
        <result property="productName" column="product_name" />
        <result property="workOrderNo" column="work_order_no" />
        <result property="inspectionType" column="inspection_type" />
        <result property="totalInspections" column="total_inspections" />
        <result property="passedQty" column="passed_qty" />
        <result property="failedQty" column="failed_qty" />
        <result property="pendingQty" column="pending_qty" />
        <result property="passRate" column="pass_rate" />
        <result property="startTime" column="start_time" />
        <result property="endTime" column="end_time" />
        <result property="createdBy" column="created_by" />
        <result property="createdTime" column="created_time" />
        <result property="updatedTime" column="updated_time" />
    </resultMap>

    <sql id="selectInspectionReportVo">
        select "obj_id", "report_no", "report_name", "product_code", "product_name", "work_order_no",
               "inspection_type", "total_inspections", "passed_qty", "failed_qty", "pending_qty", "pass_rate",
               "start_time", "end_time", "created_by", "created_time", "updated_time"
        from "inspection_report"
    </sql>

    <select id="selectInspectionReportList" parameterType="InspectionReport" resultMap="InspectionReportResult">
        <include refid="selectInspectionReportVo"/>
        <where>
            <if test="reportNo != null and reportNo != ''"> and "report_no" = #{reportNo}</if>
            <if test="reportName != null and reportName != ''"> and "report_name" like concat(concat('%', #{reportName}), '%')</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="inspectionType != null and inspectionType != ''"> and "inspection_type" = #{inspectionType}</if>
        </where>
        order by "obj_id" desc
    </select>

    <select id="selectInspectionReportByObjId" parameterType="Long" resultMap="InspectionReportResult">
        <include refid="selectInspectionReportVo"/>
        where "obj_id" = #{objId}
    </select>

    <insert id="insertInspectionReport" parameterType="InspectionReport">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_inspection_report.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into "inspection_report"
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">"obj_id",</if>
            <if test="reportNo != null">"report_no",</if>
            <if test="reportName != null">"report_name",</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="inspectionType != null">"inspection_type",</if>
            <if test="totalInspections != null">"total_inspections",</if>
            <if test="passedQty != null">"passed_qty",</if>
            <if test="failedQty != null">"failed_qty",</if>
            <if test="pendingQty != null">"pending_qty",</if>
            <if test="passRate != null">"pass_rate",</if>
            <if test="startTime != null">"start_time",</if>
            <if test="endTime != null">"end_time",</if>
            <if test="createdBy != null">"created_by",</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="reportNo != null">#{reportNo},</if>
            <if test="reportName != null">#{reportName},</if>
            <if test="productCode != null">#{productCode},</if>
            <if test="productName != null">#{productName},</if>
            <if test="workOrderNo != null">#{workOrderNo},</if>
            <if test="inspectionType != null">#{inspectionType},</if>
            <if test="totalInspections != null">#{totalInspections},</if>
            <if test="passedQty != null">#{passedQty},</if>
            <if test="failedQty != null">#{failedQty},</if>
            <if test="pendingQty != null">#{pendingQty},</if>
            <if test="passRate != null">#{passRate},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="createdBy != null">#{createdBy},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
        </trim>
    </insert>

    <update id="updateInspectionReport" parameterType="InspectionReport">
        update "inspection_report"
        <trim prefix="SET" suffixOverrides=",">
            <if test="reportNo != null">"report_no" = #{reportNo},</if>
            <if test="reportName != null">"report_name" = #{reportName},</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="inspectionType != null">"inspection_type" = #{inspectionType},</if>
            <if test="totalInspections != null">"total_inspections" = #{totalInspections},</if>
            <if test="passedQty != null">"passed_qty" = #{passedQty},</if>
            <if test="failedQty != null">"failed_qty" = #{failedQty},</if>
            <if test="pendingQty != null">"pending_qty" = #{pendingQty},</if>
            <if test="passRate != null">"pass_rate" = #{passRate},</if>
            <if test="startTime != null">"start_time" = #{startTime},</if>
            <if test="endTime != null">"end_time" = #{endTime},</if>
            <if test="updatedTime != null">"updated_time" = #{updatedTime},</if>
        </trim>
        where "obj_id" = #{objId}
    </update>

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

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