<?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.hzs.sale.deliver.mapper.SaDeliverItemsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.sale.deliver.SaDeliverItems">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_DELIVER" property="pkDeliver"/>
        <result column="PK_ORDER" property="pkOrder"/>
        <result column="PK_ORDER_ITEMS" property="pkOrderItems"/>
        <result column="PK_PRODUCT" property="pkProduct"/>
        <result column="PK_STOREHOUSE" property="pkStorehouse"/>
        <result column="QUANTITY" property="quantity"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_WARES_SPECS_SKU" property="pkWaresSpecsSku"/>
        <result column="PK_WARES_DETAIL" property="pkWaresDetail"/>
        <result column="EXCHANGE_GOODS" property="exchangeGoods"/>
        <result column="SPECS_NAME" property="specsName"/>
        <result column="SPECS_NAME_ID" property="specsNameId"/>
    </resultMap>

    <!-- 发货条码数据映射 -->
    <resultMap id="deliverBarCodeMap" type="com.hzs.sale.deliver.vo.DeliverBarCodeVO">
        <result column="pk_id" property="pkId"/>
        <result column="pk_deliver" property="pkDeliver"/>
        <result column="pk_order" property="pkOrder"/>
        <result column="order_code" property="orderCode"/>
        <result column="pk_product" property="pkProduct"/>
        <result column="product_name" property="productName"/>
        <result column="specs_name" property="specsName"/>
        <result column="quantity" property="quantity"/>
        <collection property="detailList" ofType="com.hzs.sale.deliver.vo.DeliverBarCodeDetailVO">
            <result column="deliver_bar_code_id" property="deliverBarCodeId"/>
            <result column="bar_code" property="barCode"/>
        </collection>
    </resultMap>

    <!-- 已合并发货单列表 -->
    <select id="queryDeliverHandledList" resultType="com.hzs.sale.deliver.vo.DeliverHandledVO">
        <if test="param.pkDeliverList != null and param.pkDeliverList.size > 0">
            <foreach collection="param.pkDeliverList" item="item" index="index" separator=" UNION ALL ">
                <if test="index % 500 == 0">
                    select sdi.pk_id,
                    sdi.pk_deliver,
                    sdi.pk_order,
                    sd.deliver_code,
                    bp.cover,
                    bp.product_code,
                    bp.product_name,
                    bp.shipping_channel,
                    bp.wms_code,
                    sdi.SPECS_NAME,
                    sdi.quantity,
                    soi.order_status,
                    so.order_code,
                    so.order_type,
                    cm.member_code,
                    cm.member_name,
                    cm.phone,
                    sd.rec_name,
                    sd.rec_phone,
                    sd.rec_province,
                    sd.rec_city,
                    sd.rec_county,
                    sd.rec_address,
                    so.delivery_way,
                    bwe.operate_scope,
                    so.tran_type,
                    bwe.pre_sale_status,
                    sd.print_status,
                    sd.logistics_company,
                    sd.logistics_code,
                    so.remark,
                    so.creation_time,
                    so.pay_time,
                    bu.name unit_name,
                    bs.name storehouseName,
                    bpe.is_bar_code,
                    so.SYSTEM_TYPE,
                    sd.day_batch,
                    sd.batch_number
                    from SA_DELIVER sd
                    left join SA_DELIVER_ITEMS sdi
                    on sdi.pk_deliver = sd.pk_id
                    inner join SA_ORDER so
                    on so.pk_id = sdi.pk_order
                    and so.del_flag = 0
                    inner join SA_ORDER_ITEMS soi
                    on soi.pk_order = sdi.pk_order
                    and soi.pk_id = sdi.pk_order_items
                    inner join BD_PRODUCT bp
                    on bp.pk_id = soi.pk_product
                    left join bd_product_extend bpe
                    on bpe.pk_product = soi.pk_product
                    left join BD_WARES_EXTEND bwe
                    on bwe.pk_wares = soi.pk_wares
                    left join bd_unit bu
                    on bu.pk_id = bp.unit
                    inner join cu_member cm
                    on cm.pk_id = so.pk_member
                    left join bd_storehouse bs
                    on bs.pk_id = so.pk_storehouse
                    where sd.del_flag = 0
                    and sd.pk_country = #{pkCountry}
                    and sdi.del_flag = 0
                    <if test="param.systemType != null">
                        and so.SYSTEM_TYPE = #{param.systemType}
                    </if>
                    and sd.pk_id in
                    <foreach collection="param.pkDeliverList" item="subItem" index="subItem_index" open="(" close=")" separator=",">
                        <if test="subItem_index gte index and subItem_index lt index + 500">
                            #{subItem}
                        </if>
                    </foreach>
                    <if test="param.dayBatch != null">
                        and sd.DAY_BATCH = #{param.dayBatch}
                    </if>
                    <if test="param.pkDeliver != null">
                        and sd.pk_id = #{param.pkDeliver}
                    </if>
                    <if test="param.pkStorehouse != null ">
                        and bs.pk_id = #{param.pkStorehouse}
                    </if>
                    <if test="param.pkDeliverItems != null">
                        and sdi.pk_id = #{param.pkDeliverItems}
                    </if>
                    <if test="param.deliverCode != null and param.deliverCode != ''">
                        and sd.deliver_code like '%' || #{param.deliverCode} || '%'
                    </if>
                    <if test="param.orderCode != null and param.orderCode != ''">
                        and so.order_code like '%' || #{param.orderCode} || '%'
                    </if>
                    <if test="param.memberCode != null and param.memberCode != ''">
                        and cm.member_code like #{param.memberCode} || '%'
                    </if>
                    <if test="param.memberName != null and param.memberName != ''">
                        and cm.member_name like #{param.memberName} || '%'
                    </if>
                    <if test="param.phone != null and param.phone != ''">
                        and cm.phone like #{param.phone} || '%'
                    </if>
                    <if test="param.orderType != null ">
                        and so.order_type = #{param.orderType}
                    </if>
                    <if test="param.orderStatus != null ">
                        and soi.order_status = #{param.orderStatus}
                    </if>
                    <if test="param.preSaleStatus != null ">
                        and bwe.pre_sale_status = #{param.preSaleStatus}
                    </if>
                    <if test="param.deliveryWay != null ">
                        and so.delivery_way = #{param.deliveryWay}
                    </if>
                    <if test="param.transportType != null">
                        and so.tran_type = #{param.transportType}
                    </if>
                    <if test="param.operateScope != null ">
                        and bwe.operate_scope = #{param.operateScope}
                    </if>
                    <if test="param.printStatus != null ">
                        and sd.print_status = #{param.printStatus}
                    </if>
                    <if test="param.recName != null and param.recName != '' ">
                        and so.rec_name like '%' || #{param.recName} || '%'
                    </if>
                    <if test="param.recPhone != null and param.recPhone != '' ">
                        and so.rec_phone like '%' || #{param.recPhone} || '%'
                    </if>
                    <if test="param.recProvince != null ">
                        and so.rec_province = #{param.recProvince}
                    </if>
                    <if test="param.recCity != null ">
                        and so.rec_city = #{param.recCity}
                    </if>
                    <if test="param.recCounty != null ">
                        and so.rec_county = #{param.recCounty}
                    </if>
                    <if test="param.recAddress != null and param.recAddress != '' ">
                        and so.rec_address like #{param.recAddress} || '%'
                    </if>
                    <if test="param.creationTime != null">
                        and soi.creation_time >= #{param.creationTime[0]}
                        and #{param.creationTime[1]} >= soi.creation_time
                    </if>
                    <if test="param.payTime != null">
                        and so.pay_time >= #{param.payTime[0]}
                        and #{param.payTime[1]} >= so.pay_time
                    </if>
                    <if test="param.shippingChannel != null">
                        and bp.shipping_channel = #{param.shippingChannel}
                    </if>
                    <if test="param.productName != null and param.productName != ''">
                        and bp.product_name like '%' || #{param.productName} || '%'
                    </if>
                    <if test="param.isBarCode != null">
                        and bpe.is_bar_code = #{param.isBarCode}
                    </if>
                    <if test="param.secondDeliver != null">
                        <choose>
                            <when test="param.secondDeliver == 0">
                                and so.PK_ORIGINAL_ORDER is not null
                            </when>
                            <otherwise>
                                and so.PK_ORIGINAL_ORDER is null
                            </otherwise>
                        </choose>
                    </if>
                    <!-- 地区权限处理 -->
                    <if test="param.areaScopeList != null and param.areaScopeList.size > 0">
                        and so.rec_province in
                        <foreach collection="param.areaScopeList" item="item" open="(" close=")" separator=",">
                            #{item}
                        </foreach>
                    </if>
                    <!-- 体系权限处理 -->
                    <if test="param.vertexIdList != null and param.vertexIdList.size > 0">
                        and cm.pk_vertex in
                        <foreach collection="param.vertexIdList" item="item" open="(" close=")" separator=",">
                            #{item}
                        </foreach>
                    </if>
                </if>
            </foreach>
            order by sdi.creation_time desc, deliver_code desc, order_code desc,
            bp.wms_code, sdi.specs_name
        </if>

        <if test="param.pkDeliverList == null or param.pkDeliverList.size == 0">
            select sdi.pk_id,
            sdi.pk_deliver,
            sdi.pk_order,
            sd.deliver_code,
            bp.cover,
            bp.product_code,
            bp.product_name,
            bp.shipping_channel,
            bp.wms_code,
            sdi.SPECS_NAME,
            sdi.quantity,
            soi.order_status,
            so.order_code,
            so.order_type,
            cm.member_code,
            cm.member_name,
            cm.phone,
            sd.rec_name,
            sd.rec_phone,
            sd.rec_province,
            sd.rec_city,
            sd.rec_county,
            sd.rec_address,
            so.delivery_way,
            bwe.operate_scope,
            so.tran_type,
            bwe.pre_sale_status,
            sd.print_status,
            sd.logistics_company,
            sd.logistics_code,
            so.remark,
            so.creation_time,
            so.pay_time,
            bu.name unit_name,
            bs.name storehouseName,
            bpe.is_bar_code,
            so.SYSTEM_TYPE,
            sd.day_batch,
            sd.batch_number
            from SA_DELIVER sd
            left join SA_DELIVER_ITEMS sdi
            on sdi.pk_deliver = sd.pk_id
            inner join SA_ORDER so
            on so.pk_id = sdi.pk_order
            and so.del_flag = 0
            inner join SA_ORDER_ITEMS soi
            on soi.pk_order = sdi.pk_order
            and soi.pk_id = sdi.pk_order_items
            inner join BD_PRODUCT bp
            on bp.pk_id = soi.pk_product
            left join bd_product_extend bpe
            on bpe.pk_product = soi.pk_product
            left join BD_WARES_EXTEND bwe
            on bwe.pk_wares = soi.pk_wares
            left join bd_unit bu
            on bu.pk_id = bp.unit
            inner join cu_member cm
            on cm.pk_id = so.pk_member
            left join bd_storehouse bs
            on bs.pk_id = so.pk_storehouse
            where sd.del_flag = 0
            and sd.pk_country = #{pkCountry}
            and sdi.del_flag = 0
            <if test="param.systemType != null">
                and so.SYSTEM_TYPE = #{param.systemType}
            </if>
            <if test="param.pkDeliverList != null and param.pkDeliverList.size > 0">
                and sd.pk_id in
                <foreach collection="param.pkDeliverList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="param.dayBatch != null">
                and sd.DAY_BATCH = #{param.dayBatch}
            </if>
            <if test="param.pkDeliver != null">
                and sd.pk_id = #{param.pkDeliver}
            </if>
            <if test="param.pkStorehouse != null ">
                and bs.pk_id = #{param.pkStorehouse}
            </if>
            <if test="param.pkDeliverItems != null">
                and sdi.pk_id = #{param.pkDeliverItems}
            </if>
            <if test="param.deliverCode != null and param.deliverCode != ''">
                and sd.deliver_code like '%' || #{param.deliverCode} || '%'
            </if>
            <if test="param.orderCode != null and param.orderCode != ''">
                and so.order_code like '%' || #{param.orderCode} || '%'
            </if>
            <if test="param.memberCode != null and param.memberCode != ''">
                and cm.member_code like #{param.memberCode} || '%'
            </if>
            <if test="param.memberName != null and param.memberName != ''">
                and cm.member_name like #{param.memberName} || '%'
            </if>
            <if test="param.phone != null and param.phone != ''">
                and cm.phone like #{param.phone} || '%'
            </if>
            <if test="param.orderType != null ">
                and so.order_type = #{param.orderType}
            </if>
            <if test="param.orderStatus != null ">
                and soi.order_status = #{param.orderStatus}
            </if>
            <if test="param.preSaleStatus != null ">
                and bwe.pre_sale_status = #{param.preSaleStatus}
            </if>
            <if test="param.deliveryWay != null ">
                and so.delivery_way = #{param.deliveryWay}
            </if>
            <if test="param.transportType != null">
                and so.tran_type = #{param.transportType}
            </if>
            <if test="param.operateScope != null ">
                and bwe.operate_scope = #{param.operateScope}
            </if>
            <if test="param.printStatus != null ">
                and sd.print_status = #{param.printStatus}
            </if>
            <if test="param.recName != null and param.recName != '' ">
                and so.rec_name like '%' || #{param.recName} || '%'
            </if>
            <if test="param.recPhone != null and param.recPhone != '' ">
                and so.rec_phone like '%' || #{param.recPhone} || '%'
            </if>
            <if test="param.recProvince != null ">
                and so.rec_province = #{param.recProvince}
            </if>
            <if test="param.recCity != null ">
                and so.rec_city = #{param.recCity}
            </if>
            <if test="param.recCounty != null ">
                and so.rec_county = #{param.recCounty}
            </if>
            <if test="param.recAddress != null and param.recAddress != '' ">
                and so.rec_address like #{param.recAddress} || '%'
            </if>
            <if test="param.creationTime != null">
                and soi.creation_time >= #{param.creationTime[0]}
                and #{param.creationTime[1]} >= soi.creation_time
            </if>
            <if test="param.payTime != null">
                and so.pay_time >= #{param.payTime[0]}
                and #{param.payTime[1]} >= so.pay_time
            </if>
            <if test="param.shippingChannel != null">
                and bp.shipping_channel = #{param.shippingChannel}
            </if>
            <if test="param.productName != null and param.productName != ''">
                and bp.product_name like '%' || #{param.productName} || '%'
            </if>
            <if test="param.isBarCode != null">
                and bpe.is_bar_code = #{param.isBarCode}
            </if>
            <if test="param.secondDeliver != null">
                <choose>
                    <when test="param.secondDeliver == 0">
                        and so.PK_ORIGINAL_ORDER is not null
                    </when>
                    <otherwise>
                        and so.PK_ORIGINAL_ORDER is null
                    </otherwise>
                </choose>
            </if>
            <!-- 地区权限处理 -->
            <if test="param.areaScopeList != null and param.areaScopeList.size > 0">
                and so.rec_province in
                <foreach collection="param.areaScopeList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <!-- 体系权限处理 -->
            <if test="param.vertexIdList != null and param.vertexIdList.size > 0">
                and cm.pk_vertex in
                <foreach collection="param.vertexIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            order by sdi.creation_time desc, sd.deliver_code desc, so.order_code desc,
            bp.wms_code, sdi.specs_name
        </if>
    </select>

    <!-- 批量插入 -->
    <insert id="insertBatch">
        MERGE INTO SA_DELIVER_ITEMS
        USING (
        <foreach collection="list" item="item" separator=" union ">
            select
            #{item.pkDeliver} pkDeliver,
            #{item.pkOrder} pkOrder,
            #{item.pkOrderItems} pkOrderItems,
            #{item.pkProduct} pkProduct,
            #{item.quantity} quantity,
            #{item.pkWaresSpecsSku} pkWaresSpecsSku,
            #{item.pkWaresDetail} pkWaresDetail,
            #{item.pkCountry} pkCountry,
            #{item.pkCreator} pkCreator,
            #{item.creationTime} creationTime,
            #{item.pkModified} pkModified,
            #{item.modifiedTime} modifiedTime,
            #{item.specsName, jdbcType=VARCHAR} specsName,
            #{item.specsNameId, jdbcType=VARCHAR} specsNameId
            from dual
        </foreach>
        ) tmp
        ON (0 = 1)
        WHEN NOT MATCHED THEN
        insert (
        PK_ID,
        PK_DELIVER,
        PK_ORDER,
        PK_ORDER_ITEMS,
        PK_PRODUCT,
        QUANTITY,
        PK_WARES_SPECS_SKU,
        PK_WARES_DETAIL,
        PK_COUNTRY,
        PK_CREATOR,
        CREATION_TIME,
        PK_MODIFIED,
        MODIFIED_TIME,
        SPECS_NAME,
        SPECS_NAME_ID)
        values (
        SA_DELIVER_ITEMS_SEQ.nextval,
        tmp.pkDeliver,
        tmp.pkOrder,
        tmp.pkOrderItems,
        tmp.pkProduct,
        tmp.quantity,
        tmp.pkWaresSpecsSku,
        tmp.pkWaresDetail,
        tmp.pkCountry,
        tmp.pkCreator,
        tmp.creationTime,
        tmp.pkModified,
        tmp.modifiedTime,
        tmp.specsName,
        tmp.specsNameId)
    </insert>

    <!-- 查询发货条码数据 -->
    <select id="deliverCodeData" resultMap="deliverBarCodeMap">
        select sdi.pk_id,
        sdi.pk_deliver,
        sdi.pk_order,
        so.order_code,
        sdi.pk_product,
        bp.product_name,
        sdi.specs_name,
        sdi.quantity,
        sdbc.pk_id deliver_bar_code_id,
        sdbc.bar_code
        from SA_DELIVER_ITEMS sdi
        inner join SA_DELIVER sd
        on sdi.pk_deliver = sd.pk_id
        inner join SA_ORDER_ITEMS soi
        on soi.pk_id = sdi.pk_order_items
        inner join SA_ORDER so
        on so.pk_id = sdi.pk_order
        inner join BD_PRODUCT bp
        on bp.pk_id = sdi.pk_product
        left join SA_DELIVER_BAR_CODE sdbc
        on sdbc.pk_deliver_items = sdi.pk_id
        and sdbc.pk_product = sdi.pk_product
        and sdbc.del_flag = 0
        where sdi.pk_country = #{pkCountry}
        and sdi.del_flag = 0
        and sdi.pk_deliver in
        <foreach collection="idList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="orderIdList != null and orderIdList.size > 0">
            and sdi.pk_order in
            <foreach collection="orderIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="bind != null and bind == 0">
            and sdbc.pk_id is not null
        </if>
        order by sdi.creation_time desc, sd.deliver_code desc, so.order_code desc
    </select>

    <!-- 根据订单ID或者订单明细ID列表，查询指定状态订单明细数据 -->
    <select id="queryDeliverItemsByOrder" resultMap="BaseResultMap">
        select sdi.*
        from SA_DELIVER_ITEMS sdi
        where (sdi.pk_order, sdi.pk_order_items) in
        (select soi.pk_order, soi.pk_id
        from sa_order_items soi
        where soi.del_flag = 0
        and soi.pk_country = #{pkCountry}
        and soi.pk_order = #{pkOrder}
        <if test="pkOrderItemList != null and pkOrderItemList.size > 0">
            and soi.pk_id not in
            <foreach collection="pkOrderItemList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="orderStatusList != null and orderStatusList.size > 0">
            and soi.order_status in
            <foreach collection="orderStatusList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        )
    </select>

</mapper>
