<?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.store.mapper.IcStoreHandNumMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="IcStoreHandNumExt" type="com.hzs.common.domain.scm.store.ext.IcStoreHandNumExt">
        <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_MEMBER_STORE" property="pkMemberStore" />
        <result column="PK_PRODUCT_BAS" property="pkProductBas" />
        <result column="PK_PRODUCT" property="pkProduct" />
        <result column="PK_UNIT" property="pkUnit" />
        <result column="NONHANDNUM" property="nonhandnum" />
        <result column="SPECS_NAME_ID" property="specsNameId" />
        <result column="STORE_NAME" property="storeName" />
        <result column="PRODUCT_NAME" property="productName"/>
        <result column="PRODUCT_CODE" property="productCode"/>
        <collection property="storeHandSpecsList" ofType="com.hzs.common.domain.scm.store.IcStoreHandSpecs">
            <result column="PK_SPECS_TYPE" property="pkSpecsType" />
            <result column="PK_SPECS" property="pkSpecs" />
            <result column="SPECS_NAME" property="specsName" />
        </collection>
    </resultMap>
    <update id="updateStoreHandNumByNum">
        update ic_store_hand_num set nonhandnum = nonhandnum + #{nonhandnum},
                                  pk_modified=#{pkModified},modified_time=#{modifiedTime}
        where del_flag=0 and pk_id=#{pkId}
    </update>
    <update id="mergeUpdateOnHandNumByNum">
        merge into ic_store_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>
    <select id="queryIcStoreHandNumByCondition" resultMap="IcStoreHandNumExt">
        select ih.*,ip.pk_specs specs_name_id,ips.pk_specs_type,ips.pk_specs,ips.specs_name from ic_store_hand_num ih
        inner join (
        select pk_onhandnum,max(r) pk_specs from
        (select pk_onhandnum,to_char(wm_concat(pk_specs) over (partition by pk_onhandnum order by pk_specs)) r
        from ic_store_hand_specs
        where del_flag = 0)
        group by pk_onhandnum
        ) ip
        on ih.pk_id=ip.pk_onhandnum
        inner join ic_store_hand_specs ips
        on ih.pk_id= ips.pk_onhandnum
        where ih.del_flag=0 and ips.del_flag=0
        and ih.pk_country = #{pkCountry}
        <if test="pkProduct != null" >
            and ih.pk_product=#{pkProduct}
        </if>
        <if test="pkMemberStore != null">
            and ih.pk_member_store=#{pkMemberStore}
        </if>
        <if test="storeName != null and storeName != ''">
            and ct.store_name = #{storeName}
        </if>
        <if test="pkSpecs != null and pkSpecs != ''">
            and ip.pk_specs = #{pkSpecs}
        </if>
        order by ih.pk_product
    </select>

    <select id="queryDisIcStoreHandNumByCondition" resultMap="IcStoreHandNumExt">
        select ih.*,ct.store_name,bp.product_name,bp.product_code,
        ip.specs_name specs_name_id,ips.pk_specs_type,ips.pk_specs,ips.specs_name from ic_store_hand_num ih
        inner join (
        select pk_onhandnum,max(r) specs_name from
        (select pk_onhandnum,to_char(wm_concat(specs_name) over (partition by pk_onhandnum order by pk_specs)) r
        from ic_store_hand_specs
        where del_flag = 0)
        group by pk_onhandnum
        ) ip
        on ih.pk_id=ip.pk_onhandnum
        inner join ic_store_hand_specs ips
        on ih.pk_id= ips.pk_onhandnum
        inner join cu_store ct
        on ct.pk_id=ih.pk_member_store
        inner join bd_product bp
        on ih.pk_product=bp.pk_id
        where ih.del_flag=0 and ips.del_flag=0 and ct.del_flag=0 and bp.del_flag=0
        and ih.pk_country = #{pkCountry}
        <if test="pkProduct != null" >
            and ih.pk_product=#{pkProduct}
        </if>
        <if test="pkMemberStore != null">
            and ih.pk_member_store=#{pkMemberStore}
        </if>
        <if test="storeName != null and storeName != ''">
            and ct.store_name = #{storeName}
        </if>
        <if test="productCode != null and productCode != ''">
            and bp.product_code=#{productCode}
        </if>
        <if test="productName != null and productName != ''">
            and bp.product_name =#{productName}
        </if>
        order by ih.pk_product
    </select>

</mapper>
