<?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.patientblacklist.PatientBlacklistMapper">

    <resultMap type="PatientBlacklist" id="PatientBlacklistResult">
            <result property="id" column="id"/>
            <result property="patientId" column="patient_id"/>
		    <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="abnormalCauseId" column="abnormal_cause_id"/>
            <result property="abnormalCauseValue" column="abnormal_cause_value"/>
            <result property="supplementIllustrateId" column="supplement_illustrate_id"/>
            <result property="supplementIllustrateValue" column="supplement_illustrate_value"/>
            <result property="functionLimitation" column="function_limitation"/>
            <result property="blacklistSource" column="blacklist_source"/>
	        <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="selectPatientBlacklistVo">
        select id, patient_id, hospital_agency_id,hospital_agency_name,campus_agency_id,campus_agency_name,department_id, department_name,
	    abnormal_cause_id, abnormal_cause_value, supplement_illustrate_id, supplement_illustrate_value,
        function_limitation, blacklist_source, del_flag, create_by, create_time, update_by, update_time from patient_blacklist
    </sql>

    <select id="selectPatientBlacklistList" parameterType="com.xinelu.manage.dto.patientblacklist.PatientBlacklistDto"
	    resultType="com.xinelu.manage.vo.patientblacklist.PatientBlacklistVo">
	    select b.id, b.patient_id,b.hospital_agency_id, b.hospital_agency_name,b.campus_agency_id,b.campus_agency_name,
	    b.department_id, b.department_name, b.abnormal_cause_id, b.abnormal_cause_value, b.supplement_illustrate_id,
	    b.supplement_illustrate_value, b.function_limitation, b.blacklist_source,
	    b.create_time, p.patient_name, p.patient_phone, p.card_no from patient_blacklist b
        left join patient_info p on b.patient_id = p.id
        <where>
	        b.del_flag = 0
	        <if test="patientId != null">
		        and b.patient_id = #{patientId}
	        </if>
	        <if test="patientName != null  and patientName != ''">
		        and p.patient_name like concat('%', #{patientName}, '%')
	        </if>
	        <if test="patientPhone != null  and patientPhone != ''">
		        and p.patient_phone like concat('%', #{patientPhone}, '%')
	        </if>
	        <if test="hospitalAgencyId != null ">
		        and p.hospital_agency_id = #{hospitalAgencyId}
	        </if>
	        <if test="departmentId != null ">
		        and p.department_id = #{departmentId}
	        </if>
            <if test="abnormalCauseValue != null  and abnormalCauseValue != ''">
                and b.abnormal_cause_value like concat('%', #{abnormalCauseValue}, '%')
            </if>
            <if test="blacklistSource != null  and blacklistSource != ''">
                and b.blacklist_source = #{blacklistSource}
            </if>
	        <!-- 数据范围过滤 -->
	        ${params.dataScope}
        </where>
		order by b.id desc
    </select>

    <select id="selectPatientBlacklistById" parameterType="Long"
            resultType="com.xinelu.manage.vo.patientblacklist.PatientBlacklistVo">
	    select b.id, b.patient_id,b.hospital_agency_id, b.hospital_agency_name,b.campus_agency_id,b.campus_agency_name,
	       b.department_id, b.department_name, b.abnormal_cause_id, b.abnormal_cause_value, b.supplement_illustrate_id,
	       b.supplement_illustrate_value, b.function_limitation, b.blacklist_source,
	       b.create_time, p.patient_name, p.patient_phone, p.card_no from patient_blacklist b
		 left join patient_info p on b.patient_id = p.id
            where b.id = #{id}
    </select>

    <insert id="insertPatientBlacklist" parameterType="PatientBlacklist" useGeneratedKeys="true"
            keyProperty="id">
        insert into patient_blacklist
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patientId != null">patient_id,
            </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="abnormalCauseId != null">abnormal_cause_id,
            </if>
            <if test="abnormalCauseValue != null">abnormal_cause_value,
            </if>
            <if test="supplementIllustrateId != null">supplement_illustrate_id,
            </if>
            <if test="supplementIllustrateValue != null">supplement_illustrate_value,
            </if>
            <if test="functionLimitation != null">function_limitation,
            </if>
            <if test="blacklistSource != null">blacklist_source,
            </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="patientId != null">#{patientId},
            </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="abnormalCauseId != null">#{abnormalCauseId},
            </if>
            <if test="abnormalCauseValue != null">#{abnormalCauseValue},
            </if>
            <if test="supplementIllustrateId != null">#{supplementIllustrateId},
            </if>
            <if test="supplementIllustrateValue != null">#{supplementIllustrateValue},
            </if>
            <if test="functionLimitation != null">#{functionLimitation},
            </if>
            <if test="blacklistSource != null">#{blacklistSource},
            </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="updatePatientBlacklist" parameterType="PatientBlacklist">
        update patient_blacklist
        <trim prefix="SET" suffixOverrides=",">
            <if test="patientId != null">patient_id =
                #{patientId},
            </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="abnormalCauseId != null">abnormal_cause_id =
	            #{abnormalCauseId},
	        </if>
	        <if test="abnormalCauseValue != null">abnormal_cause_value =
	            #{abnormalCauseValue},
	        </if>
	        <if test="supplementIllustrateId != null">supplement_illustrate_id =
	            #{supplementIllustrateId},
	        </if>
	        <if test="supplementIllustrateValue != null">supplement_illustrate_value =
	            #{supplementIllustrateValue},
	        </if>
	        <if test="functionLimitation != null">function_limitation =
	            #{functionLimitation},
	        </if>
	        <if test="blacklistSource != null">blacklist_source =
	            #{blacklistSource},
	        </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>

    <delete id="deletePatientBlacklistById" parameterType="Long">
        delete from patient_blacklist where id = #{id}
    </delete>

    <delete id="deletePatientBlacklistByIds" parameterType="String">
        update patient_blacklist set del_flag = 1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>