<?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.prolog.cs.biz.outbound.dao.BarcodeDMapper">
    <insert id="insertOrUpdate">
        MERGE INTO WMS_BARCODE_D t1 USING dual on (
        t1.ITEM_CODE = #{itemCode}
        AND t1.BARCODE_ID = #{barcodeId}
        AND T1.container_code = #{containerCode}
        AND T1.WMS_PICK_TASK_D_ID = #{pickTaskDId}
        )
        WHEN MATCHED THEN
        UPDATE
        SET
        t1.qty = #{qty}
        WHEN NOT MATCHED THEN
        insert
        (ITEM_CODE,item_id, id, BARCODE_ID,container_code, qty,WMS_PICK_TASK_D_ID,is_piece)
        values
        (#{itemCode}, #{itemId}, #{barcodeDId}, #{barcodeId},  #{containerCode}, #{qty},#{pickTaskDId},#{isPiece})
    </insert>

    <insert id="insertOrAdd">
        MERGE INTO WMS_BARCODE_D t1 USING dual on (
        t1.ITEM_CODE = #{itemCode}
        AND t1.BARCODE_ID = #{barcodeId}
        AND T1.container_code = #{containerCode}
        AND T1.WMS_PICK_TASK_D_ID = #{pickTaskDId}
        )
        WHEN MATCHED THEN
        UPDATE
        SET
        t1.qty = t1.qty + #{qty}
        WHEN NOT MATCHED THEN
        insert
        (ITEM_CODE,item_id, id, BARCODE_ID,container_code,qty,WMS_PICK_TASK_D_ID,is_piece)
        values
         (#{itemCode}, #{itemId}, #{barcodeDId}, #{barcodeId}, #{containerCode}, #{qty},#{pickTaskDId},#{isPiece})
    </insert>


    <update id="updateBarcodeD">
        merge into WMS_BARCODE_D barcodeD using (
        select
        WMS_BARCODE_H.id,
        WMS_BARCODE_H.bill_group_id,
        WMS_BARCODE_H.lcl_no
        from
        WMS_BARCODE_H
        ) barcodeH on (
        barcodeH.id = barcodeD.BARCODE_ID
        AND barcodeH.bill_group_id= #{billGroupId}
        AND barcodeD.item_code= #{itemCode}
        and barcodeH.lcl_no = #{lclNo}
        )
        when matched then
        update
        set
        barcodeD.qty = #{qty}
    </update>
    <update id="adJustBarcodeD">
       update  WMS_BARCODE_D t1 set qty = #{qty}
       where    t1.item_code= #{itemCode}  AND t1.BARCODE_ID = #{barcodeId}
    </update>

    <delete id="deleteByBarcodeLong">
        DELETE FROM WMS_BARCODE_D barcodeD
        WHERE BARCODE_ID = #{barcodeHId}
    </delete>
    <select id="getLclDetail" resultType="com.prolog.cs.biz.outbound.model.print.LclDetailTableVO">
        SELECT
          barcodeD.ITEM_CODE,
          barcodeD.QTY pieceQty,
          barcodeD.item_id,
          billd.item_name,
          billd.ITEM_SPECS specification,
          billd.MANUFACTURER manufacture,
          billd.CASE_CONVERSION_RATE conversionRate,
          barcodeH.lcl_no,
          wws.MANUFACTURER_LOT_NUM manufacturerLotNum ,
          odbd.REMARK ,
          wws.uom
        FROM
          WMS_BARCODE_D barcodeD
        LEFT JOIN WMS_BARCODE_H barcodeH ON barcodeH.id = barcodeD.BARCODE_ID
        LEFT JOIN WMS_PICK_TASK_D wptd ON wptd.PICK_TASK_D_ID = barcodeD.WMS_PICK_TASK_D_ID
        LEFT JOIN WMS_WAVE_STORAGE wws ON wws.id = wptd.WAVE_STORAGE_ID
        LEFT JOIN WMS_WAVE_BILL_D billd ON  billd.id = wws.BILL_D_ID
        LEFT JOIN OMS_DO_BILL_D odbd ON odbd.LINEID = billd.SRC_LINE_ID
        WHERE
          barcodeH.BILL_GROUP_ID = #{billGroupId}
          AND barcodeh."TYPE" = '0'
          AND barcodeH.LCL_NO =  #{lclNo}
    </select>

    <resultMap id="LclPrintParameterVO" type="com.prolog.cs.biz.outbound.model.vo.check.LclPrintParameterVO">
        <result column="id" property="lclNo"/>
        <result column="telephone" property="telephone"/>
        <result column="weight" property="weight"/>
        <result column="address" property="address"/>
        <result column="company" property="company"/>
        <result column="contacts" property="contacts"/>
        <result column="checker1" property="checker1"/>
        <result column="checker2" property="checker2"/>
        <result column="workstation" property="workstation"/>
        <result column="workstageCode" property="workstageCode"/>
        <result column="barcode" property="barcode"/>
        <result column="routeNo" property="routeNo"/>
        <result column="bill_type" property="billType"/>

    </resultMap>

</mapper>