<?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.io.mapper.IoOrderItemsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="IoOrderItemsExt" type="com.hzs.common.domain.scm.io.ext.IoOrderItemsExt">
        <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_ORDER" property="pkOrder" />
        <result column="PK_PRODUCT_BAS" property="pkProductBas" />
        <result column="PK_STOREHOUSE" property="pkStorehouse" />
        <result column="PK_UNIT" property="pkUnit" />
        <result column="PK_RATE" property="pkRate" />
        <result column="NNUM" property="nnum" />
        <result column="PK_PRODUCT" property="pkProduct" />
        <result column="PK_SUPPLIER" property="pkSupplier" />
        <result column="SHIPPING_CHANNEL" property="shippingChannel" />
        <result column="SUPPLY_PRICE" property="supplyPrice" />
        <result column="SPECS_NAME_ID" property="specsNameId" />
        <result column="SPECS_NAME" property="specsName" />
        <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>
    <delete id="deleteOrderItemsService">
        update io_order_items set del_flag = 1,pk_modified=#{pkMember},modified_time = sysdate where del_flag=0 and
        pk_id in
        <foreach collection="ioOrderItemsList" item="item" open="(" close=")" separator=",">
            #{item.pkId}
        </foreach>
    </delete>
    <select id="queryListIoOrderItemsByPk" resultMap="IoOrderItemsExt">
        select pt.pk_id,pt.pk_order,pt.pk_product_bas,pt.pk_storehouse,bh.name storehouse_name,pt.pk_unit,pt.pk_rate,
               pt.nnum,bn.name unit_name,bd.pk_id pk_product,bp.product_code,bp.product_name,sp.specs_name,sps.specs_name_id,
               se.pk_specs_type,se.pk_specs,se.specs_name item_specs_name,bd.shipping_channel from io_order_items pt
             left join bd_product_bas bp
                       on pt.pk_product_bas=bp.pk_id and bp.del_flag=0
             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_storehouse=bh.pk_id and bh.del_flag=0
             left join bd_unit bn
               on pt.pk_unit = bn.pk_id and bn.del_flag=0
             left join scm_item_specs se
                       on se.pk_order_items=pt.pk_id and se.order_type=11 and se.del_flag=0
             left join (
            select pk_order_items,max(r) specs_name from
                (select pk_order_items,wm_concat(specs_name) over (partition by pk_order_items order by pk_specs) r
                 from scm_item_specs
                 where del_flag=0 and order_type=11)
            group by pk_order_items
             ) sp
             on sp.pk_order_items=pt.pk_id
             left join (
            select pk_order_items,max(r) specs_name_id from
                (select pk_order_items,wm_concat(pk_specs) over (partition by pk_order_items order by pk_specs) r
                 from scm_item_specs
                 where del_flag=0 and order_type=11)
            group by pk_order_items
            ) sps
                       on sps.pk_order_items=pt.pk_id
        where pt.del_flag=0 and pt.pk_country = #{pkCountry}
          and pt.pk_order = #{pkOrder}
    </select>


</mapper>
