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

    <!-- 查询HIS住院账单明细 -->
    <select id="findHisInpatientDetail" parameterType="HashMap" resultType="HashMap">
        select HisOperCode,
               PayMethod,
               TradingStatus,
               BizType,
               PayType,
               TradeTime,
               Amount,
               PlatformTransId,
               HisTransId,
               PatientId as PATIENTID,
               PatientName,
               trade_date
        from hisbill_history
        <where>
            PayMethod = '2'  <!-- 住院账单 -->
            and PayType != '5'  <!-- 排除现金支付 -->
            and PayType != '3'  <!-- 排除军保支付(医院垫支) -->
            and PayType != '7'  <!-- 排除预交金 -->
            and PayType != '9'  <!-- 排除统筹支付 -->
            and PayType != '10' <!-- 排除账户支付 -->
            and PayType != '8'
            and PayType != '11'
            <if test="military_payment_code != null and military_payment_code != ''">
                and PayType != #{military_payment_code}
            </if>
            <if test="check_payment_code != null and check_payment_code != ''">
                and PayType != #{check_payment_code}
            </if>
            <if test="startTime != null and startTime != ''">
                and trade_date &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and trade_date &lt;= #{endTime}
            </if>
            <if test="payType != null and payType != ''">
                and PayType = #{payType}
            </if>
            <if test="bizType != null and bizType != ''">
                and BizType = #{bizType}
            </if>
            <if test="likeFiled != null and likeFiled != ''">
                and (PlatformTransId like concat('%', #{likeFiled}, '%')
                or HisTransId like concat('%', #{likeFiled}, '%')
                or PatientId like concat('%', #{likeFiled}, '%')
                or PatientName like concat('%', #{likeFiled}, '%'))
            </if>
        </where>
        order by trade_date desc, TradeTime desc
    </select>

    <!-- 查询银行住院账单明细 -->
    <select id="findBankInpatientDetail" parameterType="HashMap" resultType="HashMap">
        select C_JYRQ,
               C_JYSJ,
               C_QSRQ,
               C_LSH,
               C_SHDDH,
               C_YSDDH,
               C_JYLX,
               C_CARD,
               C_FKH,
               C_JYJE,
               C_QSJE,
               C_SXF,
               C_SJZFJE,
               C_ZDH,
               C_ZFFS,
               C_KLX
        from bankbill_history
        <where>
            is_inpatient = '1'  <!-- 住院订单 -->
            <if test="startTime != null and startTime != ''">
                and C_JYRQ &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and C_JYRQ &lt;= #{endTime}
            </if>
            <if test="c_zffs != null and c_zffs != ''">
                and C_ZFFS = #{c_zffs}
            </if>
            <if test="likeFiled != null and likeFiled != ''">
                and (C_YSDDH like concat('%', #{likeFiled}, '%')
                or C_SHDDH like concat('%', #{likeFiled}, '%')
                or C_LSH like concat('%', #{likeFiled}, '%'))
            </if>
        </where>
        order by C_JYRQ desc, C_JYSJ desc
    </select>

    <!-- 查询每日汇总数据（按日期汇总 HIS 与 银行住院账单，各一条记录） -->
    <select id="findDailySummary" parameterType="HashMap" resultType="HashMap">
        select
            t.TRADE_DATE,
            sum(t.HIS_COUNT)   as HIS_COUNT,
            sum(t.HIS_AMOUNT)  as HIS_AMOUNT,
            sum(t.BANK_COUNT)  as BANK_COUNT,
            sum(t.BANK_AMOUNT) as BANK_AMOUNT
        from (
            -- HIS 住院汇总
            select
                trade_date                                        as TRADE_DATE,
                count(distinct PlatformTransId)                   as HIS_COUNT,
                ifnull(sum(cast(Amount as decimal(18,2))), 0)     as HIS_AMOUNT,
                0                                                 as BANK_COUNT,
                0                                                 as BANK_AMOUNT
            from hisbill_history
            where PayMethod = '2'
              and PayType != '5'  <!-- 排除现金支付 -->
              and PayType != '3'  <!-- 排除军保支付(医院垫支) -->
              and PayType != '7'  <!-- 排除预交金 -->
              and PayType != '9'  <!-- 排除统筹支付 -->
              and PayType != '10' <!-- 排除账户支付 -->
            <if test="military_payment_code != null and military_payment_code != ''">
                and PayType != #{military_payment_code}
            </if>
            <if test="check_payment_code != null and check_payment_code != ''">
                and PayType != #{check_payment_code}
            </if>
            <if test="startTime != null and startTime != ''">
                and trade_date &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and trade_date &lt;= #{endTime}
            </if>
            group by trade_date

            union all

            -- 银行住院汇总
            select
                C_JYRQ                                             as TRADE_DATE,
                0                                                  as HIS_COUNT,
                0                                                  as HIS_AMOUNT,
                count(1)                                           as BANK_COUNT,
                ifnull(sum(cast(C_JYJE as decimal(18,2))), 0)      as BANK_AMOUNT
            from bankbill_history
            where is_inpatient = '1'
            <if test="startTime != null and startTime != ''">
                and C_JYRQ &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and C_JYRQ &lt;= #{endTime}
            </if>
            group by C_JYRQ
        ) t
        group by t.TRADE_DATE
        order by t.TRADE_DATE desc
    </select>

</mapper>
