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

    <resultMap type="PrintSupplementBarCode" id="PrintSupplementBarCodeResult">
        <result property="objId" column="obj_id"/>
        <result property="orderCode" column="order_code"/>
        <result property="materialCode" column="material_code"/>
        <result property="materialName" column="material_name"/>
        <result property="materialBarcode" column="material_barcode"/>
        <result property="printTime" column="print_time"/>
        <result property="printBartype" column="print_bartype"/>
        <result property="printName" column="print_name"/>
        <result property="isPrint" column="is_print"/>
        <result property="printReason" column="print_reason"/>
        <result property="recordTime" column="record_time"/>
        <result property="createBy" column="create_by"/>
        <result property="productLineCode" column="PRODUCT_LINE_CODE"/>
    </resultMap>

    <sql id="selectPrintSupplementBarCodeVo">
        SELECT PS.ORDER_CODE,
               PS.MATERIAL_CODE,
               BM.MATERIAL_NAME,
               PS.MATERIAL_BARCODE,
               PS.PRINT_TIME,
               CASE WHEN PS.PRINT_BARTYPE = '1' THEN '箱体' ELSE '内胆' END PRINT_BARTYPE,
               PS.PRINT_NAME,
               PS.print_reason,
               PS.record_time,
               PS.create_by,
               PB.PRODUCT_LINE_CODE,
               CASE WHEN PS.IS_PRINT = '1' THEN '已打印' ELSE '未打印' END    IS_PRINT
        FROM PRINT_SUPPLEMENTBARCODE PS
                 LEFT JOIN BASE_MATERIALINFO BM ON BM.MATERIAL_CODE = PS.MATERIAL_CODE
                 LEFT JOIN PRINT_BARCODE PB ON PB.MATERIAL_BARCODE = PS.MATERIAL_CODE
    </sql>

    <select id="selectPrintSupplementBarCodeList" parameterType="PrintSupplementBarCode"
            resultMap="PrintSupplementBarCodeResult">
        <include refid="selectPrintSupplementBarCodeVo"/>
        <where>
            <if test="orderCode != null  and orderCode != ''">and PS.order_code = #{orderCode}</if>
            <if test="materialCode != null  and materialCode != ''">and PS.material_code = #{materialCode}</if>
            <if test="materialName != null  and materialName != ''">and PS.material_name like concat(concat('%',
                #{materialName}), '%')
            </if>
            <if test="materialBarcode != null  and materialBarcode != ''">and PS.material_barcode = #{materialBarcode}</if>
            <if test="params.beginPrintTime != null and params.beginPrintTime != '' and params.endPrintTime != null and params.endPrintTime != ''">
                and PS.print_time between to_date(#{params.beginPrintTime}, 'yyyy-mm-dd hh24:mi:ss') and
                to_date(#{params.endPrintTime}, 'yyyy-mm-dd hh24:mi:ss')
            </if>
            <if test="printBartype != null ">and PS.print_bartype = #{printBartype}</if>
            <if test="printReason != null and printReason != '' ">and PS.print_reason like concat(concat('%',
                #{printReason}), '%')</if>
            <if test="printName != null  and printName != ''">and PS.print_name like concat(concat('%', #{printName}),
                '%')
            </if>
            <if test="isPrint != null  and isPrint != ''">and PS.is_print = #{isPrint}</if>
            <if test="productLineCode != null  and productLineCode != ''">and PB.PRODUCT_LINE_CODE = #{productLineCode}</if>
        </where>
        ORDER BY PS.record_time DESC
    </select>

    <select id="selectPrintSupplementBarCodeByObjId" parameterType="Long" resultMap="PrintSupplementBarCodeResult">
        <include refid="selectPrintSupplementBarCodeVo"/>
        where PS.obj_id = #{objId}
    </select>


    <update id="updatePrintSupplementBarCode" parameterType="PrintSupplementBarCode">
        update print_supplementbarcode
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderCode != null">order_code = #{orderCode},</if>
            <if test="materialCode != null">material_code = #{materialCode},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="materialBarcode != null">material_barcode = #{materialBarcode},</if>
            <if test="printTime != null">print_time = #{printTime},</if>
            <if test="printBartype != null">print_bartype = #{printBartype},</if>
            <if test="printName != null">print_name = #{printName},</if>
            <if test="isPrint != null">is_print = #{isPrint},</if>
            <if test="printReason != null">print_reason = #{printReason},</if>
        </trim>
        where obj_id = #{objId}
    </update>

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

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