<?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="shop.alien.mapper.LifeUserOrderMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="shop.alien.entity.store.vo.LifeUserOrderVo">
        <id column="id" property="id" />
        <result column="order_no" property="orderNo"/>
        <result column="created_time" property="createdTime"/>
        <result column="price" property="price"/>
        <result column="final_price" property="finalPrice"/>
        <result column="coupon_name" property="couponName"/>
        <result column="nominal_value" property="nominalValue"/>
        <result column="user_phone" property="userPhone"/>
        <result column="commission_rate" property="commissionRate"/>
        <result column="img_url" property="imgUrl"/>
        <result column="pay_method" property="payMethod"/>
        <result column="buy_time" property="buyTime"/>
        <result column="cancel_time" property="cancelTime"/>
        <result column="pay_time" property="payTime"/>
        <result column="finish_time" property="finishTime"/>
        <result column="coupon_type" property="couponType"/>
        <result column="coupon_id" property="couponId"/>
        <result column="offprice" property="offprice"/>
        <result column="store_id" property="storeId"/>
        <result column="store_name" property="storeName"/>
        <result column="commission_rate" property="commissionRate"/>
        <result column="img_url" property="imgUrl"/>
        <result column="user_name" property="userName"/>
        <result column="order_status" property="status"/>
        <result column="order_status_value" property="orderStatusValue"/>
        <result column="dist" property="dist"/>
        <result column="store_address" property="storeAddress"/>
        <result column="effective_date_type" property="effectiveDateType"/>
        <result column="effective_date_value" property="effectiveDateValue"/>
        <result column="use_rules" property="useRules"/>
        <result column="applicable_num" property="applicableNum"/>
        <result column="quota_type" property="quotaType"/>
        <result column="quota_value" property="quotaValue"/>
        <result column="store_tel" property="storeTel"/>
        <result column="quan_id" property="quanId"/>
        <result column="total_refund_time" property="totalRefundTime"/>
        <result column="business_status" property="businessStatus"/>
        <result column="type" property="type"/>
        <result column="store_type" property="storeType"/>
        <result column="store_status" property="storeStatus"/>
        <result column="business_type" property="businessType"/>
        <result column="collect_num" property="collectNum"/>
        <result column="business_date" property="businessDate"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="end_date" property="endDate"/>
        <result column="reservation_rules" property="reservationRules"/>
        <result column="original_price" property="originalPrice"/>
        <result column="order_str" property="orderStr"/>
        <result column="expert_order_id" property="expertOrderId"/>
        <result column="order_appraise" property="orderAppraise"/>
        <result column="user_id" property="userId"/>
        <result column="imgIds" property="imgIds"/>

        <collection property="orderCouponMiddleList" ofType="shop.alien.entity.store.vo.OrderCouponMiddleVo">
            <id column="ocmId" property="id" />
            <result column="status" property="status"/>
            <result column="coupon_code" property="couponCode"/>
            <result column="refund_time" property="refundTime"/>
            <result column="refund_reason" property="refundReason"/>
            <result column="refundPrice" property="price"/>
            <result column="used_time" property="usedTime"/>
        </collection>
    </resultMap>


    <!-- 查询用户订单详情 -->
    <select id="queryUserOrderDetail" resultMap="BaseResultMap">
        with total_coupon as(
        select id coupon_id,2 coupon_type,lgbm.group_name coupon_name,preferential_price offprice,original_price, SUBSTRING_INDEX(image_id, ',', 1) AS image_id, image_id AS imgIds,
               effective_date_type,effective_date_value,use_rules,reservation_rules,applicable_num,quota_type,quota_value,inventory_num single_qty
        from life_group_buy_main lgbm where lgbm.delete_flag = 0
        union all
        select id coupon_id,1 coupon_type,lc.name coupon_name,lc.offprice offprice,price original_price,SUBSTRING_INDEX(image_path, ',', 1) AS image_id, image_path AS imgIds,
        case when expiration_type = 1 then 0 else 1 end effective_date_type, case when expiration_type = 1 then expiration_date else validity_period end  effective_date_value,use_rule use_rules,reservation_rules,applicable_num,1 quota_type,buy_limit quota_value,single_qty
        from life_coupon lc where lc.delete_flag = 0
        )
        select luo.id,luo.order_no,luo.created_time , luo.price ,
        luo.final_price,
        luo.order_str,
        luo.pay_method,luo.buy_time,luo.cancel_time,luo.pay_time,luo.finish_time,luo.status order_status,
        CASE luo.status
        WHEN 0 THEN '待支付'
        WHEN 1 THEN '已支付/待使用'
        WHEN 2 THEN '已核销'
        WHEN 3 THEN '已过期'
        WHEN 4 THEN '已取消'
        WHEN 5 THEN '已退款'
        WHEN 6 THEN '退款失败'
        WHEN 7 THEN '已完成'
        END AS order_status_value,
        luo.quan_id,luo.refund_time total_refund_time,luo.expert_order_id,luo.order_appraise,
        tc.*,
        ldc.nominal_value,ldc.type,
        lu.user_phone,lu.user_name,lu.id user_id,
        ocm.id ocmId,ocm.status,ocm.coupon_code,ocm.refund_time,ocm.refund_reason,ocm.price refundPrice,
        si.id store_id,si.store_name,si.commission_rate,si.store_address,si.store_tel,si.business_status,si.collect_num,si.business_section store_type,si.store_status,
        su.phone store_user_tel,
        <if test="position != null and position != ''">
            ROUND(
            ST_Distance_Sphere(
            ST_GeomFromText(CONCAT('POINT(', REPLACE(#{position}, ',', ' '), ')')),
            ST_GeomFromText(CONCAT('POINT(', REPLACE(si.store_position, ',', ' '), ')'))
            ) / 1000,
            2
            ) AS dist,
        </if>
        simg.img_url,
        sbi.business_type,sbi.business_date,sbi.start_time,sbi.end_time
        from life_user_order luo
        left join store_info si on si.id = luo.store_id and si.delete_flag = 0
        left join order_coupon_middle ocm on ocm.order_id = luo.id and ocm.delete_flag = 0
        left join total_coupon tc on tc.coupon_id = ocm.coupon_id and tc.coupon_type = luo.coupon_type
        left join life_discount_coupon_user ldcu on ldcu.id = luo.quan_id and ldcu.delete_flag = 0
        left join life_discount_coupon ldc on ldc.id = ldcu.coupon_id and ldc.delete_flag = 0
        left join life_user lu on lu.id = luo.user_id and lu.delete_flag = 0
        left join store_img simg on simg.id = tc.image_id and simg.delete_flag = 0
        left join store_business_info sbi on sbi.store_id = si.id and sbi.delete_flag = 0
        left join store_user su on su.store_id = si.id and su.delete_flag = 0
        where luo.id = #{orderId}
        order by luo.created_time desc
    </select>

    <select id="selectRefundList" resultMap="BaseResultMap">
        with total_coupon as(
        select id coupon_id,2 coupon_type,lgbm.group_name coupon_name,preferential_price offprice,SUBSTRING_INDEX(image_id, ',', 1) AS image_id,
        effective_date_type,effective_date_value,use_rules,reservation_rules,applicable_num,quota_type,quota_value,inventory_num single_qty,end_time end_date
        from life_group_buy_main lgbm where lgbm.delete_flag = 0
        union all
        select id coupon_id,1 coupon_type,lc.name coupon_name,lc.offprice offprice,SUBSTRING_INDEX(image_path, ',', 1) AS image_id,
        case when expiration_type = 1 then 0 else 1 end effective_date_type,case when expiration_type = 1 then expiration_date else validity_period end  effective_date_value,use_rule use_rules,reservation_rules,applicable_num,1 quota_type,buy_limit quota_value,single_qty,end_date
        from life_coupon lc where lc.delete_flag = 0
        )
        select luo.id,luo.order_no,luo.created_time , luo.price ,luo.final_price
        ,luo.pay_method,luo.buy_time,luo.cancel_time,luo.pay_time,luo.finish_time,luo.status order_status,luo.quan_id,
        tc.*,
        ldc.nominal_value,
        lu.user_phone,lu.user_name,lu.id user_id,
        ocm.id ocmId,ocm.status,ocm.coupon_code,ocm.refund_time,ocm.refund_reason,ocm.price refundPrice,
        si.id store_id,si.store_name,si.commission_rate,si.store_address,si.store_tel,
        simg.img_url,
        sbi.business_type,sbi.business_date,sbi.start_time,sbi.end_time
        from life_user_order luo
        left join store_info si on si.id = luo.store_id and si.delete_flag = 0
        left join order_coupon_middle ocm on ocm.order_id = luo.id and ocm.delete_flag = 0
        inner join total_coupon tc on tc.coupon_id = ocm.coupon_id and tc.coupon_type = luo.coupon_type
        left join life_discount_coupon_user ldcu on ldcu.id = luo.quan_id and ldcu.delete_flag = 0
        left join life_discount_coupon ldc on ldc.id = ldcu.coupon_id and ldc.delete_flag = 0
        left join life_user lu on lu.id = luo.user_id and lu.delete_flag = 0
        left join store_img simg on simg.id = tc.image_id and simg.delete_flag = 0
        left join store_business_info sbi on sbi.store_id = si.id and sbi.delete_flag = 0
        <where>
            <!-- 引用 LambdaQueryWrapper 构建的条件 -->
            ${ew.sqlSegment}
        </where>
        order by luo.created_time desc
    </select>
</mapper>