<?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.zsElectric.boot.business.mapper.UserOrderInfoMapper">

    <!-- 获取用户支付订单信息分页列表 -->
    <select id="getUserOrderInfoPage" resultType="com.zsElectric.boot.business.model.vo.UserOrderInfoVO">
        SELECT
        a.id,
        a.user_id,
        a.order_no,
        a.order_money,
        a.order_money - a.refund_money AS last_money,
        a.pay_time,
        a.level_id,
        a.out_trade_no,
        a.order_status,
        a.order_type,
        a.transaction_id,
        a.refund_money,
        a.refund_time,
        CASE
        WHEN a.remark IS NULL OR a.remark = '' THEN '无'
        ELSE a.remark
        END AS remark,
        a.version,
        a.create_time,
        a.update_time,
        a.is_deleted,
        b.phone
        FROM
        c_user_order_info a
        left join c_user_info b on a.user_id = b.id
        <where>a.is_deleted = 0
            <if test="queryParams.userId != null">
                AND a.user_id = #{queryParams.userId}
            </if>
            <if test="queryParams.orderStatus != null">
                AND a.order_status = #{queryParams.orderStatus}
            </if>
            <if test="queryParams.phone != null and queryParams.phone != ''">
                AND b.phone like concat('%',#{queryParams.phone},'%')
            </if>
            <if test="queryParams.orderNo != null">
                AND a.order_no = #{queryParams.orderNo}
            </if>
            <if test="queryParams.startTime != null">
                AND a.create_time <![CDATA[  >=  ]]> #{queryParams.startTime,jdbcType=DATE}
            </if>
            <if test="queryParams.endTime != null">
                AND a.create_time <![CDATA[  <=  ]]> #{queryParams.endTime,jdbcType=DATE}
            </if>
            <if test="queryParams.refundStartTime != null">
                AND a.refund_time <![CDATA[  >=  ]]> #{queryParams.refundStartTime,jdbcType=DATE}
            </if>
            <if test="queryParams.refundEndTime != null">
                AND a.refund_time <![CDATA[  <=  ]]> #{queryParams.refundEndTime,jdbcType=DATE}
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

    <!-- 获取用户支付订单信息导出列表 -->
    <select id="listExportUserOrderInfo" resultType="com.zsElectric.boot.business.model.dto.UserOrderInfoExportDTO">
        SELECT
        a.order_no,
        b.phone,
        a.order_money,
        a.order_money - a.refund_money AS last_money,
        a.pay_time,
        CASE a.order_status
        WHEN 1 THEN '待支付'
        WHEN 2 THEN '已支付'
        WHEN 3 THEN '已取消'
        WHEN 4 THEN '已退款'
        WHEN 5 THEN '部分退款'
        ELSE '未知'
        END AS order_status_name,
        CASE a.order_type
        WHEN 1 THEN '微信'
        WHEN 2 THEN '第三方'
        ELSE '未知'
        END AS order_type_name,
        a.out_trade_no,
        a.transaction_id,
        a.refund_money,
        a.refund_time,
        CASE
        WHEN a.remark IS NULL OR a.remark = '' THEN '无'
        ELSE a.remark
        END AS remark,
        a.create_time
        FROM
        c_user_order_info a
        LEFT JOIN c_user_info b ON a.user_id = b.id
        <where>
            a.is_deleted = 0
            <if test="queryParams.userId != null">
                AND a.user_id = #{queryParams.userId}
            </if>
            <if test="queryParams.orderStatus != null">
                AND a.order_status = #{queryParams.orderStatus}
            </if>
            <if test="queryParams.phone != null and queryParams.phone != ''">
                AND b.phone like concat('%',#{queryParams.phone},'%')
            </if>
            <if test="queryParams.orderNo != null">
                AND a.order_no = #{queryParams.orderNo}
            </if>
            <if test="queryParams.startTime != null">
                AND a.create_time <![CDATA[  >=  ]]> #{queryParams.startTime,jdbcType=DATE}
            </if>
            <if test="queryParams.endTime != null">
                AND a.create_time <![CDATA[  <=  ]]> #{queryParams.endTime,jdbcType=DATE}
            </if>
            <if test="queryParams.refundStartTime != null">
                AND a.refund_time <![CDATA[  >=  ]]> #{queryParams.refundStartTime,jdbcType=DATE}
            </if>
            <if test="queryParams.refundEndTime != null">
                AND a.refund_time <![CDATA[  <=  ]]> #{queryParams.refundEndTime,jdbcType=DATE}
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

</mapper>
