<?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.xinelu.manage.mapper.patientprehospitalization.PatientPreHospitalizationMapper">

	<resultMap id="BaseResultMap" type="com.xinelu.manage.domain.patientprehospitalization.PatientPreHospitalization">
		<id property="id" column="id" jdbcType="BIGINT" />
		<result property="residentId" column="resident_id" jdbcType="BIGINT" />
		<result property="patientId" column="patient_id" jdbcType="BIGINT" />
		<result property="patientName" column="patient_name" jdbcType="VARCHAR" />
		<result property="patientPhone" column="patient_phone" jdbcType="VARCHAR" />
		<result property="cardNo" column="card_no" jdbcType="VARCHAR" />
		<result property="sex" column="sex" jdbcType="VARCHAR" />
		<result property="birthDate" column="birth_date" jdbcType="DATE" />
		<result property="familyMemberPhone" column="family_member_phone" jdbcType="VARCHAR" />
		<result property="address" column="address" jdbcType="VARCHAR" />
		<result property="mainDiagnosis" column="main_diagnosis" jdbcType="VARCHAR" />
		<result property="hospitalAgencyId" column="hospital_agency_id" jdbcType="BIGINT" />
		<result property="hospitalAgencyName" column="hospital_agency_name" jdbcType="VARCHAR" />
		<result property="campusAgencyId" column="campus_agency_id" jdbcType="BIGINT" />
		<result property="campusAgencyName" column="campus_agency_name" jdbcType="VARCHAR" />
		<result property="departmentId" column="department_id" jdbcType="BIGINT" />
		<result property="departmentName" column="department_name" jdbcType="VARCHAR" />
		<result property="wardId" column="ward_id" jdbcType="BIGINT" />
		<result property="wardName" column="ward_name" jdbcType="VARCHAR" />
		<result property="appointmentTreatmentGroup" column="appointment_treatment_group" jdbcType="VARCHAR" />
		<result property="registrationNo" column="registration_no" jdbcType="VARCHAR" />
		<result property="registrationDate" column="registration_date" jdbcType="DATE" />
		<result property="appointmentDate" column="appointment_date" jdbcType="DATE" />
		<result property="certificateIssuingDoctorId" column="certificate_issuing_doctor_id" jdbcType="BIGINT" />
		<result property="certificateIssuingDoctorName" column="certificate_issuing_doctor_name" jdbcType="VARCHAR" />
		<result property="responsibleNurse" column="responsible_nurse" jdbcType="VARCHAR" />
		<result property="delFlag" column="del_flag" jdbcType="TINYINT" />
		<result property="createBy" column="create_by" jdbcType="VARCHAR" />
		<result property="createTime" column="create_time" jdbcType="TIMESTAMP" />
		<result property="updateBy" column="update_by" jdbcType="VARCHAR" />
		<result property="updateTime" column="update_time" jdbcType="TIMESTAMP" />
	</resultMap>

	<sql id="Base_Column_List">
		id,resident_id,patient_id,patient_name,
        patient_phone,card_no,sex,
        birth_date,family_member_phone,address,
        main_diagnosis,hospital_agency_id,hospital_agency_name,
        campus_agency_id,campus_agency_name,department_id,
        department_name,ward_id,ward_name,
        appointment_treatment_group,registration_no,registration_date,
        appointment_date,certificate_issuing_doctor_id,certificate_issuing_doctor_name,
        responsible_nurse,del_flag,create_by,
        create_time,update_by,update_time
	</sql>

	<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List" />
		from patient_pre_hospitalization
		where id = #{id,jdbcType=BIGINT}
	</select>

	<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
		delete
		from patient_pre_hospitalization
		where id = #{id,jdbcType=BIGINT}
	</delete>
	<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.xinelu.manage.domain.patientprehospitalization.PatientPreHospitalization" useGeneratedKeys="true">
		insert into patient_pre_hospitalization
		( id, resident_id, patient_id, patient_name
		, patient_phone, card_no, sex
		, birth_date, family_member_phone, address
		, main_diagnosis, hospital_agency_id, hospital_agency_name
		, campus_agency_id, campus_agency_name, department_id
		, department_name, ward_id, ward_name
		, appointment_treatment_group, registration_no, registration_date
		, appointment_date, certificate_issuing_doctor_id, certificate_issuing_doctor_name
		, responsible_nurse, del_flag, create_by
		, create_time, update_by, update_time)
		values ( #{id,jdbcType=BIGINT}, #{residentId,jdbcType=BIGINT}, #{patientId,jdbcType=BIGINT}, #{patientName,jdbcType=VARCHAR}
		       , #{patientPhone,jdbcType=VARCHAR}, #{cardNo,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}
		       , #{birthDate,jdbcType=DATE}, #{familyMemberPhone,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}
		       , #{mainDiagnosis,jdbcType=VARCHAR}, #{hospitalAgencyId,jdbcType=BIGINT}, #{hospitalAgencyName,jdbcType=VARCHAR}
		       , #{campusAgencyId,jdbcType=BIGINT}, #{campusAgencyName,jdbcType=VARCHAR}, #{departmentId,jdbcType=BIGINT}
		       , #{departmentName,jdbcType=VARCHAR}, #{wardId,jdbcType=BIGINT}, #{wardName,jdbcType=VARCHAR}
		       , #{appointmentTreatmentGroup,jdbcType=VARCHAR}, #{registrationNo,jdbcType=VARCHAR}, #{registrationDate,jdbcType=DATE}
		       , #{appointmentDate,jdbcType=DATE}, #{certificateIssuingDoctorId,jdbcType=BIGINT}, #{certificateIssuingDoctorName,jdbcType=VARCHAR}
		       , #{responsibleNurse,jdbcType=VARCHAR}, #{delFlag,jdbcType=TINYINT}, #{createBy,jdbcType=VARCHAR}
		       , #{createTime,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP})
	</insert>
	<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.xinelu.manage.domain.patientprehospitalization.PatientPreHospitalization" useGeneratedKeys="true">
		insert into patient_pre_hospitalization
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="id != null">id,</if>
			<if test="residentId != null">resident_id,</if>
			<if test="patientId != null">patient_id,</if>
			<if test="patientName != null">patient_name,</if>
			<if test="patientPhone != null">patient_phone,</if>
			<if test="cardNo != null">card_no,</if>
			<if test="sex != null">sex,</if>
			<if test="birthDate != null">birth_date,</if>
			<if test="familyMemberPhone != null">family_member_phone,</if>
			<if test="address != null">address,</if>
			<if test="mainDiagnosis != null">main_diagnosis,</if>
			<if test="hospitalAgencyId != null">hospital_agency_id,</if>
			<if test="hospitalAgencyName != null">hospital_agency_name,</if>
			<if test="campusAgencyId != null">campus_agency_id,</if>
			<if test="campusAgencyName != null">campus_agency_name,</if>
			<if test="departmentId != null">department_id,</if>
			<if test="departmentName != null">department_name,</if>
			<if test="wardId != null">ward_id,</if>
			<if test="wardName != null">ward_name,</if>
			<if test="appointmentTreatmentGroup != null">appointment_treatment_group,</if>
			<if test="registrationNo != null">registration_no,</if>
			<if test="registrationDate != null">registration_date,</if>
			<if test="appointmentDate != null">appointment_date,</if>
			<if test="certificateIssuingDoctorId != null">certificate_issuing_doctor_id,</if>
			<if test="certificateIssuingDoctorName != null">certificate_issuing_doctor_name,</if>
			<if test="responsibleNurse != null">responsible_nurse,</if>
			<if test="delFlag != null">del_flag,</if>
			<if test="createBy != null">create_by,</if>
			<if test="createTime != null">create_time,</if>
			<if test="updateBy != null">update_by,</if>
			<if test="updateTime != null">update_time,</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="id != null">#{id,jdbcType=BIGINT},</if>
			<if test="residentId != null">#{residentId,jdbcType=BIGINT},</if>
			<if test="patientId != null">#{patientId,jdbcType=BIGINT},</if>
			<if test="patientName != null">#{patientName,jdbcType=VARCHAR},</if>
			<if test="patientPhone != null">#{patientPhone,jdbcType=VARCHAR},</if>
			<if test="cardNo != null">#{cardNo,jdbcType=VARCHAR},</if>
			<if test="sex != null">#{sex,jdbcType=VARCHAR},</if>
			<if test="birthDate != null">#{birthDate,jdbcType=DATE},</if>
			<if test="familyMemberPhone != null">#{familyMemberPhone,jdbcType=VARCHAR},</if>
			<if test="address != null">#{address,jdbcType=VARCHAR},</if>
			<if test="mainDiagnosis != null">#{mainDiagnosis,jdbcType=VARCHAR},</if>
			<if test="hospitalAgencyId != null">#{hospitalAgencyId,jdbcType=BIGINT},</if>
			<if test="hospitalAgencyName != null">#{hospitalAgencyName,jdbcType=VARCHAR},</if>
			<if test="campusAgencyId != null">#{campusAgencyId,jdbcType=BIGINT},</if>
			<if test="campusAgencyName != null">#{campusAgencyName,jdbcType=VARCHAR},</if>
			<if test="departmentId != null">#{departmentId,jdbcType=BIGINT},</if>
			<if test="departmentName != null">#{departmentName,jdbcType=VARCHAR},</if>
			<if test="wardId != null">#{wardId,jdbcType=BIGINT},</if>
			<if test="wardName != null">#{wardName,jdbcType=VARCHAR},</if>
			<if test="appointmentTreatmentGroup != null">#{appointmentTreatmentGroup,jdbcType=VARCHAR},</if>
			<if test="registrationNo != null">#{registrationNo,jdbcType=VARCHAR},</if>
			<if test="registrationDate != null">#{registrationDate,jdbcType=DATE},</if>
			<if test="appointmentDate != null">#{appointmentDate,jdbcType=DATE},</if>
			<if test="certificateIssuingDoctorId != null">#{certificateIssuingDoctorId,jdbcType=BIGINT},</if>
			<if test="certificateIssuingDoctorName != null">#{certificateIssuingDoctorName,jdbcType=VARCHAR},</if>
			<if test="responsibleNurse != null">#{responsibleNurse,jdbcType=VARCHAR},</if>
			<if test="delFlag != null">#{delFlag,jdbcType=TINYINT},</if>
			<if test="createBy != null">#{createBy,jdbcType=VARCHAR},</if>
			<if test="createTime != null">#{createTime,jdbcType=TIMESTAMP},</if>
			<if test="updateBy != null">#{updateBy,jdbcType=VARCHAR},</if>
			<if test="updateTime != null">#{updateTime,jdbcType=TIMESTAMP},</if>
		</trim>
	</insert>
	<update id="updateByPrimaryKeySelective" parameterType="com.xinelu.manage.domain.patientprehospitalization.PatientPreHospitalization">
		update patient_pre_hospitalization
		<set>
			<if test="residentId != null">
				resident_id = #{residentId,jdbcType=BIGINT},
			</if>
			<if test="patientId != null">
				patient_id = #{patientId,jdbcType=BIGINT},
			</if>
			<if test="patientName != null">
				patient_name = #{patientName,jdbcType=VARCHAR},
			</if>
			<if test="patientPhone != null">
				patient_phone = #{patientPhone,jdbcType=VARCHAR},
			</if>
			<if test="cardNo != null">
				card_no = #{cardNo,jdbcType=VARCHAR},
			</if>
			<if test="sex != null">
				sex = #{sex,jdbcType=VARCHAR},
			</if>
			<if test="birthDate != null">
				birth_date = #{birthDate,jdbcType=DATE},
			</if>
			<if test="familyMemberPhone != null">
				family_member_phone = #{familyMemberPhone,jdbcType=VARCHAR},
			</if>
			<if test="address != null">
				address = #{address,jdbcType=VARCHAR},
			</if>
			<if test="mainDiagnosis != null">
				main_diagnosis = #{mainDiagnosis,jdbcType=VARCHAR},
			</if>
			<if test="hospitalAgencyId != null">
				hospital_agency_id = #{hospitalAgencyId,jdbcType=BIGINT},
			</if>
			<if test="hospitalAgencyName != null">
				hospital_agency_name = #{hospitalAgencyName,jdbcType=VARCHAR},
			</if>
			<if test="campusAgencyId != null">
				campus_agency_id = #{campusAgencyId,jdbcType=BIGINT},
			</if>
			<if test="campusAgencyName != null">
				campus_agency_name = #{campusAgencyName,jdbcType=VARCHAR},
			</if>
			<if test="departmentId != null">
				department_id = #{departmentId,jdbcType=BIGINT},
			</if>
			<if test="departmentName != null">
				department_name = #{departmentName,jdbcType=VARCHAR},
			</if>
			<if test="wardId != null">
				ward_id = #{wardId,jdbcType=BIGINT},
			</if>
			<if test="wardName != null">
				ward_name = #{wardName,jdbcType=VARCHAR},
			</if>
			<if test="appointmentTreatmentGroup != null">
				appointment_treatment_group = #{appointmentTreatmentGroup,jdbcType=VARCHAR},
			</if>
			<if test="registrationNo != null">
				registration_no = #{registrationNo,jdbcType=VARCHAR},
			</if>
			<if test="registrationDate != null">
				registration_date = #{registrationDate,jdbcType=DATE},
			</if>
			<if test="appointmentDate != null">
				appointment_date = #{appointmentDate,jdbcType=DATE},
			</if>
			<if test="certificateIssuingDoctorId != null">
				certificate_issuing_doctor_id = #{certificateIssuingDoctorId,jdbcType=BIGINT},
			</if>
			<if test="certificateIssuingDoctorName != null">
				certificate_issuing_doctor_name = #{certificateIssuingDoctorName,jdbcType=VARCHAR},
			</if>
			<if test="responsibleNurse != null">
				responsible_nurse = #{responsibleNurse,jdbcType=VARCHAR},
			</if>
			<if test="delFlag != null">
				del_flag = #{delFlag,jdbcType=TINYINT},
			</if>
			<if test="createBy != null">
				create_by = #{createBy,jdbcType=VARCHAR},
			</if>
			<if test="createTime != null">
				create_time = #{createTime,jdbcType=TIMESTAMP},
			</if>
			<if test="updateBy != null">
				update_by = #{updateBy,jdbcType=VARCHAR},
			</if>
			<if test="updateTime != null">
				update_time = #{updateTime,jdbcType=TIMESTAMP},
			</if>
		</set>
		where id = #{id,jdbcType=BIGINT}
	</update>
	<update id="updateByPrimaryKey" parameterType="com.xinelu.manage.domain.patientprehospitalization.PatientPreHospitalization">
		update patient_pre_hospitalization
		set
			resident_id                     = #{residentId,jdbcType=BIGINT},
		    patient_id                      = #{patientId,jdbcType=BIGINT},
		    patient_name                    = #{patientName,jdbcType=VARCHAR},
		    patient_phone                   = #{patientPhone,jdbcType=VARCHAR},
		    card_no                         = #{cardNo,jdbcType=VARCHAR},
		    sex                             = #{sex,jdbcType=VARCHAR},
		    birth_date                      = #{birthDate,jdbcType=DATE},
		    family_member_phone             = #{familyMemberPhone,jdbcType=VARCHAR},
		    address                         = #{address,jdbcType=VARCHAR},
		    main_diagnosis                  = #{mainDiagnosis,jdbcType=VARCHAR},
		    hospital_agency_id              = #{hospitalAgencyId,jdbcType=BIGINT},
		    hospital_agency_name            = #{hospitalAgencyName,jdbcType=VARCHAR},
		    campus_agency_id                = #{campusAgencyId,jdbcType=BIGINT},
		    campus_agency_name              = #{campusAgencyName,jdbcType=VARCHAR},
		    department_id                   = #{departmentId,jdbcType=BIGINT},
		    department_name                 = #{departmentName,jdbcType=VARCHAR},
		    ward_id                         = #{wardId,jdbcType=BIGINT},
		    ward_name                       = #{wardName,jdbcType=VARCHAR},
		    appointment_treatment_group     = #{appointmentTreatmentGroup,jdbcType=VARCHAR},
		    registration_no                 = #{registrationNo,jdbcType=VARCHAR},
		    registration_date               = #{registrationDate,jdbcType=DATE},
		    appointment_date                = #{appointmentDate,jdbcType=DATE},
		    certificate_issuing_doctor_id   = #{certificateIssuingDoctorId,jdbcType=BIGINT},
		    certificate_issuing_doctor_name = #{certificateIssuingDoctorName,jdbcType=VARCHAR},
		    responsible_nurse               = #{responsibleNurse,jdbcType=VARCHAR},
		    del_flag                        = #{delFlag,jdbcType=TINYINT},
		    create_by                       = #{createBy,jdbcType=VARCHAR},
		    create_time                     = #{createTime,jdbcType=TIMESTAMP},
		    update_by                       = #{updateBy,jdbcType=VARCHAR},
		    update_time                     = #{updateTime,jdbcType=TIMESTAMP}
		where id = #{id,jdbcType=BIGINT}
	</update>

	<select id="selectList" parameterType="com.xinelu.manage.dto.patientinfo.PatientInfoDto" resultMap="BaseResultMap">
		select
		p.id,p.patient_id,patient.patient_name,
		patient.patient_phone,patient.card_no,patient.sex,
		patient.birth_date,patient.family_member_phone,patient.address,
		p.main_diagnosis,p.hospital_agency_id,p.hospital_agency_name,
		p.campus_agency_id,p.campus_agency_name,
		p.department_id,p.department_name,p.ward_id,p.ward_name,
		p.appointment_treatment_group,p.registration_no,p.registration_date,
		p.appointment_date, p.certificate_issuing_doctor_id,p.certificate_issuing_doctor_name, p.responsible_nurse,patient.create_time
		from patient_info patient left join patient_pre_hospitalization p on p.patient_id = patient.id
		<where>
			p.del_flag = 0 and patient.patient_type = 'PRE_HOSPITALIZED_PATIENT'
			<if test="patientName != null  and patientName != ''">
				and patient.patient_name = #{patientName}
			</if>
			<if test="patientPhone != null  and patientPhone != ''">
				and patient.patient_phone like concat('%', #{patientPhone}, '%')
			</if>
			<if test="cardNo != null  and cardNo != ''">
				and patient.card_no = #{cardNo}
			</if>
			<if test="hospitalAgencyId != null ">
				and p.hospital_agency_id = #{hospitalAgencyId}
			</if>
			<if test="campusAgencyId != null ">
				and p.campus_agency_id = #{campusAgencyId}
			</if>
			<if test="departmentId != null ">
				and p.department_id = #{departmentId}
			</if>
			<if test="wardId != null ">
				and p.ward_id = #{wardId}
			</if>
			<if test="appointmentTreatmentGroup != null  and appointmentTreatmentGroup != ''">
				and p.appointment_treatment_group = #{appointmentTreatmentGroup}
			</if>
			<if test="certificateIssuingDoctorId != null  and certificateIssuingDoctorId != ''">
				and p.certificate_issuing_doctor_id = #{certificateIssuingDoctorId}
			</if>
			<if test="appointmentDateStart != null ">
				and date_format(p.appointment_date, '%y%m%d') >= date_format(#{appointmentDateStart}, '%y%m%d')
			</if>
			<if test="appointmentDateEnd != null ">
				and date_format(p.appointment_date, '%y%m%d') &lt;= date_format(#{appointmentDateEnd}, '%y%m%d')
			</if>
			<if test="certificateIssuingDoctorId != null">
				and p.certificate_issuing_doctor_id = #{certificateIssuingDoctorId}
			</if>
			<if test="registrationNo != null  and registrationNo != ''">
				and p.registration_no = #{registrationNo}
			</if>
			<!-- 数据范围过滤 -->
			${params.dataScope}
		</where>
		order by p.id desc
	</select>

	<select id="selectApplyList" parameterType="com.xinelu.manage.domain.patientprehospitalization.PatientPreHospitalization" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List" />
		from patient_pre_hospitalization
		<where>
			del_flag = 0
			<if test="cardNo != null  and cardNo != ''">
				and card_no = #{cardNo}
			</if>
			<if test="appointmentDate != null ">
				and date_format(appointment_date, '%y%m%d') = date_format(#{appointmentDate}, '%y%m%d')
			</if>
		</where>
	</select>
	<update id="deleteByIds" parameterType="String">
		update patient_pre_hospitalization set del_flag = 1 where id in
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</update>
	<insert id="insertBatch">
		insert into patient_pre_hospitalization(id,resident_id,patient_id,patient_name,
		patient_phone,card_no,sex,
		birth_date,family_member_phone,address,
		main_diagnosis,hospital_agency_id,hospital_agency_name,
		campus_agency_id,campus_agency_name,department_id,
		department_name,ward_id,ward_name,
		appointment_treatment_group,registration_no,registration_date,
		appointment_date,certificate_issuing_doctor_id,certificate_issuing_doctor_name,
		responsible_nurse,del_flag,
		create_by,create_time,update_by,
		update_time)
		values
		<foreach collection="recordList" item="item" separator=",">
			(#{item.id,jdbcType=NUMERIC},#{item.residentId,jdbcType=NUMERIC},#{item.patientId,jdbcType=NUMERIC},#{item.patientName,jdbcType=VARCHAR},
			#{item.patientPhone,jdbcType=VARCHAR},#{item.cardNo,jdbcType=VARCHAR},#{item.sex,jdbcType=VARCHAR},
			#{item.birthDate,jdbcType=TIMESTAMP},#{item.familyMemberPhone,jdbcType=VARCHAR},#{item.address,jdbcType=VARCHAR},
			#{item.mainDiagnosis,jdbcType=VARCHAR},#{item.hospitalAgencyId,jdbcType=NUMERIC},#{item.hospitalAgencyName,jdbcType=VARCHAR},
			#{item.campusAgencyId,jdbcType=NUMERIC},#{item.campusAgencyName,jdbcType=VARCHAR},#{item.departmentId,jdbcType=NUMERIC},
			#{item.departmentName,jdbcType=VARCHAR},#{item.wardId,jdbcType=NUMERIC},#{item.wardName,jdbcType=VARCHAR},
			#{item.appointmentTreatmentGroup,jdbcType=VARCHAR},#{item.registrationNo,jdbcType=VARCHAR},#{item.registrationDate,jdbcType=TIMESTAMP},
			#{item.appointmentDate,jdbcType=TIMESTAMP},#{item.certificateIssuingDoctorId,jdbcType=NUMERIC},#{item.certificateIssuingDoctorName,jdbcType=VARCHAR},
			#{item.responsibleNurse,jdbcType=VARCHAR},#{item.delFlag,jdbcType=NUMERIC},
			#{item.createBy,jdbcType=VARCHAR},#{item.createTime,jdbcType=TIMESTAMP},#{item.updateBy,jdbcType=VARCHAR},
			#{item.updateTime,jdbcType=TIMESTAMP})
		</foreach>
	</insert>
</mapper>
