<?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.saye.hospitalgd.mapper.FinancialReconciliation.RefundStatisticsMapper">

    <!-- 查询退款统计报表数据 -->
    <select id="findRefundStatistics" parameterType="HashMap" resultType="HashMap">
        SELECT 
            <choose>
                <when test="dimension == 'time'">
                    DATE_FORMAT(trade_date, '%Y-%m-%d') as dimension_value,
                    '按日期' as dimension_name
                </when>
                <when test="dimension == 'payType'">
                    PayType as dimension_value,
                    '按支付方式' as dimension_name
                </when>
                <when test="dimension == 'refundType'">
                    TradingStatus as dimension_value,
                    '按退款类型' as dimension_name
                </when>
                <otherwise>
                    '全部' as dimension_value,
                    '全部' as dimension_name
                </otherwise>
            </choose>,
            COUNT(*) as refund_count,
            SUM(CAST(Amount AS DECIMAL(10,2))) as total_refund_amount,
            AVG(CAST(Amount AS DECIMAL(10,2))) as avg_refund_amount,
            MAX(CAST(Amount AS DECIMAL(10,2))) as max_refund_amount,
            MIN(CAST(Amount AS DECIMAL(10,2))) as min_refund_amount,
            COUNT(DISTINCT PatientId) as unique_patients,
            COUNT(DISTINCT HisOperCode) as unique_operators
        FROM hisbill_history 
        WHERE TradingStatus = '2'
        <if test="startTime != null and startTime != ''">
            AND trade_date >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND trade_date &lt;= #{endTime}
        </if>
        <if test="payType != null and payType != ''">
            AND PayType = #{payType}
        </if>
        <choose>
            <when test="dimension == 'time'">
                GROUP BY DATE_FORMAT(trade_date, '%Y-%m-%d')
                ORDER BY DATE_FORMAT(trade_date, '%Y-%m-%d') DESC
            </when>
            <when test="dimension == 'payType'">
                GROUP BY PayType
                ORDER BY total_refund_amount DESC
            </when>
            <when test="dimension == 'refundType'">
                GROUP BY TradingStatus
                ORDER BY total_refund_amount DESC
            </when>
        </choose>
    </select>

    <!-- 获取退款统计汇总数据 -->
    <select id="getRefundSummary" parameterType="HashMap" resultType="HashMap">
        SELECT 
            COUNT(*) as total_refund_count,
            SUM(CAST(Amount AS DECIMAL(10,2))) as total_refund_amount,
            AVG(CAST(Amount AS DECIMAL(10,2))) as avg_refund_amount,
            MAX(CAST(Amount AS DECIMAL(10,2))) as max_refund_amount,
            MIN(CAST(Amount AS DECIMAL(10,2))) as min_refund_amount,
            COUNT(DISTINCT PatientId) as unique_patients,
            COUNT(DISTINCT HisOperCode) as unique_operators,
            COUNT(DISTINCT PayType) as pay_type_count,
            COUNT(DISTINCT TradingStatus) as refund_type_count
        FROM hisbill_history 
        WHERE TradingStatus = '2'
        <if test="startTime != null and startTime != ''">
            AND trade_date >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND trade_date &lt;= #{endTime}
        </if>
        <if test="payType != null and payType != ''">
            AND PayType = #{payType}
        </if>
    </select>

    <!-- 获取退款趋势数据 -->
    <select id="getRefundTrend" parameterType="HashMap" resultType="HashMap">
        SELECT 
            DATE_FORMAT(trade_date, '%Y-%m-%d') as trend_date,
            COUNT(*) as daily_refund_count,
            SUM(CAST(Amount AS DECIMAL(10,2))) as daily_refund_amount,
            AVG(CAST(Amount AS DECIMAL(10,2))) as daily_avg_amount
        FROM hisbill_history 
        WHERE TradingStatus = '2'
        <if test="startTime != null and startTime != ''">
            AND trade_date >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND trade_date &lt;= #{endTime}
        </if>
        <if test="payType != null and payType != ''">
            AND PayType = #{payType}
        </if>
        GROUP BY DATE_FORMAT(trade_date, '%Y-%m-%d')
        ORDER BY DATE_FORMAT(trade_date, '%Y-%m-%d') ASC
    </select>

</mapper>
