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

    <resultMap id="BaseResultMap" type="com.lyms.talkonlineweb.domain.LymsXljcRecord">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="archId" column="arch_id" jdbcType="INTEGER"/>
            <result property="formCode" column="form_code" jdbcType="VARCHAR"/>
            <result property="formVersion" column="form_version" jdbcType="VARCHAR"/>
            <result property="examineTime" column="examine_time" jdbcType="TIMESTAMP"/>
            <result property="examineDoctor" column="examine_doctor" jdbcType="INTEGER"/>
            <result property="examineHid" column="examine_hid" jdbcType="INTEGER"/>
            <result property="examineDpid" column="examine_dpid" jdbcType="INTEGER"/>

            <result property="uploadStatus" column="upload_status" jdbcType="INTEGER"/>
            <result property="uploadTime" column="upload_time" jdbcType="TIMESTAMP"/>
            <result property="yn" column="yn" jdbcType="INTEGER"/>

            <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="queryHisExamineRecords" parameterType="com.lyms.talkonlineweb.domain.LymsXljcRecord"
    resultType="com.lyms.talkonlineweb.result.HisExamineRecord">
        select
            r.id as rid,
            DATE_FORMAT(r.examine_time,'%Y/%m/%d') as examineTime,
            f.form_type as formType,
            f.form_code as formCode,
            f.form_name as formName
        from
            lyms_xljc_record r,
            lyms_xljc_form f
        where r.arch_id = #{archId}
        and r.examine_hid = #{examineHid}
        and r.form_code = f.form_code
        and r.form_version = f.form_version
        order by r.examine_time desc

    </select>

    <select id="selectHisExamineRecord" parameterType="com.lyms.talkonlineweb.request.ExamineRecordListRequest"
            resultType="com.lyms.talkonlineweb.result.HisExamineRecord">
        select
            r.id as rid,
            f.form_type as formType,
            f.form_code as formCode,
            f.form_name as formName,
            a.pname,
            a.idno,
            a.tel,
            a.age,
            a.sex,
            a.birth,
            DATE_FORMAT(r.examine_time,'%Y/%m/%d') as examineTime,
            r.examine_doctor as examineDoctor,
            d.dname as examineDoctorName
        from
             lyms_xljc_archive a,
             lyms_xljc_record r,
             lyms_xljc_form f,
             lyms_doctor d
        where
        a.id = r.arch_id
        and r.examine_doctor = d.did
        and r.form_code = f.form_code
        and r.form_version = f.form_version
        <if test="recordListRequest.examineHid != null ">
            and r.examine_hid = #{recordListRequest.examineHid}
        </if>
        <if test="recordListRequest.examineTimeStart != null and recordListRequest.examineTimeStart != '' ">
            and r.examine_time > #{recordListRequest.examineTimeStart}
        </if>
        <if test="recordListRequest.examineTimeEnd != null and recordListRequest.examineTimeEnd != '' ">
            and r.examine_time &lt; #{recordListRequest.examineTimeEnd}
        </if>
        <if test="recordListRequest.formType != null and recordListRequest.formType != ''">
            and f.form_type = #{recordListRequest.formType}
        </if>
        <if test="recordListRequest.formCode != null and recordListRequest.formCode != ''">
            and f.form_code = #{recordListRequest.formCode}
        </if>
        <if test="recordListRequest.key != null and recordListRequest.key != '' ">
            and (a.pname = #{recordListRequest.key} or a.idno = #{recordListRequest.key} or a.tel = #{recordListRequest.key})
        </if>
        <if test="recordListRequest.doctorId != null">
            and r.examine_doctor = #{recordListRequest.doctorId}
        </if>

        order by r.examine_time desc

    </select>
<!--
    <select id="selectHisExamineRecordNoPage" parameterType="com.lyms.talkonlineweb.request.ExamineRecordListRequest"
            resultType="com.lyms.talkonlineweb.result.HisExamineRecord">
        select
        r.id as rid,
        f.form_type as formType,
        f.form_code as formCode,
        f.form_name as formName,
        a.pname,
        a.idno,
        a.tel,
        a.age,
        a.sex,
        DATE_FORMAT(r.examine_time,'%Y/%m/%d') as examineTime,
        r.examine_doctor as examineDoctor,
        d.dname as examineDoctorName
        from
        lyms_xljc_archive a,
        lyms_xljc_record r,
        lyms_xljc_form f,
        lyms_doctor d
        where
        a.id = r.arch_id
        and r.examine_hid = #{recordListRequest.examineHid}
        and r.examine_doctor = d.did
        and r.form_code = f.form_code
        and r.form_version = f.form_version
        <if test="recordListRequest.examineHid != null ">
            and r.examine_hid = #{recordListRequest.examineHid}
        </if>
        <if test="recordListRequest.examineTimeStart != null and recordListRequest.examineTimeStart != '' ">
            and r.examine_time > #{recordListRequest.examineTimeStart}
        </if>
        <if test="recordListRequest.examineTimeEnd != null and recordListRequest.examineTimeEnd != '' ">
            and r.examine_time &lt; #{recordListRequest.examineTimeEnd}
        </if>
        <if test="recordListRequest.formType != null and recordListRequest.formType != ''">
            and f.form_type = #{recordListRequest.formType}
        </if>
        <if test="recordListRequest.formCode != null and recordListRequest.formCode != ''">
            and f.form_code = #{recordListRequest.formCode}
        </if>
        <if test="recordListRequest.key != null and recordListRequest.key != '' ">
            and (a.pname = #{recordListRequest.key} or a.idno = #{recordListRequest.key} or a.tel = #{recordListRequest.key})
        </if>
        <if test="recordListRequest.doctorId != null">
            and r.examine_doctor = #{recordListRequest.doctorId}
        </if>

        order by r.examine_time desc

    </select>-->

    <select id="countExamineRecordGroupByFormType" parameterType="com.lyms.talkonlineweb.request.ExamineRecordListRequest"
        resultType="com.lyms.talkonlineweb.result.ExamineRecordCount">
        SELECT
            r.examine_hid as examineHid,
            h.hname,
            sum(case when f.form_type = 'A' then 1 else 0 end ) a,
            sum(case when f.form_type = 'B' then 1 else 0 end ) b,
            sum(case when f.form_type = 'C' then 1 else 0 end) c

        FROM
            lyms_xljc_record r,
            lyms_xljc_form f ,
            lyms_hospital h
        WHERE

          r.examine_hid = h.hid
        AND r.form_code = f.form_code
        AND r.form_version = f.form_version
          <if test = "request.examineHid != null">
            and  r.examine_hid = #{request.examineHid}
          </if>
        <if test="request.examineTimeStart != null and request.examineTimeStart != '' ">
            and r.examine_time > #{request.examineTimeStart}
        </if>
        <if test="request.examineTimeEnd != null and request.examineTimeEnd != '' ">
            and r.examine_time &lt; #{request.examineTimeEnd}
        </if>

        GROUP BY r.examine_hid,h.hname
    </select>

    <select id="queryPatientList" parameterType="com.lyms.talkonlineweb.request.XljcPatientRequest"
            resultType="com.lyms.talkonlineweb.result.XljcPatientResult">

    select
    r.id as rid,
    CONCAT('lyms',a.tel) as patientNo,
    a.pname,
    a.idno,
    a.tel,
    a.age,
    a.sex,
    a.birth,
    case when r.upload_status = 0 then '未上传' else '已上传' end uploadStatusText,
    r.upload_time uploadTime,
    r.examine_time examineTime,
    result.diagnosis,
    a.bookbuil_doctor_name as bookbuilDoctorName,
    h.hname,
    hdepart.dname as dpName
    from
    lyms_xljc_archive a,
    lyms_xljc_record r,
    lyms_xljc_record_result result,
    lyms_hospital h,
    lyms_hdepart hdepart
    where
    a.id = r.arch_id
    and r.id = result.rid
    and a.hid = h.hid
    and a.dpid = hdepart.did
    and r.yn = 1
    <if test="request.uploadStatus != null  ">
        and r.upload_status = #{request.uploadStatus}
    </if>
    <if test="request.examineTimeStart != null and request.examineTimeStart != '' ">
        and r.examine_time > #{request.examineTimeStart}
    </if>
    <if test="request.examineTimeEnd != null and request.examineTimeEnd != '' ">
        and r.examine_time &lt; #{request.examineTimeEnd}
    </if>
    <if test="request.key != null and request.key != '' ">
        and (a.pname = #{request.key} or a.idno = #{request.key} or a.tel = #{request.key})
    </if>
    <if test="request.hid != null">
        and a.hid = #{request.hid}
    </if>
    <if test="request.dpid != null">
        and a.dpid = #{request.dpid}
    </if>
    <if test="request.doctorId != null">
        and a.bookbuil_doctor = #{request.doctorId}
    </if>
    order by a.bookbuil_date desc

</select>



</mapper>
