<?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.patientinfoimportmain.PatientInfoImportMainMapper">

    <resultMap type="PatientInfoImportMain" id="PatientInfoImportMainResult">
        <result property="id" column="id"/>
        <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"/>
        <result property="fileName" column="file_name"/>
        <result property="followupStatus" column="followup_status"/>
        <result property="followupCount" column="followup_count"/>
    </resultMap>
    <resultMap type="com.xinelu.manage.vo.patientinfoimportmain.PatientInfoImportMainVO" id="PatientInfoImportVOResult">
        <result property="id" column="id"/>
        <result property="followupStatus" column="followup_status"/>
        <result property="fileName" column="file_name"/>
        <result property="followupCount" column="followup_count"/>
        <result property="hospitalAgencyId" column="hospital_agency_id"/>
        <result property="hospitalAgencyName" column="hospital_agency_name"/>
        <result property="patientCount" column="patient_count"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_date"/>
    </resultMap>
    <sql id="selectPatientInfoImportMainVo">
        select id, sn, create_time, create_by, hospital_agency_id, hospital_agency_name, file_name, followup_status, followup_count from patient_info_import_main
    </sql>

    <select id="selectPatientInfoImportMainList" parameterType="PatientInfoImportMain" resultMap="PatientInfoImportMainResult">
        <include refid="selectPatientInfoImportMainVo"/>
        <where>
            <if test="sn != null  and sn != ''">
                and sn = #{sn}
            </if>
            <if test="hospitalAgencyId != null ">
                and hospital_agency_id = #{hospitalAgencyId}
            </if>
            <if test="hospitalAgencyName != null  and hospitalAgencyName != ''">
                and hospital_agency_name like concat('%', #{hospitalAgencyName}, '%')
            </if>
            <if test="fileName != null  and fileName != ''">
                and file_name like concat('%', #{fileName}, '%')
            </if>
            <if test="followupStatus != null  and followupStatus != ''">
                and followup_status = #{followupStatus}
            </if>
            <if test="followupCount != null ">
                and followup_count = #{followupCount}
            </if>
        </where>
    </select>
    <select id="GetPatientInfoImport" resultType="com.xinelu.manage.vo.patientinfoimportmain.PatientInfoImportMainVO"
            parameterType="com.xinelu.manage.vo.patientinfoimportmain.PatientInfoImportMainVO">
        --         SELECT  sn,count(1) as patientCount,DATE_FORMAT(create_time, '%Y-%m-%d') as createTime,create_by as createBy FROM patient_info_import group by sn,DATE_FORMAT(create_time, '%Y-%m-%d'),create_by
        --         SELECT p.sn,count( 1 ) AS patientcount,DATE_FORMAT( p.create_time, '%Y-%m-%d' ) AS createTime,p.create_by FROM patient_info_import p

        select p.*,(select count(1) from patient_info_import pii where pii.sn=p.sn) as patient_count from  patient_info_import_main p
        <where>
            <if test="importTimeStart != null">
                and date_format(p.create_time,'%y%m%d') >= date_format(#{importTimeStart}, '%y%m%d')
            </if>
            <if test="importTimeEnd != null">
                and date_format(p.create_time,'%y%m%d') &lt;= date_format(#{importTimeEnd}, '%y%m%d')
            </if>
            <if test="hospitalAgencyId != null and (campusAgencyId == null  or campusAgencyId == '')">
                and p.hospital_agency_id = #{hospitalAgencyId}
            </if>
            <if test="campusAgencyId != null ">
                and p.hospital_agency_id = #{campusAgencyId}
            </if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        ORDER BY P.ID DESC
        --         GROUP BY p.sn,DATE_FORMAT( p.create_time, '%Y-%m-%d' ),p.create_by

    </select>
    <select id="selectPatientInfoImportMainById" parameterType="Long"
            resultMap="PatientInfoImportMainResult">
        <include refid="selectPatientInfoImportMainVo"/>
        where id = #{id}
    </select>

    <insert id="insertPatientInfoImportMain" parameterType="PatientInfoImportMain" useGeneratedKeys="true"
            keyProperty="id">
        insert into patient_info_import_main
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sn != null">sn,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="hospitalAgencyId != null">hospital_agency_id,
            </if>
            <if test="hospitalAgencyName != null">hospital_agency_name,
            </if>
            <if test="fileName != null">file_name,
            </if>
            <if test="followupStatus != null">followup_status,
            </if>
            <if test="followupCount != null">followup_count,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sn != null">#{sn},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="hospitalAgencyId != null">#{hospitalAgencyId},
            </if>
            <if test="hospitalAgencyName != null">#{hospitalAgencyName},
            </if>
            <if test="fileName != null">#{fileName},
            </if>
            <if test="followupStatus != null">#{followupStatus},
            </if>
            <if test="followupCount != null">#{followupCount},
            </if>
        </trim>
    </insert>

    <update id="updatePatientInfoImportMain" parameterType="PatientInfoImportMain">
        update patient_info_import_main
        <trim prefix="SET" suffixOverrides=",">
            <if test="sn != null">sn =
                #{sn},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="hospitalAgencyId != null">hospital_agency_id =
                #{hospitalAgencyId},
            </if>
            <if test="hospitalAgencyName != null">hospital_agency_name =
                #{hospitalAgencyName},
            </if>
            <if test="fileName != null">file_name =
                #{fileName},
            </if>
            <if test="followupStatus != null">followup_status =
                #{followupStatus},
            </if>
            <if test="followupCount != null">followup_count =
                #{followupCount},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePatientInfoImportMainById" parameterType="Long">
        delete from patient_info_import_main where id = #{id}
    </delete>

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