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

    <!-- 通用查询映射结果 -->
    <resultMap id="PoOrderExt" type="com.hzs.common.domain.scm.po.ext.PoOrderExt">
        <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_SALESMAN" property="pkSalesman" />
        <result column="PK_RATE" property="pkRate" />
        <result column="ORDER_STATUS" property="orderStatus" />
        <result column="PRINT_STATUS" property="printStatus" />
        <result column="PK_CUBASDOC" property="pkCubasdoc" />
        <result column="PK_CORP" property="pkCorp" />
        <result column="TOTAL_NUM" property="totalNum" />
        <result column="TOTAL_ORIGMNY" property="totalOrigmny" />
        <result column="ORDER_CODE" property="orderCode" />
        <result column="RETURN_STATUS" property="returnStatus" />
        <result column="ORDER_DATE" property="orderDate" />
        <result column="CONTRACT_CODE" property="contractCode" />
        <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="CORP_NAME" property="corpName" />
        <collection property="poOrderItemsExtList" ofType="com.hzs.common.domain.scm.po.ext.PoOrderItemsExt">
            <result column="PK_ITEMS" property="pkId" />
            <result column="PK_ORDER" property="pkOrder" />
            <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="ITEM_PK_CREATOR" property="pkCreator" />
            <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="UNIT_NAME" property="unitName" />
            <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" />
        </collection>
    </resultMap>
    <update id="updatePoOrderStatusByPurchase">
        update po_order set ORDER_STATUS= #{orderStatus},MODIFIED_TIME=sysdate,PK_MODIFIED=#{pkMember} where pk_id in(
            select pk_order from(
            select pk_order, sum(case when NNUM>NACCUMSTORENUM then 1 else 0 end) as NACCUMSTORENUM from po_order_items
            where del_flag=0 and pk_order in
                <foreach collection="purchaseInbExtList" item="item" open="(" close=")" separator=",">
                    #{item.pkPoOrder}
                </foreach>
            group by pk_order
            )
            where NACCUMSTORENUM =0)
    </update>

    <select id="queryPoOrderById" resultMap="PoOrderExt">
        select po.*,pt.pk_id pk_items,pt.pk_product_bas,pt.pk_storehouse,pt.close_date,pt.plan_date,pt.pk_unit,
               pt.nnum,pt.norigprice,pt.ntaxrate,pt.norigtaxprice,pt.norigmny,pt.ntax,pt.norigtaxmny,
               pt.naccumstorenum,pt.nbackarrvnum,pt.is_gift,pt.pk_creator item_pk_creator from po_order po
               inner join po_order_items pt
                          on po.pk_id=pt.pk_order
        where po.del_flag=0 and pt.del_flag=0
        and po.pk_id=#{pkOrder}
        <if test="orderStatus != null">
            and po.order_status = #{orderStatus}
        </if>
    </select>
    <select id="queryPoOrderByCondition" resultMap="PoOrderExt">
        select po.*,sd.dept_name,su.nick_name manager_name,bc.cust_name cubasdoc_name,sc.corp_name,
        pi.pk_id pk_items,pi.pk_unit,pi.pk_product_bas,pi.pk_storehouse,pi.nnum,pi.plan_date,pi.close_date,
        pi.norigprice,pi.ntaxrate,pi.norigtaxprice,pi.norigmny,pi.ntax,pi.norigtaxmny,
        pi.is_gift,bn.name unit_name,bh.name storehouse_name,
        bp.product_code,bp.product_name,sp.specs_name from po_order po
        inner join po_order_items pi
        on po.pk_id=pi.pk_order
        left join sys_dept sd
        on po.pk_dept=sd.dept_id
        left join sys_user su
        on po.pk_salesman=su.user_id
        left join bd_cubasdoc bc
        on po.pk_cubasdoc=bc.pk_id
        left join sys_corp sc
        on po.pk_corp=sc.pk_id
        left join bd_unit bn
        on pi.pk_unit = bn.pk_id
        left join bd_storehouse bh
        on pi.pk_storehouse=bh.pk_id
        left join bd_product_bas bp
        on pi.pk_product_bas=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=1
        group by pk_order,pk_order_items) sp
        on pi.pk_id=sp.pk_order_items
        where po.del_flag=0 and pi.del_flag=0
        <if test="orderCode != null and orderCode != ''">
            and po.order_code = #{orderCode}
        </if>
        <if test="pkCountry != null">
            and po.pk_country=#{pkCountry}
        </if>
        <if test="pkId != null">
            and po.pk_id=#{pkId}
        </if>
        <if test="orderStatus != null">
            and po.order_status = #{orderStatus}
        </if>
        <if test="pkCubasdoc != null">
            and po.pk_cubasdoc = #{pkCubasdoc}
        </if>
        <if test="pkCorp != null">
            and po.pk_corp = #{pkCorp}
        </if>
        <if test="startDate != null">
            and po.order_date >= #{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and po.order_date &lt; #{endDate, jdbcType=DATE}
        </if>
        <if test="productName != null and productName != ''">
            and bp.product_name=#{productName}
        </if>
        order by po.creation_time desc
    </select>
    <select id="queryPoOrderByOrderItems" resultMap="PoOrderExt">
        <foreach collection="orderItemsParamList" item="item" index="index" separator=" union ">
        select po.*,pt.pk_id pk_items,pt.pk_product_bas,pt.pk_storehouse,pt.close_date,pt.plan_date,pt.pk_unit,
               pt.nnum,pt.norigprice,pt.ntaxrate,pt.norigtaxprice,pt.norigmny,pt.ntax,pt.norigtaxmny,
               pt.naccumstorenum,pt.nbackarrvnum,pt.is_gift,pt.pk_creator item_pk_creator from po_order po
               inner join po_order_items pt
              on po.pk_id=pt.pk_order
        where po.del_flag=0 and pt.del_flag=0
          and po.pk_id=#{item.pkOrder} and pt.pk_id=#{item.pkId} and #{item.nnum} &lt;=pt.nnum-pt.naccumstorenum
          and pt.pk_product_bas=#{item.pkProductBas}
          and po.order_status >= 3
        </foreach>
    </select>

    <select id="queryPoOrderToStoreByCondition" resultMap="PoOrderExt">
        select po.pk_id,po.pk_dept,po.pk_salesman,po.pk_rate,po.order_status,po.pk_cubasdoc,
        po.pk_corp,po.total_num,po.total_origmny,po.order_code,po.order_date,po.contract_code,po.remark,
        sd.dept_name,su.nick_name manager_name,bc.cust_name cubasdoc_name,sc.corp_name,bn.name unit_name,
        pt.pk_id pk_items,pt.pk_order,pt.pk_product_bas,pt.pk_storehouse,bh.name storehouse_name,pt.plan_date,pt.pk_unit,
        pt.nnum,pt.norigprice,pt.ntaxrate,pt.norigtaxprice,pt.norigmny,pt.ntax,pt.norigtaxmny,pt.is_gift,pt.close_date,
        bd.pk_id pk_product,bd.product_code,bd.product_name,sp.specs_name from po_order po
        inner join po_order_items pt
        on po.pk_id=pt.pk_order
        inner join bd_product_bas bp
        on pt.pk_product_bas=bp.pk_id
        inner 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 sys_dept sd
        on po.pk_dept=sd.dept_id
        left join sys_user su
        on po.pk_salesman=su.user_id
        left join bd_cubasdoc bc
        on po.pk_cubasdoc=bc.pk_id
        left join sys_corp sc
        on po.pk_corp=sc.pk_id
        left join bd_unit bn
        on pt.pk_unit = bn.pk_id
        left join bd_storehouse bh
        on pt.pk_storehouse=bh.pk_id
        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 po.del_flag=0
        and po.pk_country=#{pkCountry}
        and 0 &lt; pt.nnum-pt.naccumstorenum
        <if test="pkCubasdoc != null">
            and po.pk_cubasdoc = #{pkCubasdoc}
        </if>
        <if test="orderStatus != null">
            and po.order_status = #{orderStatus}
        </if>
        <if test="pkCorp != null">
            and po.pk_corp = #{pkCorp}
        </if>
        <if test="startDate != null">
            and po.order_date >= #{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and po.order_date &lt; #{endDate, jdbcType=DATE}
        </if>
        <if test="orderCode != null and orderCode != ''">
            and po.order_code = #{orderCode}
        </if>
        and po.order_status =3
    </select>
</mapper>
