<?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.warn.mapper.IcWarnConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="IcWarnConfigExt" type="com.hzs.common.domain.scm.warn.ext.IcWarnConfigExt">
        <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_PRODUCT_BAS" property="pkProductBas" />
        <result column="WARN_QUANTITY" property="warnQuantity" />
        <result column="WARN_PERIOD" property="warnPeriod" />
        <result column="PRODUCT_CODE" property="productCode" />
        <result column="PRODUCT_NAME" property="productName" />
        <result column="SPECS_NAME" property="specsName" />
        <result column="STORE_NAME" property="storeName" />
        <collection property="warnConfigSpecsList" ofType="com.hzs.common.domain.scm.warn.IcWarnConfigSpecs">
            <result column="PK_SPECS_TYPE" property="pkSpecsType" />
            <result column="PK_SPECS" property="pkSpecs" />
            <result column="item_specs_name" property="specsName" />
        </collection>
    </resultMap>
    <select id="queryWarnConfigByCondition" resultMap="IcWarnConfigExt">
        select ic.*,bp.product_code,bp.product_name,bs.specs_name,ip.pk_specs_type,ip.pk_specs,ip.specs_name item_specs_name from ic_warn_config ic
        inner join bd_product_bas bp
        on ic.pk_product_bas=bp.pk_id
        inner join (select pk_warn_config,wm_concat(specs_name) specs_name from ic_warn_config_specs
        where del_flag=0
        group by pk_warn_config) bs
        on ic.pk_id = bs.pk_warn_config
        inner join ic_warn_config_specs ip
        on ip.pk_warn_config=ic.pk_id
        <if test="warnConfigParam.scmItemSpecsList != null and warnConfigParam.scmItemSpecsList.size >0">
            inner join (
            select pk_warn_config,max(r) pk_specs from
            (select pk_warn_config,wm_concat(pk_specs)
            over (partition by pk_warn_config order by pk_specs) r
            from ic_warn_config_specs
            where del_flag = 0)
            where  r=replace('<foreach collection="warnConfigParam.scmItemSpecsList" item="it"  open="" separator="," close="">${it.pkSpecs}</foreach>',' ','')
            group by pk_warn_config
            ) sp
            on ic.pk_id= sp.pk_warn_config
        </if>
        where ic.del_flag=0 and bp.del_flag=0 and ip.del_flag=0
        <if test="warnConfigParam.pkId != null">
            and ic.pk_id !=#{warnConfigParam.pkId}
        </if>
        <if test="warnConfigParam.pkProductBas != null">
            and ic.pk_product_bas = #{warnConfigParam.pkProductBas}
        </if>
        <if test="warnConfigParam.productCode != null and warnConfigParam.productCode != ''">
            and bp.product_code=#{warnConfigParam.productCode}
        </if>
        <if test="warnConfigParam.productName != null and warnConfigParam.productName != ''">
            and bp.product_name = #{warnConfigParam.productName}
        </if>
        <if test="warnConfigParam.warnQuantity != null">
            and ic.warn_quantity>#{warnConfigParam.warnQuantity}
        </if>
        <if test="warnConfigParam.warnPeriod != null">
            and ic.warn_period = #{warnConfigParam.warnPeriod}
        </if>
        order by bp.product_code,ic.creation_time
    </select>

    <select id="queryWarnConfigInventory" resultMap="IcWarnConfigExt">
        select x.*,bh.name store_name from(
        select a.pk_product_bas,b.pk_storehouse,a.product_code,a.product_name,a.specs_name,
        (case when a.warn_quantity-nvl(b.NONHANDNUM,0)>0 then b.NONHANDNUM else -1 end) warn_quantity from (
        select ic.*,bp.product_code,bp.product_name,bs.specs_name from ic_warn_config ic
                                                               inner join bd_product_bas bp
                                                                          on bp.pk_id=ic.pk_product_bas
                                                               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 and bp.del_flag=0) a
        left join (
        select pk_product_bas,pk_storehouse,sum(NONHANDNUM) NONHANDNUM,ib.specs_name from ic_on_hand_num ih
        inner join (
        select pk_onhandnum,max(r) 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) ib
        on ih.pk_id=ib.pk_onhandnum
        where ih.del_flag=0
        group by pk_product_bas,pk_storehouse,ib.specs_name) b
        on a.pk_product_bas=b.pk_product_bas and a.specs_name=b.specs_name) x
        left join bd_storehouse bh
        on bh.pk_id = x.pk_storehouse
        where x.warn_quantity>0
    </select>

</mapper>
