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

    <resultMap id="waveStorageVO" type="com.prolog.cs.biz.outbound.model.vo.WaveStorageVO">
        <result column="id" property="id"/>
        <result column="locator_id" property="locatorId"/>
        <result column="locator_type" property="locatorType"/>
        <result column="batch_num" property="batchNum"/>
        <result column="qty" property="qty"/>
        <result column="warehouse_area_code" property="warehouseAreaCode"/>
        <result column="batch_id" property="batchId"/>
        <result column="wave_id" property="waveId"/>
        <result column="warehouse_id" property="warehouseId"/>
        <result column="bill_id" property="billId"/>
        <result column="bill_d_id" property="billDId"/>
        <result column="item_code" property="itemCode"/>
        <result column="manufacturer_lot_num" property="lotNo"/>
        <result column="item_name" property="itemName"/>

    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, item_id, warehouse_id, batch_id, batch_num, qty, uom, locator_id, locator_type, expired_date, production_date, shelf_life_days, volume, weight, warehouse_area_code, pick_model, floor_num, locator_lane_code, out_index, pick_piece_group, bill_id, bill_d_id, segment3, segment4, segment5, creator, creator_name, gmt_create, modifier, modifier_name, gmt_modified, enterprise_id, status, wave_id, locator_code, manufacturer_lot_num, building_code, bldg_name, bldg_id, move_task_id, type, area_category, expired_date_show, production_date_show, item_place_type, container_code, is_function_room, container_code_qty, salesperson_name, prepared_by, remark, paymentdaytype
    </sql>

    <!-- 别名查询结果列 -->
    <sql id="Base_Column_List_Alias">
        ${alias}.id, ${alias}.item_id, ${alias}.warehouse_id, ${alias}.batch_id, ${alias}.batch_num, ${alias}.qty, ${alias}.uom, ${alias}.locator_id, ${alias}.locator_type, ${alias}.expired_date, ${alias}.production_date, ${alias}.shelf_life_days, ${alias}.volume, ${alias}.weight, ${alias}.warehouse_area_code, ${alias}.pick_model, ${alias}.floor_num, ${alias}.locator_lane_code, ${alias}.out_index, ${alias}.pick_piece_group, ${alias}.bill_id, ${alias}.bill_d_id, ${alias}.segment3, ${alias}.segment4, ${alias}.segment5, ${alias}.creator, ${alias}.creator_name, ${alias}.gmt_create, ${alias}.modifier, ${alias}.modifier_name, ${alias}.gmt_modified, ${alias}.enterprise_id, ${alias}.status, ${alias}.wave_id, ${alias}.locator_code, ${alias}.manufacturer_lot_num, ${alias}.building_code, ${alias}.bldg_name, ${alias}.bldg_id, ${alias}.move_task_id, ${alias}.type, ${alias}.area_category, ${alias}.expired_date_show, ${alias}.production_date_show, ${alias}.item_place_type, ${alias}.container_code, ${alias}.is_function_room, ${alias}.container_code_qty, ${alias}.salesperson_name, ${alias}.prepared_by, ${alias}.remark, ${alias}.paymentdaytype
    </sql>

    <update id="updateRelationWithMoveTask">
        merge into WMS_WAVE_STORAGE storage using (
        select
        WMS_MOVE_TASK.MOVE_TASK_ID,
        WMS_MOVE_TASK.BILL_D_ID,
        WMS_MOVE_TASK.DEST_WAREHOUSE_LOCATOR_ID
        from
        WMS_MOVE_TASK
        ) moveTask on (
        storage.bill_d_id = moveTask.bill_d_id
        AND moveTask.DEST_WAREHOUSE_LOCATOR_ID = STORAGE.LOCATOR_ID
        AND storage.TYPE = '1'
        and storage.bill_id = #{billId}
        )
        when matched then
        update
        set
        storage.MOVE_TASK_ID = moveTask.MOVE_TASK_ID

    </update>


    <select id="findWaveStorage" resultMap="waveStorageVO">
        select
        a.id,
        a.locator_id,
        a.locator_type,
        a.batch_num,
        a.qty,
        a.warehouse_area_code,
        a.batch_id,
        a.manufacturer_lot_num,
        wave.id as wave_id,
        wave.warehouse_id,
        biLl.id as bilL_id,
        bill.code as bill_code,
        billd.id as bill_d_id,
        billd.item_code,
        billd.item_name
        from
        wms_wave_storage a
        left join wms_wave_bill_d biLld on billd.id = a.bill_d_id
        left join wms_wave_bill bill on bill.id = billd.bill_id
        left join wms_wave wave on wave.id = bill.wave_id
        where 1=1
        <if test="waveId != null and waveId != ''">
            and wave.id = #{waveId}
        </if>
        <if test="billCode != null and billCode != ''">
            and bill.code  like concat(#{billCode},'%')
        </if>
        <if test="warehouseId != null and warehouseId != ''">
            and wave.warehouse_id = #{warehouseId}
        </if>
        <if test="billId != null and billId != ''">
            and bill.id like concat(#{billId},'%')
        </if>
        <if test="billDId != null and billDId != ''">
            and billd.id = #{billDId}
        </if>
        <if test="itemCode != null and itemCode != ''">
            and billd.item_code = #{itemCode}
        </if>
        <if test="locatorId != null and locatorId != ''">
            and a.locator_id = #{locatorId}
        </if>
        order by a.gmt_create desc
    </select>

    <select id="getStorageByPickTaskDId" resultType="com.prolog.cs.biz.outbound.model.entity.WaveStorageDO">
        SELECT s.*  FROM  WMS_WAVE_STORAGE s
        inner  JOIN WMS_PICK_TASK_D picktaskd ON PICKTASKD.WAVE_STORAGE_ID  = s.id
        WHERE PICKTASKD.PICK_TASK_D_ID = #{pickTaskDId}
    </select>

    <select id="getStorageByPickTaskDIds" resultType="com.prolog.cs.biz.outbound.model.entity.WaveStorageDO">
        SELECT s.*  FROM  WMS_WAVE_STORAGE s
        inner  JOIN WMS_PICK_TASK_D picktaskd ON PICKTASKD.WAVE_STORAGE_ID  = s.id
        WHERE PICKTASKD.PICK_TASK_D_ID in
        <foreach item="item" index="index" collection="pickTaskDIds" open="(" close=")" separator=",">
            #{item}
        </foreach>

    </select>

    <select id="getStorageListByBarCode" resultType="com.prolog.cs.biz.outbound.model.entity.WaveStorageDO">
        select  <include refid="Base_Column_List_Alias">
                    <property name="alias" value="s"/>
                </include>
        from wms_barcode_h h
                 left join wms_barcode_d d on h.id = d.barcode_id
                 left join wms_pick_task_d td on td.pick_task_d_id = d.wms_pick_task_d_id
                 left join wms_wave_storage s on s.id = td.wave_storage_id
        where h.barcode = #{barcode}
    </select>

    <select id="getOutbound4EisByWaveId" resultType="com.prolog.entity.vo.OutBoundRequestItemVO">
        select ws.area_category                               as wmsLocationCode,
               ws.warehouse_area_code                         as targetLocation,
               ptd.item_id                                    as itemId,
               ptd.batch_id                                   as lotId,
               ptd.qty                                        as qty,
               ptd.pick_task_id                               as taskId,
               ptd.pick_task_d_id                             as taskLineId,
               wb.type,
               case
                   when wb.type = 1 then '20'
                   when wb.type = 2 then '20'
                   when wb.type = 4 then '20'
                   when wb.type = 5 then '20'
                   when wb.type = 6 then '40'
                   when wb.type = 7 then '50'
                   when wb.type = 20 then '30'
                   end                                        as workType,
               case
                   when wb.type = 1 then 'DO_SO'
                   when wb.type = 2 then 'DO_RCVRETURN'
                   when wb.type = 4 then 'DO_WKO'
                   when wb.type = 5 then 'DO_DB_CP'
                   when wb.type = 6 then 'INV_PLANCHECK'
                   when wb.type = 7 then 'INV_CONSERVE'
                   when wb.type = 20 then 'INV_RTM'
                   end                                        as businessType,
               ws.wave_id                                     as waveNo,
               ptd.src_code                                   as billNo,
               if(oh.attr_str3 > 0 or oh.attr_str4 > 0, 1, 0) as str1,
               '1'                                            as str2,
               wb.shipper_id                                  as companyId,
               wb.shipper_name                                as companyName,
               ws.container_code                              as containerNo,
               oh.priority_level                              as priority,
               ws.enterprise_id                               as enterpriseId,
               ws.locator_code                                as locNo,
               wb.consignor_id                                as ownerId,
               '0'                                            as packId,
               '2'                                            as inventoryShortageType,
               '0'                                            as isOver,
               '0'                                            as isCheck,
               '2'                                            as isMaterial,
               ''                                             as originalArea,
               ''                                             as targetArea
        from wms_pick_task_d ptd
                 left join wms_wave_storage ws on ptd.wave_storage_id = ws.id
                 left join wms_wave_bill_d wbd on ws.bill_d_id = wbd.id
                 left join wms_wave_bill wb on wb.id = wbd.bill_id
                 left join oms_do_bill_h oh on oh.billid = wb.src_id
        where ws.wave_id = #{waveId}
    </select>

    <select id="getGiftItemByWaveId" resultType="com.prolog.cs.biz.outbound.model.entity.WaveStorageDO">
        select  wws.* from wms_pick_task_d wptd join wms_wave_bill wwb on wptd.bill_id  = wwb.id
        join wms_wave ww on ww.id  = wwb.wave_id
        join oms_do_bill_h odbh on odbh.billid  = wwb.src_id
        join oms_do_bill_d odbd on odbd.lineid  = wptd.src_line_id
        join wms_wave_storage wws on wws.id  = wptd.wave_storage_id
        where odbh.attr_str7 != '4'
        and odbd.attr_str2 = '1'
        and wptd.flow_status = '0'
        and wwb.wave_id = #{waveId}
    </select>

</mapper>