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

    <!-- 通用查询映射结果 -->
    <resultMap id="IcTransOutHExt" type="com.hzs.common.domain.scm.trans.ext.IcTransOutHExt">
        <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="ORDER_DATE" property="orderDate" />
        <result column="ORDER_STATUS" property="orderStatus" />
        <result column="PRINT_STATUS" property="printStatus" />
        <result column="PK_IN_CORP" property="pkInCorp" />
        <result column="PK_CORP" property="pkCorp" />
        <result column="TOTAL_NUM" property="totalNum" />
        <result column="ORDER_CODE" property="orderCode" />
        <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_IN_NAME" property="corpInName" />
        <result column="CORP_NAME" property="corpName" />
        <result column="COUNTRY_IN_NAME" property="countryInName" />
        <collection property="transOutbExtList" ofType="com.hzs.common.domain.scm.trans.ext.IcTransOutBExt">
            <result column="PK_ITEM_ID" property="pkId"/>
            <result column="PK_UNIT" property="pkUnit" />
            <result column="PK_TRANSFER" property="pkTransfer" />
            <result column="PK_TRANSFER_ITEMS" property="pkTransferItems" />
            <result column="PK_TRANSFER_OUT" property="pkTransferOut" />
            <result column="PK_CLOCATION" property="pkClocation" />
            <result column="PK_PRODUCT_BAS" property="pkProductBas" />
            <result column="PK_PRODUCT" property="pkProduct" />
            <result column="TRANS_TYPE" property="transType" />
            <result column="BIZ_DATE" property="bizDate" />
            <result column="PLAN_ARRIVE_DATE" property="planArriveDate" />
            <result column="PK_CUBASDOC" property="pkCubasdoc" />
            <result column="PK_CUSTOMER" property="pkCustomer" />
            <result column="PK_IN_STOREHOUSE" property="pkInStorehouse" />
            <result column="PK_STOREHOUSE" property="pkStorehouse" />
            <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="PK_BATCH_CODE" property="pkBatchCode" />
            <result column="UNIT_NAME" property="unitName" />
            <result column="CLOCATION_NAME" property="clocationName" />
            <result column="STOREHOUSE_NAME" property="storehouseName" />
            <result column="BATCH_CODE" property="batchCode" />
            <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" />
        </collection>
    </resultMap>
    <select id="queryTransOutById" resultMap="IcTransOutHExt">
        select ih.*,ib.pk_id pk_item_id,ib.pk_product_bas,ib.pk_unit,ib.pk_clocation,ib.pk_product,ib.trans_type,ib.biz_date,
               ib.pk_storehouse,ib.pk_rate,ib.nnum,ib.norigprice,ib.ntaxrate,ib.norigtaxprice,
               ib.norigmny,ib.ntax,ib.norigtaxmny,ib.pk_batch_code from ic_transout_h ih
        inner join ic_transout_b ib
                  on ih.pk_id=ib.pk_transfer_out
        where ih.del_flag=0 and ib.del_flag=0
          and ih.pk_id = #{pkId}
          and ih.order_status= #{orderStatus}
    </select>
    <select id="queryDisTransOutExtByCondition" resultMap="IcTransOutHExt">
        select ih.*,ib.pk_id pk_item_id,ib.pk_transfer,ib.pk_transfer_items,ib.pk_product_bas,sd.dept_name,su.nick_name manager_name,sc.corp_name,
        ib.pk_unit,ib.pk_clocation,ib.plan_arrive_date,ib.pk_cubasdoc,bc.cust_name cubasdoc_name,
        ib.pk_product,ib.biz_date,ib.pk_storehouse,ib.pk_rate,ib.nnum,ib.norigprice,ib.ntaxrate,ib.norigtaxprice,
        ib.norigmny,ib.ntax,ib.norigtaxmny,ib.pk_batch_code,bn.name unit_name,bh.name storehouse_name,ic.batch_code,
        bp.product_code,bp.product_name,sp.specs_name,bct.name country_in_name,bcp.corp_name corp_in_name,ib.pk_in_storehouse,
        bcs.name in_storehouse_name from ic_transout_h ih
        inner join ic_transout_b ib
        on ih.pk_id=ib.pk_transfer_out
        left join sys_dept sd
        on ih.pk_dept=sd.dept_id
        left join sys_user su
        on ih.pk_whsmanager=su.user_id
        left join sys_corp sc
        on ih.pk_corp=sc.pk_id
        left join bd_unit bn
        on ib.pk_unit = bn.pk_id
        left join bd_storehouse bh
        on ib.pk_storehouse=bh.pk_id
        left join ic_batch_code ic
        on ib.pk_batch_code=ic.pk_id
        left join bd_product bp
        on ib.pk_product=bp.pk_id
        left join bd_country bct
        on bct.pk_id=ih.pk_in_country
        left join bd_cubasdoc bc
        on bc.pk_id=ib.pk_cubasdoc and bc.del_flag=0
        left join sys_corp bcp
        on bcp.pk_id=ih.pk_in_corp
        left join bd_storehouse bcs
        on bcs.pk_id =ib.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=9
        group by pk_order,pk_order_items) sp
        on ib.pk_id=sp.pk_order_items
        where ih.del_flag=0 and ib.del_flag=0
        <if test="pkCountry != null">
            and ih.pk_country=#{pkCountry}
        </if>
        <if test="pkId != null">
            and ih.pk_id=#{pkId}
        </if>
        <if test="orderStatus != null">
            and ih.order_status = #{orderStatus}
        </if>
        <if test="pkCorp != null">
            and ih.pk_corp = #{pkCorp}
        </if>
        <if test="orderCode != null and orderCode != ''">
            and ih.order_code = #{orderCode}
        </if>
        <if test="startDate != null">
            and ih.order_date >= #{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and ih.order_date &lt; #{endDate, jdbcType=DATE}
        </if>
        <if test="productName != null and productName != ''">
            and bp.product_name=#{productName}
        </if>
        order by ih.order_code desc,ih.order_date desc
    </select>
    <select id="queryTransferOutByOrderItems" resultMap="IcTransOutHExt">
        <foreach collection="orderItemsParamList" item="item" index="index" separator=" union ">
            select tr.*,ti.pk_id PK_ITEM_ID,ti.pk_product_bas,ti.pk_storehouse,ti.pk_unit,
            ti.nnum,ti.norigprice,ti.ntaxrate,ti.norigtaxprice,ti.norigmny,ti.ntax,ti.norigtaxmny,
            ti.pk_creator item_pk_creator from ic_transout_h tr
            inner join ic_transout_b ti
            on tr.pk_id=ti.pk_transfer_out
            where tr.del_flag=0 and ti.del_flag=0
            and tr.pk_id=#{item.pkTransferOut} and ti.pk_id=#{item.pkTransferOutB}
            and ti.pk_product_bas=#{item.pkProductBas}
            and tr.order_status =3
        </foreach>
    </select>
    <select id="queryTransferOutToStoreByCondition" resultMap="IcTransOutHExt">
        select tr.*,sd.dept_name,su.nick_name manager_name,sc.corp_name,
        ti.pk_id pk_item_id,bnn.pk_id pk_unit,ti.pk_product_bas,bd.pk_id pk_product,ti.pk_storehouse,ti.nnum,
        ti.norigprice,ti.ntaxrate,ti.norigtaxprice,ti.norigmny,ti.ntax,ti.norigtaxmny,
        bnn.name unit_name,bh.name storehouse_name,ti.pk_in_storehouse,ti.pk_cubasdoc,
        bp.product_code,bp.product_name,sp.specs_name,bc.cust_name cubasdoc_name,
        bct.name country_in_name,bcp.corp_name corp_in_name,bcs.name in_storehouse_name from ic_transout_h tr
        inner join ic_transout_b ti
        on tr.pk_id=ti.pk_transfer_out
        inner join transfer_order_items toi
        on ti.pk_transfer_items=toi.pk_id
        inner join bd_product bd
        on bd.pk_product_bas=ti.pk_product_bas and bd.pk_country=tr.pk_in_country
        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 bd.unit = bn.pk_id and bn.del_flag=0
        left join bd_unit bnn
        on bn.name=bnn.name and bnn.pk_country=tr.pk_in_country and bnn.del_flag=0
        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=9
        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 and bp.del_flag=0
        and 0 &lt; toi.nnum-toi.total_trans_in_num
        <if test="pkCountry != null">
            and tr.pk_in_country=#{pkCountry}
        </if>
        <if test="pkId != null">
            and tr.pk_id=#{pkId}
        </if>
        <if test="orderStatus != null">
            and tr.order_status = #{orderStatus}
        </if>
        <if test="pkCorp != null">
            and tr.pk_corp = #{pkCorp}
        </if>
        <if test="orderCode != null and orderCode != ''">
            and tr.order_code=#{orderCode}
        </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>
    </select>

</mapper>
