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

    <!-- ResultMap for proper field mapping -->
    <resultMap id="MedicalInsuranceResultMap" type="HashMap">
        <result column="id" property="id" jdbcType="VARCHAR"/>
        <result column="trade_date" property="trade_date" jdbcType="VARCHAR"/>
        <result column="insutype" property="insutype" jdbcType="VARCHAR"/>
        <result column="clr_type" property="clr_type" jdbcType="VARCHAR"/>
        <result column="medfee_sumamt" property="medfee_sumamt" jdbcType="DECIMAL"/>
        <result column="fund_pay_sumamt" property="fund_pay_sumamt" jdbcType="DECIMAL"/>
        <result column="acct_pay" property="acct_pay" jdbcType="DECIMAL"/>
        <result column="fixmedins_setl_cnt" property="fixmedins_setl_cnt" jdbcType="INTEGER"/>
        <result column="stmt_rslt" property="stmt_rslt" jdbcType="VARCHAR"/>
        <result column="stmt_rslt_dscr" property="stmt_rslt_dscr" jdbcType="VARCHAR"/>
        <result column="execute_type" property="execute_type" jdbcType="VARCHAR"/>
        <result column="check_round" property="check_round" jdbcType="INTEGER"/>
        <result column="recheck_flag" property="recheck_flag" jdbcType="VARCHAR"/>
        <result column="prev_stmt_rslt" property="prev_stmt_rslt" jdbcType="VARCHAR"/>
        <result column="recheck_time" property="recheck_time" jdbcType="VARCHAR"/>
        <result column="recheck_user" property="recheck_user" jdbcType="VARCHAR"/>
        <result column="api_result" property="api_result" jdbcType="VARCHAR"/>
        <result column="create_time" property="create_time" jdbcType="VARCHAR"/>
        <result column="modify_time" property="modify_time" jdbcType="VARCHAR"/>
    </resultMap>

    <!-- 插入医保对账结果 -->
    <insert id="insertMedicalInsuranceReconciliationResult" parameterType="HashMap">
        insert into medical_insurance_reconciliation_result (
            id, trade_date, insutype, clr_type,
            medfee_sumamt, fund_pay_sumamt, acct_pay, fixmedins_setl_cnt,
            stmt_rslt, stmt_rslt_dscr,
            execute_type, check_round, recheck_flag, prev_stmt_rslt, recheck_time, recheck_user,
            api_result, create_time, modify_time
        ) values (
            #{id}, #{trade_date}, #{insutype}, #{clr_type},
            #{medfee_sumamt}, #{fund_pay_sumamt}, #{acct_pay}, #{fixmedins_setl_cnt},
            #{stmt_rslt}, #{stmt_rslt_dscr},
            #{execute_type}, #{check_round}, #{recheck_flag}, #{prev_stmt_rslt}, nullif(#{recheck_time}, ''), #{recheck_user},
            #{api_result}, #{create_time}, #{modify_time}
        )
    </insert>

    <!-- 批量插入医保对账结果 -->
    <insert id="insertMedicalInsuranceReconciliationResultBatch" parameterType="java.util.List">
        insert into medical_insurance_reconciliation_result (
            id, trade_date, insutype, clr_type,
            medfee_sumamt, fund_pay_sumamt, acct_pay, fixmedins_setl_cnt,
            stmt_rslt, stmt_rslt_dscr,
            execute_type, check_round, recheck_flag, prev_stmt_rslt, recheck_time, recheck_user,
            api_result, create_time, modify_time
        ) values
        <foreach collection="list" index="index" item="itm" separator=",">
            (
            #{itm.id}, #{itm.trade_date}, #{itm.insutype}, #{itm.clr_type},
            #{itm.medfee_sumamt}, #{itm.fund_pay_sumamt}, #{itm.acct_pay}, #{itm.fixmedins_setl_cnt},
            #{itm.stmt_rslt}, #{itm.stmt_rslt_dscr},
            #{itm.execute_type}, #{itm.check_round}, #{itm.recheck_flag}, #{itm.prev_stmt_rslt}, nullif(#{itm.recheck_time}, ''), #{itm.recheck_user},
            #{itm.api_result}, #{itm.create_time}, #{itm.modify_time}
            )
        </foreach>
    </insert>

    <!-- 查询医保对账结果 -->
    <select id="findMedicalInsuranceReconciliationResult" parameterType="HashMap" resultMap="MedicalInsuranceResultMap">
        select 
            id, 
            trade_date, 
            insutype, 
            clr_type,
            medfee_sumamt, 
            fund_pay_sumamt, 
            acct_pay, 
            fixmedins_setl_cnt,
            stmt_rslt, 
            stmt_rslt_dscr, 
            execute_type,
            check_round,
            recheck_flag,
            prev_stmt_rslt,
            DATE_FORMAT(recheck_time, '%Y-%m-%d %H:%i:%s') as recheck_time,
            recheck_user,
            api_result,
            DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as create_time,
            DATE_FORMAT(modify_time, '%Y-%m-%d %H:%i:%s') as modify_time
        from medical_insurance_reconciliation_result
        <where>
            <if test="trade_date != null and trade_date != ''">
                and trade_date = #{trade_date}
            </if>
            <if test="startDate != null and startDate != ''">
                and trade_date &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != ''">
                and trade_date &lt;= #{endDate}
            </if>
            <if test="insutype != null and insutype != ''">
                and insutype = #{insutype}
            </if>
            <if test="clr_type != null and clr_type != ''">
                and clr_type = #{clr_type}
            </if>
            <if test="clrType != null and clrType != ''">
                and clr_type = #{clrType}
            </if>
            <if test="stmt_rslt != null and stmt_rslt != ''">
                and stmt_rslt = #{stmt_rslt}
            </if>
            <if test="stmtRslt != null and stmtRslt != ''">
                and stmt_rslt = #{stmtRslt}
            </if>
        </where>
        order by create_time desc, trade_date desc, insutype, clr_type
    </select>

    <!-- 删除医保对账结果 -->
    <delete id="deleteMedicalInsuranceReconciliationResult" parameterType="HashMap">
        delete from medical_insurance_reconciliation_result
        where trade_date = #{trade_date}
    </delete>

</mapper>
