<?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.shopingfundsdetail.mapper.ShoppingFundsDetailMapper">
    <!-- C端购物金明细 -->
    <select id="queryShoppingFundsDetail" resultType="com.ylx.shopingfundsdetail.domain.vo.ShoppingFundsDetailQueryVo">
        select
            d.id AS id,
            d.user_id AS userId,
            d.expense_type AS expenseType,
            CASE
             WHEN d.expense_type = 0 THEN '收入'
             WHEN d.expense_type = 1 THEN '支出'
            ELSE '未知'
            END            AS expenseTypeName,
            d.amount AS amount,
            d.balance AS balance,
            DATE_FORMAT(d.create_time, '%c.%d %H:%i:%s') AS createTime
        from t_shopping_funds_detail d
        where 1=1
        <if test="dto.userId != null">
            and d.user_id = #{dto.userId}
        </if>
        <if test="dto.startTime != null and dto.endTime != null and dto.startTime != '' and dto.endTime != ''">
            and DATE_FORMAT(d.create_time, '%Y-%m-%d') >= #{dto.startTime}
            and DATE_FORMAT(d.create_time, '%Y-%m-%d') &lt;= #{dto.endTime}
        </if>
    </select>

    <!--查询支出的购物金明细-->
    <select id="selectPcExpenseShoppingFundsDetail" resultType="com.ylx.giftCard.domain.vo.UserShoppingFundsDetailItemVO">
        SELECT
            d.order_no AS orderNo,
            NULL AS giftCardName,
            -d.amount AS orderAmount,
            d.create_time AS orderTime
        FROM t_shopping_funds_detail d
        <where>
            d.user_id = #{dto.userId}
            AND d.expense_type = 1
            <if test="dto.startTime != null and dto.startTime != ''">
                AND d.create_time &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime != ''">
                AND d.create_time &lt;= #{dto.endTime}
            </if>
        </where>
        ORDER BY
            d.create_time DESC
    </select>

    <!--查询支出的购物金明细摘要-->
    <select id="selectPcExpenseShoppingFundsSummary" resultType="com.ylx.giftCard.domain.vo.UserShoppingFundsSummaryVO">
        SELECT
            COALESCE(SUM(d.amount), 0) AS totalAmount,
            COUNT(1) AS totalCount
        FROM t_shopping_funds_detail d
        <where>
            d.user_id = #{dto.userId}
            AND d.expense_type = 1
            <if test="dto.startTime != null and dto.startTime != ''">
                AND d.create_time &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime != ''">
                AND d.create_time &lt;= #{dto.endTime}
            </if>
        </where>
    </select>
</mapper>
