<?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.report.scm.mapper.InventoryMapper"
>

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberSettleExt" type="com.hzs.common.domain.member.ext.CuMemberSettleExt">

    </resultMap>
    <select id="queryInventorySaleListByParam" resultType="com.hzs.report.scm.vo.InventorySaleVO">
        select bp.product_code,bp.product_name,ih.specs_name,bt.name country_name,
        ih.nonhandnum non_hand_num,nvl(poi.nnum,0) transit_num,nvl(tra.nnum,0) transfer_num,
        sale.quantity sale_num,(sale.quantity/#{monthNum}) averageNum from(
        select pk_product,pk_product_bas,ic.pk_country,specs_name,sum(nonhandnum) nonhandnum from ic_on_hand_num ic
        inner join (select pk_onhandnum, max(r) specs_name
        from (select pk_onhandnum,
        wm_concat(specs_name) over(partition by pk_onhandnum order by specs_name) r
        from ic_on_hand_specs
        where del_flag = 0)
        group by pk_onhandnum) ip
        on ic.pk_id = ip.pk_onhandnum
        where ic.del_flag = 0
        group by pk_product,pk_product_bas,ic.pk_country,specs_name
        ) ih
        inner join bd_product bp
        on ih.pk_product=bp.pk_id
        left join bd_country bt
        on bt.pk_id=ih.pk_country and bt.del_flag=0
        left join(
        select pk_product,specs_name,sum(quantity) quantity from sa_order_items st
        inner join sa_order so
        on so.pk_id=st.pk_order
        where st.del_flag=0 and so.del_flag=0 and so.order_status=1
        and pay_time >= #{startDate,jdbcType=DATE} and pay_time &lt;= #{endDate,jdbcType=DATE}
        group by pk_product,specs_name
        ) sale
        on sale.pk_product=ih.pk_product and sale.specs_name=ih.specs_name
        left join(
        select pt.pk_product_bas,po.pk_country,sp.specs_name,sum(NNUM-NACCUMSTORENUM) nnum from po_order po
        inner join po_order_items pt
        on po.pk_id=pt.pk_order
        inner join (
        select pk_order_items,max(r) specs_name from
        (select pk_order_items,wm_concat(specs_name) over (partition by pk_order_items order by specs_name) r
        from scm_item_specs
        where del_flag = 0 and order_type=1)
        group by pk_order_items) sp
        on sp.pk_order_items=pt.pk_id
        where po.del_flag=0 and pt.del_flag=0
        and po.RETURN_STATUS=1 and pt.pk_product_bas=17 and NNUM>NACCUMSTORENUM
        and po.order_date >= #{startDate,jdbcType=DATE} and po.order_date &lt;= #{endDate,jdbcType=DATE}
        group by pt.pk_product_bas,po.pk_country,sp.specs_name
        ) poi
        on poi.pk_product_bas=ih.pk_product_bas and poi.specs_name=ih.specs_name and poi.pk_country=ih.pk_country
        left join(
        select ib.pk_product_bas,ib.pk_product,sp.specs_name,sum(ib.nnum) nnum from ic_transout_h ih
        inner join ic_transout_b ib
        on ih.pk_id=ib.pk_transfer_out
        inner join (
        select pk_order_items,max(r) specs_name from
        (select pk_order_items,wm_concat(specs_name) over (partition by pk_order_items order by specs_name) r
        from scm_item_specs
        where del_flag = 0 and order_type=9)
        group by pk_order_items) sp
        on sp.pk_order_items=ib.pk_id
        and ih.order_date >=#{startDate,jdbcType=DATE} and ih.order_date &lt;= #{endDate,jdbcType=DATE}
        where ih.del_flag=0 and ib.del_flag=0
        group by ib.pk_product_bas,ib.pk_product,sp.specs_name
        ) tra
        on ih.pk_product=tra.pk_product and ih.specs_name=tra.specs_name
        where bp.del_flag=0
        <if test="pkCountry != null">
            and ih.pk_country=#{pkCountry}
        </if>
        <if test="productCode != null and productCode !='' ">
            AND bp.PRODUCT_CODE like '%' || #{productCode}||'%'
        </if>
        <if test="productName != null and productName !='' ">
            AND bp.PRODUCT_NAME like '%' || #{productName}||'%'
        </if>
        <if test="specsName != null and specsName != ''">
            and ih.specs_name like '%' || #{specsName} || '%'
        </if>
        order by ih.pk_country,bp.product_code,ih.specs_name
    </select>


</mapper>
