<?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.purchase.mapper.IcPurchaseInHMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="IcPurchaseInHExt" type="com.hzs.common.domain.scm.purchase.ext.IcPurchaseInHExt">
        <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_DEPT" property="pkDept" />
        <result column="PK_WHSMANAGER" property="pkWhsmanager" />
        <result column="PK_CUBASDOC" property="pkCubasdoc" />
        <result column="PK_STOREHOUSE" property="pkStorehouse" />
        <result column="ORDER_DATE" property="orderDate" />
        <result column="ORDER_STATUS" property="orderStatus" />
        <result column="RETURN_STATUS" property="returnStatus" />
        <result column="PRINT_STATUS" property="printStatus" />
        <result column="PK_CORP" property="pkCorp" />
        <result column="TOTAL_NUM" property="totalNum" />
        <result column="ORDER_CODE" property="orderCode" />
        <result column="RETURN_REASON" property="returnReason" />
        <result column="REMARK" property="remark" />
        <result column="DEPT_NAME" property="deptName" />
        <result column="MANAGER_NAME" property="managerName" />
        <result column="CUBASDOC_NAME" property="cubasdocName" />
        <result column="STOREHOUSE_NAME" property="storehouseName" />
        <result column="CORP_NAME" property="corpName" />
        <collection property="icPurchaseInbList" ofType="com.hzs.common.domain.scm.purchase.ext.IcPurchaseInBExt">
            <result column="PK_ITEM_ID" property="pkId"/>
            <result column="PK_PO_ORDER" property="pkPoOrder" />
            <result column="PK_PO_ORDER_ITEMS" property="pkPoOrderItems" />
            <result column="PK_SOURCE" property="pkSource" />
            <result column="PK_SOURCE_B" property="pkSourceB" />
            <result column="PK_PURCHASE" property="pkPurchase" />
            <result column="PK_UNIT" property="pkUnit" />
            <result column="PK_CLOCATION" property="pkClocation" />
            <result column="PK_PRODUCT_BAS" property="pkProductBas" />
            <result column="PK_PRODUCT" property="pkProduct" />
            <result column="BIZ_DATE" property="bizDate" />
            <result column="B_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="IS_GIFT" property="isGift" />
            <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" />
        </collection>
    </resultMap>
    <update id="mergePurchaseInExt">
        merge into ic_purchasein_h ib
        using(
        select pk_purchase,(case when sum(nassistnum)=0 then 7 else 6 end) order_status from(
        <foreach collection="icPurchaseInbList" item="item" index="index" separator=" union ">
            select pk_purchase,nassistnum from ic_purchasein_b
            where pk_purchase = #{item.pkSource} and del_flag=0
        </foreach>)
        group by pk_purchase
        ) b
        on (ib.pk_id=b.pk_purchase)
        when matched then
        update set ib.order_status=b.order_status,pk_modified=#{userId},modified_time=sysdate
    </update>

    <select id="queryPurchaseInhExtById" resultMap="IcPurchaseInHExt">
        select ih.*,ib.pk_id pk_item_id,ib.pk_po_order,ib.pk_po_order_items,ib.pk_purchase,ib.pk_unit,ib.pk_clocation,
               ib.pk_product_bas,ib.pk_product,ib.biz_date,ib.pk_storehouse b_pk_storehouse,ib.pk_rate,ib.nnum,
               ib.norigprice,ib.ntaxrate,ib.norigtaxprice,ib.norigmny,ib.ntax,ib.norigtaxmny,
               ib.pk_batch_code,ib.is_gift from ic_purchasein_h ih
           inner join ic_purchasein_b ib
                      on ih.pk_id=ib.pk_purchase
        where ih.del_flag=0 and ib.del_flag=0 and ih.pk_id=#{pkId}
         <if test="pkCountry != null">
             and ih.pk_country=#{pkCountry}
         </if>
         <if test="orderStatus != null">
             and ih.order_status = #{orderStatus}
         </if>
    </select>
    <select id="queryDisPurchaseInhExtByCondition" resultMap="IcPurchaseInHExt">
        select ih.*,sd.dept_name,su.nick_name manager_name,bc.cust_name cubasdoc_name,sc.corp_name,
        ib.pk_id pk_item_id,ib.pk_po_order,ib.pk_po_order_items,ib.pk_purchase,ib.pk_unit,ib.pk_clocation,
               ib.pk_product_bas,ib.pk_product,ib.biz_date,ib.pk_storehouse b_pk_storehouse,ib.pk_rate,ib.nnum,
               ib.norigprice,ib.ntaxrate,ib.norigtaxprice,ib.norigmny,ib.ntax,ib.norigtaxmny,
               ib.pk_batch_code,ib.is_gift,bn.name unit_name,bh.name storehouse_name,ic.batch_code,
               bp.product_code,bp.product_name,sp.specs_name from ic_purchasein_h ih
          inner join ic_purchasein_b ib
                     on ih.pk_id=ib.pk_purchase
          left join ic_purchasein_h ibh
          on ib.pk_source=ibh.pk_id and ibh.del_flag=0
          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 bd_cubasdoc bc
                    on ih.pk_cubasdoc=bc.pk_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 (
            select pk_order,pk_order_items,wm_concat(specs_name) specs_name from scm_item_specs
            where del_flag=0 and order_type=2
            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="saOrderCode != null">
            and ibh.order_code = #{saOrderCode}
        </if>
        <if test="returnStatus != null">
            and ih.return_status = #{returnStatus}
        </if>
        <if test="orderStatus != null">
            and ih.order_status = #{orderStatus}
        </if>
        <if test="pkCubasdoc != null">
            and ih.pk_cubasdoc = #{pkCubasdoc}
        </if>
        <if test="pkCorp != null">
            and ih.pk_corp = #{pkCorp}
        </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="orderCode != null and orderCode != ''">
            and ih.order_code = #{orderCode}
        </if>
        <if test="productName != null and productName != ''">
            and bp.product_name=#{productName}
        </if>
        order by ih.creation_time desc
    </select>
    <select id="queryTotalPurchaseInhExtByCondition" resultMap="IcPurchaseInHExt">
        select ih.order_status,sd.dept_name,su.nick_name manager_name,bc.cust_name cubasdoc_name,ih.order_code,ih.order_date,sc.corp_name,ih.remark,
               bp.product_code,bp.product_name,sp.specs_name,bn.name unit_name,ic.batch_code,bh.name storehouse_name,
               ib.biz_date,ib.nnum,ib.norigprice,ib.ntaxrate,ib.norigtaxprice,ib.norigmny,ib.ntax,ib.norigtaxmny,ib.is_gift
        from ic_purchasein_h ih
                 inner join ic_purchasein_b ib
                            on ih.pk_id=ib.pk_purchase
                 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 bd_cubasdoc bc
                           on ih.pk_cubasdoc=bc.pk_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 (
            select pk_order,pk_order_items,wm_concat(specs_name) specs_name from scm_item_specs
            where del_flag=0 and order_type=2
            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 and ih.order_status>=3
        <if test="pkCountry != null">
            and ih.pk_country=#{pkCountry}
        </if>
        <if test="returnStatus != null">
            and ih.return_status = #{returnStatus}
        </if>
        <if test="pkCubasdoc != null">
            and ih.pk_cubasdoc = #{pkCubasdoc}
        </if>
        <if test="pkCorp != null">
            and ih.pk_corp = #{pkCorp}
        </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="orderCode != null and orderCode != ''">
            and ih.order_code like #{orderCode} || '%'
        </if>
        <if test="productName != null and productName != ''">
            and bp.product_name like #{productName} || '%'
        </if>
        <if test="productCode != null and productCode != ''">
            and bp.product_code like #{productCode} || '%'
        </if>
        <if test="specsName != null and specsName != ''">
            and sp.specs_name like #{specsName} || '%'
        </if>
        <if test="isGift != null">
            and ib.is_gift = #{isGift}
        </if>
        <if test="pkStorehouse != null">
            and ib.pk_storehouse = #{pkStorehouse}
        </if>
        order by ih.order_date,ib.pk_product,ib.pk_batch_code
    </select>

</mapper>
