<?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="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="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="queryAvailableNumBySpecsId" resultType="com.hzs.common.domain.scm.ic.IcAvailableNum">
        select ia.* from ic_available_num ia
        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="specsIdList" item="it" open="" separator="," close="">${it}</foreach>',' ','')
        group by pk_available_num
        ) sp
        on ia.pk_id = sp.pk_available_num
        where ia.del_flag=0
        and ia.pk_storehouse= #{pkStorehouse}
        and ia.pk_product=#{pkProduct}
    </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>

</mapper>
