<?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.giftCard.mapper.GiftCardOrderMapper">

    <select id="selectAdminGiftCardOrderPage" resultType="com.ylx.giftCard.domain.vo.GiftCardOrderPageVO">
        SELECT
            gco.id,
            gco.order_no AS orderId,
            gco.order_no AS orderNo,
            gco.gift_card_name AS giftCardName,
            gco.pay_amount AS payAmount,
            gco.user_name AS userName,
            gco.user_phone AS userPhone,
            gco.create_time AS orderTime,
            gco.commission_amount AS commissionAmount,
            mt.te_phone AS merchantAccount,
            gco.merchant_name AS merchantName,
            gco.merchant_nick_name AS merchantNickName
        FROM gift_card_order gco
        LEFT JOIN ma_technician mt ON mt.id = gco.merchant_id
        <where>
            <if test="dto.userName != null and dto.userName != ''">
                AND gco.user_name LIKE CONCAT('%', #{dto.userName}, '%')
            </if>
            <if test="dto.merchantName != null and dto.merchantName != ''">
                AND gco.merchant_name LIKE CONCAT('%', #{dto.merchantName}, '%')
            </if>
            <if test="dto.startTime != null and dto.startTime != ''">
                AND gco.create_time &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime != ''">
                AND gco.create_time &lt;= #{dto.endTime}
            </if>
        </where>
        ORDER BY
            gco.create_time DESC
    </select>

    <select id="selectAdminGiftCardOrderExportList" resultType="com.ylx.giftCard.domain.vo.GiftCardOrderExportVO">
        SELECT
            gco.id AS orderId,
            gco.gift_card_name AS giftCardName,
            gco.pay_amount AS payAmount,
            gco.user_name AS userName,
            gco.user_phone AS userPhone,
            gco.create_time AS orderTime,
            gco.commission_amount AS commissionAmount,
            mt.te_phone AS merchantAccount,
            gco.merchant_name AS merchantName,
            gco.merchant_nick_name AS merchantNickName
        FROM gift_card_order gco
        LEFT JOIN ma_technician mt ON mt.id = gco.merchant_id
        <where>
            <if test="dto.userName != null and dto.userName != ''">
                AND gco.user_name LIKE CONCAT('%', #{dto.userName}, '%')
            </if>
            <if test="dto.merchantName != null and dto.merchantName != ''">
                AND gco.merchant_name LIKE CONCAT('%', #{dto.merchantName}, '%')
            </if>
            <if test="dto.startTime != null and dto.startTime != ''">
                AND gco.create_time &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime != ''">
                AND gco.create_time &lt;= #{dto.endTime}
            </if>
        </where>
        ORDER BY
            gco.create_time DESC
    </select>

    <select id="selectPcPurchaseShoppingFundsDetail" resultType="com.ylx.giftCard.domain.vo.UserShoppingFundsDetailItemVO">
        SELECT
            gco.order_no AS orderNo,
            gco.gift_card_name AS giftCardName,
            gco.pay_amount AS orderAmount,
            gco.create_time AS orderTime
        FROM gift_card_order gco
        <where>
            gco.user_id = #{dto.userId}
            AND gco.status = 1
            <if test="dto.startTime != null and dto.startTime != ''">
                AND gco.create_time &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime != ''">
                AND gco.create_time &lt;= #{dto.endTime}
            </if>
        </where>
        ORDER BY
            gco.create_time DESC
    </select>

    <select id="selectPcPurchaseShoppingFundsSummary" resultType="com.ylx.giftCard.domain.vo.UserShoppingFundsSummaryVO">
        SELECT
            COALESCE(SUM(gco.pay_amount), 0) AS totalAmount,
            COUNT(1) AS totalCount
        FROM gift_card_order gco
        <where>
            gco.user_id = #{dto.userId}
            AND gco.status = 1
            <if test="dto.startTime != null and dto.startTime != ''">
                AND gco.create_time &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime != ''">
                AND gco.create_time &lt;= #{dto.endTime}
            </if>
        </where>
    </select>

</mapper>
