<?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.ylx.massage.mapper.ProductOrderInfoMapper">

    <resultMap id="ProductOrderDetailVoResult" type="com.ylx.massage.domain.vo.ProductOrderDetailVo">
        <id property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="openId" column="open_id"/>
        <result property="orderStatus" column="order_status"/>
        <result property="payStatus" column="pay_status"/>
        <result property="payType" column="pay_type"/>
        <result property="payTime" column="pay_time"/>
        <result property="payExpireTime" column="pay_expire_time"/>
        <result property="totalAmount" column="total_amount"/>
        <result property="discountAmount" column="discount_amount"/>
        <result property="freightAmount" column="freight_amount"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="pointsUsed" column="points_used"/>
        <result property="addressId" column="address_id"/>
        <result property="deliveryType" column="delivery_type"/>
        <result property="logisticsCompany" column="logistics_company"/>
        <result property="logisticsNo" column="logistics_no"/>
        <result property="deliveryTime" column="delivery_time"/>
        <result property="receiveTime" column="receive_time"/>
        <result property="buyerRemark" column="buyer_remark"/>
        <result property="createTime" column="create_time"/>
        <result property="productId" column="product_id"/>
        <result property="productName" column="product_name"/>
        <result property="skuSpec" column="sku_spec"/>
        <result property="skuImage" column="sku_image"/>
        <result property="receiverName" column="user_name"/>
        <result property="receiverName" column="user_name"/>
        <result property="receiverPhone" column="user_phone"/>
        <result property="addressName" column="user_address"/>
    </resultMap>

    <resultMap id="ProductOrderListVoResult" type="com.ylx.massage.domain.vo.ProductOrderListVo">
        <id property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="orderStatus" column="order_status"/>
        <result property="payStatus" column="pay_status"/>
        <result property="totalAmount" column="total_amount"/>
        <result property="createTime" column="create_time"/>
        <result property="productId" column="product_id"/>
        <result property="productName" column="product_name"/>
        <result property="paymentType" column="payment_type"/>
        <result property="skuSpec" column="sku_spec"/>
        <result property="pointsUsed" column="points_used"/>
    </resultMap>

    <!-- 分页查询用户订单列表 -->
    <select id="selectProductOrderListByOpenId" resultMap="ProductOrderListVoResult">
        SELECT
            poi.id,
            poi.order_no,
            poi.order_status,
            poi.pay_status,
            poi.total_amount,
            poi.points_used,
            poi.create_time,
            poit.product_id,
            poit.product_name,
            p.payment_type,
            poit.sku_spec
        FROM
            product_order_info poi
                LEFT JOIN product_order_item poit ON poi.id = poit.order_id
                LEFT JOIN product p ON poit.product_id = p.id
        WHERE
            poi.open_id = #{openId}
        ORDER BY
            poi.create_time DESC
    </select>

    <!-- 查询订单详情 -->
    <select id="selectProductOrderDetailByOrderNoAndOpenId" resultMap="ProductOrderDetailVoResult">
        SELECT
            poi.id,
            poi.order_no,
            poi.open_id,
            poi.order_status,
            poi.pay_status,
            poi.pay_type,
            poi.pay_time,
            poi.pay_expire_time,
            poi.total_amount,
            poi.discount_amount,
            poi.freight_amount,
            poi.pay_amount,
            poi.points_used,
            poi.address_id,
            poi.user_name,
            poi.user_phone,
            poi.user_address,
            poi.delivery_type,
            poi.logistics_company,
            poi.logistics_no,
            poi.delivery_time,
            poi.receive_time,
            poi.buyer_remark,
            poi.create_time,
            poi2.product_id,
            poi2.product_name,
            poi2.sku_spec,
            poi2.sku_image
        FROM product_order_info poi left join product_order_item poi2 on poi.id = poi2.order_id
        WHERE poi.order_no = #{orderNo} AND poi.open_id = #{openId}
    </select>

    <select id="adminPage" resultType="com.ylx.massage.domain.vo.ProductOrderPageItemVo">
        SELECT
            oi.id,
            oi.order_no AS orderNo,
            oi.order_status AS orderStatus,
            oi.pay_status AS payStatus,
            oi.points_used AS pointsUsed,
            oi.logistics_no AS logisticsNo,
            oi.delivery_time AS deliveryTime,
            oi.create_time AS createTime,
            item.product_id,
            item.product_name AS productName,
            item.quantity AS quantity,
            item.price AS price,
            item.price AS payAmount,
            oi.open_id,
            u.c_nick_name AS nickName,
            u.c_phone AS phone,
            p.payment_type AS paymentType
        FROM
            product_order_info oi
        LEFT JOIN product_order_item item ON oi.id = item.order_id
        LEFT JOIN t_wx_user u ON oi.open_id = u.c_openid
        LEFT JOIN product p ON p.id = item.product_id
        WHERE
            oi.is_deleted = 0
        <!-- 查询条件 -->
        <if test="dto.orderNo != null and dto.orderNo != ''">
            AND oi.order_no LIKE CONCAT('%', #{dto.orderNo}, '%')
        </if>
        <if test="dto.logisticsNo != null and dto.logisticsNo != ''">
            AND oi.logistics_no LIKE CONCAT('%', #{dto.logisticsNo}, '%')
        </if>
        <!-- 根据需求：1：待发货 2：待收货 3：已收货 4：售后中 -->
        <if test="dto.orderStatus == null">
            AND oi.order_status IN (1, 2, 3, 4)
        </if>
        <if test="dto.orderStatus != null">
            AND oi.order_status = #{dto.orderStatus}
        </if>
        <if test="dto.payStatus != null">
            AND oi.pay_status = #{dto.payStatus}
        </if>
        <if test="dto.startTime != null and dto.endTime != null">
            AND oi.create_time BETWEEN #{dto.startTime} AND #{dto.endTime}
        </if>

        ORDER BY oi.create_time DESC
    </select>

    <select id="getOrderTotalCount" resultType="com.ylx.massage.domain.vo.ProductOrderCountVo">
        SELECT
            COUNT(CASE WHEN order_status = 1 THEN 1 END) AS wait_delivery_count,
            COUNT(CASE WHEN order_status IN (2,3)
                AND NOT EXISTS (SELECT 1 FROM after_sale_order a
                                WHERE a.order_no = o.order_no
                                  AND a.is_deleted=0
                                  AND a.after_sale_status NOT IN(1,5,6))
                           THEN 1 END) AS processing_count,
            COUNT(DISTINCT CASE WHEN EXISTS (SELECT 1 FROM after_sale_order a
                                             WHERE a.order_no = o.order_no
                                               AND a.is_deleted=0
                                               AND a.after_sale_status IN(0,2,3,4))
                                    THEN o.order_no END) AS after_sale_ing_count
        FROM product_order_info o
        WHERE
            o.order_status IN (1,2,3,4)
            AND o.is_deleted = 0
    </select>

    <select id="getOrderBaseInfoById" resultType="com.ylx.massage.domain.vo.OrderBaseInfoVo">
        SELECT
            poi.id,
            poi.order_no,
            poi.order_status,
            poi.pay_status,
            item.product_name,
            item.product_id,
            item.quantity,
            poi.pay_amount AS payAmount,
            poi.points_used,
            p.payment_type,
            u.c_phone AS phone,
            poi.delivery_time,
            poi.create_time,
            poi.logistics_no,
            poi.buyer_remark,
            poi.address_id,
            poi.user_name,
            poi.user_phone,
            poi.user_address
        FROM
            product_order_info poi
        LEFT JOIN product_order_item item ON poi.id = item.order_id
        LEFT JOIN product p ON item.product_id = p.id
        LEFT JOIN t_wx_user u ON poi.open_id = u.c_openid
        WHERE
            poi.id = #{orderId}
        AND poi.is_deleted = 0
    </select>

</mapper>