<?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.upcloud.base.wh.operate.dao.WmsReplenishDMapper">
    <select id="selectReplenishList" resultType="com.prolog.upcloud.base.wh.operate.vo.ReplenishListVO">
        select H.SOURCE_TYPE,
        H.WAREHOUSE_ID,
        H.BILL_TYPE,
        H.AUTO_BILL_NUM,H.CUSTOM_BILL_NUM,
        H.BUSINESS_TYPE,
        H.HEADER_ID,
        D.DETAIL_ID,
        D.ENTERPRISE_ID,
        D.CARGO_OWNER_ID,
        D.LINE_NUM,
        D.STATUS,
        D.ITEM_ID,
        D.ITEM_CODE,
        D.WH_AREA_CATEGORY,
        D.WH_AREA_ID,
        D.WH_AREA_CODE,
        D.WH_LOCATOR_ID,
        D.WH_LOCATOR_CODE,
        D.BATCH_ID,
        D.BATCH_NUM,
        D.MANUFACTURER_LOT_NUM,
        D.BATCH_PRODUCTION_DATE,
        D.BATCH_EXPIRED_DATE,
        D.UOM,
        D.CONVERSION_RATE,
        D.QUANTITY,
        D.PIECE_QUANTITY,
        D.TO_WH_AREA_CATEGORY,
        D.TO_WH_AREA_ID,
        D.TO_WH_AREA_CODE,
        D.TO_WH_LOCATOR_ID,
        D.TO_WH_LOCATOR_CODE,
        D.OMS_BILL_NO,D.ERP_SRC_ID,H.HEADER_ID ERP_SRC_NO,D.ERP_SRC_LINE_ID
        from WMS_REPLENISH_H H
        INNER JOIN WMS_REPLENISH_D D on H.HEADER_ID = D.HEADER_ID
        WHERE D.type = 0 and WAREHOUSE_ID = #{query.warehouseId} and H.ENTERPRISE_ID= #{enterpriseId}
        <if test="query.detailIds !=null and query.detailIds.size() > 0">
            AND D.DETAIL_ID in
            <foreach collection="query.detailIds" separator="," open="(" close=")" item="detailId" index="index">
                #{detailId}
            </foreach>
        </if>
        <if test="query.detailId !='' and query.detailId != null">
            AND DETAIL_ID = #{query.detailId}
        </if>
        <if test="query.cargoOwnerId !='' and query.cargoOwnerId != null">
            AND CARGO_OWNER_ID = #{query.cargoOwnerId}
        </if>
        <if test="query.businessType !='' and query.businessType != null">
            AND BUSINESS_TYPE = #{query.businessType}
        </if>
        <if test="query.autoBillNum !='' and query.autoBillNum != null">
            AND AUTO_BILL_NUM = #{query.autoBillNum}
        </if>
        <if test="query.status !='' and query.status != null">
            AND STATUS = #{query.status}
        </if>
        <if test="query.omsBillNo !='' and query.omsBillNo != null">
            AND OMS_BILL_NO like CONCAT(CONCAT('%',#{query.omsBillNo}),'%')
        </if>
        <if test="query.erpSrcId !='' and query.erpSrcId != null">
            AND ERP_SRC_ID like CONCAT(CONCAT('%',#{query.erpSrcId}),'%')
        </if>
        <if test="query.erpSrcNo !='' and query.erpSrcNo != null">
            AND H.HEADER_ID like CONCAT(CONCAT('%',#{query.erpSrcNo}),'%')
        </if>
        <if test="query.erpSrcLineId !='' and query.erpSrcLineId != null">
            AND ERP_SRC_LINE_ID like CONCAT(CONCAT('%',#{query.erpSrcLineId}),'%')
        </if>
        <if test="query.toLocatorCode !='' and query.toLocatorCode != null">
            AND D.TO_WH_LOCATOR_CODE = #{query.toLocatorCode}
        </if>
        order by H.GMT_CREATE,D.LINE_NUM DESC
    </select>

    <select id="getReplenishUnshelveList" resultType="com.prolog.upcloud.base.wh.operate.vo.ReplenishUnshelveVO">
        SELECT
            d.detail_id AS id,
            d.header_id AS hrdId,
            d.item_id AS itemId,
            d.conversion_rate AS caseConversionRate,
            d.batch_id AS batchId,
            d.manufacturer_lot_num AS manufacturerLotNum,
            d.piece_quantity as pieceQuantity,
            d.wh_locator_code AS locatorCode,
            d.oms_bill_no as omsBillNo,
            d.wh_locator_code as locator
        FROM `wms_replenish_d` d
        LEFT JOIN wms_replenish_h h ON d.header_id = h.header_id
        WHERE h.enterprise_id = #{enterpriseId}
          AND h.warehouse_id = #{warehouseId}
        <if test="conditions != null and conditions.size() > 0">
            AND d.detail_id in
            <foreach collection="conditions" separator="," open="(" close=")" item="condition" index="index">
                #{condition}
            </foreach>
        </if>
        <if test="businessType != null and businessType!= ''">
            AND h.business_type = #{businessType}
        </if>
          AND `status` in (0,1)
          AND (ASSIGN_UID = #{userId} OR ASSIGN_UID = '' OR ASSIGN_UID is null)
        ORDER BY d.gmt_modified DESC
    </select>

    <select id="findByDetailId" resultType="com.prolog.upcloud.base.wh.operate.model.WmsReplenishD">
        select
        H.WAREHOUSE_ID,
        D.*
        from WMS_REPLENISH_H H
        INNER JOIN WMS_REPLENISH_D D on H.HEADER_ID = D.HEADER_ID
        WHERE D.type=0 and H.ENTERPRISE_ID= #{enterpriseId}
        AND D.DETAIL_ID IN(
        <foreach collection="list" item="item" index="index" separator=",">
            #{item}
        </foreach>
        )
        order by H.GMT_CREATE,D.LINE_NUM DESC
    </select>

    <update id="updateBatch">
        UPDATE
        WMS_REPLENISH_D
        SET status =CASE
        <foreach collection="list" item="item" index="index" separator=" ">
            WHEN DETAIL_ID = #{item.detailId} THEN #{item.status}
        </foreach>
        ELSE STATUS END,

        to_wh_area_category =CASE
        <foreach collection="list" item="item" index="index" separator=" ">
            WHEN DETAIL_ID = #{item.detailId} THEN #{item.toWhAreaCategory}
        </foreach>
        ELSE to_wh_area_category END,

        to_wh_area_id =CASE
        <foreach collection="list" item="item" index="index" separator=" ">
            WHEN DETAIL_ID = #{item.detailId} THEN #{item.toWhAreaId}
        </foreach>
        ELSE to_wh_area_id END,

        to_wh_area_code =CASE
        <foreach collection="list" item="item" index="index" separator=" ">
            WHEN DETAIL_ID = #{item.detailId} THEN #{item.toWhAreaCode}
        </foreach>
        ELSE to_wh_area_code END,

        to_wh_locator_id =CASE
        <foreach collection="list" item="item" index="index" separator=" ">
            WHEN DETAIL_ID = #{item.detailId} THEN #{item.toWhLocatorId}
        </foreach>
        ELSE to_wh_locator_id END,

        to_wh_locator_code =CASE
        <foreach collection="list" item="item" index="index" separator=" ">
            WHEN DETAIL_ID = #{item.detailId} THEN #{item.toWhLocatorCode}
        </foreach>
        ELSE to_wh_locator_code END,

        gmt_modified = NOW()
        WHERE DETAIL_ID IN(
        <foreach collection="list" item="item" index="index" separator=",">
            #{item.detailId}
        </foreach>
        )
    </update>
</mapper>
