<?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.TenpaySummaryMapper">

    <select id="findTenpayBankSummary" parameterType="HashMap" resultType="HashMap">
        SELECT '微信支付' AS PAY_NAME,
               COUNT(1) AS CNT,
               CAST(IFNULL(SUM(CAST(C_JYJE AS DECIMAL(19,2))),0) AS DECIMAL(19,2)) AS AMT
        FROM bankbill_history
        <where>
            <if test="startTime != null and startTime != ''">
                AND C_JYRQ &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND C_JYRQ &lt;= #{endTime}
            </if>
            AND bill_table_name in ('微信支付账单','微信医保账单')
            AND C_JYLX = '1'
        </where>
        UNION ALL
        SELECT '支付宝支付' AS PAY_NAME,
               0 AS CNT,
               CAST(0 AS DECIMAL(19,2)) AS AMT
        UNION ALL
        SELECT '刷卡支付' AS PAY_NAME,
               0 AS CNT,
               CAST(0 AS DECIMAL(19,2)) AS AMT
        UNION ALL
        SELECT '退款' AS PAY_NAME,
               COUNT(1) AS CNT,
               CAST(IFNULL(SUM(CAST(C_JYJE AS DECIMAL(19,2))),0) AS DECIMAL(19,2)) AS AMT
        FROM bankbill_history
        <where>
            <if test="startTime != null and startTime != ''">
                AND C_JYRQ &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND C_JYRQ &lt;= #{endTime}
            </if>
            AND bill_table_name in ('微信支付账单','微信医保账单')
            AND C_JYLX = '2'
        </where>
        UNION ALL
        SELECT '合计' AS PAY_NAME,
               COUNT(1) AS CNT,
               CAST(IFNULL(SUM(CAST(C_JYJE AS DECIMAL(19,2))),0) AS DECIMAL(19,2)) AS AMT
        FROM bankbill_history
        <where>
            <if test="startTime != null and startTime != ''">
                AND C_JYRQ &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND C_JYRQ &lt;= #{endTime}
            </if>
            AND bill_table_name in ('微信支付账单','微信医保账单')
            AND C_JYLX IN ('1','2')
        </where>
    </select>

    <select id="findTenpayHisSummary" parameterType="HashMap" resultType="HashMap">
        SELECT '微信支付' AS PAY_NAME,
               COUNT(1) AS CNT,
               CAST(IFNULL(SUM(CAST(Amount AS DECIMAL(19,2))),0) AS DECIMAL(19,2)) AS AMT
        FROM hisbill_history
        <where>
            <if test="startTime != null and startTime != ''">
                AND trade_date &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND trade_date &lt;= #{endTime}
            </if>
            AND LOWER(HisOperCode) in  ('wx' , 'yb')
            AND TradingStatus = '1'
            AND PayType in ('2','1')
        </where>
        UNION ALL
        SELECT '支付宝支付' AS PAY_NAME,
               0 AS CNT,
               CAST(0 AS DECIMAL(19,2)) AS AMT
        UNION ALL
        SELECT '刷卡支付' AS PAY_NAME,
               0 AS CNT,
               CAST(0 AS DECIMAL(19,2)) AS AMT
        UNION ALL
        SELECT '退款' AS PAY_NAME,
               COUNT(1) AS CNT,
               CAST(IFNULL(SUM(CAST(Amount AS DECIMAL(19,2))),0) AS DECIMAL(19,2)) AS AMT
        FROM hisbill_history
        <where>
            <if test="startTime != null and startTime != ''">
                AND trade_date &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND trade_date &lt;= #{endTime}
            </if>
            AND LOWER(HisOperCode) in  ('wx' , 'yb')
            AND TradingStatus = '2'
            AND PayType in ('2','1')
        </where>
        UNION ALL
        SELECT '合计' AS PAY_NAME,
               COUNT(1) AS CNT,
               CAST(IFNULL(SUM(CAST(Amount AS DECIMAL(19,2))),0) AS DECIMAL(19,2)) AS AMT
        FROM hisbill_history
        <where>
            <if test="startTime != null and startTime != ''">
                AND trade_date &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND trade_date &lt;= #{endTime}
            </if>
            AND LOWER(HisOperCode) in  ('wx' , 'yb')
            AND TradingStatus IN ('1','2')
            AND PayType in ('2','1')
        </where>
    </select>

</mapper>
