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

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        pick_task_d_id, pick_task_id, wave_storage_id, warehouse_id, qty, pick_qty, check_qty, creator, creator_name, gmt_create, modifier, modifier_name, gmt_modified, enterprise_id, picker_id, picker_no, picker_name, checker_id, checker_no, checker_name, checker_id_2, checker_no_2, checker_name_2, pick_time, check_time, supervise_code, whole_num, print_count, sale_red_qty, checker2_qty, flow_status, pick_task_type, src_pick_task_d_id, barcode_d_id, container_code, ob_msg_id, pick_red_type, src_line_id, src_id, src_code, bill_d_id, bill_id, detail_id, item_id, batch_id, locator_id, warehouse_area_code, pick_model, split_source_id
    </sql>

    <update id="addNum">
        update WMS_PICK_TASK_D
        set check_qty = check_qty + #{incrementQty}
        <if test="user1 != null and user1 != ''">
            , checker_name = #{user1}
        </if>
        <if test="user2 != null and user2 != ''">
            , checker_name_2 = #{user2}
        </if>
        where PICK_TASK_D_ID= #{pickTaskDId}
    </update>

    <update id="updateNum">
        update WMS_PICK_TASK_D
        set check_qty = #{newQty}
        <if test="user1 != null and user1 != ''">
            , checker_name = #{user1}
        </if>
        <if test="user2 != null and user2 != ''">
            , checker_name_2 = #{user2}
        </if>
        where PICK_TASK_D_ID= #{pickTaskDId}
    </update>


    <resultMap id="PickTaskDVO" type="com.prolog.cs.biz.outbound.model.vo.check.InnerCheckTaskDVO">
        <result column="pick_task_d_id" property="pickTaskDId"/>
        <result column="batch_num" property="batchNum"/>
        <result column="qty" property="qty"/>
        <result column="pick_qty" property="pickQty"/>
        <result column="expired_date" property="expiredDate"/>
        <result column="production_date" property="productionDate"/>
        <result column="item_id" property="itemId"/>
        <result column="item_code" property="itemCode"/>
        <result column="item_name" property="itemName"/>
        <result column="MANUFACTURER_LOT_NUM" property="lotNo"/>
        <result column="wave_storage_id" property="waveStorageId"/>
        <result column="billd_id" property="billDId"/>
        <result column="bill_id" property="billId"/>
        <result column="bill_code" property="billCode"/>
        <result column="SHIPPER_ID" property="shipperId"/>
        <result column="CONSIGNOR_ID" property="consignorId"/>
        <result column="type" property="billType"/>
        
        <result column="src_line_id" property="srcLineId"/>
        <result column="item_health_is_double" property="itemHealthIsDouble"/>
        <result column="ITEM_CATEGORY_CODE" property="itemCategoryCode"/>

        <result column="CHECK_QTY" property="checkQty"/>
        <result column="CHECKER2_QTY" property="check2Qty"/>
        <result column="red_qty" property="redQty"/>
        <result column="red_reason" property="redReason"/>
        <result column="check_time" property="checkTime"/>
        <result column="CHECKER1" property="checker1"/>
        <result column="CHECKER2" property="checker2"/>
        <result column="picker_name" property="pickerName"/>
        <result column="PICK_TIME" property="pickTime"/>
        <result column="uom" property="uom"/>
        <result column="VOLUME" property="volume"/>
        <result column="weight" property="weight"/>
        <result column="ITEM_SPECS" property="spec"/>
        <result column="warehouse_id" property="warehouseId"/>
        <result column="innercheck_d_id" property="innerCheckDId"/>
        <result column="CASE_CONVERSION_RATE" property="caseConversionRate"/>
        <result column="locator_code" property="locatorCode"/>
        <result column="IS_CASE_OR_PIECE" property="isCaseOrPiece"/>

        <result column="src_id" property="srcId"/>
    </resultMap>

    <resultMap id="PickTaskDV2VO" type="com.prolog.cs.biz.outbound.model.vo.check.InnerCheckTaskDV2VO">
        <result column="pick_task_d_id" property="pickTaskDId"/>
        <result column="batch_num" property="batchNum"/>
        <result column="qty" property="qty"/>
        <result column="pick_qty" property="pickQty"/>
        <result column="expired_date" property="expiredDate"/>
        <result column="production_date" property="productionDate"/>
        <result column="item_id" property="itemId"/>
        <result column="item_code" property="itemCode"/>
        <result column="item_name" property="itemName"/>
        <result column="MANUFACTURER_LOT_NUM" property="lotNo"/>
        <result column="wave_storage_id" property="waveStorageId"/>
        <result column="billd_id" property="billDId"/>
        <result column="bill_id" property="billId"/>
        <result column="bill_code" property="billCode"/>
        <result column="SHIPPER_ID" property="shipperId"/>
        <result column="CONSIGNOR_ID" property="consignorId"/>
        <result column="type" property="billType"/>

        <result column="src_line_id" property="srcLineId"/>
        <result column="item_health_is_double" property="itemHealthIsDouble"/>
        <result column="ITEM_CATEGORY_CODE" property="itemCategoryCode"/>

        <result column="CHECK_QTY" property="checkQty"/>
        <result column="CHECKER2_QTY" property="check2Qty"/>
        <result column="red_qty" property="redQty"/>
        <result column="red_reason" property="redReason"/>
        <result column="check_time" property="checkTime"/>
        <result column="CHECKER1" property="checker1"/>
        <result column="CHECKER2" property="checker2"/>
        <result column="picker_name" property="pickerName"/>
        <result column="PICK_TIME" property="pickTime"/>
        <result column="uom" property="uom"/>
        <result column="VOLUME" property="volume"/>
        <result column="weight" property="weight"/>
        <result column="ITEM_SPECS" property="spec"/>
        <result column="warehouse_id" property="warehouseId"/>
        <result column="innercheck_d_id" property="innerCheckDId"/>
        <result column="CASE_CONVERSION_RATE" property="caseConversionRate"/>
        <result column="locator_code" property="locatorCode"/>
        <result column="IS_CASE_OR_PIECE" property="isCaseOrPiece"/>
        <result column="barcodeDId" property="barcodeDId"/>
        <result column="LCL_NO" property="lclNo"/>
        <result column="lclQty" property="lclQty"/>
        <result column="src_id" property="srcId"/>
        <result column="flow_status" property="flowStatus"/>
    </resultMap>

    <select id="getPickTaskd" resultMap="PickTaskDVO">
        SELECT
        taskd.pick_task_d_id ,
        taskd.wave_storage_id,
        wws.batch_num ,
        COALESCE(taskd.qty,0) qty,
        COALESCE(taskd.pick_qty,0) pick_qty,
        wws.production_date,
        wws.expired_date,
        wws.MANUFACTURER_LOT_NUM,
        billd.item_id,
        billd.bill_id,
        billd.id as billd_id,
        bill.src_id,
        bill.code bill_code,
        bill.warehouse_id,
        bill.SHIPPER_ID,bill.CONSIGNOR_ID,bill.type,
        billd.item_code,
        billd.item_name,
        billd.src_line_id,
        billd.item_health_is_double,
        billd.ITEM_CATEGORY_CODE,
        billd.CASE_CONVERSION_RATE ,
        billd.IS_CASE_OR_PIECE ,
        billd.VOLUME ,
        BILLD.WEIGHT ,
        billd.ITEM_SPECS ,
        taskd.CHECK_QTY ,
        taskd.CHECKER2_QTY ,
        taskd.CHECK_TIME check_time,
        taskd.CHECKER_NAME_2 CHECKER1 ,
        taskd.CHECKER_NAME_2 CHECKER2 ,
        (select sum(coalesce(QTY,0)) from WMS_SALE_RED_RECORD r where r.BILL_ID=bill.id and r.BILL_D_ID=wws.BILL_D_ID
        and r.type=4 and r.PICK_TASK_D_ID=taskd.PICK_TASK_D_ID) red_qty ,
        (select rr.REASON from WMS_SALE_RED_RECORD rr where rr.id=(select max(id) from WMS_SALE_RED_RECORD r
        where r.BILL_ID=bill.id and r.BILL_D_ID=wws.BILL_D_ID
        and r.type=4 and r.PICK_TASK_D_ID=taskd.PICK_TASK_D_ID)) red_reason,
        taskd.picker_name,
        wws.uom,
        wws.locator_code,
        <if test="showLcl != null and 'true'.toString() == showLcl.toString()">
            b.id barcodeDId,
            h.LCL_NO,
        </if>
        taskd.PICK_TIME,
        taskd.PICK_TASK_TYPE
        FROM WMS_PICK_TASK_D taskd
        left join WMS_PICK_TASK_H taskh on taskh.PICK_TASK_ID = TASKD.PICK_TASK_ID
        left join WMS_WAVE_STORAGE wws on wws.id = taskd.WAVE_STORAGE_ID
        LEFT JOIN wms_wave_bill_d billd ON wws.BILL_D_ID =billd.id
        left join WMS_WAVE_BILL bill on bill.id = billd.bill_id
        <if test="showLcl != null and 'true'.toString() == showLcl.toString()">
            left join WMS_BARCODE_D b on taskd.PICK_TASK_D_ID = b.WMS_PICK_TASK_D_ID
            left join WMS_BARCODE_H h on h.id = b.BARCODE_ID
        </if>
        where taskh.PICK_TASK_ID = #{pickTaskId}
        order by billd.item_code
    </select>

    <select id="getPickTaskdV2" resultMap="PickTaskDV2VO">
        SELECT
        taskd.pick_task_d_id ,
        taskd.wave_storage_id,
        wws.batch_num ,
        COALESCE(taskd.qty,0) qty,
        COALESCE(taskd.pick_qty,0) pick_qty,
        wws.production_date,
        wws.expired_date,
        wws.MANUFACTURER_LOT_NUM,
        wws.item_id,
        wws.bill_id,
        wws.BILL_D_ID as billd_id,
        <!--            bill.src_id,-->
        <!--            bill.code bill_code,-->
        wws.warehouse_id,
        <!--            bill.SHIPPER_ID,bill.CONSIGNOR_ID,bill.type,-->
        <!--            billd.item_code,-->
        <!--            billd.item_name,-->
        <!--            billd.src_line_id,-->
        <!--            billd.item_health_is_double,-->
        <!--            billd.ITEM_CATEGORY_CODE,-->
        <!--            billd.CASE_CONVERSION_RATE ,-->
        <!--            billd.IS_CASE_OR_PIECE ,-->
        wws.VOLUME ,
        wws.WEIGHT ,
        <!--            billd.ITEM_SPECS ,-->
        taskd.CHECK_QTY ,
        taskd.CHECKER2_QTY ,
        taskd.CHECK_TIME check_time,
        taskd.CHECKER_NAME_2 CHECKER1 ,
        taskd.CHECKER_NAME_2 CHECKER2 ,

        taskd.picker_name,
        wws.uom,
        wws.locator_code,
        <if test="showLcl != null and 'true'.toString() == showLcl.toString()">
            b.id barcodeDId,
            b.qty lclQty,
            h.LCL_NO,
        </if>
        taskd.PICK_TIME,
        taskd.PICK_TASK_TYPE
        FROM WMS_PICK_TASK_D taskd
        inner join WMS_WAVE_STORAGE wws on wws.id = taskd.WAVE_STORAGE_ID
        <if test="showLcl != null and 'true'.toString() == showLcl.toString()">
            left join WMS_BARCODE_D b on taskd.PICK_TASK_D_ID = b.WMS_PICK_TASK_D_ID
            left join WMS_BARCODE_H h on h.id = b.BARCODE_ID
        </if>
        where taskd.PICK_TASK_ID = #{pickTaskId}
        order by taskd.pick_task_d_id
    </select>

    <select id="listPrintTask" parameterType="com.prolog.cs.biz.outbound.model.dto.PrintAgainVo" resultType="com.prolog.cs.biz.outbound.model.dto.RePrintWholeDto">

        SELECT * FROM(
        SELECT
            b.pick_task_id taskId,
            b.container_code containerno,
            (SELECT SRC_CODE FROM wms_wave_bill WHERE id = (
                SELECT bill_id FROM wms_wave_bill_d a WHERE a.id = (
                    SELECT bill_d_id FROM wms_wave_storage c WHERE c.id = (
                        SELECT wave_storage_id FROM wms_pick_task_d d WHERE d.pick_task_id = b.pick_task_id limit 1
                    ) limit 1
                )limit 1)
            )billno,
            b.gmt_create createtime,
            (SELECT SUM(d.qty) FROM wms_pick_task_d d WHERE d.pick_task_id = b.pick_task_id)qty,
            ((SELECT SUM(d.qty) FROM wms_pick_task_d d WHERE d.pick_task_id = b.pick_task_id)/(SELECT case_conversion_rate FROM wms_wave_bill_d a WHERE a.id = (
            SELECT bill_d_id FROM wms_wave_storage c WHERE c.id = (
            SELECT wave_storage_id FROM wms_pick_task_d d WHERE d.pick_task_id = b.pick_task_id limit 1
            ) limit 1
            )limit 1
            ) )pcs,
            (SELECT ship_bill_no FROM wms_wave_bill_d a WHERE a.id = (
                SELECT bill_d_id FROM wms_wave_storage c WHERE c.id = (
                    SELECT wave_storage_id FROM wms_pick_task_d d WHERE d.pick_task_id = b.pick_task_id limit 1
                    ) limit 1
                )limit 1
            )shipno,
            (IF(type = 1, (SELECT SUM(IFNULL(print_count, 0)) FROM wms_pick_task_d d WHERE d.pick_task_id = b.pick_task_id), 0))printcount1,
            (IF(type is null OR type = 0, (SELECT SUM(IFNULL(print_count, 0)) FROM wms_pick_task_d d WHERE d.pick_task_id = b.pick_task_id), 0))printcount2,
            (SELECT SUM(IFNULL(cx_label_print, 0)) FROM wms_pick_task_d d WHERE d.pick_task_id = b.pick_task_id)printcount3
        FROM `wms_pick_task_h` b
        WHERE b.pick_task_id IN (SELECT pick_task_id FROM wms_pick_task_d GROUP BY pick_task_id HAVING SUM(IFNULL(print_count, 0)) > 0)
        <if test="endTimeStr != null and endTimeStr != ''">
            AND b.gmt_create &lt;= #{endTimeStr}
        </if>
        <if test="startTimeStr != null and startTimeStr != ''">
            AND b.gmt_create &gt;= #{startTimeStr}
        </if>
        <if test="taskId != null and taskId != ''">
            AND b.pick_task_id = #{taskId}
        </if>
        <if test="containerCode != null and containerCode != ''">
            AND t.container_code = #{containerCode}
        </if>
        ) t
        WHERE 1=1
          <if test="billNo != null and billNo != ''">
            AND t.billno = #{billNo}
          </if>
          <if test="shipNo != null and shipNo != ''">
            AND t.shipno = #{shipNo}
          </if>
    </select>

    <select id="listWholeLabelRepair" parameterType="com.prolog.cs.biz.outbound.model.dto.PrintAgainVo"
            resultType="com.prolog.cs.biz.outbound.model.dto.RePrintWholeDto">
        SELECT
            bh.barcode barcode,
            bh.warehouse_id warehouseId,
            bh.barcode_type barcodeType,
            n.consignor_id consignorId,
            n.BILL_GROUP_ID billId,
            d.print_count printFlag,
            d.PICK_TASK_ID pickTaskId,
            m.ITEM_ID itemId,
            m.ITEM_NAME itemName,
            m.ITEM_CODE itemCode,
            n.SHIPPER_ID shipperId,
            n.SHIPPER_TYPE shipperType,
            s.manufacturer_lot_num lotNo,
            s.EXPIRED_DATE_SHOW expiredDateShow,
            s.PRODUCTION_DATE_SHOW productionDateShow,
            IFNULL(d.container_code, 'N/A') containerCode,
            d.pick_time pickTime,
            d.PICKER_NAME pickerName,
            a.qty,
            d.PICK_QTY pickQty,
            m.CASE_CONVERSION_RATE conversionRate,
            s.WAREHOUSE_AREA_CODE warehouseAreaCode,
            s.LOCATOR_CODE locatorCode,
            v.dest_warehouse_locator_num toLocatorCode,
            obh.remark,
        (CASE
        WHEN r.cancel_flag != '1' AND r.cancel_flag IS NOT NULL THEN r.cancel_flag
        ELSE CASE obh.carrier_type
        WHEN '2' THEN NULL
        ELSE obh.carrier_code
        END
        END) AS carrierCode,
            e.GMT_CREATE
        FROM
            WMS_BARCODE_D a
        LEFT JOIN wms_barcode_h bh on bh.id = a.barcode_id
        LEFT JOIN wms_pick_task_d d ON a.wms_pick_task_d_id = d.pick_task_d_id
        LEFT JOIN wms_pick_task_h h on d.pick_task_id = h.pick_task_id
        LEFT JOIN wms_pick_order e on e.pick_order_id = h.pick_order_id
        LEFT JOIN wms_wave_storage s on d.WAVE_STORAGE_ID = s.id
        LEFT JOIN WMS_WAVE_BILL_D m on m.ID = s.BILL_D_ID
        LEFT JOIN WMS_WAVE_BILL n on m.bill_id = n.id
        LEFT JOIN OMS_DO_BILL_H obh on obh.BILLID = n.src_id
        LEFT JOIN wms_task_out_back ob on ob.billno = obh.billno
        LEFT JOIN WMS_MOVE_TASK v on v.BILL_D_ID = m.id
        LEFT JOIN WMS_FACE_ORDER r on r.waybill_code = bh.barcode
        WHERE  1=1
            AND h.PICK_TASK_TYPE = '0'
            AND h.status > 0
            AND bh.type='1'
            AND bh.barcode is not NULL
        AND (bh.cancel_flag <![CDATA[ <> ]]> '1' OR bh.cancel_flag IS NULL)
            AND d.ENTERPRISE_ID=#{enterpriseId}
            <if test="endTimeStr != null and endTimeStr != ''">
                AND d.pick_time &lt;= #{endTimeStr}
            </if>
            <if test="startTimeStr != null and startTimeStr != ''">
                AND d.pick_time &gt;= #{startTimeStr}
            </if>
            <if test="warehouseId != null and warehouseId != ''">
                AND d.WAREHOUSE_ID=#{warehouseId}
            </if>
            <if test="shipNo != null and shipNo != ''">
                AND m.ship_bill_no=#{shipNo}
            </if>
            <if test="pickOrderId != null and pickOrderId != ''">
                AND h.pick_order_id like concat(concat('%',#{pickOrderId}),'%')
            </if>
            <if test="billNo != null and billNo != ''">
                AND n.BILL_GROUP_ID like concat(concat('%',#{billNo}),'%')
            </if>
            <if test="containerCode != null and containerCode != ''">
                AND ob.container_no = #{containerCode}
            </if>
        <if test="shipperName != null and shipperName != ''">
            AND n.shipper_name like concat(concat('%',#{shipperName}),'%')
        </if>
        <if test="barcode != null and barcode != ''">
            AND bh.barcode like concat(concat('%',#{barcode}),'%')
        </if>
        GROUP BY bh.barcode,
            bh.warehouse_id,
            bh.barcode_type,
            n.consignor_id,
            n.BILL_GROUP_ID,
            d.print_count,
            d.PICK_TASK_ID,
            m.ITEM_ID,
            m.ITEM_NAME,
            m.ITEM_CODE,
            n.SHIPPER_ID,
            n.SHIPPER_TYPE,
            s.manufacturer_lot_num,
            s.EXPIRED_DATE_SHOW,
            s.PRODUCTION_DATE_SHOW,
            ob.container_no,
            d.pick_time,
            d.PICKER_NAME,
            d.qty,
            d.PICK_QTY,
            m.CASE_CONVERSION_RATE,
            s.WAREHOUSE_AREA_CODE,
            s.LOCATOR_CODE,
            v.dest_warehouse_locator_num,
            obh.remark
            ORDER BY e.GMT_CREATE desc,d.PICK_TASK_ID asc
    </select>

    <!-- 查询维度为订单-->
    <select id="listSpellLabelRepair" parameterType="com.prolog.cs.biz.outbound.model.dto.PrintAgainVo" resultType="com.prolog.cs.biz.outbound.model.dto.RePrintBulkDto">
        SELECT
            DISTINCT
            obh.BILLNO id,
            n.consignor_id consignorId,
            obh.BILLID billId,
            n.SHIPPER_ID shipperId,
            n.SHIPPER_TYPE shipperType,
            obh.remark,
            bh.barcode,
        (CASE
        WHEN r.cancel_flag != '1' AND r.cancel_flag IS NOT NULL THEN r.cancel_flag
        ELSE CASE obh.carrier_type
        WHEN '2' THEN NULL
        ELSE obh.carrier_code
        END
        END) AS carrierCode,
            GROUP_CONCAT(DISTINCT IFNULL(d.checker_name_2, d.checker_name)) checkerName,
            GROUP_CONCAT(DISTINCT h.station_name) stationName,
            MAX(stage.STAGE_CODE) stageCodeMax,
            MIN(stage.STAGE_CODE) stageCodeMin,
            MAX(d.check_time) checkTime
        FROM
            wms_pick_task_d d
        LEFT JOIN wms_barcode_d bd ON d.pick_task_d_id  = bd.wms_pick_task_d_id
        LEFT JOIN wms_barcode_h bh ON bh.id  = bd.barcode_id
        LEFT JOIN wms_pick_task_h h on d.pick_task_id = h.pick_task_id
        LEFT JOIN wms_wave_storage s on d.WAVE_STORAGE_ID = s.id
        LEFT JOIN WMS_WAVE_BILL_D m on m.ID = s.BILL_D_ID
        LEFT JOIN WMS_WAVE_BILL n on m.bill_id = n.id
        LEFT JOIN OMS_DO_BILL_H obh on obh.BILLID = n.src_id
        LEFT JOIN wms_task_out_back ob on ob.billno = obh.billno
        LEFT JOIN WMS_WORK_STAGE_RUNTIME stage ON stage.BILL_GROUP_ID=n.BILL_GROUP_ID
        LEFT JOIN WMS_FACE_ORDER r on r.waybill_code = bh.barcode
        WHERE  1=1
        AND h.PICK_TASK_TYPE IN('1', '2')
--         AND bd.print_cnt > 0
        AND h.status > 0
        AND bh.barcode is not null
        AND (bh.cancel_flag <![CDATA[ <> ]]> '1' OR bh.cancel_flag IS NULL)
        AND d.ENTERPRISE_ID=#{enterpriseId}
        <if test="endTimeStr != null and endTimeStr != ''">
            AND d.pick_time &lt;= #{endTimeStr}
        </if>
        <if test="startTimeStr != null and startTimeStr != ''">
            AND d.pick_time &gt;= #{startTimeStr}
        </if>
        <if test="warehouseId != null and warehouseId != ''">
            AND d.WAREHOUSE_ID=#{warehouseId}
        </if>
        <if test="shipNo != null and shipNo != ''">
            AND m.ship_bill_no=#{shipNo}
        </if>
        <if test="pickOrderId != null and pickOrderId != ''">
            AND h.pick_order_id like concat(concat('%',#{pickOrderId}),'%')
        </if>
        <if test="billNo != null and billNo != ''">
            AND n.BILL_GROUP_ID like concat(concat('%',#{billNo}),'%')
        </if>
        <if test="shipperName != null and shipperName != ''">
            AND n.shipper_name like concat(concat('%',#{shipperName}),'%')
        </if>
        <if test="barcode != null and barcode != ''">
            AND bh.barcode like concat(concat('%',#{barcode}),'%')
        </if>
        GROUP BY obh.BILLNO,
            n.consignor_id,
            obh.BILLID,
            n.SHIPPER_ID,
            n.SHIPPER_TYPE,
            obh.remark,
            bh.barcode,
            d.PICK_TASK_ID
        ORDER BY checkTime desc,d.PICK_TASK_ID asc
    </select>

    <!-- 查询维度为拣货任务明细-->
    <select id="listSpellLabelDetailRepair" resultType="com.prolog.cs.biz.outbound.model.dto.RePrintBulkDetailDto">
        SELECT
            DISTINCT
            b.lcl_no lclNo,
            m.ITEM_ID itemId,
            m.ITEM_NAME itemName,
            m.ITEM_CODE itemCode,
            m.ITEM_SPECS itemSpecs,
            s.manufacturer_lot_num lotNo,
            s.EXPIRED_DATE_SHOW expiredDateShow,
            s.PRODUCTION_DATE_SHOW productionDateShow,
            IFNULL(ob.container_no, 'N/A') containerCode,
            IFNULL(d.checker_name_2, d.checker_name) checkerName,
            IFNULL(d.checker2_qty, d.check_qty)qty,
            m.CASE_CONVERSION_RATE conversionRate,
            s.batch_id batchId
        FROM wms_pick_task_d d
        LEFT JOIN WMS_BARCODE_D a ON a.id = d.barcode_d_id
        LEFT JOIN wms_pick_task_h h on d.pick_task_id = h.pick_task_id
        LEFT JOIN wms_pick_order e on e.pick_order_id = h.pick_order_id
        LEFT JOIN wms_wave_storage s on d.WAVE_STORAGE_ID = s.id
        LEFT JOIN WMS_WAVE_BILL_D m on m.ID = s.BILL_D_ID
        LEFT JOIN WMS_WAVE_BILL n on m.bill_id = n.id
        LEFT JOIN OMS_DO_BILL_H obh on obh.BILLID = n.src_id
        LEFT JOIN wms_task_out_back ob on ob.billno = obh.billno
        LEFT JOIN WMS_MOVE_TASK v on v.BILL_D_ID = m.id
        LEFT JOIN wms_barcode_h b on a.barcode_id = b.id
        WHERE  1=1
        AND h.PICK_TASK_TYPE IN('1','2')
--         AND a.print_cnt > 0
        AND h.status > 0
        AND obh.BILLNO IN
        <foreach collection="billNos" close=")" index="index" item="item" open="(" separator=",">
            #{item}
        </foreach>
        ORDER BY b.lcl_no
    </select>

    <!-- 查询维度为拣货任务明细-->
    <select id="listWholeBulkDetailRepair" resultType="com.prolog.cs.biz.outbound.model.dto.RePrintWholeBulkDetailDto">
        SELECT
            DISTINCT
            m.ITEM_ID itemId,
            m.ITEM_NAME itemName,
            m.ITEM_CODE itemCode,
            m.ITEM_SPECS itemSpecs,
            s.manufacturer_lot_num lotNo,
            s.EXPIRED_DATE_SHOW expiredDateShow,
            s.PRODUCTION_DATE_SHOW productionDateShow,
            s.LOCATOR_CODE locatorCode,
            IFNULL(ob.container_no, 'N/A') containerCode,
            IFNULL(d.checker_name_2, d.checker_name) checkerName,
            IFNULL(d.checker2_qty, d.check_qty)qty,
            m.CASE_CONVERSION_RATE conversionRate,
            s.WAREHOUSE_AREA_CODE warehouseAreaCode,
            s.batch_id batchId,
            d.pick_task_d_id,
            d.pick_task_id
        FROM wms_pick_task_d d
        LEFT JOIN WMS_BARCODE_D a ON a.id = d.barcode_d_id
        LEFT JOIN wms_pick_task_h h on d.pick_task_id = h.pick_task_id
        LEFT JOIN wms_pick_order e on e.pick_order_id = h.pick_order_id
        LEFT JOIN wms_wave_storage s on d.WAVE_STORAGE_ID = s.id
        LEFT JOIN WMS_WAVE_BILL_D m on m.ID = s.BILL_D_ID
        LEFT JOIN WMS_WAVE_BILL n on m.bill_id = n.id
        LEFT JOIN OMS_DO_BILL_H obh on obh.BILLID = n.src_id
        LEFT JOIN wms_task_out_back ob on ob.billno = obh.billno
        LEFT JOIN WMS_MOVE_TASK v on v.BILL_D_ID = m.id
        LEFT JOIN wms_barcode_h b on a.barcode_id = b.id
        WHERE  1=1
        AND h.PICK_TASK_TYPE = '2'
        AND e.print_cnt > 0
        AND h.status > 0
        AND e.pick_order_id IN
        <foreach collection="billIds" close=")" index="index" item="item" open="(" separator=",">
            #{item}
        </foreach>
        ORDER BY d.PICK_TASK_ID asc
    </select>

    <!-- 查询维度为订单-->
    <select id="listWholeBulkRepair" parameterType="com.prolog.cs.biz.outbound.model.dto.PrintAgainVo" resultType="com.prolog.cs.biz.outbound.model.dto.RePrintWholeBulkDto">
        SELECT
            e.pick_order_id id,
            n.consignor_id consignorId,
            n.BILL_GROUP_ID billId,
            n.SHIPPER_ID shipperId,
            n.SHIPPER_TYPE shipperType,
            obh.remark,
            GROUP_CONCAT(DISTINCT d.picker_name) pickerName,
            GROUP_CONCAT(DISTINCT h.station_name) stationName,
            MAX(stage.STAGE_CODE) stageCodeMax,
            MIN(stage.STAGE_CODE) stageCodeMin,
            MAX(d.pick_time) pickTime
        FROM
            wms_pick_task_d d
        LEFT JOIN wms_pick_task_h h on d.pick_task_id = h.pick_task_id
        LEFT JOIN wms_wave_storage s on d.WAVE_STORAGE_ID = s.id
        LEFT JOIN wms_pick_order e on e.pick_order_id = h.pick_order_id
        LEFT JOIN WMS_WAVE_BILL_D m on m.ID = s.BILL_D_ID
        LEFT JOIN WMS_WAVE_BILL n on m.bill_id = n.id
        LEFT JOIN OMS_DO_BILL_H obh on obh.BILLID = n.src_id
        LEFT JOIN wms_task_out_back ob on ob.billno = obh.billno
        LEFT JOIN WMS_WORK_STAGE_RUNTIME stage ON stage.BILL_GROUP_ID=n.BILL_GROUP_ID
        WHERE  1=1
        AND h.PICK_TASK_TYPE = '2'
        AND e.print_cnt > 0
        AND h.status > 0
        AND d.ENTERPRISE_ID=#{enterpriseId}
        <if test="endTimeStr != null and endTimeStr != ''">
            AND d.pick_time &lt;= #{endTimeStr}
        </if>
        <if test="startTimeStr != null and startTimeStr != ''">
            AND d.pick_time &gt;= #{startTimeStr}
        </if>
        <if test="warehouseId != null and warehouseId != ''">
            AND d.WAREHOUSE_ID=#{warehouseId}
        </if>
        <if test="shipNo != null and shipNo != ''">
            AND m.ship_bill_no=#{shipNo}
        </if>
        <if test="pickOrderId != null and pickOrderId != ''">
            AND h.pick_order_id like concat(concat('%',#{pickOrderId}),'%')
        </if>
        <if test="billNo != null and billNo != ''">
            AND n.BILL_GROUP_ID like concat(concat('%',#{billNo}),'%')
        </if>
        GROUP BY e.pick_order_id,
            n.consignor_id,
            n.BILL_GROUP_ID,
            n.SHIPPER_ID,
            n.SHIPPER_TYPE,
            obh.remark,
            d.PICK_TASK_ID
        ORDER BY pickTime desc,d.PICK_TASK_ID asc
    </select>

    <select id="listCxPrintData" resultType="map">
        SELECT
            wbd.id,
            wbd.total_pcs boxcnt,
            wbd.ship_bill_no ydbh,
            wbh.contact_name shhr,
            cx.WDMCH wdmch,
            cx.ZCQFX zcdfx,
            wbd.cx_tag_print printcnt,
            (SELECT MAX(pick_time) FROM wms_pick_task_d pd WHERE pd.pick_task_id IN(SELECT pick_task_id FROM wms_pick_task_h ph WHERE ph.BILL_ID = wbh.src_code))picktime
        FROM wms_wave_bill_d wbd
        LEFT JOIN wms_wave_bill wbh ON wbh.id = wbd.bill_id
        LEFT JOIN base_cxpsd_wdinfo cx ON wbh.receive_address = cx.WDDZ
        WHERE wbd.ship_bill_no IN(
            SELECT
            wd.ship_bill_no
            FROM wms_wave_bill_d wd
            LEFT JOIN wms_wave_bill wb ON wd.bill_id = wb.id
            LEFT JOIN OUTCOMING_RECORD_H h ON wb.src_code = h.BILLID
            WHERE h.CARRIER_NAME = '创新物流'
            AND wd.ship_bill_no is not null
            AND h.BILLID IN (SELECT DISTINCT BILL_ID FROM wms_pick_task_h a WHERE 1=1 AND

            <if test="warehouseId != null and warehouseId != ''">
                wb.warehouse_id = #{warehouseId}
            </if>
               AND a.pick_task_id IN
            <foreach collection="pickTaskIds" item="item" separator="," index="index" open="(" close=")">
                #{item}
            </foreach>)
        )
    </select>

    <select id="getPickTaskdAndWareHouseV2" resultMap="PickTaskDV2VO">
        SELECT
            taskd.pick_task_d_id ,
            taskd.wave_storage_id,
            wws.batch_num ,
            COALESCE(taskd.qty,0) qty,
            COALESCE(taskd.pick_qty,0) pick_qty,
            wws.production_date,
            wws.expired_date,
            wws.MANUFACTURER_LOT_NUM,
            wws.item_id,
            wws.bill_id,
            wws.BILL_D_ID as billd_id,
            wws.warehouse_id,
            wws.VOLUME ,
            wws.WEIGHT ,
            taskd.CHECK_QTY ,
            taskd.CHECKER2_QTY ,
            taskd.CHECK_TIME check_time,
            taskd.CHECKER_NAME_2 CHECKER1 ,
            taskd.CHECKER_NAME_2 CHECKER2 ,
            taskd.picker_name,
            wws.uom,
            wws.locator_code,
            taskd.PICK_TIME,
            taskd.flow_status,
            taskd.PICK_TASK_TYPE,
            wws.salesperson_name salespersonName,
            wws.prepared_by createby,
            wws.remark remark,
            wws.paymentdaytype paymentType,
            bill.consignor_id,
            bill.shipper_id,
            bill.type,
            bill.warehouse_id,
            bill.src_code bill_code
        FROM WMS_PICK_TASK_D taskd
        inner join WMS_WAVE_STORAGE wws on wws.id = taskd.WAVE_STORAGE_ID
        INNER JOIN wms_wave_bill bill on bill.id = wws.bill_id
        where taskd.PICK_TASK_ID = #{pickTaskId}
        <if test="warehouseId != null and warehouseId != ''">
            and taskd.WAREHOUSE_ID = #{warehouseId}
        </if>
        order by taskd.pick_task_d_id
    </select>

    <select id="getPickTaskdByContainerCode" resultType="com.prolog.cs.biz.outbound.model.PickTaskD">
        SELECT
            d.pick_task_id,
            d.pick_task_d_id,
            d.flow_status,
            h.bill_group_id,
            h.bill_id
        FROM
            wms_pick_task_h h
            JOIN wms_pick_task_d d ON h.pick_task_id = d.pick_task_id
        WHERE
            d.container_code = #{containerCode}
    </select>

    <select id="getPickTaskdByPickOrderIdAndContainerCode" resultType="com.prolog.cs.biz.outbound.model.PickTaskD">
        SELECT
        d.*
        FROM
        wms_pick_task_h h
        JOIN wms_pick_task_d d ON h.pick_task_id = d.pick_task_id
        WHERE
        d.container_code = #{containerCode} and h.pick_order_id = #{pickOrderId}
    </select>

    <resultMap id="PickTaskDVoMap" type="com.prolog.cs.biz.outbound.model.vo.PickTaskDVo">
        <result property="qty" column="QTY"/>
        <result property="itemId" column="item_id"/>
        <result property="locateId" column="locator_id"/>
        <result property="manufacturerLotNum" column="manufacturer_lot_num"/>
        <result property="pk" column="pk"/>
        <result property="unConfirmCount" column="unConfirmCount"/>
    </resultMap>
    <select id="getBulkPickTaskDList" resultMap="PickTaskDVoMap">
        SELECT
            SUM(a.qty) QTY,
            b.item_id,
            b.locator_id,
            b.manufacturer_lot_num,
            CONCAT(IFNULL(b.item_id,''),IFNULL(b.locator_id,''),IFNULL(b.manufacturer_lot_num,'')) pk,
            (SUM(IF(a.flow_status >= 3, 0, 1))) unConfirmCount
        FROM wms_pick_task_d a
        LEFT JOIN wms_wave_storage b ON a.wave_storage_id = b.id
        LEFT JOIN wms_pick_task_h d ON a.pick_task_id = d.pick_task_id
        WHERE 1=1
        <if test="warehouseId != null and warehouseId!=''">
            AND a.warehouse_id = #{warehouseId}
        </if>
        <if test="enterpriseId != null and enterpriseId!=''">
            AND a.enterprise_id = #{enterpriseId}
        </if>
        <if test="bldgId != null and bldgId!=''">
            AND d.bldg_id = #{bldgId}
        </if>
        <if test="pickOrderId != null and pickOrderId!=''">
            AND d.pick_order_id = #{pickOrderId}
        </if>
        <if test="pks != null and pks.size() > 0">
            AND CONCAT(IFNULL(b.item_id,''),IFNULL(b.locator_id,''),IFNULL(b.manufacturer_lot_num,'')) IN
            <foreach collection="pks" open="(" close=")" separator="," item="item" index="index">
                #{item}
            </foreach>
        </if>
        GROUP BY pk, item_id,locator_id, manufacturer_lot_num
        ORDER BY qty
    </select>
    <resultMap id="ContainerQtyMap" type="com.prolog.cs.biz.outbound.model.vo.ContainerQtyVo">
        <result property="qty" column="QTY"/>
        <result property="containerCode" column="container_code"/>
        <result property="serialNumber" column="serial_number"/>
        <result property="pk" column="pk"/>
    </resultMap>
    <select id="getContainerQty" resultMap="ContainerQtyMap">
        SELECT
            SUM(a.qty) QTY,
            b.item_id,
            b.locator_id,
            b.manufacturer_lot_num,
            d.container_code,
            d.serial_number,
            CONCAT(IFNULL(b.item_id,''),IFNULL(b.locator_id,''),IFNULL(b.manufacturer_lot_num,'')) pk
        FROM wms_pick_task_d a
        LEFT JOIN wms_wave_storage b ON a.wave_storage_id = b.id
        LEFT JOIN wms_pick_task_h d ON a.pick_task_id = d.pick_task_id
        WHERE 1=1
        <if test="warehouseId != null and warehouseId!=''">
            AND a.warehouse_id = #{warehouseId}
        </if>
        <if test="enterpriseId != null and enterpriseId!=''">
            AND a.enterprise_id = #{enterpriseId}
        </if>
        <if test="bldgId != null and bldgId!=''">
            AND d.bldg_id = #{bldgId}
        </if>
        <if test="pickOrderId != null and pickOrderId!=''">
            AND d.pick_order_id = #{pickOrderId}
        </if>
        <if test="pks != null and pks.size() > 0">
            AND CONCAT(IFNULL(b.item_id,''),IFNULL(b.locator_id,''),IFNULL(b.manufacturer_lot_num,'')) IN
            <foreach collection="pks" open="(" close=")" separator="," item="item" index="index">
                #{item}
            </foreach>
        </if>
        GROUP BY pk, item_id, locator_id, manufacturer_lot_num, container_code, serial_number
        ORDER BY qty
    </select>
    <resultMap id="PickTaskDPickMap" type="com.prolog.cs.biz.outbound.model.vo.PickTaskDPickVo">
        <result property="qty" column="qty"/>
        <result property="containerCode" column="container_code"/>
        <result property="serialNumber" column="serial_number"/>
        <result property="pickTaskDId" column="pick_task_d_id"/>
    </resultMap>
    <select id="getByPickOrder" resultMap="PickTaskDPickMap">
        SELECT
            a.pick_task_d_id,
            a.qty,
            d.container_code,
            d.serial_number
        FROM wms_pick_task_d a
        LEFT JOIN wms_wave_storage b ON a.wave_storage_id = b.id
        LEFT JOIN wms_pick_task_h d ON a.pick_task_id = d.pick_task_id
        WHERE a.flow_status &lt; 3
        <if test="pickOrderId != null and pickOrderId!=''">
            d.pick_order_id = #{pickOrderId}
        </if>
        <if test="itemId != null and itemId!=''">
            b.item_id = #{itemId}
        </if>
        <if test="locateId != null and locateId!=''">
            b.locator_id = #{locateId}
        </if>
        <if test="manufacturerLotNum != null and manufacturerLotNum!=''">
            b.manufacturer_lot_num = #{manufacturerLotNum}
        </if>
    </select>
    <select id="getLclNo" resultType="string">
        SELECT lcl_no FROM wms_barcode_h h
        JOIN wms_barcode_d d ON d.barcode_id = h.id
        JOIN wms_pick_task_d pd ON pd.pick_task_d_id = d.wms_pick_task_d_id
        JOIN wms_pick_task_h ph ON pd.pick_task_id = ph.pick_task_id
        WHERE h.status = 0 AND ph.bill_id = #{billId} AND ph.station_code = #{stationCode}
        limit 1;
    </select>
    <select id="listInnerCheckPickTaskD" resultType="com.prolog.cs.biz.outbound.model.PickTaskD">
        SELECT pd.* FROM wms_pick_task_d pd
        LEFT JOIN wms_barcode_d d ON d.wms_pick_task_d_id = pd.pick_task_id
        LEFT JOIN wms_pick_task_h ph ON pd.pick_task_id = ph.pick_task_id
        WHERE (d.id IS NULL OR pd.flow_status &lt; 5) AND h.pick_task_id = #{pickTaskId}
    </select>

    <select id="listPickTaskDByBarcodeDId" resultMap="PickTaskDV2VO">
        SELECT
            taskd.pick_task_d_id,
            taskd.wave_storage_id,
            wws.batch_num,
            IFNULL(taskd.qty,0) qty,
            IFNULL(taskd.pick_qty,0) pick_qty,
            IFNULL(taskd.sale_red_qty,0) red_qty,
            wws.production_date,
            wws.expired_date,
            wws.MANUFACTURER_LOT_NUM,
            wws.item_id,
            wws.bill_id,
            wws.BILL_D_ID as billd_id,
            wws.warehouse_id,
            wws.VOLUME,
            wws.WEIGHT,
            taskd.CHECK_QTY,
            taskd.CHECKER2_QTY,
            taskd.CHECK_TIME check_time,
            taskd.CHECKER_NAME_2 CHECKER1,
            taskd.CHECKER_NAME_2 CHECKER2,
            taskd.picker_name,
            wws.uom,
            wws.locator_code,
            taskd.PICK_TIME,
            taskd.flow_status,
            taskd.PICK_TASK_TYPE,
            taskd.barcode_d_id barcodeDId
        FROM WMS_PICK_TASK_D taskd
        inner join WMS_WAVE_STORAGE wws on wws.id = taskd.WAVE_STORAGE_ID
        where taskd.barcode_d_id IN
        <foreach collection=" barcodeDIds" separator="," open="(" item="item" index="index" close=")">
            #{item}
        </foreach>
        order by taskd.pick_task_d_id
    </select>

    <update id="updatePrintCount">
        UPDATE wms_pick_task_d SET print_count = IFNULL(print_count, 0) + 1 WHERE pick_task_d_id IN
        <foreach collection="pickTaskDIds" index="index" item="item" separator="," close=")" open="(">
            #{item}
        </foreach>
    </update>
    <update id="updatePrintCountByBarcode">
        UPDATE wms_pick_task_d SET print_count = IFNULL(print_count, 0) + 1 WHERE pick_task_d_id IN
        (SELECT wms_pick_task_d_id FROM wms_barcode_d WHERE id IN
        <foreach collection="ids" index="index" item="item" separator="," close=")" open="(">
            #{item}
        </foreach>)
    </update>
    <update id="updateVersion">
        UPDATE wms_pick_task_d SET version = #{version} + 1
        WHERE pick_task_d_id = #{pickTaskDId}
        AND (version IS NULL OR (version IS NOT NULL AND version = #{version}))
    </update>
    <update id="updateZtPickTaskD">
        UPDATE wms_pick_task_d set pick_qty = qty,check_qty = qty,picker_id=#{userDTO.id},picker_no=#{userNo},picker_name=#{userDTO.nickname},pick_time=#{date},checker_id=#{userDTO.id},checker_no=#{userNo},checker_name=#{userDTO.nickname},check_time=#{date},flow_status = #{flowStatus}
        where pick_task_id in
        <foreach collection="pickTaskIds" index="index" item="pickTaskId" separator="," close=")" open="(">
            #{pickTaskId}
        </foreach>
    </update>
    <update id="updateByWaveStorageIds">
        UPDATE wms_pick_task_d set pick_qty = qty,check_qty = qty,flow_status = '7'
        where wave_storage_id in
        <foreach collection="waveStorageIds" index="index" item="item" separator="," close=")" open="(">
            #{item}
        </foreach>
    </update>

    <select id="listPickD4EisConfirm" resultType="com.prolog.cs.biz.outbound.model.PickTaskD">
        SELECT d.*, s.locator_id locatorId
        FROM wms_pick_task_d d
        LEFT JOIN wms_wave_storage s ON d.WAVE_STORAGE_ID = s.id
        WHERE d.ob_msg_id IN
        <foreach collection="ids" index="index" item="item" separator="," close=")" open="(">
            #{item}
        </foreach>
    </select>

    <select id="getPickTaskDListByPickTaskIds" resultType="com.prolog.cs.biz.outbound.model.PickTaskD">
        select <include refid="Base_Column_List" />
        from wms_pick_task_d
        where pick_task_id in
        <foreach collection="pickTaskIds" index="index" item="pickTaskId" separator="," close=")" open="(">
            #{pickTaskId}
        </foreach>
    </select>

    <select id="getCheckStatusBySrcCode" resultType="java.util.Map">
        select pd.src_id as billId,bh.check_status as checkStatus
        from wms_pick_task_d pd
        left join wms_barcode_d bd on bd.wms_pick_task_d_id = pd.pick_task_d_id
        left join wms_barcode_h bh on bh.id = bd.barcode_id
        where pd.src_id in
        <foreach collection="billIds" index="index" item="billId" separator="," close=")" open="(">
            #{billId}
        </foreach>
    </select>
    <select id="getUnOutCheckBillNos" resultType="java.lang.String">
        select distinct bill_group_id
        from wms_pick_task_h
        where bill_group_id not in (select billno
        from outcoming_record_h)
        and bill_group_id not in (select bill_group_id
        from wms_pick_task_h
        where status <![CDATA[ < ]]> 2)
        and bill_group_id not in (select src_id
        from wms_pick_task_d
        where flow_status <![CDATA[ < ]]> 3 and src_id is not null)
        and bill_group_id in (
        select bill_group_id from wms_barcode_h where 1=1 and barcode is not null and barcode != '' and ifnull(check_status,0) = 4
        )
        and bill_group_id not in (
        select bill_group_id from wms_barcode_h where 1=1 and (barcode is null or (type = 0 and ifnull(check_status,0) <![CDATA[ < ]]> 4))
        )
        and bill_group_id not in (
        select billno from oms_do_bill_h where ifnull(attr_num2,0) <![CDATA[ > ]]> #{times}
        )
        and status = 2
        and pick_task_type = 1
        order by gmt_create asc
        limit 1


    </select>
</mapper>