<?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.TransferOrderMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="TransferOrderExt" type="com.hzs.common.domain.scm.trans.ext.TransferOrderExt">
        <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_COUNTRY" property="pkCountry" />
        <result column="PK_CREATOR" property="pkCreator" />
        <result column="PK_MODIFIED" property="pkModified" />
        <result column="PK_DEPT" property="pkDept" />
        <result column="PK_WHSMANAGER" property="pkWhsmanager" />
        <result column="PK_CORP" property="pkCorp" />
        <result column="PK_IN_CORP" property="pkInCorp" />
        <result column="PK_RATE" property="pkRate" />
        <result column="ORDER_STATUS" property="orderStatus" />
        <result column="PRINT_STATUS" property="printStatus" />
        <result column="TRANS_TYPE" property="transType" />
        <result column="TOTAL_NUM" property="totalNum" />
        <result column="TOTAL_ORIGMNY" property="totalOrigmny" />
        <result column="ORDER_CODE" property="orderCode" />
        <result column="ORDER_DATE" property="orderDate" />
        <result column="REMARK" property="remark" />
        <result column="PK_IN_COUNTRY" property="pkInCountry" />
        <result column="DEPT_NAME" property="deptName" />
        <result column="MANAGER_NAME" property="managerName" />
        <result column="CORP_NAME" property="corpName" />
        <result column="COUNTRY_IN_NAME" property="countryInName" />
        <result column="CORP_IN_NAME" property="corpInName" />
        <collection property="transferOrderItemsList" ofType="com.hzs.common.domain.scm.trans.ext.TransferOrderItemsExt">
            <result column="pk_items" property="pkId" />
            <result column="ITEM_PK_CREATOR" property="pkCreator" />
            <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="UNIT_NAME" property="unitName" />
            <result column="PRODUCT_CODE" property="productCode" />
            <result column="PRODUCT_NAME" property="productName" />
            <result column="SPECS_NAME" property="specsName" />
            <result column="CUBASDOC_NAME" property="cubasdocName" />
            <result column="IN_STOREHOUSE_NAME" property="inStorehouseName" />
            <result column="STOREHOUSE_NAME" property="storehouseName" />
        </collection>
    </resultMap>
    <update id="updateTransferOrderStatusByTrans">
        update transfer_order set ORDER_STATUS= #{orderStatus},MODIFIED_TIME=sysdate,PK_MODIFIED=#{pkMember} where pk_id in(
        select pk_transfer from(
        select pk_transfer, sum(case when NNUM>total_trans_in_num then 1 else 0 end) as NACCUMSTORENUM from transfer_order_items
        where del_flag=0 and pk_id in
        (select pk_transfer_items from ic_transout_b where del_flag=0 and pk_id in
        <foreach collection="transferInbList" item="item" open="(" close=")" separator=",">
            #{item.pkTransferOutB}
        </foreach>
        )
        group by pk_transfer
        )
        where NACCUMSTORENUM =0)
    </update>
    <select id="queryTransferOrderById" resultMap="TransferOrderExt">
        select tr.*,ti.pk_id pk_items,ti.pk_product_bas,ti.pk_storehouse,ti.plan_date,ti.pk_unit,
        ti.nnum,ti.norigprice,ti.ntaxrate,ti.norigtaxprice,ti.norigmny,ti.ntax,ti.norigtaxmny,
        ti.pk_creator item_pk_creator from transfer_order tr
        inner join transfer_order_items ti
        on tr.pk_id=ti.pk_transfer
        where tr.del_flag=0 and ti.del_flag=0
        and tr.pk_id=#{pkTransfer}
        <if test="orderStatus != null">
            and tr.order_status = #{orderStatus}
        </if>
    </select>
    <select id="queryTransferOrderByCondition" resultMap="TransferOrderExt">
        select tr.*,sd.dept_name,su.nick_name manager_name,sc.corp_name,
        ti.pk_id pk_items,ti.pk_unit,ti.pk_product_bas,ti.pk_storehouse,ti.nnum,
        ti.norigprice,ti.ntaxrate,ti.norigtaxprice,ti.norigmny,ti.ntax,ti.norigtaxmny,
        bn.name unit_name,bh.name storehouse_name,ti.plan_date,
        bp.product_code,bp.product_name,sp.specs_name,ti.pk_cubasdoc,bc.cust_name cubasdoc_name,
        bct.name country_in_name,bcp.corp_name corp_in_name,bcs.name in_storehouse_name from transfer_order tr
        inner join transfer_order_items ti
        on tr.pk_id=ti.pk_transfer
        left join sys_dept sd
        on tr.pk_dept=sd.dept_id
        left join sys_user su
        on tr.pk_whsmanager=su.user_id
        left join sys_corp sc
        on tr.pk_corp=sc.pk_id
        left join bd_unit bn
        on ti.pk_unit = bn.pk_id
        left join bd_storehouse bh
        on ti.pk_storehouse=bh.pk_id
        left join bd_product_bas bp
        on ti.pk_product_bas=bp.pk_id
        left join bd_cubasdoc bc
        on bc.pk_id=ti.pk_cubasdoc and bc.del_flag=0
        left join bd_country bct
        on bct.pk_id=tr.pk_in_country
        left join sys_corp bcp
        on bcp.pk_id=tr.pk_in_corp
        left join bd_storehouse bcs
        on bcs.pk_id =ti.pk_in_storehouse
        left join (
        select pk_order,pk_order_items,wm_concat(specs_name) specs_name from scm_item_specs
        where del_flag=0 and order_type=7
        group by pk_order,pk_order_items) sp
        on ti.pk_id=sp.pk_order_items
        where tr.del_flag=0 and ti.del_flag=0
        <if test="pkCountry != null">
            and tr.pk_country=#{pkCountry}
        </if>
        <if test="pkId != null">
            and tr.pk_id=#{pkId}
        </if>
        <if test="orderCode != null and orderCode != ''">
            and tr.order_code = #{orderCode}
        </if>
        <if test="orderStatus != null">
            and tr.order_status = #{orderStatus}
        </if>
        <if test="pkCorp != null">
            and tr.pk_corp = #{pkCorp}
        </if>
        <if test="startDate != null">
            and tr.order_date >= #{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and tr.order_date &lt; #{endDate, jdbcType=DATE}
        </if>
        <if test="productName != null and productName != ''">
            and bp.product_name=#{productName}
        </if>
        order by tr.order_date desc
    </select>
    <select id="queryTransferOrderByOrderItems" resultMap="TransferOrderExt">
        <foreach collection="orderItemsParamList" item="item" index="index" separator=" union ">
            select tr.*,ti.pk_id pk_items,ti.pk_product_bas,ti.pk_storehouse,ti.plan_date,ti.pk_unit,
            ti.nnum,ti.norigprice,ti.ntaxrate,ti.norigtaxprice,ti.norigmny,ti.ntax,ti.norigtaxmny,
            ti.pk_creator item_pk_creator from transfer_order tr
            inner join transfer_order_items ti
            on tr.pk_id=ti.pk_transfer
            where tr.del_flag=0 and ti.del_flag=0
            and tr.pk_id=#{item.pkTransfer} and ti.pk_id=#{item.pkTransferItems}
            and ti.pk_product_bas=#{item.pkProductBas}
            and tr.order_status =3
        </foreach>
    </select>
    <select id="queryTransferOrderToStoreByCondition" resultMap="TransferOrderExt">
        select tr.*,bn.name unit_name,
        sd.dept_name,su.nick_name manager_name,sc.corp_name,
        ti.pk_id pk_items,ti.pk_transfer,ti.pk_product_bas,ti.pk_storehouse,bh.name storehouse_name,ti.plan_date,ti.pk_unit,
        ti.nnum,ti.norigprice,ti.ntaxrate,ti.norigtaxprice,ti.norigmny,ti.ntax,ti.norigtaxmny,ti.pk_in_storehouse,
        bd.pk_id pk_product,bd.product_code,bd.product_name,sp.specs_name,
        bct.name country_in_name,bcp.corp_name corp_in_name,bcs.name in_storehouse_name from transfer_order tr
        inner join transfer_order_items ti
        on tr.pk_id=ti.pk_transfer
        inner join bd_product_bas bp
        on ti.pk_product_bas=bp.pk_id
        inner join bd_product bd
        on ti.pk_product_bas=bd.pk_product_bas and bd.pk_country=ti.pk_country and bd.del_flag=0
        left join sys_dept sd
        on tr.pk_dept=sd.dept_id
        left join sys_user su
        on tr.pk_whsmanager=su.user_id
        left join bd_storehouse bh
        on ti.pk_storehouse=bh.pk_id
        left join bd_country bct
        on bct.pk_id=tr.pk_in_country
        left join sys_corp sc
        on tr.pk_corp=sc.pk_id
        left join bd_unit bn
        on ti.pk_unit = bn.pk_id
        left join sys_corp bcp
        on bcp.pk_id=tr.pk_in_corp
        left join bd_storehouse bcs
        on bcs.pk_id =ti.pk_in_storehouse
        left join (select pk_order_items,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=ti.pk_id
        where ti.del_flag=0 and tr.del_flag=0 and bp.del_flag=0
        and tr.pk_country=#{pkCountry}
        and 0 &lt; ti.nnum-ti.total_trans_out_num
        <if test="pkCorp != null">
            and tr.pk_corp = #{pkCorp}
        </if>
        <if test="startDate != null">
            and tr.order_date >= #{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and tr.order_date &lt; #{endDate, jdbcType=DATE}
        </if>
        <if test="orderCode != null and orderCode != ''">
            and tr.order_code = #{orderCode}
        </if>
        and tr.order_status =3
    </select>

</mapper>
