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

    <resultMap type="RecordSwitchOrder" id="RecordSwitchOrderResult">
        <result property="objId" column="obj_id"/>
        <result property="materialBarcode" column="material_barcode"/>
        <result property="switchStationCode" column="switch_station_code"/>
        <result property="switchStationName" column="switch_station_name"/>
        <result property="oldOrderCode" column="old_order_code"/>
        <result property="isSwitch" column="is_switch"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="orderCode" column="order_code"/>
        <result property="isPrint" column="is_print"/>
    </resultMap>

    <sql id="selectRecordSwitchOrderVo">
        SELECT rso.obj_id,
               rso.material_barcode,
               rso.switch_station_code,
               pl.PRODUCT_LINE_NAME switch_station_name,
               rso.old_order_code,
               rso.is_switch,
               rso.create_by,
               rso.create_time,
               rso.update_by,
               rso.update_time,
               rso.remark,
               PB.ORDER_CODE,
               ps.IS_PRINT          is_print
        FROM record_switch_order rso
                 left join BASE_PRODUCTLINE pl ON rso.switch_station_code = pl.PRODUCT_LINE_CODE
                 left join PRINT_BARCODE PB ON PB.MATERIAL_BARCODE = rso.MATERIAL_BARCODE
                 left join (SELECT MATERIAL_BARCODE, MIN(IS_PRINT) IS_PRINT
                            FROM PRINT_SUPPLEMENTBARCODE
                            GROUP BY MATERIAL_BARCODE) ps ON ps.MATERIAL_BARCODE = rso.MATERIAL_BARCODE
    </sql>

    <select id="selectRecordSwitchOrderList" parameterType="RecordSwitchOrder" resultMap="RecordSwitchOrderResult">
        <include refid="selectRecordSwitchOrderVo"/>
        <where>
            <if test="objId != null ">and rso.obj_id = #{objId}</if>
            <if test="materialBarcode != null  and materialBarcode != ''">and rso.material_barcode = #{materialBarcode}</if>
            <if test="switchStationCode != null  and switchStationCode != ''">and rso.switch_station_code =
                #{switchStationCode}
            </if>
            <if test="oldOrderCode != null  and oldOrderCode != ''">and rso.old_order_code = #{oldOrderCode}</if>
            <if test="isSwitch != null  and isSwitch != ''">and rso.is_switch = #{isSwitch}</if>
            <if test="createBy != null  and createBy != ''">and rso.create_by = #{createBy}</if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
                and rso.create_time between to_date(#{params.beginCreateTime}, 'yyyy-mm-dd hh24:mi:ss') and
                to_date(#{params.endCreateTime}, 'yyyy-mm-dd hh24:mi:ss')
            </if>
            <if test="updateBy != null  and updateBy != ''">and rso.update_by = #{updateBy}</if>
            <if test="params.beginUpdateTime != null and params.beginUpdateTime != '' and params.endUpdateTime != null and params.endUpdateTime != ''">
                and rso.update_time between to_date(#{params.beginUpdateTime}, 'yyyy-mm-dd hh24:mi:ss') and
                to_date(#{params.endUpdateTime}, 'yyyy-mm-dd hh24:mi:ss')
            </if>
            <if test="orderCode != null  and orderCode != ''">and PB.order_code = #{orderCode}</if>
            <if test="isPrint != null  and isPrint != ''">and rso.is_print = #{isPrint}</if>
        </where>
        order by rso.create_time desc
    </select>

    <select id="selectRecordSwitchOrderByObjId" parameterType="Long" resultMap="RecordSwitchOrderResult">
        <include refid="selectRecordSwitchOrderVo"/>
        where rso.obj_id = #{objId}
    </select>

    <insert id="insertRecordSwitchOrder" parameterType="RecordSwitchOrder">
        insert into record_switch_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="materialBarcode != null">material_barcode,</if>
            <if test="switchStationCode != null">switch_station_code,</if>
            <if test="oldOrderCode != null">old_order_code,</if>
            <if test="isSwitch != null">is_switch,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="orderCode != null">order_code,</if>
            <if test="isPrint != null">is_print,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="materialBarcode != null">#{materialBarcode},</if>
            <if test="switchStationCode != null">#{switchStationCode},</if>
            <if test="oldOrderCode != null">#{oldOrderCode},</if>
            <if test="isSwitch != null">#{isSwitch},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="orderCode != null">#{orderCode},</if>
            <if test="isPrint != null">#{isPrint},</if>
        </trim>
    </insert>

    <update id="updateRecordSwitchOrder" parameterType="RecordSwitchOrder">
        update record_switch_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialBarcode != null">material_barcode = #{materialBarcode},</if>
            <if test="switchStationCode != null">switch_station_code = #{switchStationCode},</if>
            <if test="oldOrderCode != null">old_order_code = #{oldOrderCode},</if>
            <if test="isSwitch != null">is_switch = #{isSwitch},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="orderCode != null">order_code = #{orderCode},</if>
            <if test="isPrint != null">is_print = #{isPrint},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <update id="updatePrintCode" parameterType="RecordSwitchOrder">
        update PRINT_BARCODE
        set ORDER_CODE = #{orderCode}
        where MATERIAL_BARCODE = #{materialBarcode}
    </update>

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

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

    <select id="selectPrintBarcode" resultType="java.util.HashMap">
        SELECT PB.ORDER_CODE,
               PB.MATERIAL_CODE,
               PB.MATERIAL_NAME,
               PB.MATERIAL_BARCODE,
               PB.PRINT_BARTYPE,
               OI.PRINT_NAME,
               NVL(MIN(PS.IS_PRINT), 1) IS_PRINT
        FROM BASE_ORDERINFO OI
                 JOIN PRINT_BARCODE PB ON PB.ORDER_CODE = OI.ORDER_CODE
                 LEFT JOIN PRINT_SUPPLEMENTBARCODE PS ON PS.MATERIAL_BARCODE = PB.MATERIAL_BARCODE
        WHERE PB.MATERIAL_BARCODE = #{boxCode}
        GROUP BY PB.ORDER_CODE, PB.MATERIAL_CODE, PB.MATERIAL_NAME, PB.MATERIAL_BARCODE, PB.PRINT_BARTYPE,
                 OI.PRINT_NAME
    </select>

    <insert id="printSupplementBarcode">
        INSERT INTO PRINT_SUPPLEMENTBARCODE
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ORDER_CODE != null">ORDER_CODE,</if>
            <if test="MATERIAL_CODE != null">MATERIAL_CODE,</if>
            <if test="MATERIAL_NAME != null">MATERIAL_NAME,</if>
            <if test="MATERIAL_BARCODE != null">MATERIAL_BARCODE,</if>
            <if test="PRINT_BARTYPE != null">PRINT_BARTYPE,</if>
            <if test="PRINT_NAME != null">PRINT_NAME,</if>
            <if test="print_reason != null">print_reason,</if>
            <if test="CREATE_BY != null">CREATE_BY,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ORDER_CODE != null">#{ORDER_CODE},</if>
            <if test="MATERIAL_CODE != null">#{MATERIAL_CODE},</if>
            <if test="MATERIAL_NAME != null">#{MATERIAL_NAME},</if>
            <if test="MATERIAL_BARCODE != null">#{MATERIAL_BARCODE},</if>
            <if test="PRINT_BARTYPE != null">#{PRINT_BARTYPE},</if>
            <if test="PRINT_NAME != null">#{PRINT_NAME},</if>
            <if test="print_reason != null">#{print_reason}</if>
            <if test="CREATE_BY != null">#{CREATE_BY},</if>
        </trim>
    </insert>
</mapper>