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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.PatientInfo">
        <id column="id" property="id" />
        <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" />
        <result column="hospital_code" property="hospitalCode" />
        <result column="code" property="code" />
        <result column="name" property="name" />
        <result column="sex" property="sex" />
        <result column="birthday" property="birthday" />
        <result column="id_type" property="idType" />
        <result column="id_no" property="idNo" />
        <result column="address" property="address" />
        <result column="phone" property="phone" />
        <result column="identity_num" property="identityNum" />
        <result column="postcode" property="postcode" />
        <result column="contacts" property="contacts" />
        <result column="contact_phone" property="contactPhone" />
        <result column="work_unit" property="workUnit" />
        <result column="operation" property="operation" />
        <result column="country" property="country" />
        <result column="nationality" property="nationality" />
        <result column="matrimony" property="matrimony" />
        <result column="remark" property="remark" />
    </resultMap>

    <select id="getPatientInfos" resultType="com.diagbot.dto.PatientInfoDTO">
        SELECT a.* FROM `tran_patient_info` a
        LEFT JOIN tran_hospital_info b ON a.hospital_code = b.`code`
        WHERE a.is_deleted = 'N' AND b.is_deleted = 'N'
        AND a.`code` = #{patientCode} AND b.`code` = #{hospitalCode}
    </select>
    
    <select id="getTopPatientInfo" parameterType="com.diagbot.vo.GetTopPatientInfoVO" resultType="com.diagbot.dto.GetTopPatientInfoDTO">
   		SELECT
		a.id as hospitalId,
		a.`code` as hospitalCode,
		a.`name` as hospitalName,
		b.id as hospitalDeptId,
		b.icss_dept_name as selfDeptName,
		b.`code` as hospitalDeptCode,
		b.`name` as hospitalDeptName,
		c.id as doctorId,
		c.`code` as doctorCode,
		c.`name` as doctorName,
		d.id as patientId,
		d.`code` as patientCode,
		d.`name` as patientName,
		case when d.sex=1 then '男' when d.sex=2 then '女' else '未知' end as patientSex,
		d.id_no as patientIdNo,
		d.birthday as birthday
		FROM tran_hospital_info a LEFT JOIN tran_hospital_dept b 
		ON a.`code`=b.hospital_code and b.`status`=1 and b.is_deleted='N' and b.`code`=#{hospitalDeptCode} 	
		LEFT JOIN tran_doctor_info c 
		ON b.hospital_code=c.hospital_code and b.`code`=c.hospital_dept_code and c.is_deleted='N' and c.`code`=#{doctorCode}
		LEFT JOIN tran_patient_info d 
		ON a.code=d.hospital_code and d.is_deleted='N' and d.`code`=#{patientCode}
		WHERE a.is_deleted='N' 
		AND a.`status`=1
		AND a.`code`=#{hospitalCode} 
    </select>
</mapper>
