<?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.lyms.talkonlineweb.mapper.LymsXljcRecordTraceMapper">

    <resultMap id="BaseResultMap" type="com.lyms.talkonlineweb.domain.LymsXljcRecordTrace">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="rid" column="rid" jdbcType="INTEGER"/>
            <result property="nextExamineDate" column="next_examine_date" jdbcType="TIMESTAMP"/>
            <result property="nextExamineStatus" column="next_examine_status" jdbcType="INTEGER"/>
            <result property="traceTime" column="trace_time" jdbcType="TIMESTAMP"/>
            <result property="tracePerson" column="trace_person" jdbcType="VARCHAR"/>
            <result property="traceWay" column="trace_way" jdbcType="INTEGER"/>
            <result property="result" column="result" jdbcType="VARCHAR"/>
            <result property="createdby" column="createdby" jdbcType="INTEGER"/>
            <result property="createdtime" column="createdtime" jdbcType="TIMESTAMP"/>
            <result property="updatedby" column="updatedby" jdbcType="INTEGER"/>
            <result property="updatedtime" column="updatedtime" jdbcType="TIMESTAMP"/>
    </resultMap>


        <select id="queryXljcTraceRecords" parameterType="com.lyms.talkonlineweb.request.XljcTraceRequest"
                resultType="com.lyms.talkonlineweb.result.XljcTraceRecord">
                select * from (
                select
                r.id as rid,
                r.arch_id as archId,
                trace.id as traceId,
                a.pname,
                a.idno,
                a.tel,
                a.age,
                a.sex,
                a.birth,
                trace.next_examine_date as nextExamineDate,
                trace.next_examine_status as nextExamineStatus,
                trace.trace_way as traceWay,
                trace.trace_person as tracePerson,
                trace.trace_time as traceDate,
                DATEDIFF((case when next_examine_status = 0 then now() else trace_time end),
                next_examine_date) as overDays,
                trace.result
                from
                lyms_xljc_archive a,
                lyms_xljc_record r,
                lyms_xljc_record_trace trace
                where
                a.id = r.arch_id
                and r.id = trace.rid
                and trace.yn = 1
                and trace.next_examine_date &lt;= DATE_SUB(CURDATE() ,INTERVAL 7 DAY)
                <if test="trace.examineHid != null">
                    and r.examine_hid = #{trace.examineHid}
                </if>
                <if test="trace.overDays != null">
                  and trace.next_examine_date = DATE_SUB(CURDATE() ,INTERVAL #{trace.overDays} DAY)
                </if>
                <if test="trace.traceTimeStart != null and trace.traceTimeStart != '' ">
                    and trace.trace_time > STR_TO_DATE(#{trace.traceTimeStart},'%Y-%m-%d')
                </if>
                <if test="trace.traceTimeEnd != null and trace.traceTimeEnd !='' ">
                    and trace.trace_time &lt; STR_TO_DATE(#{trace.traceTimeEnd},'%Y-%m-%d %H:%i:%s')
                </if>

                <if test="trace.nextExamineStatus != null">
                    and trace.next_examine_status = #{trace.nextExamineStatus}
                </if>
                <if test="trace.key != null and trace.key != '' ">
                    and (a.pname = #{trace.key } or a.idno = #{trace.key } or a.tel = #{trace.key })
                </if>
                ) a
                         where 1=1
                <if test="trace.overDaysMin != null">
                        and  a.overDays >= #{trace.overDaysMin}
                </if>
                <if test="trace.overDaysMax != null">
                        and  a.overDays &lt;= #{trace.overDaysMax}
                </if>

                order by a.nextExamineDate

        </select>


</mapper>
