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

    <!-- 通用查询映射结果 -->
    <resultMap id="IcOnHandNumExt" type="com.hzs.common.domain.scm.ic.ext.IcOnHandNumExt">
        <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="PRODUCT_DATE" property="productDate" />
        <result column="EXPIRE_DATE" property="expireDate" />
        <result column="PK_UNIT" property="pkUnit" />
        <result column="PK_STOREHOUSE" property="pkStorehouse" />
        <result column="NONHANDNUM" property="nonhandnum" />
        <result column="PK_BATCH_CODE" property="pkBatchCode" />
        <result column="PRODUCT_CODE" property="productCode" />
        <result column="PRODUCT_NAME" property="productName" />
        <result column="H_SPECS_NAME" property="specsName" />
        <result column="UNIT_NAME" property="unitName" />
        <result column="CUBASDOC_NAME" property="cubasdocName" />
        <result column="STOREHOUSE_NAME" property="storehouseName" />
        <result column="CORP_NAME" property="corpName" />
        <result column="VALID_PERIOD" property="validPeriod" />
        <result column="BATCH_CODE" property="batchCode" />
        <result column="DISPLAY_RED" property="displayRed" />
        <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="updateOnHandNumByNum">
        update ic_on_hand_num set product_date=#{productDate, jdbcType=DATE},
        expire_date=#{expireDate, jdbcType=DATE},nonhandnum = nonhandnum + #{nonhandnum},
        pk_modified=#{pkModified},modified_time=#{modifiedTime}
        where del_flag=0 and pk_id=#{pkId}
    </update>
    <update id="mergeUpdateOnHandNumByNum">
        merge into ic_on_hand_num a
        using(
        <foreach collection="icOnHandNumList" item="item" separator=" union all ">
            select #{item.pkId} pk_id,#{item.nonhandnum} nonhandnum,
            #{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.nonhandnum = a.nonhandnum + b.nonhandnum,
        a.pk_modified=b.pk_modified,a.modified_time=b.modified_time
            where a.del_flag=0 and a.nonhandnum + b.nonhandnum >=0
    </update>
    <update id="mergeUpdateBackOnHandNumByNum">
        merge into ic_on_hand_num a
        using(
        select pk_onhandnum pk_id,sum(nnum) nnum,#{pkMember} pk_modified,sysdate modified_time from (
        <foreach collection="icOnHandNumbList" item="item" separator=" union ">
            select #{item.pkOnhandnum} pk_onhandnum,#{item.nnum} nnum from dual
        </foreach>
        )
        group by pk_onhandnum
        ) b
        on (a.pk_id=b.pk_id)
        when matched then
        update set a.nonhandnum = a.nonhandnum + b.nnum,
        a.pk_modified=b.pk_modified,a.modified_time=b.modified_time
    </update>
    <select id="createIcOnHandNumId" resultType="java.lang.Long">
        SELECT IC_ON_HAND_NUM_SEQ.NEXTVAL FROM DUAL
    </select>

    <select id="queryIcOnHandNumByCondition" resultMap="IcOnHandNumExt">
        select ih.*,ips.pk_specs_type,ips.pk_specs,ips.specs_name from ic_on_hand_num ih
        inner join (
        select pk_onhandnum,max(r) pk_specs from
        (select pk_onhandnum,wm_concat(pk_specs) over (partition by pk_onhandnum order by pk_specs) r
        from ic_on_hand_specs
        where del_flag = 0)
        group by pk_onhandnum
        ) ip
        on ih.pk_id=ip.pk_onhandnum
        inner join ic_on_hand_specs ips
        on ih.pk_id= ips.pk_onhandnum
        where ih.del_flag=0 and ips.del_flag=0 and ih.pk_country = #{icOnHandNum.pkCountry}
        and ih.pk_product=#{icOnHandNum.pkProduct} and ih.pk_storehouse=#{icOnHandNum.pkStorehouse}
        <if test="icOnHandNum.pkBatchCode != null">
            and ih.pk_batch_code=#{icOnHandNum.pkBatchCode}
        </if>
        and ip.pk_specs = #{pkSpecs}
        order by ih.pk_product,ih.pk_storehouse,ih.expire_date,ips.pk_specs
    </select>

    <select id="queryOnHandNumByCondition" resultMap="IcOnHandNumExt">
        select ih.*,ip.h_specs_name from ic_on_hand_num ih
        inner join (
        select pk_onhandnum,max(r) h_specs_name from
        (select pk_onhandnum,wm_concat(pk_specs) over (partition by pk_onhandnum order by pk_specs) r
        from ic_on_hand_specs
        where del_flag = 0)
        group by pk_onhandnum) ip
        on ih.pk_id=ip.pk_onhandnum
        where ih.del_flag=0
        and ih.pk_country = #{icOnHandNum.pkCountry}
        and ih.pk_product=#{icOnHandNum.pkProduct} and ih.pk_storehouse=#{icOnHandNum.pkStorehouse}
        <if test="icOnHandNum.pkBatchCode != null">
            and ih.pk_batch_code=#{icOnHandNum.pkBatchCode}
        </if>
        order by ih.pk_product,ih.pk_storehouse,ih.expire_date
    </select>

    <select id="queryInventoryProductByParam" resultMap="IcOnHandNumExt">
        select ih.*,ip.h_specs_name,bp.product_code,bp.product_name,sc.corp_name,bh.name storehouse_name,
        bc.cust_name cubasdoc_name,bn.name unit_name,ib.valid_period,ib.batch_code,
        (case when ih.expire_date-sysdate &lt;b.warn_period then 0 else 1 end) display_red,bp.shipping_channel from ic_on_hand_num ih
        inner join (
        select pk_onhandnum,max(r) h_specs_name from
        (select pk_onhandnum,wm_concat(specs_name) over (partition by pk_onhandnum order by pk_specs) r
        from ic_on_hand_specs
        where del_flag = 0)
        group by pk_onhandnum
        ) ip
        on ih.pk_id=ip.pk_onhandnum
        inner join bd_product bp
        on ih.pk_product=bp.pk_id
        inner join bd_product_bas bba
        on ih.pk_product_bas=bba.pk_id and bba.del_flag=0
        left join bd_cubasdoc bc
        on ih.pk_cubasdoc=bc.pk_id and bc.del_flag=0
        left join bd_unit bn
        on ih.pk_unit = bn.pk_id and bn.del_flag=0
        left join ic_batch_code ib
        on ih.pk_batch_code=ib.pk_id and ib.del_flag=0
        left join sys_corp sc
        on ih.pk_corp=sc.pk_id and sc.del_flag=0
        left join bd_storehouse bh
        on ih.pk_storehouse=bh.pk_id and bh.del_flag=0
        left join(
        select pk_product_bas,warn_period,bs.specs_name from ic_warn_config ic
        inner join (
        select pk_warn_config,max(r) specs_name from
        (select pk_warn_config,wm_concat(specs_name) over (partition by pk_warn_config order by pk_specs) r
        from ic_warn_config_specs
        where del_flag = 0)
        group by pk_warn_config
        ) bs
        on ic.pk_id=bs.pk_warn_config
        where ic.del_flag=0
        ) b
        on ih.pk_product_bas=b.pk_product_bas and ip.h_specs_name=b.specs_name
        where ih.del_flag=0 and bp.del_flag=0
        <if test="pkCountry != null">
            and ih.pk_country=#{pkCountry}
        </if>
        <if test="pkCorp != null">
            and ih.pk_corp = #{pkCorp}
        </if>
        <if test="pkCubasdoc != null">
            and ih.pk_cubasdoc = #{pkCubasdoc}
        </if>
        <if test="productCode != null and productCode != ''">
            and bp.product_code like '%' || #{productCode} || '%'
        </if>
        <if test="shippingChannel !=null">
            and bp.shipping_channel=#{shippingChannel}
        </if>
        <if test="productName != null and productName != ''">
            and bp.product_name like '%' || #{productName}  || '%'
        </if>
        <if test="pkStorehouse != null ">
            and ih.pk_storehouse=#{pkStorehouse}
        </if>
        <if test="batchCode != null and batchCode != ''">
            and ib.batch_code=#{batchCode}
        </if>
        <if test="pkWarehouse != null">
            and ih.pk_storehouse=#{pkWarehouse}
        </if>
    </select>
    <select id="queryOnHandNumByList" resultMap="IcOnHandNumExt">
        select ih.*,ip.pk_specs_type,ip.pk_specs,ip.specs_name from ic_on_hand_num ih
        inner join ic_on_hand_specs ip
                   on ih.pk_id= ip.pk_onhandnum
        where ih.del_flag=0 and ip.del_flag=0 and ih.pk_id in
        <foreach item="item" collection="idList" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

</mapper>
