<?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.report.mapper.MaterialCompletionMapper">

    <resultMap type="MaterialCompletion" id="MaterialCompletionResult">
        <result property="objId" column="obj_id"/>
        <result property="orderCode" column="order_code"/>
        <result property="materialBarcode" column="material_barcode"/>
        <result property="materialCode" column="material_code"/>
        <result property="materialName" column="material_name"/>
        <result property="stationName" column="station_name"/>
        <result property="completeDate" column="complete_date"/>
        <result property="planCode" column="plan_code"/>
        <result property="productLineCode" column="product_line_code"/>
        <result property="isDownLine" column="is_down_line"/>
        <result property="productionCode" column="production_code"/>
    </resultMap>

    <sql id="selectMaterialCompletionVo">
        select obj_id,
               order_code,
               material_barcode,
               material_code,
               material_name,
               station_name,
               complete_date,
               plan_code,
               product_line_code,
               is_down_line,
               production_code
        from material_completion
    </sql>

    <select id="selectMaterialCompletionList" parameterType="MaterialCompletion" resultMap="MaterialCompletionResult">
        <include refid="selectMaterialCompletionVo"/>
        <where>
            <if test="orderCode != null  and orderCode != ''">and order_code = #{orderCode}</if>
            <if test="materialBarcode != null  and materialBarcode != ''">and material_barcode = #{materialBarcode}</if>
            <if test="materialCode != null  and materialCode != ''">and material_code = #{materialCode}</if>
            <if test="materialName != null  and materialName != ''">and material_name like concat(concat('%',
                #{materialName}), '%')
            </if>
            <if test="stationName != null  and stationName != ''">and station_name like concat(concat('%',
                #{stationName}), '%')
            </if>
            <if test="completeDate != null ">and complete_date = #{completeDate}</if>
            <if test="planCode != null  and planCode != ''">and plan_code = #{planCode}</if>
            <if test="productLineCode != null  and productLineCode != ''">and product_line_code = #{productLineCode}
            </if>
            <if test="isDownLine != null ">and is_down_line = #{isDownLine}</if>
            <if test="productionCode != null  and productionCode != ''">and production_code = #{productionCode}</if>
        </where>
    </select>

    <select id="selectMaterialCompletionByObjId" parameterType="Long" resultMap="MaterialCompletionResult">
        <include refid="selectMaterialCompletionVo"/>
        where obj_id = #{objId}
    </select>

    <insert id="insertMaterialCompletion" parameterType="MaterialCompletion">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_material_completion.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into material_completion
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="orderCode != null">order_code,</if>
            <if test="materialBarcode != null">material_barcode,</if>
            <if test="materialCode != null">material_code,</if>
            <if test="materialName != null">material_name,</if>
            <if test="stationName != null">station_name,</if>
            <if test="completeDate != null">complete_date,</if>
            <if test="planCode != null">plan_code,</if>
            <if test="productLineCode != null">product_line_code,</if>
            <if test="isDownLine != null">is_down_line,</if>
            <if test="productionCode != null">production_code,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="orderCode != null">#{orderCode},</if>
            <if test="materialBarcode != null">#{materialBarcode},</if>
            <if test="materialCode != null">#{materialCode},</if>
            <if test="materialName != null">#{materialName,jdbcType=NVARCHAR},</if>
            <if test="stationName != null">#{stationName},</if>
            <if test="completeDate != null">#{completeDate},</if>
            <if test="planCode != null">#{planCode},</if>
            <if test="productLineCode != null">#{productLineCode},</if>
            <if test="isDownLine != null">#{isDownLine},</if>
            <if test="productionCode != null">#{productionCode},</if>
        </trim>
    </insert>

    <update id="updateMaterialCompletion" parameterType="MaterialCompletion">
        update material_completion
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderCode != null">order_code = #{orderCode},</if>
            <if test="materialBarcode != null">material_barcode = #{materialBarcode},</if>
            <if test="materialCode != null">material_code = #{materialCode},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="completeDate != null">complete_date = #{completeDate},</if>
            <if test="planCode != null">plan_code = #{planCode},</if>
            <if test="productLineCode != null">product_line_code = #{productLineCode},</if>
            <if test="isDownLine != null">is_down_line = #{isDownLine},</if>
            <if test="productionCode != null">production_code = #{productionCode},</if>
        </trim>
        where obj_id = #{objId}
    </update>

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

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