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

    <update id="instore">
        update WMS_MOVE_TASK set status = #{status},
        GMT_MODIFIED = now(),
        MODIFIER = #{opUserId},
        MODIFIER_NAME = #{opUserName},
        <choose>
            <when test='status =="1"'>
                INSTORE_WORKER_NAME = #{opUserName}
            </when>
            <otherwise>
                OUTSTORE_WORKER_NAME = #{opUserName}
            </otherwise>
        </choose>
        <where>
            detail_id IN
            <foreach item="item" index="index" collection="detailIds" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
    </update>

    <select id="getList" resultType="com.prolog.cs.biz.outbound.model.entity.MoveTaskDO">
        select * from WMS_MOVE_TASK where status ='1'
        and detail_id IN
        <foreach item="item" index="index" collection="detailIds" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="queryMoveTaskInfo" resultType="com.prolog.cs.biz.outbound.model.entity.MoveTaskDO">
        SELECT
        move.*
        FROM
        WMS_MOVE_TASK_REF moveRef
        LEFT JOIN WMS_MOVE_TASK move ON moveRef.REF_LINE_BILL_ID = move.detail_id
        WHERE move.status != '2' and moveRef.LINE_BILL_ID = #{lineBillId}
    </select>

    <select id="getPickModel" resultType="java.lang.String">
        SELECT PICK_MODEL from WMS_WAVE_STORAGE where warehouse_area_code=#{whAreaCode} and warehouse_id =#{warehouseId} and PICK_MODEL is not null ORDER BY gmt_create desc limit 1
    </select>

    <select id="getMoveTask" resultType="com.prolog.cs.biz.outbound.model.entity.MoveTaskDO">
        select DISTINCT v.*
        from WMS_MOVE_TASK v
        inner join wms_wave_storage s on v.MOVE_TASK_ID = s.MOVE_TASK_ID
        inner join wms_pick_task_d d on d.WAVE_STORAGE_ID = s.id
        where d.WAREHOUSE_ID = #{warehouseId}
        and d.PICK_TASK_D_ID = #{pickTaskDId}
    </select>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        move_task_id, warehouse_id, status, waves_id, cargo_owner_id, item_id, batch_id, batch_num, manufacturer_lot_num, qty, uom, src_warehouse_locator_id, src_warehouse_locator_num, src_warehouse_area_id, src_warehouse_area_num, src_warehouse_locator_type, dest_warehouse_locator_id, dest_warehouse_locator_num, dest_warehouse_area_id, dest_warehouse_area_num, dest_warehouse_locator_type, pick_rule, creator, creator_name, gmt_create, modifier, modifier_name, gmt_modified, enterprise_id, bill_id, bldg_name, bldg_code, bldg_id, move_type, conversion_rate, bill_d_id, src_warehouse_area_category, dest_warehouse_area_category, header_id, detail_id, instore_worker_name, outstore_worker_name, src_floor, dest_floor, src_item_place_type, dest_item_place_type
    </sql>
    <select id="getReplenishTasksByIds" resultType="com.prolog.cs.biz.outbound.model.entity.MoveTaskDO">
        SELECT <include refid="Base_Column_List"/> FROM wms_move_task
        WHERE detail_id in
        <foreach collection="taskIds" index="index" item="taskId" close=")" open="(" separator=",">
            #{taskId}
        </foreach>
    </select>

    <select id="getUnShelveReplenishUnshelveListByPickTaskId"
            resultType="com.prolog.cs.biz.outbound.model.entity.MoveTaskDO">
        SELECT
            mt.move_task_id,
            mt.warehouse_id,
            mt.status,
            mt.waves_id,
            mt.cargo_owner_id,
            mt.item_id,
            mt.batch_id,
            mt.batch_num,
            mt.manufacturer_lot_num,
            mt.qty,
            mt.uom,
            mt.src_warehouse_locator_id,
            mt.src_warehouse_locator_num,
            mt.src_warehouse_area_id,
            mt.src_warehouse_area_num,
            mt.src_warehouse_locator_type,
            mt.dest_warehouse_locator_id,
            mt.dest_warehouse_locator_num,
            mt.dest_warehouse_area_id,
            mt.dest_warehouse_area_num,
            mt.dest_warehouse_locator_type,
            mt.pick_rule,
            mt.creator,
            mt.creator_name,
            mt.gmt_create,
            mt.modifier,
            mt.modifier_name,
            mt.gmt_modified,
            mt.enterprise_id,
            mt.bill_id,
            mt.bldg_name,
            mt.bldg_code,
            mt.bldg_id,
            mt.move_type,
            mt.conversion_rate,
            mt.bill_d_id,
            mt.src_warehouse_area_category,
            mt.dest_warehouse_area_category,
            mt.header_id, mt.detail_id,
            mt.instore_worker_name,
            mt.outstore_worker_name,
            mt.src_floor,
            mt.dest_floor,
            mt.src_item_place_type,
            mt.dest_item_place_type,
            bd.id barcode
        FROM wms_move_task mt
        LEFT JOIN wms_wave_storage sto ON mt.move_task_id = sto.move_task_id
        LEFT JOIN wms_pick_task_d d ON d.wave_storage_id = sto.id
        LEFT JOIN wms_barcode_d bd ON d.pick_task_d_id = bd.wms_pick_task_d_id
        LEFT JOIN wms_pick_task_h h on d.pick_task_id = h.pick_task_id
        LEFT JOIN wms_task_out_back wto ON wto.msg_id = d.ob_msg_id
        WHERE d.flow_status = '2' AND h.`status` = '1' AND d.print_count > 0
        AND mt.status = '0'
        <if test="pickTaskId!=null and pickTaskId!=''">
            AND (
                d.pick_task_id  = #{pickTaskId}
                or (wto.container_no = #{pickTaskId} AND wto.wmflg = 'Y')
                OR bd.id = #{pickTaskId}
                OR d.pick_task_id IN(SELECT pick_task_id FROM wms_pick_task_d WHERE pick_task_d_id IN(SELECT wms_pick_task_d_id FROM wms_barcode_d WHERE id = #{pickTaskId})))
        </if>
        <if test="employeeNo != null and employeeNo != ''">
            and (d.picker_no = #{employeeNo} or d.picker_no is null)
        </if>
    </select>

    <select id="getPutAwayReplenishUnshelveListByPickTaskId"
            resultType="com.prolog.cs.biz.outbound.model.entity.MoveTaskDO">
        SELECT
            mt.move_task_id,
            mt.warehouse_id,
            mt.status,
            mt.waves_id,
            mt.cargo_owner_id,
            mt.item_id,
            mt.batch_id,
            mt.batch_num,
            mt.manufacturer_lot_num,
            mt.qty,
            mt.uom,
            mt.src_warehouse_locator_id,
            mt.src_warehouse_locator_num,
            mt.src_warehouse_area_id,
            mt.src_warehouse_area_num,
            mt.src_warehouse_locator_type,
            mt.dest_warehouse_locator_id,
            mt.dest_warehouse_locator_num,
            mt.dest_warehouse_area_id,
            mt.dest_warehouse_area_num,
            mt.dest_warehouse_locator_type,
            mt.pick_rule, mt.creator,
            mt.creator_name,
            mt.gmt_create,
            mt.modifier,
            mt.modifier_name,
            mt.gmt_modified,
            mt.enterprise_id,
            mt.bill_id,
            mt.bldg_name,
            mt.bldg_code,
            mt.bldg_id,
            mt.move_type,
            mt.conversion_rate,
            mt.bill_d_id,
            mt.src_warehouse_area_category,
            mt.dest_warehouse_area_category,
            mt.header_id,
            mt.detail_id,
            mt.instore_worker_name,
            mt.outstore_worker_name,
            mt.src_floor,
            mt.dest_floor,
            mt.src_item_place_type,
            mt.dest_item_place_type,
            bd.id barcode
        FROM wms_move_task mt
        LEFT JOIN wms_wave_storage sto ON mt.move_task_id = sto.move_task_id
        LEFT JOIN wms_pick_task_d d ON d.wave_storage_id = sto.id
        LEFT JOIN wms_barcode_d bd ON d.pick_task_d_id = bd.wms_pick_task_d_id
        LEFT JOIN wms_task_out_back wto ON wto.msg_id = d.ob_msg_id
        WHERE  mt.status = '1'
        <if test="pickTaskId!=null and pickTaskId!=''">
            AND (
                d.pick_task_id  = #{pickTaskId}
                or (wto.container_no = #{pickTaskId} AND wto.wmflg = 'Y')
                OR bd.id = #{pickTaskId}
                OR d.pick_task_id IN(SELECT pick_task_id FROM wms_pick_task_d WHERE pick_task_d_id IN(SELECT wms_pick_task_d_id FROM wms_barcode_d WHERE id = #{pickTaskId})))
        </if>
        <if test="employeeNo != null and employeeNo != ''">
            and (d.picker_no = #{employeeNo} or d.picker_no is null)
        </if>
    </select>

    <select id="getMoveTaskByPickTaskId"
            resultType="com.prolog.cs.biz.outbound.model.entity.MoveTaskDO">
        SELECT
        mt.move_task_id,
        mt.warehouse_id,
        mt.status,
        mt.waves_id,
        mt.cargo_owner_id,
        mt.item_id,
        mt.batch_id,
        mt.batch_num,
        mt.manufacturer_lot_num,
        mt.qty,
        mt.uom,
        mt.src_warehouse_locator_id,
        mt.src_warehouse_locator_num,
        mt.src_warehouse_area_id,
        mt.src_warehouse_area_num,
        mt.src_warehouse_locator_type,
        mt.dest_warehouse_locator_id,
        mt.dest_warehouse_locator_num,
        mt.dest_warehouse_area_id,
        mt.dest_warehouse_area_num,
        mt.dest_warehouse_locator_type,
        mt.pick_rule, mt.creator,
        mt.creator_name,
        mt.gmt_create,
        mt.modifier,
        mt.modifier_name,
        mt.gmt_modified,
        mt.enterprise_id,
        mt.bill_id,
        mt.bldg_name,
        mt.bldg_code,
        mt.bldg_id,
        mt.move_type,
        mt.conversion_rate,
        mt.bill_d_id,
        mt.src_warehouse_area_category,
        mt.dest_warehouse_area_category,
        mt.header_id,
        mt.detail_id,
        mt.instore_worker_name,
        mt.outstore_worker_name,
        mt.src_floor,
        mt.dest_floor,
        mt.src_item_place_type,
        mt.dest_item_place_type,
        bd.id barcode
        FROM wms_move_task mt
        LEFT JOIN wms_wave_storage sto ON mt.move_task_id = sto.move_task_id
        LEFT JOIN wms_pick_task_d d ON d.wave_storage_id = sto.id
        LEFT JOIN wms_barcode_d bd ON d.pick_task_d_id = bd.wms_pick_task_d_id
        WHERE  mt.detail_id = #{pickTaskId}
        <if test="employeeNo != null and employeeNo != ''">
            and (d.picker_no = #{employeeNo} or d.picker_no is null)
        </if>
    </select>

    <select id="getDetailIdByBillDId"
            resultType="com.prolog.cs.biz.outbound.model.entity.MoveTaskDO">
        SELECT move.* FROM WMS_MOVE_TASK_REF moveRef LEFT JOIN WMS_MOVE_TASK move on moveRef.REF_LINE_BILL_ID = move.BILL_D_ID where moveRef.LINE_BILL_ID = #{billDId}
    </select>

    <!-- 别名查询结果列 -->
    <sql id="Base_Column_List_Alias">
        ${alias}.move_task_id,${alias}.warehouse_id,${alias}.status,${alias}.waves_id,${alias}.cargo_owner_id,${alias}.item_id,${alias}.batch_id,${alias}.batch_num,${alias}.manufacturer_lot_num,${alias}.qty,${alias}.uom,${alias}.src_warehouse_locator_id,${alias}.src_warehouse_locator_num,${alias}.src_warehouse_area_id,${alias}.src_warehouse_area_num,${alias}.src_warehouse_locator_type,${alias}.dest_warehouse_locator_id,${alias}.dest_warehouse_locator_num,${alias}.dest_warehouse_area_id,${alias}.dest_warehouse_area_num,${alias}.dest_warehouse_locator_type,${alias}.pick_rule,${alias}.creator,${alias}.creator_name,${alias}.gmt_create,${alias}.modifier,${alias}.modifier_name,${alias}.gmt_modified,${alias}.enterprise_id,${alias}.bill_id,${alias}.bldg_name,${alias}.bldg_code,${alias}.bldg_id,${alias}.move_type,${alias}.conversion_rate,${alias}.bill_d_id,${alias}.src_warehouse_area_category,${alias}.dest_warehouse_area_category,${alias}.header_id,${alias}.detail_id,${alias}.instore_worker_name,${alias}.outstore_worker_name,${alias}.src_floor,${alias}.dest_floor,${alias}.src_item_place_type,${alias}.dest_item_place_type
    </sql>
    <select id="getDetailIdByBillDIds" resultType="com.prolog.cs.biz.outbound.model.entity.MoveTaskDO">
        SELECT <include refid="Base_Column_List_Alias"><property name="alias" value="move"/></include> FROM WMS_MOVE_TASK_REF moveRef LEFT JOIN WMS_MOVE_TASK move on moveRef.REF_LINE_BILL_ID = move.BILL_D_ID
        where moveRef.LINE_BILL_ID in
        <foreach collection="billDIds" index="index" item="billDId" close=")" open="(" separator=",">
            #{billDId}
        </foreach>
    </select>
</mapper>