<?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.patientvisitrecord.PatientVisitRecordMapper">

    <resultMap type="com.xinelu.manage.domain.patientvisitrecord.PatientVisitRecord" id="PatientVisitRecordResult">
	    <result property="id" column="id"/>
	    <result property="residentId" column="resident_id"/>
	    <result property="patientId" column="patient_id"/>
	    <result property="patientPreHospitalizationId" column="patient_pre_hospitalization_id"/>
	    <result property="cardNo" column="card_no"/>
	    <result property="patientName" column="patient_name"/>
	    <result property="patientPhone" column="patient_phone"/>
	    <result property="familyMemberPhone" column="family_member_phone"/>
	    <result property="address" column="address"/>
	    <result property="sex" column="sex"/>
	    <result property="birthDate" column="birth_date"/>
	    <result property="age" column="age"/>
	    <result property="nation" column="nation"/>
	    <result property="visitMethod" column="visit_method"/>
	    <result property="visitDate" column="visit_date"/>
	    <result property="visitName" column="visit_name"/>
	    <result property="hospitalAgencyId" column="hospital_agency_id"/>
	    <result property="hospitalAgencyName" column="hospital_agency_name"/>
	    <result property="campusAgencyId" column="campus_agency_id"/>
	    <result property="campusAgencyName" column="campus_agency_name"/>
	    <result property="departmentId" column="department_id"/>
	    <result property="departmentName" column="department_name"/>
	    <result property="wardId" column="ward_id"/>
	    <result property="wardName" column="ward_name"/>
	    <result property="attendingPhysicianId" column="attending_physician_id"/>
	    <result property="attendingPhysicianName" column="attending_physician_name"/>
	    <result property="mainDiagnosis" column="main_diagnosis"/>
	    <result property="marriage" column="marriage"/>
	    <result property="medicalHistoryNarrator" column="medical_history_narrator"/>
	    <result property="admissionTime" column="admission_time"/>
	    <result property="dischargeTime" column="discharge_time"/>
	    <result property="recordTime" column="record_time"/>
	    <result property="outpatientVisitInfo" column="outpatient_visit_info"/>
	    <result property="hospitalizationDays" column="hospitalization_days"/>
	    <result property="inHospitalInfo" column="in_hospital_info"/>
	    <result property="outHospitalInfo" column="out_hospital_info"/>
	    <result property="visitSerialNumber" column="visit_serial_number"/>
	    <result property="inHospitalNumber" column="in_hospital_number"/>
	    <result property="responsibleNurse" column="responsible_nurse"/>
	    <result property="surgicalName" column="surgical_name"/>
	    <result property="surgicalRecord" column="surgical_record"/>
	    <result property="delFlag" column="del_flag"/>
	    <result property="createBy" column="create_by"/>
	    <result property="createTime" column="create_time"/>
	    <result property="updateBy" column="update_by"/>
	    <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectPatientVisitRecordVo">
	    select id, resident_id, patient_id, card_no, patient_name, patient_phone, family_member_phone, address, sex, birth_date, age, nation,
	           visit_method, visit_date, visit_name, hospital_agency_id, hospital_agency_name, campus_agency_id, campus_agency_name,
	           department_id, department_name, ward_id, ward_name, attending_physician_id, attending_physician_name, main_diagnosis,
	           marriage, medical_history_narrator, admission_time, discharge_time, record_time, outpatient_visit_info, hospitalization_days,
	           in_hospital_info, out_hospital_info, visit_serial_number, in_hospital_number, responsible_nurse, surgical_name, surgical_record,del_flag,
	           create_by, create_time, update_by, update_time from patient_visit_record
    </sql>

    <select id="selectPatientVisitRecordList" parameterType="com.xinelu.manage.dto.patientvisitrecord.PatientVisitRecordDto" resultMap="PatientVisitRecordResult">
        <include refid="selectPatientVisitRecordVo"/>
        <where>
			del_flag = 0
	        <if test="residentId != null ">
		        and resident_id = #{residentId}
	        </if>
	        <if test="patientId != null ">
		        and patient_id = #{patientId}
	        </if>
	        <if test="hospitalAgencyId != null">
	        	and hospital_agency_id = #{hospitalAgencyId}
	        </if>
	        <if test="visitMethod != null ">
		        and visit_method = #{visitMethod}
	        </if>
	        <if test="inHospitalNumber != null ">
		        and in_hospital_number = #{inHospitalNumber}
	        </if>
            <if test="visitDateStart != null ">
                and date_format(visit_date,'%y%m%d') >= date_format(#{visitDateStart},'%y%m%d')
            </if>
	        <if test="visitDateEnd != null ">
		        and date_format(visit_date,'%y%m%d') &lt;= date_format(#{visitDateEnd},'%y%m%d')
	        </if>
        </where>
        order by visit_date desc
    </select>

	<select id="selectPatientVisitRecordById" parameterType="Long"
		resultMap="PatientVisitRecordResult">
		<include refid="selectPatientVisitRecordVo"/>
		where id = #{id}
	</select>

	<select id="getByPatientId" parameterType="Long"
		resultType="com.xinelu.manage.vo.patientvisitrecord.PatientVisitRecordVo">
		select patient.id as patientId, patient.resident_id, patient.patient_name, patient.patient_phone, patient.family_member_phone,
		       patient.birth_date, patient.card_no, patient.sex, patient.address, patient.visit_method,
		       patient.hospital_agency_id, patient.hospital_agency_name, patient.campus_agency_id, patient.campus_agency_name,
		       patient.department_id, patient.department_name, patient.ward_id, patient.ward_name, patient.patient_source,
		       record.id as patientVisitRecordId, record.visit_method, record.visit_date, record.main_diagnosis,
		       record.admission_time, record.discharge_time, record.in_hospital_info, record.out_hospital_info, record.surgical_name, record.surgical_record,
		       sign.portait_check_status, sign.portait_check_date
        from patient_info patient
		left join patient_visit_record record on patient.patient_visit_record_id = record.id
        left join sign_patient_record sign on sign.id = patient.sign_patient_record_id
		where patient.id = #{patientId}
	</select>

	<insert id="insertPatientVisitRecord" parameterType="PatientVisitRecord" useGeneratedKeys="true"
		keyProperty="id">
		insert into patient_visit_record
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="residentId != null">resident_id,
			</if>
			<if test="patientId != null">patient_id,
			</if>
			<if test="patientPreHospitalizationId != null">patient_pre_hospitalization_id,
			</if>
			<if test="cardNo != null">card_no,
			</if>
			<if test="patientName != null">patient_name,
			</if>
			<if test="patientPhone != null">patient_phone,
			</if>
			<if test="familyMemberPhone != null">family_member_phone,
			</if>
			<if test="address != null">address,
			</if>
			<if test="sex != null">sex,
			</if>
			<if test="birthDate != null">birth_date,
			</if>
			<if test="age != null">age,
			</if>
			<if test="nation != null">nation,
			</if>
			<if test="visitMethod != null">visit_method,
			</if>
			<if test="visitDate != null">visit_date,
			</if>
			<if test="visitName != null">visit_name,
			</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="attendingPhysicianId != null">attending_physician_id,
			</if>
			<if test="attendingPhysicianName != null">attending_physician_name,
			</if>
			<if test="mainDiagnosis != null">main_diagnosis,
			</if>
			<if test="marriage != null">marriage,
			</if>
			<if test="medicalHistoryNarrator != null">medical_history_narrator,
			</if>
			<if test="admissionTime != null">admission_time,
			</if>
			<if test="dischargeTime != null">discharge_time,
			</if>
			<if test="recordTime != null">record_time,
			</if>
			<if test="outpatientVisitInfo != null">outpatient_visit_info,
			</if>
			<if test="hospitalizationDays != null">hospitalization_days,
			</if>
			<if test="inHospitalInfo != null">in_hospital_info,
			</if>
			<if test="outHospitalInfo != null">out_hospital_info,
			</if>
			<if test="visitSerialNumber != null">visit_serial_number,
			</if>
			<if test="inHospitalNumber != null">in_hospital_number,
			</if>
			<if test="responsibleNurse != null">responsible_nurse,
			</if>
			<if test="surgicalName != null">surgical_name,
			</if>
			<if test="surgicalRecord != null">surgical_record,
			</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="residentId != null">#{residentId},
			</if>
			<if test="patientId != null">#{patientId},
			</if>
			<if test="patientPreHospitalizationId != null">#{patientPreHospitalizationId},
			</if>
			<if test="cardNo != null">#{cardNo},
			</if>
			<if test="patientName != null">#{patientName},
			</if>
			<if test="patientPhone != null">#{patientPhone},
			</if>
			<if test="familyMemberPhone != null">#{familyMemberPhone},
			</if>
			<if test="address != null">#{address},
			</if>
			<if test="sex != null">#{sex},
			</if>
			<if test="birthDate != null">#{birthDate},
			</if>
			<if test="age != null">#{age},
			</if>
			<if test="nation != null">#{nation},
			</if>
			<if test="visitMethod != null">#{visitMethod},
			</if>
			<if test="visitDate != null">#{visitDate},
			</if>
			<if test="visitName != null">#{visitName},
			</if>
			<if test="hospitalAgencyId != null">#{hospitalAgencyId},
			</if>
			<if test="hospitalAgencyName != null">#{hospitalAgencyName},
			</if>
			<if test="campusAgencyId != null">#{campusAgencyId},
			</if>
			<if test="campusAgencyName != null">#{campusAgencyName},
			</if>
			<if test="departmentId != null">#{departmentId},
			</if>
			<if test="departmentName != null">#{departmentName},
			</if>
			<if test="wardId != null">#{wardId},
			</if>
			<if test="wardName != null">#{wardName},
			</if>
			<if test="attendingPhysicianId != null">#{attendingPhysicianId},
			</if>
			<if test="attendingPhysicianName != null">#{attendingPhysicianName},
			</if>
			<if test="mainDiagnosis != null">#{mainDiagnosis},
			</if>
			<if test="marriage != null">#{marriage},
			</if>
			<if test="medicalHistoryNarrator != null">#{medicalHistoryNarrator},
			</if>
			<if test="admissionTime != null">#{admissionTime},
			</if>
			<if test="dischargeTime != null">#{dischargeTime},
			</if>
			<if test="recordTime != null">#{recordTime},
			</if>
			<if test="outpatientVisitInfo != null">#{outpatientVisitInfo},
			</if>
			<if test="hospitalizationDays != null">#{hospitalizationDays},
			</if>
			<if test="inHospitalInfo != null">#{inHospitalInfo},
			</if>
			<if test="outHospitalInfo != null">#{outHospitalInfo},
			</if>
			<if test="visitSerialNumber != null">#{visitSerialNumber},
			</if>
			<if test="inHospitalNumber != null">#{inHospitalNumber},
			</if>
			<if test="responsibleNurse != null">#{responsibleNurse},
			</if>
			<if test="surgicalName != null">#{surgicalName},
			</if>
			<if test="surgicalRecord != null">#{surgicalRecord},
			</if>
			<if test="delFlag != null">#{delFlag},
			</if>
			<if test="createBy != null">#{createBy},
			</if>
			<if test="createTime != null">#{createTime},
			</if>
			<if test="updateBy != null">#{updateBy},
			</if>
			<if test="updateTime != null">#{updateTime},
			</if>
		</trim>
	</insert>

	<update id="updatePatientVisitRecord" parameterType="PatientVisitRecord">
		update patient_visit_record
		<trim prefix="SET" suffixOverrides=",">
			<if test="residentId != null">resident_id =
				#{residentId},
			</if>
			<if test="patientId != null">patient_id =
				#{patientId},
			</if>
			<if test="cardNo != null">card_no =
				#{cardNo},
			</if>
			<if test="patientName != null">patient_name =
				#{patientName},
			</if>
			<if test="patientPhone != null">patient_phone =
				#{patientPhone},
			</if>
			<if test="familyMemberPhone != null">family_member_phone =
				#{familyMemberPhone},
			</if>
			<if test="address != null">address =
				#{address},
			</if>
			<if test="sex != null">sex =
				#{sex},
			</if>
			<if test="birthDate != null">birth_date =
				#{birthDate},
			</if>
			<if test="age != null">age =
				#{age},
			</if>
			<if test="nation != null">nation =
				#{nation},
			</if>
			<if test="visitMethod != null">visit_method =
				#{visitMethod},
			</if>
			<if test="visitDate != null">visit_date =
				#{visitDate},
			</if>
			<if test="visitName != null">visit_name =
				#{visitName},
			</if>
			<if test="hospitalAgencyId != null">hospital_agency_id =
				#{hospitalAgencyId},
			</if>
			<if test="hospitalAgencyName != null">hospital_agency_name =
				#{hospitalAgencyName},
			</if>
			<if test="campusAgencyId != null">campus_agency_id =
				#{campusAgencyId},
			</if>
			<if test="campusAgencyName != null">campus_agency_name =
				#{campusAgencyName},
			</if>
			<if test="departmentId != null">department_id =
				#{departmentId},
			</if>
			<if test="departmentName != null">department_name =
				#{departmentName},
			</if>
			<if test="wardId != null">ward_id =
				#{wardId},
			</if>
			<if test="wardName != null">ward_name =
				#{wardName},
			</if>
			<if test="attendingPhysicianId != null">attending_physician_id =
				#{attendingPhysicianId},
			</if>
			<if test="attendingPhysicianName != null">attending_physician_name =
				#{attendingPhysicianName},
			</if>
			<if test="mainDiagnosis != null">main_diagnosis =
				#{mainDiagnosis},
			</if>
			<if test="marriage != null">marriage =
				#{marriage},
			</if>
			<if test="medicalHistoryNarrator != null">medical_history_narrator =
				#{medicalHistoryNarrator},
			</if>
			<if test="admissionTime != null">admission_time =
				#{admissionTime},
			</if>
			<if test="dischargeTime != null">discharge_time =
				#{dischargeTime},
			</if>
			<if test="recordTime != null">record_time =
				#{recordTime},
			</if>
			<if test="outpatientVisitInfo != null">outpatient_visit_info =
				#{outpatientVisitInfo},
			</if>
			<if test="hospitalizationDays != null">hospitalization_days =
				#{hospitalizationDays},
			</if>
			<if test="inHospitalInfo != null">in_hospital_info =
				#{inHospitalInfo},
			</if>
			<if test="outHospitalInfo != null">out_hospital_info =
				#{outHospitalInfo},
			</if>
			<if test="visitSerialNumber != null">visit_serial_number =
				#{visitSerialNumber},
			</if>
			<if test="inHospitalNumber != null">in_hospital_number =
				#{inHospitalNumber},
			</if>
			<if test="responsibleNurse != null">responsible_nurse =
				#{responsibleNurse},
			</if>
			<if test="surgicalName != null">surgical_name =
				#{surgicalName},
			</if>
			<if test="surgicalRecord != null">surgical_record =
				#{surgicalRecord},
			</if>
			<if test="delFlag != null">del_flag =
				#{delFlag},
			</if>
			<if test="createBy != null">create_by =
				#{createBy},
			</if>
			<if test="createTime != null">create_time =
				#{createTime},
			</if>
			<if test="updateBy != null">update_by =
				#{updateBy},
			</if>
			<if test="updateTime != null">update_time =
				#{updateTime},
			</if>
		</trim>
		where id = #{id}
	</update>
    <update id="deletePatientVisitRecordById" parameterType="Long">
        update patient_visit_record set del_flag = 1 where id = #{id}
    </update>

    <update id="deletePatientVisitRecordByIds" parameterType="String">
	    update patient_visit_record set del_flag = 1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
	<insert id="insertBatch">
		insert into patient_visit_record(resident_id,patient_id,card_no,
		patient_name,patient_phone,family_member_phone,
		address,sex,birth_date,
		age,nation,visit_method,
		visit_date,visit_name,hospital_agency_id,
		hospital_agency_name,campus_agency_id,campus_agency_name,
		department_id,department_name,ward_id,
		ward_name,attending_physician_id,attending_physician_name,
		main_diagnosis,marriage,medical_history_narrator,
		admission_time,discharge_time,record_time,
		outpatient_visit_info,hospitalization_days,in_hospital_info,
		out_hospital_info,visit_serial_number,in_hospital_number,
		responsible_nurse,surgical_name,surgical_record,del_flag,
		create_by,create_time,update_by,update_time)
		values
		<foreach collection="patientVisitRecordCollection" item="item" separator=",">
			(#{item.residentId,jdbcType=BIGINT},#{item.patientId,jdbcType=BIGINT},#{item.cardNo,jdbcType=VARCHAR},
			#{item.patientName,jdbcType=VARCHAR},#{item.patientPhone,jdbcType=VARCHAR},#{item.familyMemberPhone,jdbcType=VARCHAR},
			#{item.address,jdbcType=VARCHAR},#{item.sex,jdbcType=VARCHAR},#{item.birthDate,jdbcType=TIMESTAMP},
			#{item.age,jdbcType=NUMERIC},#{item.nation,jdbcType=VARCHAR},#{item.visitMethod,jdbcType=VARCHAR},
			#{item.visitDate,jdbcType=TIMESTAMP},#{item.visitName,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.attendingPhysicianId,jdbcType=NUMERIC},#{item.attendingPhysicianName,jdbcType=VARCHAR},
			#{item.mainDiagnosis,jdbcType=VARCHAR},#{item.marriage,jdbcType=VARCHAR},#{item.medicalHistoryNarrator,jdbcType=VARCHAR},
			#{item.admissionTime,jdbcType=TIMESTAMP},#{item.dischargeTime,jdbcType=TIMESTAMP},#{item.recordTime,jdbcType=TIMESTAMP},
			#{item.outpatientVisitInfo,jdbcType=VARCHAR},#{item.hospitalizationDays,jdbcType=NUMERIC},#{item.inHospitalInfo,jdbcType=VARCHAR},
			#{item.outHospitalInfo,jdbcType=VARCHAR},#{item.visitSerialNumber,jdbcType=VARCHAR},#{item.inHospitalNumber,jdbcType=VARCHAR},
			#{item.responsibleNurse,jdbcType=VARCHAR},#{item.surgicalName,jdbcType=VARCHAR},#{item.surgicalRecord,jdbcType=VARCHAR},#{item.delFlag,jdbcType=TINYINT},
			#{item.createBy,jdbcType=VARCHAR},#{item.createTime,jdbcType=TIMESTAMP},#{item.updateBy,jdbcType=VARCHAR},
			#{item.updateTime,jdbcType=TIMESTAMP})
		</foreach>
	</insert>
	<select id="getLastRecord" resultMap="PatientVisitRecordResult">
		<include refid="selectPatientVisitRecordVo"/>
		<where>
			del_flag = 0
			<if test="patientId != null">
				AND patient_id = #{patientId,jdbcType=BIGINT}
			</if>
			<if test="hospitalAgencyId != null">
				and hospital_agency_id = #{hospitalAgencyId,jdbcType=BIGINT}
			</if>
			<if test="inHospitalNumber != null">
				and in_hospital_number = #{inHospitalNumber,jdbcType=VARCHAR}
			</if>
			<if test="cardNo != null">
				and card_no != #{cardNo,jdbcType=VARCHAR}
			</if>
		</where>
	</select>
</mapper>