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

    <!--根据id查询记录-->
    <select id="findBankbillHistoryById" parameterType="HashMap" resultType="HashMap">
        select I_JYRQ
             , I_JYSJ
             , I_JZRQ
             , I_YHLSH
             , I_SHLSH
             , I_DDH
             , I_DDZT
             , I_FKFZH
             , I_FKFHM
             , I_DDJE
             , I_JYJE
             , I_SXF
             , I_JSJE
             , I_GTDM
             , I_FXK
             , I_ZFKZ
             , I_JYLX
             , I_QS

        from bankbills_history
        where I_DDH = #{i_ddh}
    </select>

    <!--批量插入银行交易记录-->
    <insert id="insertAllBankHistory" parameterType="java.util.List">
        insert into
        bankbill_history(C_QSRQ,C_JYRQ,C_JYSJ,C_ZDH,C_CARD,C_JYLX,C_JYJE,C_QSJE,C_SXF,C_SJZFJE,C_CKH,C_LSH,C_KLX,C_FKH,C_ZFFS,C_YSDDH,C_SHDDH,C_BZZD,C_QBYHJE,C_SHYHJE,C_YJYLSH,C_FQQS,C_FQSXF,C_FQFWF,C_FQFXF,C_QTYHJE,C_THDDH,C_FKFY,C_FDJC,C_ZDDH,bill_table_name)
        values
        <foreach collection="list" index="index" item="itm" separator=",">
            (
            #{itm.cQsrq},#{itm.cJyrq},#{itm.cJysj},#{itm.cZdh},#{itm.cCard},#{itm.cJylx},#{itm.cJyje},#{itm.cQsje},#{itm.cSxf},#{itm.cSjzfje},#{itm.cCkh},#{itm.cLsh},#{itm.cKlx}
            ,#{itm.cFkh},#{itm.cZffs},#{itm.cYsddh},#{itm.cShddh},#{itm.cBzzd},#{itm.cQbyhje},#{itm.cShyhje},#{itm.cYjylsh},#{itm.cFqqs},#{itm.cFqsxf},#{itm.cFqfwf},#{itm.cFqfxf},#{itm.cQtyhje},#{itm.cThddh},#{itm.cFkfy},#{itm.cFdjc},#{itm.cZddh},#{itm.billTableName}
            )
        </foreach>
    </insert>

    <select id="findBankbillHistoryList" 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
        ,C_JYLX
        ,C_FQQS


        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>
            <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('%',concat(#{likeFiled},'%'))
            </if>
        </where>
        order by C_JYRQ,C_JYSJ
    </select>

    <!--获取支付方式 -->
    <select id="findCzffs" resultType="HashMap">
        select distinct c_zffs
        from bankbill_history
    </select>

    <!--删除记录-->
    <delete id="deleteBankbillHistoryByParam" parameterType="HashMap">
        delete
        from bankbill_history
        where C_QSRQ = #{trade_date}
          and bill_table_name = #{bill_table_name}
    </delete>

    <delete id="deleteTempTable">
        delete
        from temp_bankbill_history
    </delete>

    <!--将记录添加到临时表 对账是用交易日期而非清算日期-->
    <insert id="findBankbillHistoryToTemp" parameterType="HashMap">
        insert into temp_bankbill_history
        select a.C_QSRQ,
               C_JYRQ,
               C_JYSJ,
               C_ZDH,
               C_CARD,
               C_JYLX,
               C_JYJE,
               C_QSJE,
               C_SXF,
               C_SJZFJE,
               C_CKH,
               C_LSH,
               C_KLX,
               C_FKH,
               C_ZFFS,
               a.C_YSDDH,
               C_SHDDH,
               C_BZZD,
               C_QBYHJE,
               C_SHYHJE,
               C_YJYLSH,
               C_FQQS,
               C_FQSXF,
               C_FQFWF,
               C_FQFXF,
               C_QTYHJE,
               C_THDDH,
               C_FKFY,
               C_FDJC,
               C_ZDDH,
               bill_table_name
        from bankbill_history a
                 left join (select C_YSDDH from bankbill_history where C_JYLX = '2' and C_JYRQ = #{trade_date}) b
                           on a.C_YSDDH = b.C_YSDDH
        where a.C_JYRQ = #{trade_date}
          and b.C_YSDDH is null
    </insert>

    <select id="findBankbillHistoryByParam" parameterType="HashMap" resultType="HashMap">
        select
        C_QSRQ,C_JYRQ,C_JYSJ,C_ZDH,C_CARD,C_JYLX,C_JYJE,C_QSJE,C_SXF,C_SJZFJE,C_CKH,C_LSH,C_KLX,C_FKH,C_ZFFS,C_YSDDH,C_SHDDH,C_BZZD,C_QBYHJE,C_SHYHJE,C_YJYLSH,C_FQQS,C_FQSXF,C_FQFWF,C_FQFXF,C_QTYHJE,C_THDDH,C_FKFY,C_FDJC,C_ZDDH,bill_table_name
        from bankbill_history
        where C_JYRQ = #{trade_date}
        <if test="c_ysddh!=null and c_ysddh!=''">
            and C_YSDDH=#{c_ysddh}
        </if>
        <if test="c_jylx!=null and c_jylx!=''">
            and C_JYLX=#{c_jylx}
        </if>

        <if test="orderby_je!=null and orderby_je!=''">
            order by C_JYJE desc
        </if>
    </select>

    <select id="findBankbillHistoryByTimeAndPlatformTransId" parameterType="HashMap" resultType="HashMap">
        select C_QSRQ,
               C_JYRQ,
               C_JYSJ,
               C_ZDH,
               C_CARD,
               C_JYLX,
               C_JYJE,
               C_QSJE,
               C_SXF,
               C_SJZFJE,
               C_CKH,
               C_LSH,
               C_KLX,
               C_FKH,
               C_ZFFS,
               C_YSDDH,
               C_SHDDH,
               C_BZZD,
               C_QBYHJE,
               C_SHYHJE,
               C_YJYLSH,
               C_FQQS,
               C_FQSXF,
               C_FQFWF,
               C_FQFXF,
               C_QTYHJE,
               C_THDDH,
               C_FKFY,
               C_FDJC,
               C_ZDDH,
               bill_table_name
        from bankbill_history
        where C_JYRQ = #{searchTime}
          and C_YSDDH = #{platformTransId}
    </select>

    <!--交易次数 此处查交易类型为收款的，也就是即使这笔交易退款也算交易次数-->
    <select id="findBankBillNumByTime" parameterType="HashMap" resultType="HashMap">
        select C_JYRQ, C_ZFFS, count(1) as num
        from bankbill_history
        where C_JYRQ &gt;= #{startTime}
          and C_JYRQ &lt;= #{endTime}
          and C_JYLX = '1'
        group by C_ZFFS, C_JYRQ
        order by C_ZFFS, C_JYRQ
    </select>

    <!--  交易金额 此处统计所有数据计算金额 因为存在退款是退余额的情况  -->
    <select id="findBankBillMoneyByTime" parameterType="HashMap" resultType="HashMap">
        select C_JYRQ, C_ZFFS, sum(cast(C_JYJE as decimal(18, 2))) as money
        from bankbill_history
        where C_JYRQ &gt;= #{startTime}
          and C_JYRQ &lt;= #{endTime}

        group by C_ZFFS, C_JYRQ
        order by C_ZFFS, C_JYRQ
    </select>

    <select id="findbankbillHistoryCount" parameterType="HashMap" resultType="HashMap">
        select count(1) as num
        from bankbill_history
        where C_JYRQ &gt;= #{startTime}
          and C_JYRQ &lt;= #{endTime}
    </select>

    <select id="findbankbillHistorySum" parameterType="HashMap" resultType="HashMap">
        select IFNULL(sum(C_JYJE), 0) as Money
        from bankbill_history
        where C_JYRQ &gt;= #{startTime}
          and C_JYRQ &lt;= #{endTime}
    </select>

    <insert id="insertBankbillOriginal" parameterType="HashMap">
        insert into
        bankbill_original(C_QSRQ,C_JYRQ,C_JYSJ,C_ZDH,C_CARD,C_JYLX,C_JYJE,C_QSJE,C_SXF,C_SJZFJE,C_CKH,C_LSH,C_KLX,C_FKH,C_ZFFS,C_YSDDH,C_SHDDH,C_BZZD,C_QBYHJE,C_SHYHJE,C_YJYLSH,C_FQQS,C_FQSXF,C_FQFWF,C_FQFXF,C_QTYHJE,C_THDDH,C_FKFY,C_FDJC,C_ZDDH,bill_table_name)
        values
        <foreach collection="list" index="index" item="itm" separator=",">
            (
            #{itm.cQsrq},#{itm.cJyrq},#{itm.cJysj},#{itm.cZdh},#{itm.cCard},#{itm.cJylx},#{itm.cJyje},#{itm.cQsje},#{itm.cSxf},#{itm.cSjzfje},#{itm.cCkh},#{itm.cLsh},#{itm.cKlx}
            ,#{itm.cFkh},#{itm.cZffs},#{itm.cYsddh},#{itm.cShddh},#{itm.cBzzd},#{itm.cQbyhje},#{itm.cShyhje},#{itm.cYjylsh},#{itm.cFqqs},#{itm.cFqsxf},#{itm.cFqfwf},#{itm.cFqfxf},#{itm.cQtyhje},#{itm.cThddh},#{itm.cFkfy},#{itm.cFdjc},#{itm.cZddh},#{itm.billTableName}
            )
        </foreach>
    </insert>

    <delete id="deleteBankbillOriginalByParam" parameterType="HashMap">
        delete
        from bankbill_original
        where C_QSRQ = #{trade_date}
          and bill_table_name = #{bill_table_name}
    </delete>

    <select id="findBankbillCountData" parameterType="HashMap" resultType="HashMap">
        select count(1) as num,cast(IFNULL(sum(C_JYJE),0) as decimal(19,2)) as money
        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>
            <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('%',concat(#{likeFiled},'%'))
            </if>
        </where>
    </select>
</mapper>