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

    <!--查询his和三方关联的账单-->
    <select id="findHisAndThirdJoinData" parameterType="HashMap" resultType="HashMap">
        select JYSJ
        ,I_DDH
        ,I_JYJE
        ,I_JYLX
        ,I_JYQD
        ,TradeTime
        ,TranID
        ,BizType
        ,H_JYLX
        ,Amount
        ,PayType
        ,HisOperNum
        ,PatientID
        ,PatientName
        ,sort_date
        ,trade_date
        ,err_type
        ,check_result

        from third_join_his
        <where>
            <if test="operCode!=null and operCode!=''">
                and HisOperNum=#{operCode}
            </if>
            <if test="BizType!=null and BizType!=''">
                and BizType=#{BizType}
            </if>
            <if test="payType!=null and payType!=''">
                and PayType=#{payType}
            </if>
            <if test="thirdPay!=null and thirdPay!=''">
                and I_JYQD=#{thirdPay}
            </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="check_result!=null and check_result!=''">
                and check_result =#{check_result}
            </if>
            <if test="is_active!=null and is_active!=''">
                and is_active =#{is_active}
            </if>
        </where>
        order by trade_date, sort_date desc
    </select>

    <select id="findHisUnilateral" resultType="HashMap">
        select a.PlatformTransId, a.trade_date, a.TradeTime, a.PayType, a.Amount
        from temp_hisbill_history a
                 left join temp_bankbill_history b on a.PlatformTransId = b.C_YSDDH
        where b.C_YSDDH is null
    </select>

    <select id="findBankUnilateral" resultType="HashMap">
        select a.C_YSDDH, a.C_JYRQ as trade_date, concat(a.C_JYRQ, ' ', a.C_JYSJ) as TradeTime, a.c_zffs, a.C_JYJE
        from temp_bankbill_history a
                 left join temp_hisbill_history b on a.C_YSDDH = b.PlatformTransId
        where b.PlatformTransId is null
    </select>

    <select id="findDiff" resultType="HashMap">
        select a.PlatformTransId, a.Amount, b.C_JYJE, a.PayType, b.C_ZFFS, a.trade_date, a.TradeTime
        from temp_hisbill_history a
                 left join temp_bankbill_history b on a.PlatformTransId = b.C_YSDDH
        where ROUND(CAST(a.Amount AS DECIMAL(18,2)), 2) != ROUND(CAST(b.C_JYJE AS DECIMAL(18,2)), 2)
    </select>

    <select id="findHisAndThirdJoinDataNumByTime" parameterType="HashMap" resultType="HashMap">
        select b.C_ZFFS, b.C_JYRQ, count(1) as totalNum
        from hisbill_history a
                 inner join bankbill_history b on a.PlatformTransId = b.C_YSDDH
        where b.C_JYRQ &gt;= #{startTime}
          and b.C_JYRQ &lt;= #{endTime}
        <if test="excludeMilitaryOperators != null and excludeMilitaryOperators.size() > 0">
          and a.HisOperCode not in
          <foreach collection="excludeMilitaryOperators" item="operator" open="(" separator="," close=")">
              #{operator.HisOperCode}
          </foreach>
        </if>
        group by b.C_ZFFS, b.C_JYRQ
    </select>

    <insert id="insertJoinDataToHisAndThird" parameterType="HashMap">
        insert into third_join_his( JYSJ, I_DDH, I_JYJE, I_JYLX, I_JYQD, TradeTime, TranID, BizType, H_JYLX, Amount
                                  , PayType
                                  , HisOperNum, PatientID, PatientName
                                  , sort_date, trade_date, err_type, check_result, is_active)
        select concat(b.C_JYRQ, ' ', b.C_JYSJ) as jysj
             , b.C_SHDDH
             , b.C_JYJE
             , b.C_JYLX
             , b.C_ZFFS
             , b.C_JYSJ
             , a.PlatformTransId
             , a.BizType
             , a.TradingStatus
             , a.Amount
             , a.PayType
             , a.HisOperCode
             , a.PatientId
             , a.PatientName
             , a.TradeTime
             , a.trade_date
             , ''
             , '0'
             , '1'
        from (select * from hisbill_history where trade_date = #{trade_date} and payType!=#{cash_code}
        <if test="military_code != null and military_code != ''">
            and payType!=#{military_code}
        </if>
        <if test="medical_insurance_code != null and medical_insurance_code != ''">
            and payType!=#{medical_insurance_code}
        </if>
        ) a
                 inner join
                 (select * from bankbill_history where C_JYRQ = #{trade_date}) b
                 on a.PlatformTransId = b.C_SHDDH and a.TradingStatus = b.C_JYLX and ROUND(CAST(a.Amount AS DECIMAL(18,2)), 2) = ROUND(CAST(b.C_JYJE AS DECIMAL(18,2)), 2)
    </insert>

    <insert id="insertHisUnilateral" parameterType="HashMap">
        insert into third_join_his( JYSJ, I_DDH, I_JYJE, I_JYLX, I_JYQD, TradeTime, TranID, BizType, H_JYLX, Amount
                                  , PayType
                                  , HisOperNum, PatientID, PatientName
                                  , sort_date, trade_date, err_type, check_result, is_active)
        select a.TradeTime as jysj
             , b.C_SHDDH
             , b.C_JYJE
             , b.C_JYLX
             , b.C_ZFFS
             , b.C_JYSJ
             , a.PlatformTransId
             , a.BizType
             , a.TradingStatus
             , a.Amount
             , a.PayType
             , a.HisOperCode
             , a.PatientId
             , a.PatientName
             , a.TradeTime
             , a.trade_date
             , '1'
             , '1'
             , '1'
        from (select * from hisbill_history where trade_date = #{trade_date}  and payType!=#{cash_code}
        <if test="military_code != null and military_code != ''">
            and payType!=#{military_code}
        </if>
        <if test="medical_insurance_code != null and medical_insurance_code != ''">
            and payType!=#{medical_insurance_code}
        </if>
        ) a
                 left join
                 (select * from bankbill_history where C_JYRQ = #{trade_date}) b
                 on a.PlatformTransId = b.C_SHDDH and a.TradingStatus = b.C_JYLX and ROUND(CAST(a.Amount AS DECIMAL(18,2)), 2) = ROUND(CAST(b.C_JYJE AS DECIMAL(18,2)), 2)
        where b.C_SHDDH is null
    </insert>

    <insert id="insertThirdUnilateral" parameterType="HashMap">
        insert into third_join_his( JYSJ, I_DDH, I_JYJE, I_JYLX, I_JYQD, TradeTime, TranID, BizType, H_JYLX, Amount
                                  , PayType
                                  , HisOperNum, PatientID, PatientName
                                  , sort_date, trade_date, err_type, check_result, is_active)
        select concat(b.C_JYRQ, ' ', b.C_JYSJ) as jysj
             , b.C_SHDDH
             , b.C_JYJE
             , b.C_JYLX
             , b.C_ZFFS
             , b.C_JYSJ
             , a.PlatformTransId
             , a.BizType
             , a.TradingStatus
             , a.Amount
             , a.PayType
             , a.HisOperCode
             , a.PatientId
             , a.PatientName
             , b.C_JYSJ
             , b.C_JYRQ
             , '2'
             , '1'
             , '1'
        from (select * from hisbill_history where trade_date = #{trade_date}  and payType!=#{cash_code}
        <if test="military_code != null and military_code != ''">
            and payType!=#{military_code}
        </if>
        <if test="medical_insurance_code != null and medical_insurance_code != ''">
            and payType!=#{medical_insurance_code}
        </if>
        ) a
                 right join
                 (select * from bankbill_history where C_JYRQ = #{trade_date}) b
                 on a.PlatformTransId = b.C_SHDDH and a.TradingStatus = b.C_JYLX and ROUND(CAST(a.Amount AS DECIMAL(18,2)), 2) = ROUND(CAST(b.C_JYJE AS DECIMAL(18,2)), 2)
        where a.PlatformTransId is null
    </insert>

    <insert id="insertJoinDataToHisAndCash" parameterType="HashMap">
        insert into third_join_his(jysj, c_ysddh, c_jyje, c_zffs, c_jylx, czyh, paymethod, tradingstatus, biztype,
                                   paytype, tradetime, hisopercode, amount, platformtransid, patientid, patientname,
                                   sort_date, trade_date, err_type, check_result, is_active)
        select b.trade_date
             , ''
             , b.jyje
             , #{cash_code}
             , ''
             , b.czyh
             , a.PayMethod
             , a.TradingStatus
             , a.BizType
             , a.PayType
             , a.TradeTime
             , a.HisOperCode
             , a.Amount
             , a.PlatformTransId
             , a.patientid
             , a.patientname
             , b.trade_date
             , b.trade_date
             , ''
             , '0'
             , '1'
        from (select * from hisbill_history where trade_date = #{trade_date} and payType = #{cash_code}
        <if test="excludeMilitaryOperators != null and excludeMilitaryOperators.size() > 0">
            and HisOperCode not in
            <foreach collection="excludeMilitaryOperators" item="operator" open="(" separator="," close=")">
                #{operator.HisOperCode}
            </foreach>
        </if>
        ) a
                 inner join
                 (select * from cash_record where trade_date = #{trade_date}) b
                 on a.HisOperCode = b.czyh
    </insert>

    <insert id="insertHisCashUnilateral" parameterType="HashMap">
        insert into third_join_his(jysj, c_ysddh, c_jyje, c_zffs, c_jylx, czyh, paymethod, tradingstatus, biztype,
                                   paytype, tradetime, hisopercode, amount, platformtransid, patientid, patientname,
                                   sort_date, trade_date, err_type, check_result, is_active)
        select b.trade_date
             , ''
             , b.jyje
             , #{cash_code}
             , ''
             , b.czyh
             , a.PayMethod
             , a.TradingStatus
             , a.BizType
             , a.PayType
             , a.TradeTime
             , a.HisOperCode
             , a.Amount
             , a.PlatformTransId
             , a.patientid
             , a.patientname
             , a.trade_date
             , a.trade_date
             , '1'
             , '1'
             , '1'
        from (select * from hisbill_history where trade_date = #{trade_date} and payType = #{cash_code}
        <if test="excludeMilitaryOperators != null and excludeMilitaryOperators.size() > 0">
            and HisOperCode not in
            <foreach collection="excludeMilitaryOperators" item="operator" open="(" separator="," close=")">
                #{operator.HisOperCode}
            </foreach>
        </if>
        ) a
                 left join
                 (select * from cash_record where trade_date = #{trade_date}) b
                 on a.HisOperCode = b.czyh
        where b.czyh is null
    </insert>

    <insert id="insertCashUnilateral" parameterType="HashMap">
        insert into third_join_his(jysj, c_ysddh, c_jyje, c_zffs, c_jylx, czyh, paymethod, tradingstatus, biztype,
                                   paytype, tradetime, hisopercode, amount, platformtransid, patientid, patientname,
                                   sort_date, trade_date, err_type, check_result, is_active)
        select b.trade_date
             , ''
             , b.jyje
             , #{cash_code}
             , ''
             , b.czyh
             , a.PayMethod
             , a.TradingStatus
             , a.BizType
             , a.PayType
             , a.TradeTime
             , a.HisOperCode
             , a.Amount
             , a.PlatformTransId
             , a.patientid
             , a.patientname
             , b.trade_date
             , b.trade_date
             , '2'
             , '1'
             , '1'
        from (select * from hisbill_history where trade_date = #{trade_date} and payType = #{cash_code}
        <if test="excludeMilitaryOperators != null and excludeMilitaryOperators.size() > 0">
            and HisOperCode not in
            <foreach collection="excludeMilitaryOperators" item="operator" open="(" separator="," close=")">
                #{operator.HisOperCode}
            </foreach>
        </if>
        ) a
                 right join
                 (select * from cash_record where trade_date = #{trade_date}) b
                 on a.HisOperCode = b.czyh
        where a.HisOperCode is null
    </insert>

    <update id="updateHisAndThirdError" parameterType="HashMap">
        update third_join_his
        set err_type='3',
            check_result='1'
        where trade_date = #{trade_date}
          and check_result = '0'
          and ROUND(CAST(I_JYJE AS DECIMAL(18,2)), 2) != ROUND(CAST(Amount AS DECIMAL(18,2)), 2)
    </update>

    <delete id="deleteHisAndThirdJoinData" parameterType="HashMap">
        delete
        from third_join_his
        where trade_date = #{trade_date}
    </delete>

    <select id="findHisAndThirdJoinDataByParam" parameterType="HashMap" resultType="HashMap">
        select JYSJ
        ,I_DDH
        ,I_JYJE
        ,I_JYLX
        ,I_JYQD
        ,TradeTime
        ,TranID
        ,BizType
        ,H_JYLX
        ,Amount
        ,PayType
        ,HisOperNum
        ,PatientID
        ,PatientName
        ,sort_date
        ,trade_date
        ,err_type
        ,check_result
        ,is_active
        from third_join_his
        <where>
            <if test="trade_date!=null and trade_date!=''">
                and trade_date=#{trade_date}
            </if>
            <if test="BizType!=null and BizType!=''">
                and BizType=#{BizType}
            </if>
            <if test="check_result!=null and check_result!=''">
                and check_result=#{check_result}
            </if>
            <if test="err_type!=null and err_type!=''">
                and err_type=#{err_type}
            </if>
            <if test="i_ddh!=null and i_ddh!=''">
                and I_DDH=#{i_ddh}
            </if>
            <if test="i_jylx!=null and i_jylx!=''">
                and I_JYLX=#{i_jylx}
            </if>
            <if test="tranid!=null and tranid!=''">
                and TranID=#{tranid}
            </if>
            <if test="h_jylx!=null and h_jylx!=''">
                and H_JYLX=#{h_jylx}
            </if>
        </where>
    </select>

    <insert id="insertHisAndThirdJoinData" parameterType="HashMap">
        insert into third_join_his(jysj, c_ysddh, c_jyje, c_zffs, c_jylx, paymethod, tradingstatus, biztype, paytype,
                                   tradetime, hisopercode, amount, platformtransid, patientid, patientname, sort_date,
                                   trade_date, err_type, check_result)
        values (#{jysj}, #{c_ysddh}, #{c_jyje}, #{c_zffs}, #{c_jylx}, #{paymethod}, #{tradingstatus}, #{biztype},
                #{paytype}, #{tradetime}, #{hisopercode}, #{amount}, #{platformtransid}, #{patientid}, #{patientname},
                #{sort_date}, #{trade_date}, #{err_type}, #{check_result})
    </insert>

    <update id="updateJoinDate" parameterType="HashMap">
        update third_join_his set check_result=#{check_result}
        <where>
            <if test="tranID!=null and tranID!=''">
                and TranID=#{tranID}
            </if>
            <if test="h_jylx!=null and h_jylx!=''">
                and H_JYLX=#{h_jylx}
            </if>
            <if test="i_ddh!=null and i_ddh!=''">
                and I_DDH=#{i_ddh}
            </if>
            <if test="i_jylx!=null and i_jylx!=''">
                and I_JYLX=#{i_jylx}
            </if>
        </where>
    </update>

    <update id="updateJoinSetActiveByThird" parameterType="HashMap">
        update third_join_his
        set is_active=#{is_active}
        where I_DDH = #{i_ddh}
          and I_JYLX = #{i_jylx}
    </update>

    <update id="updateJoinSetActiveByHis" parameterType="HashMap">
        update third_join_his
        set is_active=#{is_active}
        where TranID = #{tranID}
          and H_JYLX = #{h_jylx}
    </update>

    <update id="updateJoinSetActiveByThirdCash" parameterType="HashMap">
        update third_join_his
        set is_active=#{is_active}
        where czyh = #{hisOperCode}
          and trade_date = #{trade_date}
          and C_ZFFS = #{c_zffs}
    </update>

    <update id="updateJoinSetActiveByHisCash" parameterType="HashMap">
        update third_join_his
        set is_active=#{is_active}
        where HisOperCode = #{hisOperCode}
          and trade_date = #{trade_date}
          and PayType = #{payType}
    </update>

    <!-- 根据三方信息更新关联表记录（用于自费对账自动核销） -->
    <update id="updateJoinDateByThird" parameterType="HashMap">
        update third_join_his 
        set check_result=#{check_result}
        <where>
            <if test="i_ddh!=null and i_ddh!=''">
                and I_DDH=#{i_ddh}
            </if>
            <if test="i_jylx!=null and i_jylx!=''">
                and I_JYLX=#{i_jylx}
            </if>
            <if test="trade_date!=null and trade_date!=''">
                and trade_date=#{trade_date}
            </if>
        </where>
    </update>

    <update id="updateJoinSetActiveByZhiPiao" parameterType="HashMap">
        update third_join_his
        set is_active=#{is_active}
        where HisOperCode = #{hisOperCode}
          and trade_date = #{trade_date}
          and PayType = #{payType}
          and payMethod = #{payMethod}
    </update>

    <delete id="deleteJoinDataByThird" parameterType="HashMap">
        delete from third_join_his
        <where>
            <if test="i_ddh!=null and i_ddh!=''">
                and I_DDH=#{i_ddh}
            </if>
            <if test="i_jylx!=null and i_jylx!=''">
                and I_JYLX=#{i_jylx}
            </if>
            <if test="trade_date!=null and trade_date!=''">
                and trade_date=#{trade_date}
            </if>
        </where>
    </delete>

    <select id="findAllBankDetailNum" parameterType="HashMap" resultType="HashMap">
        select count(1) as num
        from third_join_his
        where trade_date &gt;= #{startTime}
          and trade_date &lt;= #{endTime}
          and is_active = '1'
          and I_JYJE is not null
    </select>

    <select id="findAllBankDetailSum" parameterType="HashMap" resultType="HashMap">
        select IFNULL(sum(I_JYJE), 0) as Money
        from third_join_his
        where trade_date &gt;= #{startTime}
          and trade_date &lt;= #{endTime}
          and is_active = '1'
          and I_JYJE is not null
    </select>

    <select id="findDBZNum" parameterType="HashMap" resultType="HashMap">
        SELECT trade_date, count(1) as num
        FROM third_join_his
        where trade_date &gt;= #{startTime}
          and trade_date &lt;= #{endTime}
          and check_result = '1'
          and is_active = '1'
        group by trade_date
    </select>

    <select id="findJoinDataNumByTime" parameterType="HashMap" resultType="HashMap">
        select aI_ZFFS as I_ZFFS, trade_date, totalNum
        from (select (case when I_JYQD is null then PayType else I_JYQD end) as aI_ZFFS,
                     trade_date,
                     count(1)                                                as totalNum
              from third_join_his
              where trade_date &gt;= #{startTime}
                and trade_date &lt;= #{endTime}
                and is_active = '1'
              group by aI_ZFFS, trade_date) t
    </select>

    <select id="findDBZFLNumByTime" parameterType="HashMap" resultType="HashMap">
        select aI_ZFFS as I_ZFFS, trade_date, num
        from (SELECT (case when I_JYQD is null then PayType else I_JYQD end) as aI_ZFFS, trade_date, count(1) as num
              FROM third_join_his
              where trade_date &gt;= #{startTime}
                and trade_date &lt;= #{endTime}
                and is_active = '1'
                and check_result = '1'
              group by aI_ZFFS, trade_date) t
    </select>

    <select id="findBankBillNumByTime" parameterType="HashMap" resultType="HashMap">
        select trade_date, I_JYQD, count(1) as num
        from third_join_his
        where trade_date &gt;= #{startTime}
          and trade_date &lt;= #{endTime}
          and I_JYLX = '1'
          and is_active = '1'
          and I_JYJE is not null
        group by I_JYQD, trade_date
        order by I_JYQD, trade_date
    </select>

    <select id="findBankBillMoneyByTime" parameterType="HashMap" resultType="HashMap">
        select trade_date, I_JYQD, sum(cast(I_JYJE as decimal(18, 2))) as money
        from third_join_his
        where trade_date &gt;= #{startTime}
          and trade_date &lt;= #{endTime}
          and is_active = '1'
          and I_JYJE is not null
        group by I_JYQD, trade_date
        order by I_JYQD, trade_date
    </select>

    <!--查询关联表中不唯一的记录-->
    <!--    <select id="findHisAndThirdJoinDataByParamAndNotUnique" parameterType="HashMap" resultType="HashMap">
            select jysj, c_ysddh, c_jyje, c_zffs, c_jylx, paymethod, tradingstatus, biztype, paytype, tradetime, hisopercode, amount, platformtransid, patientid, patientname, sort_date, trade_date, err_type, check_result ,join_id
            from third_join_his a where trade_date=#{trade_date}
            and EXISTS( select 1 from (
                    select PlatformTransId,TradingStatus from third_join_his where trade_date=#{trade_date} and PlatformTransId!='' group by PlatformTransId,TradingStatus having count(PlatformTransId)>1
                ) b where a.PlatformTransId=b.PlatformTransId and a.TradingStatus=b.TradingStatus )
            order by PlatformTransId
        </select>-->

    <select id="findHisAndThirdJoinDataByParamAndNotUnique" parameterType="HashMap" resultType="HashMap">
        select TranID, H_JYLX,Amount
        from third_join_his
        where trade_date = #{trade_date}
          and TranID != ''
        group by TranID, H_JYLX,Amount
        having count(TranID) > 1
    </select>

    <!--删除所有重复记录-->
    <delete id="deleteHisAndThirdJoinDataByParamAndNotUnique" parameterType="HashMap">
        delete
        from third_join_his
        where trade_date = #{trade_date}
          and TranID = #{tranID}
          and H_JYLX = #{h_jylx}
    </delete>

    <!--添加修改后的重复记录回数据库-->
    <insert id="addNotUniqueData" parameterType="java.util.List">
        insert into third_join_his(JYSJ
        ,I_DDH
        ,I_JYJE
        ,I_JYLX
        ,I_JYQD
        ,TradeTime
        ,TranID
        ,BizType
        ,H_JYLX
        ,Amount
        ,PayType
        ,HisOperNum
        ,PatientID
        ,PatientName
        ,sort_date
        ,trade_date
        ,err_type
        ,check_result
        ,is_active
        ,join_id
        )
        values
        <foreach collection="list" index="index" item="itm" separator=",">
            (
            #{itm.jysj}, #{itm.i_ddh}, #{itm.i_jyje}, #{itm.i_jylx}, #{itm.i_jyqd},
            #{itm.tradeTime}, #{itm.tranID}, #{itm.BizType}, #{itm.h_jylx}, #{itm.amount}, #{itm.payType},
            #{itm.hisOperNum},
            #{itm.patientid}, #{itm.patientname}, #{itm.sort_date},
            #{itm.trade_date}, #{itm.err_type}, #{itm.check_result}, #{itm.is_active}, #{itm.join_id}
            )
        </foreach>
    </insert>


</mapper>