<?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.lantone.security.mapper.DoctorMapper">
    <select id="getDoctorPage" resultType="com.lantone.security.dto.GetDoctorPageDTO">
        SELECT DISTINCT
        *
        FROM
        (
        SELECT
        h.id hospitalId,
        h.`name` hospitalName,
        dept.id deptId,
        dept.`name` deptName,
        doc.id ,
        doc.`name` ,
        doc.mobilePhone,
        IF (
        detail.detail_id IS NULL,
        '0',
        '1'
        ) relation
        FROM
        sys_hospital h,
        sys_dept dept,
        sys_dept_doctor dd,
        sys_doctor doc
        LEFT JOIN sys_data_auth_detail detail ON detail.data_type = 7
        AND detail.detail_type = 3
        AND detail.detail_id = doc.id
        AND detail.is_deleted = 'N'
        WHERE
        dept.id = dd.dept_id
        AND h.id = dept.hospital_id
        AND doc.id = dd.doctor_id
        AND h.is_deleted = 'N'
        AND dept.is_deleted = 'N'
        AND doc.is_deleted = 'N'
        AND dd.is_deleted = 'N'
        <if test="doctorVO.name!=null and doctorVO.name!=''">
            AND doc.`name` LIKE concat('%',#{doctorVO.name},'%')
        </if>
        <if test="doctorVO.mobilePhone!=null and doctorVO.mobilePhone!=''">
            AND doc.mobilePhone LIKE concat('%',#{doctorVO.mobilePhone},'%')
        </if>
        ) t
        WHERE 1=1
        <if test="doctorVO.relation != null ">
            AND t.relation = #{doctorVO.relation}
        </if>
        <if test="doctorVO.hospitals !=null and doctorVO.hospitals.size!=0">
            AND t.hospitalId IN
            <foreach collection="doctorVO.hospitals" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="doctorVO.hospitals !=null and doctorVO.hospitals.size!=0 and doctorVO.depts !=null and doctorVO.depts.size!=0">
            OR t.deptId IN
            <foreach collection="doctorVO.depts" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="(doctorVO.hospitals ==null or doctorVO.hospitals.size==0) and doctorVO.depts !=null and doctorVO.depts.size!=0">
            AND t.deptId IN
            <foreach collection="doctorVO.depts" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="doctorVO.map!=null and doctorVO.map.dataAuthFilter!=null and doctorVO.map.dataAuthFilter!=''">
            AND ${doctorVO.map.dataAuthFilter}
        </if>
    </select>
</mapper>