<?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.scm.ic.mapper.IcAvailableNumMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="IcAvailableNumExt" type="com.hzs.common.domain.scm.ic.ext.IcAvailableNumExt">
        <id column="PK_ID" property="pkId" />
        <result column="CREATION_TIME" property="creationTime" />
        <result column="MODIFIED_TIME" property="modifiedTime" />
        <result column="DEL_FLAG" property="delFlag" />
        <result column="PK_CREATOR" property="pkCreator" />
        <result column="PK_MODIFIED" property="pkModified" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="PK_CORP" property="pkCorp" />
        <result column="PK_CUBASDOC" property="pkCubasdoc" />
        <result column="PK_PRODUCT_BAS" property="pkProductBas" />
        <result column="PK_PRODUCT" property="pkProduct" />
        <result column="PK_UNIT" property="pkUnit" />
        <result column="PK_STOREHOUSE" property="pkStorehouse" />
        <result column="AVAILABLE_NUM" property="availableNum" />
        <result column="PRODUCT_CODE" property="productCode" />
        <result column="PRODUCT_NAME" property="productName" />
        <result column="H_SPECS_NAME" property="specsName" />
        <result column="STOREHOUSE_NAME" property="storehouseName" />
        <result column="UNIT_NAME" property="unitName" />
        <result column="CORP_NAME" property="corpName" />
        <result column="CUBASDOC_NAME" property="cubasdocName" />
        <result column="NONHANDNUM" property="nonhandnum" />
        <collection property="scmItemSpecsList" ofType="com.hzs.common.domain.scm.pub.ScmItemSpecs">
            <result column="PK_SPECS_TYPE" property="pkSpecsType" />
            <result column="PK_SPECS" property="pkSpecs" />
            <result column="SPECS_NAME" property="specsName" />
        </collection>
    </resultMap>
    <update id="mergeIcAvailableNumByIcOnHandNum">
        merge into ic_available_num a
        using(
        <foreach collection="availableNumList" item="item" separator=" union ">
            select #{item.pkId} pk_id,#{item.availableNum} available_num,#{item.pkModified} pk_modified,
                   #{item.modifiedTime} modified_time from dual
        </foreach>
            ) b
        on (a.pk_id=b.pk_id)
        when matched then
            update set a.available_num=a.available_num-b.available_num,a.pk_modified=b.pk_modified,
                a.modified_time=b.modified_time
    </update>
    <update id="updateAvailableNumByNum">
        update ic_available_num set available_num = available_num + #{availableNum},
        <if test="pkCubasdoc != null">
            pk_cubasdoc=#{pkCubasdoc,jdbcType=INTEGER},
        </if>
         pk_modified=#{pkModified},modified_time=#{modifiedTime}
        where del_flag=0 and pk_id=#{pkId}
    </update>

    <select id="queryIcAvailableNumByCondition" resultMap="IcAvailableNumExt">
        select ih.*,ip.h_specs_name from ic_available_num ih
        inner join (
            select pk_available_num,max(r) h_specs_name from
                (select pk_available_num,wm_concat(pk_specs)
                    over (partition by pk_available_num order by pk_specs) r
                 from ic_available_specs
                 where del_flag = 0)
            group by pk_available_num) ip
        on ih.pk_id= ip.pk_available_num
        where ih.del_flag=0
        and ih.pk_country = #{icOnHandNum.pkCountry}
        and ih.pk_product=#{icOnHandNum.pkProduct} and ih.pk_storehouse=#{icOnHandNum.pkStorehouse}
    </select>
    <select id="queryAvailableNumByProduct" resultMap="IcAvailableNumExt">
        select pk_id,sum(available_num) available_num,nvl(sum(availableNum),1) pk_unit from(
        <foreach collection="availableNumList" item="item" separator=" union ">
        select pk_id,#{item.availableNum} available_num,(case when #{item.preSaleStatus}=1 then 0
            when an.available_num>=#{item.availableNum} then 0 else 1 end) availableNum from IC_AVAILABLE_NUM an
            inner join (
            select pk_available_num,max(r) pk_specs from
            (select pk_available_num,wm_concat(pk_specs)
            over (partition by pk_available_num order by pk_specs) r
            from ic_available_specs
            where del_flag = 0)
            where  r=replace('<foreach collection="item.scmItemSpecsList" item="it"  open="" separator="," close="">${it.pkSpecs}</foreach>',' ','')
            group by pk_available_num
            ) sp
          on an.pk_id = sp.pk_available_num
        where an.del_flag=0
          and an.pk_corp = #{item.pkCorp} and an.pk_storehouse= #{item.pkStorehouse}
          and an.pk_product = #{item.pkProduct}
        </foreach>
        )
        group by pk_id
    </select>
    <select id="queryAvailableNumByCondition" resultMap="IcAvailableNumExt">
        select im.*,b.h_specs_name,bp.product_code,bp.product_name,sc.corp_name,bh.name storehouse_name,
               bn.name unit_name,bc.cust_name cubasdoc_name from ic_available_num im
            inner join (
        select pk_available_num,max(r) h_specs_name from
        (select pk_available_num,wm_concat(specs_name)over (partition by pk_available_num order by pk_specs) r
        from ic_available_specs
        where del_flag = 0 )
        group by pk_available_num) b
         on im.pk_id=b.pk_available_num
          inner join bd_product bp
                     on im.pk_product=bp.pk_id
            left join bd_cubasdoc bc
            on im.pk_cubasdoc=bc.pk_id and bc.del_flag=0
          left join sys_corp sc
                    on im.pk_corp=sc.pk_id and sc.del_flag=0
          left join bd_unit bn
                    on im.pk_unit = bn.pk_id and bn.del_flag=0
          left join bd_storehouse bh
                    on im.pk_storehouse=bh.pk_id and bh.del_flag=0
        where im.del_flag=0 and bp.del_flag=0
        <if test="pkCountry != null">
            and im.pk_country=#{pkCountry}
        </if>
        <if test="pkCorp != null">
            and im.pk_corp=#{pkCorp}
        </if>
        <if test="pkCubasdoc != null">
            and im.pk_cubasdoc = #{pkCubasdoc}
        </if>
        <if test="pkProduct != null">
            and im.pk_product = #{pkProduct}
        </if>
        <if test="productCode != null and productCode != ''">
            and bp.product_code = #{productCode}
        </if>
        <if test="productName != null and productName != ''">
            and bp.product_name like #{productName} || '%'
        </if>
    </select>
    <select id="queryAvailableNumByAvaliable" resultType="com.hzs.common.domain.scm.ic.ext.IcAvailableNumExt">
        select an.* from ic_available_num an
        inner join (
        select pk_available_num,max(r) pk_specs from
        (select pk_available_num,wm_concat(pk_specs)
        over (partition by pk_available_num order by pk_specs) r
        from ic_available_specs
        where del_flag = 0)
        where  r=replace('<foreach collection="availableNumExt.scmItemSpecsList" item="it"  open="" separator="," close="">${it.pkSpecs}</foreach>',' ','')
        group by pk_available_num
        ) sp
        on an.pk_id = sp.pk_available_num
        where an.del_flag=0
        and an.pk_corp = #{availableNumExt.pkCorp} and an.pk_storehouse= #{availableNumExt.pkStorehouse}
        and an.pk_product = #{availableNumExt.pkProduct}
    </select>
    <select id="queryAvailableNumByOrder" resultType="com.hzs.common.domain.scm.ic.ext.IcOnHandNumExt">
        select so.pk_country,st.pk_product,bp.unit pk_unit,nvl(#{saOrderExt.pkStorehouse},nvl(st.pk_storehouse, so.pk_storehouse)) pk_storehouse,st.quantity nonhandnum,bh.pk_corp,
               bp.pk_product_bas,st.specs_name_id from sa_order so
         inner join sa_order_items st
                    on so.pk_id=st.pk_order
         inner join bd_storehouse bh
                    on so.pk_storehouse=bh.pk_id
         inner join bd_product bp
                    on bp.pk_id=st.pk_product
        where bh.del_flag=0 and bp.del_flag=0
          and so.order_code=#{saOrderExt.orderCode}
          and st.pk_id in
        <foreach collection="saOrderExt.orderItemsExtList" item="item" open="(" close=")" separator=",">
            #{item.pkId}
        </foreach>
    </select>

</mapper>
