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

    <!--
        <resultMap id="LclPrintParameterVO" type="com.prolog.cs.biz.outbound.model.OmsDoBillHDO">
            <result column="billid" property="billid"/>
            <result column="billno" property="billno"/>
            <result column="billtype" property="billtype"/>
            <result column="ref_billtype" property="refBilltype"/>
            <result column="ref_billid" property="refBillid"/>
            <result column="ref_billno" property="refBillno"/>
            <result column="consignorid" property="consignorid"/>
            <result column="consignorname" property="consignorname"/>
            <result column="branchid" property="branchid"/>
            <result column="branchcode" property="branchcode"/>
            <result column="branchname" property="branchname"/>
            <result column="branchareaid" property="branchareaid"/>
            <result column="bill_type" property="billType"/>

        </resultMap>
        -->

    <select id="findOmsBillForWave" resultType="com.prolog.cs.biz.outbound.model.OmsDoBillHDO">
        SELECT
         distinct omsbill.*
        FROM
          OMS_DO_BILL_H omsbill
        LEFT JOIN WMS_WAVE_BILL bill ON bill.src_id = omsbill.billid
        WHERE
          (bill.id IS NULL OR bill.status IN ('CANCELED'))
        <if test="pEnterpriseid != null and pEnterpriseid != ''">
            and P_Enterpriseid = #{pEnterpriseid}
        </if>
        <if test="consignorid != null and consignorid != ''">
            and consignorid = #{consignorid}
        </if>
        <if test="branchid != null and branchid != ''">
            and branchid = #{branchid}
        </if>
    </select>
    <select id="findOmsBill" resultType="com.prolog.cs.biz.outbound.model.OmsDoBillHDO">
        SELECT
        distinct omsbill.*
        FROM
        OMS_DO_BILL_H omsbill
        LEFT JOIN WMS_WAVE_BILL bill ON bill.src_id = omsbill.billid
        WHERE omsbill.IS_CANCELLED IS null
        and (omsbill.CON_CHANGE_STATUS is null or omsbill.CON_CHANGE_STATUS= '0')
        <if test="billQueryDTO.status == null or billQueryDTO.status == '' or billQueryDTO.status == '10'">
            and bill.id IS NULL
        </if>
        <if test="pEnterpriseid != null and pEnterpriseid != ''">
            and omsbill.P_Enterpriseid = #{pEnterpriseid}
        </if>
        <if test="billQueryDTO.consignorid != null and billQueryDTO.consignorid != ''">
            and omsbill.consignorid = #{billQueryDTO.consignorid}
        </if>
        <if test="billQueryDTO.branchid != null and billQueryDTO.branchid != ''">
            and omsbill.branchid = #{billQueryDTO.branchid}
        </if>

        <if test="billQueryDTO.billno != null and billQueryDTO.billno != ''">
            and omsbill.billno  like concat(concat('%',#{billQueryDTO.billno}),'%')
        </if>
        <if test="billQueryDTO.shipLineId != null and billQueryDTO.shipLineId != ''">
            and omsbill.SHIP_LINE_ID   = #{billQueryDTO.shipLineId}
        </if>
        <if test="billQueryDTO.shippername != null and billQueryDTO.shippername != ''">
            and omsbill.shippername  like concat(concat('%',#{billQueryDTO.shippername}),'%')
        </if>
        <if test="billQueryDTO.carrierName != null and billQueryDTO.carrierName != ''">
            and omsbill.carrier_name   like concat(concat('%',#{billQueryDTO.carrierName}),'%')
        </if>
        <if test="billQueryDTO.billtypes != null and billQueryDTO.billtypes.size()>0">
            and (omsbill.billtype in
            <foreach collection="billQueryDTO.billtypes" item="billtype" open="(" separator="," close=")">
                #{billtype}
            </foreach>
            or (omsbill.billtype ='4' and omsbill.ISUNCHECPRODUCTS = 'Y'))
        </if>
        <if test="billQueryDTO.excludeBilltypes != null and billQueryDTO.excludeBilltypes.size()>0">
            and (omsbill.billtype not in
            <foreach collection="billQueryDTO.excludeBilltypes" item="billtype" open="(" separator="," close=")">
                #{billtype}
            </foreach>
            or (omsbill.billtype ='4' and (omsbill.ISUNCHECPRODUCTS is null or omsbill.ISUNCHECPRODUCTS = 'N')))
        </if>
        <if test="billQueryDTO.salesPersonName != null and billQueryDTO.salesPersonName != ''">
            and omsbill.SALESPERSON_NAME like concat(concat('%',#{billQueryDTO.salesPersonName}),'%')
        </if>
        <if test="billQueryDTO.saleBranchname != null and billQueryDTO.saleBranchname != ''">
            and omsbill.sale_branchname like concat(concat('%',#{billQueryDTO.saleBranchname}),'%')
        </if>
        <if test="billQueryDTO.isEmergency != null and billQueryDTO.isEmergency != ''">
            and omsbill.is_emergency  =  #{billQueryDTO.isEmergency}
        </if>
        <if test="billQueryDTO.arrivalMode != null and billQueryDTO.arrivalMode != ''">
            and omsbill.arrival_mode like concat(concat('%',#{billQueryDTO.arrivalMode}),'%')
        </if>
        <if test="billQueryDTO.billDateStart != null">
            and bill_date &gt; #{billQueryDTO.billDateStart,jdbcType=DATE}
        </if>

        <if test="billQueryDTO.billDateEnd != null">
            and bill_date &lt; #{billQueryDTO.billDateEnd,jdbcType=DATE}
        </if>
        <if test="billQueryDTO.status != null and billQueryDTO.status != ''">
            and omsbill.status = #{billQueryDTO.status}
        </if>
    </select>

    <select id="getSpecialOmsBill" resultType="com.prolog.cs.biz.outbound.model.OmsDoBillHDO">
        SELECT distinct omsbill.* FROM OMS_DO_BILL_H omsbill
        WHERE omsbill.IS_CANCELLED IS null
        and (omsbill.billtype in ('8','9') or (omsbill.billtype ='4' and omsbill.ISUNCHECPRODUCTS ='Y'))
        and (omsbill.CON_CHANGE_STATUS is null or omsbill.CON_CHANGE_STATUS= '0')
--         and omsbill.status &lt;&gt; '10'
        <if test="billQueryDTO.status != null and billQueryDTO.status != ''">
            and omsbill.status = #{billQueryDTO.status}
        </if>
        <if test="pEnterpriseid != null and pEnterpriseid != ''">
            and omsbill.P_Enterpriseid = #{pEnterpriseid}
        </if>
        <if test="billQueryDTO.consignorid != null and billQueryDTO.consignorid != ''">
            and omsbill.consignorid = #{billQueryDTO.consignorid}
        </if>
        <if test="billQueryDTO.branchid != null and billQueryDTO.branchid != ''">
            and omsbill.branchid = #{billQueryDTO.branchid}
        </if>
        <if test="billQueryDTO.billno != null and billQueryDTO.billno != ''">
            and omsbill.billno  like concat(concat('%',#{billQueryDTO.billno}),'%')
        </if>
        <if test="billQueryDTO.isEmergency != null and billQueryDTO.isEmergency != ''">
            and omsbill.is_emergency  =  #{billQueryDTO.isEmergency}
        </if>
        <if test="billQueryDTO.billDateStart != null">
            and bill_date &gt; #{billQueryDTO.billDateStart,jdbcType=DATE}
        </if>
        <if test="billQueryDTO.billDateEnd != null">
            and bill_date &lt; #{billQueryDTO.billDateEnd,jdbcType=DATE}
        </if>
    </select>

    <select id="getOmsBillDList" resultType="com.prolog.cs.biz.outbound.model.dto.BillDDTO">
        select ITEMID itemId,LOT_NO lotNo,sum(ITEM_QTY) qty from OMS_DO_BILL_D where BILLID = #{billId} and BRANCHID = #{branchId}
        group by ITEMID,LOT_NO
    </select>

</mapper>