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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.prolog.cs.biz.outbound.model.entity.WaveBillDO">
        <!-- id -->
        <result column="ID" property="id"/>
        <!-- 编码 -->
        <result column="CODE" property="code"/>
        <!-- 来源单id -->
        <result column="SRC_ID" property="srcId"/>
        <!-- 来源单编码 -->
        <result column="SRC_CODE" property="srcCode"/>
        <!-- 仓库id -->
        <result column="WAREHOUSE_ID" property="warehouseId"/>
        <!-- 货主id -->
        <result column="CONSIGNOR_ID" property="consignorId"/>
        <!-- 类型 -->
        <result column="TYPE" property="type"/>
        <!-- 优先级 -->
        <result column="PRIORITY" property="priority"/>
        <!-- 波次id -->
        <result column="WAVE_ID" property="waveId"/>
        <!-- 波次订单分组id -->
        <result column="WAVE_BILL_GROUP_ID" property="waveBillGroupId"/>
        <!-- 订单状态（CREATED(已经创建)/DISTRIBUTED(已经下发)） -->
        <result column="STATUS" property="status"/>
        <!-- 客户类型 -->
        <result column="SHIPPER_TYPE" property="shipperType"/>
        <!-- 客户编码 -->
        <result column="SHIPPER_CODE" property="shipperCode"/>
        <!-- 客户名称 -->
        <result column="SHIPPER_NAME" property="shipperName"/>
        <!-- 客户id -->
        <result column="SHIPPER_ID" property="shipperId"/>
        <!-- 联系人姓名 -->
        <result column="CONTACT_NAME" property="contactName"/>
        <!-- 联系人电话 -->
        <result column="CONTACT_TEL" property="contactTel"/>
        <!-- 联系人地址 -->
        <result column="RECEIVE_ADDRESS" property="receiveAddress"/>
        <!-- 收货方式 -->
        <result column="RECEIVE_WAY" property="receiveWay"/>
        <!-- 预定到货日期 -->
        <result column="ATTAIN_DATE" property="attainDate"/>
        <!-- 运输路线 -->
        <result column="SHIP_LINE_ID" property="shipLineId"/>
        <!-- 承运商名称 -->
        <result column="CARRIER_NAME" property="carrierName"/>
        <!-- 企业id -->
        <result column="ENTERPRISE_ID" property="enterpriseId"/>
        <!-- 创建日期 -->
        <result column="GMT_CREATE" property="gmtCreate"/>
        <!-- 创建人 -->
        <result column="CREATOR" property="creator"/>
        <!-- CREATED_NAME -->
        <result column="CREATOR_NAME" property="creatorName"/>
        <!-- 最后修改日期 -->
        <result column="GMT_MODIFIED" property="gmtModified"/>
        <!-- 最后修改人 -->
        <result column="MODIFIER" property="modifier"/>
        <!-- LAST_UPDATED_NAME -->
        <result column="MODIFIER_NAME" property="modifierName"/>
        <result column="ERROR_MSG" property="errorMsg"/>
        <!-- 总重量 -->
        <result column="TOTAL_WEIGHT" property="totalWeight"/>
        <!-- 总件数 -->
        <result column="TOTAL_PCS" property="totalPcs"/>
        <!-- 总数量 -->
        <result column="TOTAL_QTY" property="totalQty"/>
        <!-- 总体积 -->
        <result column="TOTAL_VOLUME" property="totalVolume"/>
        <!-- 销售部门 -->
        <result column="SALE_BRANCH_NAME" property="saleBranchName"/>
        <!-- 订单合并后的新id -->
        <result column="BILL_GROUP_ID" property="billGroupId"/>
        <result column="payment_day_type" property="paymentDayType"/>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        t.ID,
        t.CODE,
        t.SRC_ID,
        t.SRC_CODE,
        t.WAREHOUSE_ID,
        t.CONSIGNOR_ID,
        t.TYPE,
        t.PRIORITY,
        t.WAVE_ID,
        t.WAVE_BILL_GROUP_ID,
        t.STATUS,
        t.SHIPPER_TYPE,
        t.SHIPPER_CODE,
        t.SHIPPER_NAME,
        t.SHIPPER_ID,
        t.CONTACT_NAME,
        t.CONTACT_TEL,
        t.RECEIVE_ADDRESS,
        t.RECEIVE_WAY,
        t.ATTAIN_DATE,
        t.SHIP_LINE_ID,
        t.CARRIER_NAME,
        t.ENTERPRISE_ID,
        t.GMT_CREATE,
        t.CREATOR,
        t.CREATOR_NAME,
        t.GMT_MODIFIED,
        t.MODIFIER,
        t.MODIFIER_NAME,
        t.ERROR_MSG,
        t.TOTAL_WEIGHT,
        t.TOTAL_PCS,
        t.TOTAL_QTY,
        t.TOTAL_VOLUME,
        t.SALE_BRANCH_NAME,
        t.BILL_GROUP_ID,
        t.payment_day_type,
        t.is_merged
    </sql>

    <!-- 通用查询语句(数据库comment中含有?则不显示字段注释)) -->
    <sql id="Base_Where_List">


        <!-- id查询 -->
        <if test="params.id != null and params.id != ''">
            AND t.ID = #{params.id}
        </if>


        <!-- id模糊查询 -->
        <if test="params.idLike != null and params.idLike != ''">
            AND t.ID LIKE '%' || #{params.idLike} || '%'
        </if>


        <!-- 编码查询 -->
        <if test="params.code != null and params.code != ''">
            AND t.CODE = #{params.code}
        </if>


        <!-- 编码模糊查询 -->
        <if test="params.codeLike != null and params.codeLike != ''">
            AND t.CODE LIKE '%' || #{params.codeLike} || '%'
        </if>


        <!-- 来源单id查询 -->
        <if test="params.srcId != null and params.srcId != ''">
            AND t.SRC_ID = #{params.srcId}
        </if>


        <!-- 来源单id模糊查询 -->
        <if test="params.srcIdLike != null and params.srcIdLike != ''">
            AND t.SRC_ID LIKE '%' || #{params.srcIdLike} || '%'
        </if>


        <!-- 来源单编码查询 -->
        <if test="params.srcCode != null and params.srcCode != ''">
            AND t.SRC_CODE = #{params.srcCode}
        </if>


        <!-- 来源单编码模糊查询 -->
        <if test="params.srcCodeLike != null and params.srcCodeLike != ''">
            AND t.SRC_CODE LIKE '%' || #{params.srcCodeLike} || '%'
        </if>


        <!-- 仓库id查询 -->
        <if test="params.warehouseId != null and params.warehouseId != ''">
            AND t.WAREHOUSE_ID = #{params.warehouseId}
        </if>


        <!-- 仓库id模糊查询 -->
        <if test="params.warehouseIdLike != null and params.warehouseIdLike != ''">
            AND t.WAREHOUSE_ID LIKE '%' || #{params.warehouseIdLike} || '%'
        </if>


        <!-- 货主id查询 -->
        <if test="params.consignorId != null and params.consignorId != ''">
            AND t.CONSIGNOR_ID = #{params.consignorId}
        </if>

        <!-- 货主id模糊查询 -->
        <if test="params.consignorIdLike != null and params.consignorIdLike != ''">
            AND t.CONSIGNOR_ID LIKE '%' || #{params.consignorIdLike} || '%'
        </if>


        <!-- 类型查询 -->
        <if test="params.type != null and params.type != ''">
            AND t.TYPE = #{params.type}
        </if>


        <!-- 类型模糊查询 -->
        <if test="params.typeLike != null and params.typeLike != ''">
            AND t.TYPE LIKE '%' || #{params.typeLike} || '%'
        </if>


        <!-- 优先级查询 -->
        <if test="params.priority != null and params.priority != ''">
            AND t.PRIORITY = #{params.priority}
        </if>


        <!-- 波次id查询 -->
        <if test="params.waveId != null and params.waveId != ''">
            AND t.WAVE_ID = #{params.waveId}
        </if>


        <!-- 波次id模糊查询 -->
        <if test="params.waveIdLike != null and params.waveIdLike != ''">
            AND t.WAVE_ID LIKE '%' || #{params.waveIdLike} || '%'
        </if>


        <!-- 波次订单分组id查询 -->
        <if test="params.waveBillGroupId != null and params.waveBillGroupId != ''">
            AND t.WAVE_BILL_GROUP_ID = #{params.waveBillGroupId}
        </if>


        <!-- 波次订单分组id模糊查询 -->
        <if test="params.waveBillGroupIdLike != null and params.waveBillGroupIdLike != ''">
            AND t.WAVE_BILL_GROUP_ID LIKE '%' || #{params.waveBillGroupIdLike} || '%'
        </if>


        <!-- 订单状态（CREATED(已经创建)/DISTRIBUTED(已经下发)）查询 -->
        <if test="params.status != null and params.status != ''">
            AND t.STATUS = #{params.status}
        </if>


        <!-- 订单状态（CREATED(已经创建)/DISTRIBUTED(已经下发)）模糊查询 -->
        <if test="params.statusLike != null and params.statusLike != ''">
            AND t.STATUS LIKE '%' || #{params.statusLike} || '%'
        </if>


        <!-- 客户类型查询 -->
        <if test="params.shipperType != null and params.shipperType != ''">
            AND t.SHIPPER_TYPE = #{params.shipperType}
        </if>


        <!-- 客户类型模糊查询 -->
        <if test="params.shipperTypeLike != null and params.shipperTypeLike != ''">
            AND t.SHIPPER_TYPE LIKE '%' || #{params.shipperTypeLike} || '%'
        </if>


        <!-- 客户编码查询 -->
        <if test="params.shipperCode != null and params.shipperCode != ''">
            AND t.SHIPPER_CODE = #{params.shipperCode}
        </if>


        <!-- 客户编码模糊查询 -->
        <if test="params.shipperCodeLike != null and params.shipperCodeLike != ''">
            AND t.SHIPPER_CODE LIKE '%' || #{params.shipperCodeLike} || '%'
        </if>


        <!-- 客户名称查询 -->
        <if test="params.shipperName != null and params.shipperName != ''">
            AND t.SHIPPER_NAME = #{params.shipperName}
        </if>


        <!-- 客户名称模糊查询 -->
        <if test="params.shipperNameLike != null and params.shipperNameLike != ''">
            AND t.SHIPPER_NAME LIKE '%' || #{params.shipperNameLike} || '%'
        </if>


        <!-- 客户id查询 -->
        <if test="params.shipperId != null and params.shipperId != ''">
            AND t.SHIPPER_ID = #{params.shipperId}
        </if>


        <!-- 客户id模糊查询 -->
        <if test="params.shipperIdLike != null and params.shipperIdLike != ''">
            AND t.SHIPPER_ID LIKE '%' || #{params.shipperIdLike} || '%'
        </if>


        <!-- 联系人姓名查询 -->
        <if test="params.contactName != null and params.contactName != ''">
            AND t.CONTACT_NAME = #{params.contactName}
        </if>


        <!-- 联系人姓名模糊查询 -->
        <if test="params.contactNameLike != null and params.contactNameLike != ''">
            AND t.CONTACT_NAME LIKE '%' || #{params.contactNameLike} || '%'
        </if>


        <!-- 联系人电话查询 -->
        <if test="params.contactTel != null and params.contactTel != ''">
            AND t.CONTACT_TEL = #{params.contactTel}
        </if>


        <!-- 联系人电话模糊查询 -->
        <if test="params.contactTelLike != null and params.contactTelLike != ''">
            AND t.CONTACT_TEL LIKE '%' || #{params.contactTelLike} || '%'
        </if>


        <!-- 联系人地址查询 -->
        <if test="params.receiveAddress != null and params.receiveAddress != ''">
            AND t.RECEIVE_ADDRESS = #{params.receiveAddress}
        </if>

        <!-- 联系人地址模糊查询 -->
        <if test="params.receiveAddressLike != null and params.receiveAddressLike != ''">
            AND t.RECEIVE_ADDRESS LIKE '%' || #{params.receiveAddressLike} || '%'
        </if>


        <!-- 收货方式查询 -->
        <if test="params.receiveWay != null and params.receiveWay != ''">
            AND t.RECEIVE_WAY = #{params.receiveWay}
        </if>

        <!-- 收货方式模糊查询 -->
        <if test="params.receiveWayLike != null and params.receiveWayLike != ''">
            AND t.RECEIVE_WAY LIKE '%' || #{params.receiveWayLike} || '%'
        </if>


        <!-- 预定到货日期开始时间查询 -->
        <if test="params.attainDateStart != null and params.attainDateStart != ''">
            AND t.ATTAIN_DATE &gt;= to_date(#{params.attainDateStart}, 'yyyy-mm-dd HH24:MI:SS')
        </if>

        <!-- 预定到货日期结束时间查询 -->
        <if test="params.attainDateEnd != null and params.attainDateEnd != ''">
            AND t.ATTAIN_DATE &lt;= to_date(#{params.attainDateEnd}, 'yyyy-mm-dd HH24:MI:SS')
        </if>

        <!-- 运输路线查询 -->
        <if test="params.shipLineId != null and params.shipLineId != ''">
            AND t.SHIP_LINE_ID = #{params.shipLineId}
        </if>

        <!-- 运输路线模糊查询 -->
        <if test="params.shipLineIdLike != null and params.shipLineIdLike != ''">
            AND t.SHIP_LINE_ID LIKE '%' || #{params.shipLineIdLike} || '%'
        </if>

        <!-- 承运商名称查询 -->
        <if test="params.carrierName != null and params.carrierName != ''">
            AND t.CARRIER_NAME = #{params.carrierName}
        </if>


        <!-- 承运商名称模糊查询 -->
        <if test="params.carrierNameLike != null and params.carrierNameLike != ''">
            AND t.CARRIER_NAME LIKE '%' || #{params.carrierNameLike} || '%'
        </if>


        <!-- 企业id查询 -->
        <if test="params.enterpriseId != null and params.enterpriseId != ''">
            AND t.ENTERPRISE_ID = #{params.enterpriseId}
        </if>


        <!-- 企业id模糊查询 -->
        <if test="params.enterpriseIdLike != null and params.enterpriseIdLike != ''">
            AND t.ENTERPRISE_ID LIKE '%' || #{params.enterpriseIdLike} || '%'
        </if>


        <!-- 创建日期开始时间查询 -->
        <if test="params.gmtCreateStart != null and params.gmtCreateStart != ''">
            AND t.GMT_CREATE &gt;= to_date(#{params.gmtCreateStart}, 'yyyy-mm-dd HH24:MI:SS')
        </if>

        <!-- 创建日期结束时间查询 -->
        <if test="params.gmtCreateEnd != null and params.gmtCreateEnd != ''">
            AND t.GMT_CREATE &lt;= to_date(#{params.gmtCreateEnd}, 'yyyy-mm-dd HH24:MI:SS')
        </if>

        <!-- 创建人查询 -->
        <if test="params.creator != null and params.creator != ''">
            AND t.CREATOR = #{params.creator}
        </if>

        <!-- 创建人模糊查询 -->
        <if test="params.creatorLike != null and params.creatorLike != ''">
            AND t.CREATOR LIKE '%' || #{params.creatorLike} || '%'
        </if>

        <!-- CREATED_NAME查询 -->
        <if test="params.creatorName != null and params.creatorName != ''">
            AND t.CREATOR_NAME = #{params.creatorName}
        </if>


        <!-- CREATED_NAME模糊查询 -->
        <if test="params.creatorNameLike != null and params.creatorNameLike != ''">
            AND t.CREATOR_NAME LIKE '%' || #{params.creatorNameLike} || '%'
        </if>


        <!-- 最后修改日期开始时间查询 -->
        <if test="params.gmtModifiedStart != null and params.gmtModifiedStart != ''">
            AND t.GMT_MODIFIED &gt;= to_date(#{params.gmtModifiedStart}, 'yyyy-mm-dd HH24:MI:SS')
        </if>

        <!-- 最后修改日期结束时间查询 -->
        <if test="params.gmtModifiedEnd != null and params.gmtModifiedEnd != ''">
            AND t.GMT_MODIFIED &lt;= to_date(#{params.gmtModifiedEnd}, 'yyyy-mm-dd HH24:MI:SS')
        </if>


        <!-- 最后修改人查询 -->
        <if test="params.modifier != null and params.modifier != ''">
            AND t.MODIFIER = #{params.modifier}
        </if>

        <!-- 最后修改人模糊查询 -->
        <if test="params.modifierLike != null and params.modifierLike != ''">
            AND t.MODIFIER LIKE '%' || #{params.modifierLike} || '%'
        </if>


        <!-- LAST_UPDATED_NAME查询 -->
        <if test="params.modifierName != null and params.modifierName != ''">
            AND t.MODIFIER_NAME = #{params.modifierName}
        </if>


        <!-- LAST_UPDATED_NAME模糊查询 -->
        <if test="params.modifierNameLike != null and params.modifierNameLike != ''">
            AND t.MODIFIER_NAME LIKE '%' || #{params.modifierNameLike} || '%'
        </if>

        <if test="params.errorMsg != null and params.errorMsg != ''">
            AND t.ERROR_MSG = #{params.errorMsg}
        </if>


        <if test="params.errorMsgLike != null and params.errorMsgLike != ''">
            AND t.ERROR_MSG LIKE '%' || #{params.errorMsgLike} || '%'
        </if>

        <!-- 总重量查询 -->
        <if test="params.totalWeight != null and params.totalWeight != ''">
            AND t.TOTAL_WEIGHT = #{params.totalWeight}
        </if>

        <!-- 总件数查询 -->
        <if test="params.totalPcs != null and params.totalPcs != ''">
            AND t.TOTAL_PCS = #{params.totalPcs}
        </if>

        <!-- 总数量查询 -->
        <if test="params.totalQty != null and params.totalQty != ''">
            AND t.TOTAL_QTY = #{params.totalQty}
        </if>

        <!-- 总体积查询 -->
        <if test="params.totalVolume != null and params.totalVolume != ''">
            AND t.TOTAL_VOLUME = #{params.totalVolume}
        </if>

        <!-- 销售部门查询 -->
        <if test="params.saleBranchName != null and params.saleBranchName != ''">
            AND t.SALE_BRANCH_NAME = #{params.saleBranchName}
        </if>


        <!-- 销售部门模糊查询 -->
        <if test="params.saleBranchNameLike != null and params.saleBranchNameLike != ''">
            AND t.SALE_BRANCH_NAME LIKE '%' || #{params.saleBranchNameLike} || '%'
        </if>


        <!-- 订单合并后的新id查询 -->
        <if test="params.mergedBillId != null and params.mergedBillId != ''">
            AND t.BILL_GROUP_ID = #{params.mergedBillId}
        </if>

        <!-- 订单合并后的新id模糊查询 -->
        <if test="params.mergedBillIdLike != null and params.mergedBillIdLike != ''">
            AND t.BILL_GROUP_ID LIKE '%' || #{params.mergedBillIdLike} || '%'
        </if>

    </sql>


    <update id="updateBillInfo" parameterType="com.prolog.cs.biz.outbound.model.dto.BillDTO">
        update wms_wave_bill
        <set>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="shipperName != null">
                shipper_name = #{shipperName},
            </if>

            <if test="contactName != null">
                contact_name = #{contactName},
            </if>
            <if test="contactTel != null">
                contact_tel = #{contactName},
            </if>
            <if test="receiveAddress != null">
                receive_address = #{receiveAddress},
            </if>

            <if test="receiveWay != null">
                receive_way = #{receiveWay},
            </if>

            <if test="attainDate != null">
                attain_date = #{attainDate},
            </if>

            <if test="priority != null">
                priority = #{priority},
            </if>
        </set>
        where id = #{id}
    </update>

    <update id="updateBillPriority">
        update WMS_WAVE_BILL set PRIORITY = #{priority} where id in(
            SELECT id FROM(
            SELECT DISTINCT b.id from WMS_MOVE_TASK_REF a,WMS_WAVE_BILL b where a.MOVE_BILL_ID = b.id and a.SRC_CODE= #{billGroupId}
        <if test="warehouseId != null and warehouseId != ''">
            and b.WAREHOUSE_ID= #{warehouseId}
        </if>
        )a
        )
    </update>

    <delete id="deleteByWaveId">
        DELETE FROM wms_wave_bill WHERE WAVE_ID = #{waveId}
    </delete>

    <select id="queryBills" resultType="com.prolog.cs.biz.outbound.model.entity.WaveBillDO">
        select * from wms_wave_bill
        where 1=1
        <if test="waveId != null and waveId != ''">
            and wave.id = #{waveId}
        </if>
        <if test="warehouseId != null and warehouseId != ''">
            and wave.warehouse_id = #{warehouseId}
        </if>
        <if test="billCode != null and billCode != ''">
            and bill.code = #{billCode}
        </if>
    </select>

    <select id="getTotalWeight" resultType="java.math.BigDecimal">
        SELECT sum(billd.TOTAL_WEIGHT) FROM WMS_WAVE_BILL_D billd
        LEFT JOIN wms_wave_bill bill ON billd.BILL_ID = bill.id
        where 1=1
        <if test="waveId != null and waveId != ''">
            and bill.wave_id = #{waveId}
        </if>
        <if test="billId != null and billId != ''">
            and bill.id = #{billId}
        </if>
    </select>

    <select id="getTotalVolume" resultType="java.math.BigDecimal">
        SELECT sum(billd.TOTAL_VOLUME) FROM WMS_WAVE_BILL_D billd
        LEFT JOIN wms_wave_bill bill ON billd.BILL_ID = bill.id
        where 1=1
        <if test="waveId != null and waveId != ''">
            and bill.wave_id = #{waveId}
        </if>
        <if test="billId != null and billId != ''">
            and bill.id = #{billId}
        </if>
    </select>

    <select id="getTotalQty" resultType="java.math.BigDecimal">
        SELECT sum(billd.PIECE_QTY) FROM WMS_WAVE_BILL_D billd
        LEFT JOIN wms_wave_bill bill ON billd.BILL_ID = bill.id
        where 1=1
        <if test="waveId != null and waveId != ''">
            and bill.wave_id = #{waveId}
        </if>
        <if test="billId != null and billId != ''">
            and bill.id = #{billId}
        </if>
    </select>


    <select id="getNumOfBill" resultType="java.lang.Long">
        SELECT count(*) FROM WMS_WAVE_BILL bill
        where 1=1
        <if test="waveId != null and waveId != ''">
            and bill.wave_id = #{waveId}
        </if>
    </select>
    <select id="getNumOfItem" resultType="java.lang.Long">
        SELECT count(*) FROM WMS_WAVE_BILL_D billd
        where 1=1
        <if test="billId != null and billId != ''">
            and billd.bill_id = #{billId}
        </if>
    </select>
    <select id="getPendingBill" resultType="com.prolog.cs.biz.outbound.model.vo.check.WavePendingBillVO">
        SELECT
        wave_id as waveId,
        wave.code AS waveCode,
        wave.WAREHOUSE_ID as warehouseId,
        OMSBILL.shippername as shipperName,
        bill.id AS billId,
        bill.CONSIGNOR_ID as consignorId,
        OMSBILL.CONSIGNORNAME as consignorName,
        bill.status as status,
        bill.ERROR_MSG as errorMsg,
        bill.code AS billCode
        FROM wms_wave_bill bill
        LEFT JOIN OMS_DO_BILL_H omsbill ON omsbill.BILLID = bill.SRC_ID
        LEFT JOIN wms_wave wave ON bill.wave_id = wave.id
        where 1=1
        and (not EXISTS( select * from wms_wave_storage s where s.bill_id = bill.id)
        or not EXISTS( select * from wms_pick_task_h th where th.bill_id = bill.id))
        <!--<choose>
            <when test="type != null and type != ''">
                and wave.type != '1'
            </when>
            <otherwise>
                and (wave.type is null or wave.type='1')
            </otherwise>
        </choose>-->
        and bill.status in ('POSITION_ERROR','WORKSTAGE_ERROR','INNER_CHECK_ERROR','OUT_CHECK_ERROR','CANCELED','BATCH_LIMIT')
        <if test="waveId != null and waveId != ''">
            and wave.id = #{waveId}
        </if>
        <if test="warehouseId != null and warehouseId != ''">
            and wave.warehouse_id = #{warehouseId}
        </if>
        <if test="billCode != null and billCode != ''">
            and bill.code  like concat(concat('%',#{billCode}),'%')
        </if>
        <if test="consignorId != null and consignorId != ''">
            and bill.CONSIGNOR_ID = #{consignorId}
        </if>
        and omsbill.status != 5
        order by bill.gmt_create desc
    </select>

    <select id="getTotalInfo" resultType="java.util.Map">
        SELECT
        sum(billd.TOTAL_WEIGHT) TOTAL_WEIGHT,
        sum(billd.TOTAL_VOLUME) TOTAL_VOLUME,
        sum(billd.PIECE_QTY) PIECE_QTY,
        sum(billd.TOTAL_PCS) TOTAL_PCS
        FROM
        WMS_WAVE_BILL_D billd
        LEFT JOIN wms_wave_bill bill ON billd.BILL_ID = bill.id
        where 1=1
        <if test="waveId != null and waveId != ''">
            and bill.wave_id = #{waveId}
        </if>
        <if test="billId != null and billId != ''">
            and bill.id = #{billId}
        </if>
    </select>

    <select id="query" resultType="com.prolog.cs.biz.outbound.model.entity.WaveBillDO">
        SELECT distinct
        t.ID,
        t.CODE,
        t.SRC_ID,
        t.SRC_CODE,
        t.WAREHOUSE_ID,
        t.CONSIGNOR_ID,
        t.TYPE,
        t.PRIORITY,
        t.WAVE_ID,
        t.WAVE_BILL_GROUP_ID,
        t.STATUS,
        t.SHIPPER_TYPE,
        t.SHIPPER_CODE,
        t.SHIPPER_NAME,
        t.SHIPPER_ID,
        t.CONTACT_NAME,
        t.CONTACT_TEL,
        t.RECEIVE_ADDRESS,
        t.RECEIVE_WAY,
        t.ATTAIN_DATE,
        t.SHIP_LINE_ID,
        t.CARRIER_NAME,
        t.ENTERPRISE_ID,
        t.GMT_CREATE,
        t.CREATOR,
        t.CREATOR_NAME,
        t.GMT_MODIFIED,
        t.MODIFIER,
        t.MODIFIER_NAME,
        t.ERROR_MSG,
        t.TOTAL_WEIGHT,
        t.TOTAL_PCS,
        t.TOTAL_QTY,
        t.TOTAL_VOLUME,
        t.SALE_BRANCH_NAME,
        t.BILL_GROUP_ID,
        t.payment_day_type,
        t.is_merged,
        h.is_emergency isEmergency,
        h.createby,
        h.remark,
       -- h.createby createBy,
       -- w.code,
        t.CONSIGNOR_NAME consignorName
        FROM WMS_WAVE_BILL t
        left join OMS_DO_BILL_H h on t.SRC_CODE = h.billno
        left join WMS_BARCODE_H r on t.SRC_CODE = r.BILL_GROUP_ID
        left join wms_wave w on w.id = t.wave_id
        WHERE 1=1 and (r.CHECK_STATUS IS NULL OR r.CHECK_STATUS &lt;&gt; '5')
        <if test="waveId != null and waveId != ''">
            and t.wave_id = #{waveId}
        </if>
        <if test="shipperId != null and shipperId != ''">
            and t.shipper_id = #{shipperId}
        </if>
        <if test="shipperName != null and shipperName != ''">
            and t.shipper_name like concat(concat('%',#{shipperName}),'%')
        </if>
        <if test="saleBranchName != null and saleBranchName != ''">
            and t.sale_branch_name like concat(concat('%',#{saleBranchName}),'%')
        </if>
        <if test="startDate != null">
            and t.gmt_create &gt; #{startDate,jdbcType=DATE}
        </if>
        <if test="endDate!= null">
            and t.gmt_create &lt; #{endDate ,jdbcType=DATE}
        </if>
        <if test=" type!= null and type != ''">
            and t.type = #{type }
        </if>
        <if test=" code!= null and code != ''">
            and t.code like concat(concat('%',#{code}),'%')
        </if>
        <if test=" warehouseId!= null and warehouseId != ''">
            and t.warehouse_id = #{warehouseId }
        </if>
        <if test="status != null and status != ''">
            and t.status = #{status }
        </if>
        <if test="shipLineId != null and shipLineId != ''">
            and t.ship_line_id = #{shipLineId}
        </if>
        <if test="isEmergency != null and isEmergency == '2'.toString()">
            and h.is_emergency = #{isEmergency}
        </if>
        <if test="createby != null and createby != ''">
            and h.createby like concat(concat('%',#{createby}),'%')
        </if>
        <if test="remark != null and remark != ''">
            and h.remark like concat(concat('%',#{remark}),'%')
        </if>


        <if test="customerId != null and customerId != ''">
            and t.shipper_id = #{customerId}
        </if>
        <if test="waveCode != null and waveCode != ''">
            and t.WAVE_ID = #{waveCode}
        </if>
        <if test="billTypeCode != null and billTypeCode != ''">
            and t.TYPE = #{billTypeCode}
        </if>
        <if test="createBy != null and createBy != ''">
            and h.createby = #{createBy}
        </if>

        ORDER BY t.gmt_create DESC
    </select>

    <select id="queryForMerge" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        FROM WMS_WAVE_BILL t
        where 1=1
        and wave_id = #{waveId}
        <if test="billIds != null and billIds.size()>0">
            and t.id in
            <foreach collection="billIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>
    <select id="getBillsForDistribute" resultType="com.prolog.cs.biz.outbound.model.entity.WaveBillDO">
        select
        <include refid="Base_Column_List"/>
        from WMS_WAVE_BILL t
        where 1=1
        and wave_id = #{waveId}
        <if test="billId != null and billId != ''">
            and id = #{billId}
        </if>
        and is_merged !=2
    </select>
    <select id="getLclDetaiHeadForPrint" resultType="com.prolog.cs.biz.outbound.model.print.LclDetailHeadVO">
        SELECT
          distinct
          omsbill.line_name ship_line,
          omsbill.BILLNO erp_bill_no,
          bill.code bill_no,
          omsbill.SALE_BRANCHNAME dept_name,
          omsBill.CREATEBY drawer,
          omsbill.CONSIGNORNAME cargo_owner_name,
          omsbill.SHIPPERNAME carrier,
          bill.CONTACT_NAME customer,
          bill.RECEIVE_WAY pickWay,
          omsbill.SALESPERSON_NAME biz_user,
          omsbill.remark,
          omsbill.PAYMENTDAYTYPECODE payment_day_type
        FROM
          WMS_WAVE_BILL bill
          LEFT JOIN OMS_DO_BILL_H omsbill ON OMSBILL.BILLID = bill.SRC_ID
        WHERE
          bill.BILL_GROUP_ID =  #{billGroupId}

    </select>

    <resultMap id="InnerCheckBillVO" type="com.prolog.cs.biz.outbound.model.vo.check.InnerCheckBillVO">
        <result column="consignor_id" property="consignorId"/>
        <result column="CUST_PERSON" property="clientName"/>
        <result column="consignorname" property="consignorName"/>
        <result column="bill_code" property="billCode"/>
        <result column="type" property="billType"/>
        <result column="BILL_GROUP_ID" property="billGroupId"/>
    </resultMap>

    <select id="getBillDIdList" resultType="java.lang.String">
        SELECT distinct billd.id
        FROM WMS_WAVE_BILL_D billd,WMS_WAVE_BILL bill where bill.id = billd.BILL_ID
        <if test=" warehouseId!= null and warehouseId != ''">
            and bill.warehouse_id =#{warehouseId}
        </if>
        <if test=" waveId!= null and waveId != ''">
            and bill.wave_id =#{waveId} and (bill.status is null or bill.status &lt;&gt; 'POSITION_ERROR')
        </if>
        <if test="srcCodes != null and srcCodes.size()>0">
            and billd.merged_bill_id in
            <foreach collection="srcCodes" item="srcCode" open="(" separator="," close=")">
                #{srcCode}
            </foreach>
            and bill.status &lt;&gt; 'POSITIONED'
        </if>
    </select>

    <select id="getAgvPickCount" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM wms_pick_task_h h
        left join wms_pick_task_d d on d.pick_task_id = h.pick_task_id
        left join wms_wave_storage s on d.WAVE_STORAGE_ID = s.id
        where h.warehouse_id = #{warehouseId} and h.bill_group_id= #{billGroupId}
        and s.pick_model='Device' and s.type='0' and h.status &lt; 2
    </select>

    <!-- 查询订单 是否已组波次-->
    <select id="getWaveByBillid" resultType="java.lang.String">
        SELECT
        DISTINCT t.src_id as billid
        FROM
        wms_wave_bill t
        WHERE 1=1
        <if test="billIds != null and billIds.size()>0">
            and t.src_id in
            <foreach collection="billIds" item="billid" open="(" separator="," close=")">
                #{billid}
            </foreach>
        </if>
    </select>
</mapper>