<?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.trans.mapper.TransferOrderItemsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="TransferOrderItemsExt" type="com.hzs.common.domain.scm.trans.ext.TransferOrderItemsExt">
        <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_TRANSFER" property="pkTransfer" />
        <result column="PK_PRODUCT_BAS" property="pkProductBas" />
        <result column="PK_UNIT" property="pkUnit" />
        <result column="PLAN_DATE" property="planDate" />
        <result column="PK_STOREHOUSE" property="pkStorehouse" />
        <result column="PK_IN_STOREHOUSE" property="pkInStorehouse" />
        <result column="PK_CUBASDOC" property="pkCubasdoc" />
        <result column="PK_CUSTOMER" property="pkCustomer" />
        <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="TOTAL_TRANS_OUT_NUM" property="totalTransOutNum" />
        <result column="TOTAL_TRANS_IN_NUM" property="totalTransInNum" />
        <result column="OUT_END" property="outEnd" />
        <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" />
        <result column="CUBASDOC_NAME" property="cubasdocName" />
        <result column="STOREHOUSE_NAME" property="storehouseName" />
        <result column="IN_STOREHOUSE_NAME" property="inStorehouseName" />
        <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="item_specs_name" property="specsName" />
        </collection>
    </resultMap>
    <update id="updateTransferOrderItemsTotalInNum">
        merge into transfer_order_items a
        using(
        select tr.pk_transfer,tr.pk_transfer_items,b.nnum from ic_transout_b tr
        inner join(
        <foreach collection="transferInbList" item="item" separator=" union ">
            select #{item.pkTransferOut} pk_transfer_out,#{item.pkTransferOutB} pk_transfer_out_b,#{item.nnum} nnum
            from dual
        </foreach>
        ) b
        on tr.pk_transfer_out=b.pk_transfer_out and tr.pk_id=b.pk_transfer_out_b
        ) b
        on (a.pk_id=b.pk_transfer_items and a.pk_transfer=b.pk_transfer)
        when matched then
        update set a.total_trans_in_num=a.total_trans_in_num + b.nnum
    </update>
    <update id="updateTransferOrderItemsTotalOutNum">
        merge into transfer_order_items a
        using(
        <foreach collection="transferOutBiList" item="item" separator=" union ">
            select #{item.pkTransfer} pk_transfer,#{item.pkTransferItems} pk_transfer_items,#{item.nnum} nnum
            from dual
        </foreach>
        ) b
        on (a.pk_id=b.pk_transfer_items and a.pk_transfer=b.pk_transfer)
        when matched then
        update set a.total_trans_out_num=a.total_trans_out_num + b.nnum
    </update>
    <update id="updateTransferOrderItemsTotalOutEnd">
        update transfer_order_items set out_end = 0
        where del_flag=0 and out_end = 1 and nnum &lt;= total_trans_out_num and pk_id in
        <foreach collection="transferOutBiList" item="item" open="(" close=")" separator=",">
            #{item.pkTransferItems}
        </foreach>
    </update>
    <delete id="deleteOrderItemsService">
        update transfer_order_items set del_flag = 1,pk_modified=#{pkMember},modified_time = sysdate where del_flag=0 and
        pk_id in
        <foreach collection="transferOrderItemsList" item="item" open="(" close=")" separator=",">
            #{item.pkId}
        </foreach>
    </delete>
    <select id="queryTransferOrderItemsByPk" resultMap="TransferOrderItemsExt">
        select pt.pk_id,pt.pk_transfer,pt.pk_product_bas,pt.pk_storehouse,bh.name in_storehouse_name,
               bih.name storehouse_name, pt.plan_date,pt.pk_unit,pt.pk_rate,pt.pk_in_storehouse,
               pt.nnum,pt.norigprice,pt.ntaxrate,pt.norigtaxprice,pt.norigmny,pt.ntax,pt.norigtaxmny,
               pt.total_trans_out_num,pt.total_trans_in_num,bn.name unit_name,
               pt.pk_cubasdoc,bc.cust_name cubasdoc_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 item_specs_name from transfer_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_in_storehouse=bh.pk_id
              left join bd_storehouse bih
                        on pt.pk_storehouse=bih.pk_id
              left join bd_unit bn
                        on bn.pk_id=pt.pk_unit
              left join bd_cubasdoc bc
                        on bc.pk_id=pt.pk_cubasdoc and bc.del_flag=0
              left join scm_item_specs se
                        on se.pk_order_items=pt.pk_id and se.order_type=7
              left join (select pk_order_items,to_char(wm_concat(specs_name)) specs_name from scm_item_specs where del_flag=0
                        and order_type=7
                         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_transfer = #{pkTransfer} and 0 &lt;= pt.nnum-pt.total_trans_out_num
    </select>

</mapper>
