<?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>
    <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>

</mapper>