<?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.report.member.mapper.CuMemberPerkDetailOrderMapper">

    <!-- 订单 + 详情返回映射 -->
    <resultMap id="saOrderExtResultMap" type="com.hzs.common.domain.sale.ext.SaOrderExt">
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="ORDER_CODE" property="orderCode"/>
        <result column="ORDER_TYPE" property="orderType"/>
        <result column="ORDER_AMOUNT" property="orderAmount"/>
        <result column="ORDER_ACHIEVE" property="orderAchieve"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <collection property="orderItemsExtList" ofType="com.hzs.common.domain.sale.ext.SaOrderItemsExt">
            <result column="PRODUCT_CODE" property="productCode"/>
            <result column="PRODUCT_NAME" property="productName"/>
            <result column="QUANTITY" property="quantity"/>
            <result column="SPECS_NAME" property="specsName"/>
        </collection>
    </resultMap>

    <!-- 查询明细订单列表 -->
    <select id="queryOrderList" resultMap="saOrderExtResultMap">
        select cm.member_code,
               cm.member_name,
               so.order_code,
               so.order_type,
               so.order_amount,
               so.order_achieve,
               so.del_flag,
               bp.product_code,
               bp.product_name,
               soi.quantity,
               soi.specs_name
        from sa_order so
                 left join cu_member cm
                           on cm.pk_id = so.pk_member
                 left join sa_order_items soi
                           on soi.pk_order = so.pk_id
                 left join bd_product bp
                           on bp.pk_id = soi.pk_product
                 left join bd_wares bw
                           on bw.pk_id = soi.pk_wares
        where so.pk_id in (
            select cmpdo.pk_order
            from CU_MEMBER_PERK_DETAIL_ORDER cmpdo
            where cmpdo.pk_member_perk = #{pkMemberPerk}
            minus
        select cmpdo.pk_order
        from cu_member_perk_detail_order cmpdo
        where cmpdo.pk_member_perk in
              (select cmpdm.distinct_pk_member_perk
               from CU_MEMBER_PERK_DETAIL_MEMBER cmpdm
               where cmpdm.pk_member_perk = #{pkMemberPerk}) )
   and so.order_status = 1
        order by so.pay_time desc
    </select>

</mapper>
