<?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.po.mapper.PoOrderItemsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="PoOrderItemsExt" type="com.hzs.common.domain.scm.po.ext.PoOrderItemsExt">
        <id column="PK_ID" property="pkId" />
        <result column="PK_ORDER" property="pkOrder" />
        <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="PK_STOREHOUSE" property="pkStorehouse" />
        <result column="CLOSE_DATE" property="closeDate" />
        <result column="PLAN_DATE" property="planDate" />
        <result column="PK_UNIT" property="pkUnit" />
        <result column="PK_RATE" property="pkRate" />
        <result column="NNUM" property="nnum" />
        <result column="NORIGPRICE" property="norigprice" />
        <result column="NTAXRATE" property="ntaxrate" />
        <result column="NORIGTAXPRICE" property="norigtaxprice" />
        <result column="NORIGMNY" property="norigmny" />
        <result column="NTAX" property="ntax" />
        <result column="NORIGTAXMNY" property="norigtaxmny" />
        <result column="NACCUMSTORENUM" property="naccumstorenum" />
        <result column="NBACKARRVNUM" property="nbackarrvnum" />
        <result column="IS_GIFT" property="isGift" />
        <result column="PK_PRODUCT" property="pkProduct" />
        <result column="PRODUCT_CODE" property="productCode" />
        <result column="PRODUCT_NAME" property="productName" />
        <result column="SPECS_NAME" property="specsName" />
        <result column="STOREHOUSE_NAME" property="storehouseName" />
        <result column="UNIT_NAME" property="unitName" />
        <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>
    <sql id="query_column">
        select pt.pk_id,pt.pk_order,pt.pk_product_bas,pt.pk_storehouse,bh.name storehouse_name,pt.plan_date,pt.pk_unit,pt.pk_rate,
               pt.nnum,pt.norigprice,pt.ntaxrate,pt.norigtaxprice,pt.norigmny,pt.ntax,pt.norigtaxmny,pt.is_gift,pt.close_date,bn.name unit_name,
               bp.pk_id pk_product,bp.product_code,bp.product_name,sp.specs_name,se.pk_specs_type,se.pk_specs,se.specs_name from po_order_items pt
             left join bd_product_bas bp
                       on pt.pk_product_bas=bp.pk_id
             left join bd_product bd
                       on pt.pk_product_bas=bd.pk_product_bas and bd.pk_country=pt.pk_country and bd.del_flag=0
             left join bd_storehouse bh
                       on pt.pk_storehouse=bh.pk_id
             left join bd_unit bn
                       on pt.pk_unit = bn.pk_id
             left join scm_item_specs se
                       on se.pk_order_items=pt.pk_id and se.order_type=1
             left join (select pk_order_items,wm_concat(specs_name) specs_name from scm_item_specs where del_flag=0
                                                                                                     and order_type=1
                        group by pk_order_items) sp
                       on sp.pk_order_items=pt.pk_id
        where pt.del_flag=0 and pt.pk_country = #{pkCountry}
          and pt.pk_order = #{pkOrder}
    </sql>
    <update id="deleteOrderItemsService">
        update po_order_items set del_flag = 1,pk_modified=#{pkMember},modified_time = sysdate where del_flag=0 and
        pk_id in
        <foreach collection="poOrderItemsList" item="item" open="(" close=")" separator=",">
            #{item.pkId}
        </foreach>
    </update>
    <update id="updatePoOrderItemsTotalNum">
        merge into po_order_items a
        using(
        <foreach collection="icPurchaseInbList" item="item" separator=" union ">
            select #{item.pkPoOrder} pk_po_order,#{item.pkPoOrderItems} pk_po_order_items,
            #{item.bizDate} biz_date,#{item.nnum} nnum
            from dual
        </foreach>
            ) b
        on (a.pk_id=b.pk_po_order_items and a.pk_order=b.pk_po_order)
        when matched then
            update set a.naccumstorenum=a.naccumstorenum + b.nnum,a.close_date=b.biz_date
    </update>
    <select id="queryPoOrderItemsByPk" resultMap="PoOrderItemsExt">
        <include refid="query_column"></include>
        and 0 &lt;= pt.nnum-pt.naccumstorenum
        and close_date is null
    </select>
    <select id="queryListPoOrderItemsByPk" resultMap="PoOrderItemsExt">
        <include refid="query_column"></include>
    </select>
</mapper>
