<?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.historyLog.ReconciliationLogMapper">

    <select id="findReconciliationLogPageList" parameterType="HashMap" resultType="HashMap">
        select 
            t.trade_date as TRADE_DATE,
            t.modify_time as MODIFY_TIME,
            t.create_time as CREATE_TIME,
            t.manager_num as MANAGER_NUM,
            t.status as STATUS,
            t.user_name as USER_NAME,
            t.remark as REMARK
        from (
            select 
                trade_date,
                modify_time,
                create_time,
                manager_num,
                status,
                user_name,
                remark,
                ROW_NUMBER() OVER (PARTITION BY trade_date ORDER BY create_time DESC) as rn
            from reconciliation_info
            <where>
                <!-- 确保trade_date是有效的日期格式 -->
                and trade_date is not null 
                and trade_date != ''
                and trade_date REGEXP '^[0-9]{4}-[0-9]{2}-[0-9]{2}$'
                <!-- 确保status是有效值 -->
                and status in ('0', '1')
                <!-- 确保manager_num是整数且不包含小数点（排除金额数据） -->
                and manager_num is not null
                and manager_num not like '%.%'
                and manager_num REGEXP '^[0-9]+$'
                and CAST(manager_num AS UNSIGNED) &lt; 10000
                <!-- 确保时间字段不包含小数点（排除金额数据） -->
                and (modify_time is null or modify_time not like '%.%')
                and (create_time is null or create_time not like '%.%')
                
                <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="status!=null and status!=''">
                    and status=#{status}
                </if>
            </where>
        ) t
        where t.rn = 1
        order by t.trade_date desc
    </select>

    <select id="findReconciliationLogByParam" parameterType="HashMap" resultType="HashMap">
        select trade_date,
               modify_time,
               create_time,
               manager_num,
               status,
               user_name,
               remark,
               quartz_id,
               quartz_name
        from reconciliation_info
        where trade_date = #{trade_date}
    </select>

    <insert id="insertReconciliationLog" parameterType="HashMap">
        insert into reconciliation_info(trade_date, modify_time, create_time, manager_num,
                                        status, user_name, quartz_id, quartz_name)
        values (#{trade_date}, #{modify_time}, #{create_time}, #{manager_num},
                #{status}, #{user_name}, #{quartz_id}, #{quartz_name})
        ON DUPLICATE KEY UPDATE modify_time=values(modify_time),
                                user_name=values(user_name),
                                status=values(status),
                                manager_num=values(manager_num)
    </insert>
</mapper>