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

    <resultMap type="SamplingPlan" id="SamplingPlanResult">
        <result property="objId" column="obj_id" />
        <result property="planCode" column="plan_code" />
        <result property="planName" column="plan_name" />
        <result property="aqlStandard" column="aql_standard" />
        <result property="inspectionLevel" column="inspection_level" />
        <result property="samplingType" column="sampling_type" />
        <result property="batchRange" column="batch_range" />
        <result property="sampleSize" column="sample_size" />
        <result property="acceptNumber" column="accept_number" />
        <result property="rejectNumber" column="reject_number" />
        <result property="remark" column="remark" />
        <result property="isFlag" column="is_flag" />
        <result property="createdTime" column="created_time" />
        <result property="updatedTime" column="updated_time" />
    </resultMap>

    <sql id="selectSamplingPlanVo">
        select "obj_id", "plan_code", "plan_name", "aql_standard", "inspection_level", "sampling_type",
               "batch_range", "sample_size", "accept_number", "reject_number", "remark", "is_flag",
               "created_time", "updated_time"
        from "sampling_plan"
    </sql>

    <select id="selectSamplingPlanList" parameterType="SamplingPlan" resultMap="SamplingPlanResult">
        <include refid="selectSamplingPlanVo"/>
        <where>
            <if test="planCode != null and planCode != ''"> and "plan_code" = #{planCode}</if>
            <if test="planName != null and planName != ''"> and "plan_name" like concat(concat('%', #{planName}), '%')</if>
            <if test="aqlStandard != null and aqlStandard != ''"> and "aql_standard" = #{aqlStandard}</if>
            <if test="inspectionLevel != null and inspectionLevel != ''"> and "inspection_level" = #{inspectionLevel}</if>
            <if test="samplingType != null and samplingType != ''"> and "sampling_type" = #{samplingType}</if>
            <if test="isFlag != null"> and "is_flag" = #{isFlag}</if>
        </where>
        order by "obj_id" desc
    </select>

    <select id="selectSamplingPlanByObjId" parameterType="Long" resultMap="SamplingPlanResult">
        <include refid="selectSamplingPlanVo"/>
        where "obj_id" = #{objId}
    </select>

    <insert id="insertSamplingPlan" parameterType="SamplingPlan">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_sampling_plan.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into "sampling_plan"
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">"obj_id",</if>
            <if test="planCode != null">"plan_code",</if>
            <if test="planName != null">"plan_name",</if>
            <if test="aqlStandard != null">"aql_standard",</if>
            <if test="inspectionLevel != null">"inspection_level",</if>
            <if test="samplingType != null">"sampling_type",</if>
            <if test="batchRange != null">"batch_range",</if>
            <if test="sampleSize != null">"sample_size",</if>
            <if test="acceptNumber != null">"accept_number",</if>
            <if test="rejectNumber != null">"reject_number",</if>
            <if test="remark != null">"remark",</if>
            <if test="isFlag != null">"is_flag",</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="planCode != null">#{planCode},</if>
            <if test="planName != null">#{planName},</if>
            <if test="aqlStandard != null">#{aqlStandard},</if>
            <if test="inspectionLevel != null">#{inspectionLevel},</if>
            <if test="samplingType != null">#{samplingType},</if>
            <if test="batchRange != null">#{batchRange},</if>
            <if test="sampleSize != null">#{sampleSize},</if>
            <if test="acceptNumber != null">#{acceptNumber},</if>
            <if test="rejectNumber != null">#{rejectNumber},</if>
            <if test="remark != null">#{remark},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
        </trim>
    </insert>

    <update id="updateSamplingPlan" parameterType="SamplingPlan">
        update "sampling_plan"
        <trim prefix="SET" suffixOverrides=",">
            <if test="planCode != null">"plan_code" = #{planCode},</if>
            <if test="planName != null">"plan_name" = #{planName},</if>
            <if test="aqlStandard != null">"aql_standard" = #{aqlStandard},</if>
            <if test="inspectionLevel != null">"inspection_level" = #{inspectionLevel},</if>
            <if test="samplingType != null">"sampling_type" = #{samplingType},</if>
            <if test="batchRange != null">"batch_range" = #{batchRange},</if>
            <if test="sampleSize != null">"sample_size" = #{sampleSize},</if>
            <if test="acceptNumber != null">"accept_number" = #{acceptNumber},</if>
            <if test="rejectNumber != null">"reject_number" = #{rejectNumber},</if>
            <if test="remark != null">"remark" = #{remark},</if>
            <if test="isFlag != null">"is_flag" = #{isFlag},</if>
            <if test="updatedTime != null">"updated_time" = #{updatedTime},</if>
        </trim>
        where "obj_id" = #{objId}
    </update>

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

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