<?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.patientinfoimport.PatientInfoImportMapper">

    <resultMap type="com.xinelu.manage.domain.patientinfoimport.PatientInfoImport" id="PatientInfoImportResult">
        <result property="id" column="id"/>
        <result property="orderNum" column="order_num"/>
        <result property="deptAlias" column="dept_alias"/>
        <result property="visitDate" column="visit_date"/>
        <result property="inHospitalNumber" column="in_hospital_number"/>
        <result property="patientName" column="patient_name"/>
        <result property="cardNo" column="card_no"/>
        <result property="age" column="age"/>
        <result property="mainDiagnosis" column="main_diagnosis"/>
        <result property="patientPhone" column="patient_phone"/>
        <result property="departmentId" column="department_id"/>
        <result property="departmentName" column="department_name"/>
        <result property="sn" column="sn"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="hospitalAgencyId" column="hospital_agency_id"/>
        <result property="hospitalAgencyName" column="hospital_agency_name"/>
    </resultMap>

    <sql id="selectPatientInfoImportVo">
        select id,
               order_num,
               dept_alias,
               visit_date,
               in_hospital_number,
               patient_name,
               card_no,
               age,
               main_diagnosis,
               patient_phone,
               department_id,
               department_name,
               sn,
               create_time,
               create_by,
               hospital_agency_id,
               hospital_agency_name
        from patient_info_import
    </sql>

    <select id="selectPatientInfoImportList"
            parameterType="com.xinelu.manage.domain.patientinfoimport.PatientInfoImport"
            resultMap="PatientInfoImportResult">
        <include refid="selectPatientInfoImportVo"/>
        <where>
            <if test="orderNum != null ">
                and order_num = #{orderNum}
            </if>
            <if test="deptAlias != null  and deptAlias != ''">
                and dept_alias = #{deptAlias}
            </if>
            <if test="visitDate != null ">
                and visit_date = #{visitDate}
            </if>
            <if test="inHospitalNumber != null  and inHospitalNumber != ''">
                and in_hospital_number = #{inHospitalNumber}
            </if>
            <if test="patientName != null  and patientName != ''">
                and patient_name like concat('%', #{patientName}, '%')
            </if>
            <if test="cardNo != null  and cardNo != ''">
                and card_no = #{cardNo}
            </if>
            <if test="age != null  and age != ''">
                and age = #{age}
            </if>
            <if test="mainDiagnosis != null  and mainDiagnosis != ''">
                and main_diagnosis = #{mainDiagnosis}
            </if>
            <if test="patientPhone != null  and patientPhone != ''">
                and patient_phone = #{patientPhone}
            </if>
            <if test="departmentId != null ">
                and department_id = #{departmentId}
            </if>
            <if test="departmentName != null  and departmentName != ''">
                and department_name like concat('%', #{departmentName}, '%')
            </if>
            <if test="sn != null  and sn != ''">
                and sn = #{sn}
            </if>
            <if test="createTime != null">
                and DATE_FORMAT(create_time, '%Y-%m-%d') = DATE_FORMAT(#{createTime}, '%Y-%m-%d')
            </if>
        </where>
    </select>

    <select id="selectPatientInfoImportById" parameterType="Long"
            resultMap="PatientInfoImportResult">
        <include refid="selectPatientInfoImportVo"/>
        where id = #{id}
    </select>

    <insert id="insertPatientInfoImport" parameterType="com.xinelu.manage.domain.patientinfoimport.PatientInfoImport">
        insert into patient_info_import
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,
            </if>
            <if test="orderNum != null">order_num,
            </if>
            <if test="deptAlias != null">dept_alias,
            </if>
            <if test="visitDate != null">visit_date,
            </if>
            <if test="inHospitalNumber != null">in_hospital_number,
            </if>
            <if test="patientName != null">patient_name,
            </if>
            <if test="cardNo != null">card_no,
            </if>
            <if test="age != null">age,
            </if>
            <if test="mainDiagnosis != null">main_diagnosis,
            </if>
            <if test="patientPhone != null">patient_phone,
            </if>
            <if test="departmentId != null">department_id,
            </if>
            <if test="departmentName != null">department_name,
            </if>
            <if test="sn != null">sn,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="createBy != null">create_by,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},
            </if>
            <if test="orderNum != null">#{orderNum},
            </if>
            <if test="deptAlias != null">#{deptAlias},
            </if>
            <if test="visitDate != null">#{visitDate},
            </if>
            <if test="inHospitalNumber != null">#{inHospitalNumber},
            </if>
            <if test="patientName != null">#{patientName},
            </if>
            <if test="cardNo != null">#{cardNo},
            </if>
            <if test="age != null">#{age},
            </if>
            <if test="mainDiagnosis != null">#{mainDiagnosis},
            </if>
            <if test="patientPhone != null">#{patientPhone},
            </if>
            <if test="departmentId != null">#{departmentId},
            </if>
            <if test="departmentName != null">#{departmentName},
            </if>
            <if test="sn != null">#{sn},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
        </trim>
    </insert>

    <update id="updatePatientInfoImport" parameterType="com.xinelu.manage.domain.patientinfoimport.PatientInfoImport">
        update patient_info_import
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderNum != null">order_num =
                #{orderNum},
            </if>
            <if test="deptAlias != null">dept_alias =
                #{deptAlias},
            </if>
            <if test="visitDate != null">visit_date =
                #{visitDate},
            </if>
            <if test="inHospitalNumber != null">in_hospital_number =
                #{inHospitalNumber},
            </if>
            <if test="patientName != null">patient_name =
                #{patientName},
            </if>
            <if test="cardNo != null">card_no =
                #{cardNo},
            </if>
            <if test="age != null">age =
                #{age},
            </if>
            <if test="mainDiagnosis != null">main_diagnosis =
                #{mainDiagnosis},
            </if>
            <if test="patientPhone != null">patient_phone =
                #{patientPhone},
            </if>
            <if test="departmentId != null">department_id =
                #{departmentId},
            </if>
            <if test="departmentName != null">department_name =
                #{departmentName},
            </if>
            <if test="sn != null">sn =
                #{sn},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePatientInfoImportById" parameterType="Long">
        delete
        from patient_info_import
        where id = #{id}
    </delete>

    <delete id="deletePatientInfoImportByIds" parameterType="String">
        delete from patient_info_import where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="insertPatientInfoImportList" useGeneratedKeys="true"
            keyProperty="id">
        insert into patient_info_import(
        order_num,
        dept_alias,
        visit_date,
        in_hospital_number,
        patient_name,
        card_no,
        age,
        main_diagnosis,
        patient_phone,
        department_id,
        department_name,
        sn,
        create_time,
        create_by,
        hospital_agency_id,
        hospital_agency_name
        ) values
        <foreach item="PatientInfoImport" index="index" collection="list" separator=",">
            (
            #{PatientInfoImport.orderNum},
            #{PatientInfoImport.deptAlias},
            #{PatientInfoImport.visitDate},
            #{PatientInfoImport.inHospitalNumber},
            #{PatientInfoImport.patientName},
            #{PatientInfoImport.cardNo},
            #{PatientInfoImport.age},
            #{PatientInfoImport.mainDiagnosis},
            #{PatientInfoImport.patientPhone},
            #{PatientInfoImport.departmentId},
            #{PatientInfoImport.departmentName},
            #{PatientInfoImport.sn},
            #{PatientInfoImport.createTime},
            #{PatientInfoImport.createBy},
            #{PatientInfoImport.hospitalAgencyId},
            #{PatientInfoImport.hospitalAgencyName}
            )
        </foreach>
    </insert>

    <select id="selectPatientInfoImportBySn"
            resultType="com.xinelu.manage.domain.patientinfoimport.PatientInfoImport">
        <include refid="selectPatientInfoImportVo"/>
        where sn = #{sn}
    </select>
</mapper>