<?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.diagbot.mapper.MedManagementInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.MedManagementInfo">
        <id column="id" property="id" />
        <result column="hospital_id" property="hospitalId" />
        <result column="position_id" property="positionId" />
        <result column="start_date" property="startDate" />
        <result column="end_date" property="endDate" />
        <result column="work_person" property="workPerson" />
        <result column="is_deleted" property="isDeleted" />
        <result column="gmt_create" property="gmtCreate" />
        <result column="gmt_modified" property="gmtModified" />
        <result column="creator" property="creator" />
        <result column="modifier" property="modifier" />
    </resultMap>

    <select id="medicalRecordIndicator" parameterType="com.diagbot.entity.FilterRecordVO" resultType="java.util.Map">
      select a.behospital_code as acode,
             b.`name`,
             b.hospital_id,
             b.beh_dept_id ,
             b.behospital_date,
             b.leave_hospital_date,
             b.diagnose_icd ,
             b.diagnose ,
             b.is_placefile,
             c.cases_entry_id ,
             c.msg
             <if test="filterRecordVO.type != null and (filterRecordVO.type== 1 or filterRecordVO.type==2)">
                 ,COUNT(DISTINCT a.behospital_code) as countNum
             </if>
             <if test="filterRecordVO.type != null and (filterRecordVO.type== 3 or filterRecordVO.type==4)">
                 ,sum(c.cases_entry_id = 2170) as '2170手术记录未在术后24h内完成',
                 sum(c.cases_entry_id = 2287) as '2287会诊记录次数与会诊次数不一致',
                 sum(c.cases_entry_id = 2658) as '2287会诊记录次数与会诊次数不一致',
                 sum(c.cases_entry_id = 2635) as '2635出院记录未在患者出院24小时内完成'
             </if>
      from
             med_qcresult_cases a,
             med_behospital_info b,
             med_qcresult_detail c,
             qc_cases_entry d

       where
             a.hospital_id = #{filterRecordVO.hospitalId}
        and  a.hospital_id = b.hospital_id
        and  a.hospital_id = c.hospital_id
        and  a.cases_id = 243
        and  b.is_placefile = #{filterRecordVO.isPlacefile}
        and  a.behospital_code = b.behospital_code
        and  a.behospital_code = c.behospital_code
        and  c.cases_entry_id = d.id
        and  a.is_deleted='N'
        and  b.is_deleted ='N'
        and c.is_deleted = 'N'
        and d.is_deleted = 'N'
        <if test="filterRecordVO.type != null and filterRecordVO.type==1 ">
            and #{start} &lt;= b.leave_hospital_date
            and b.leave_hospital_date &lt;= #{end}
        </if>
        <if test="filterRecordVO.type != null and (filterRecordVO.type==2 or filterRecordVO.type==0) ">
            and #{start} &lt;= b.behospital_date
            and b.behospital_date &lt;= #{end}
        </if>
        <if test="filterRecordVO.type != null and filterRecordVO.type== 3 ">
            and #{start} &lt;= b.behospital_date
            and  b.behospital_date &lt;= #{end}
            GROUP BY  c.cases_entry_id
        </if>
        <if test="filterRecordVO.type != null and filterRecordVO.type== 4 ">
            and #{start} &lt;= b.leave_hospital_date
            and  b.leave_hospital_date &lt;= #{end}
            GROUP BY  c.cases_entry_id
        </if>

    </select>
</mapper>
