<?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.DeptMapper">
    <select id="getDeptInfo" resultType="com.lantone.security.dto.GetDeptTreeDTO">
        SELECT
            d.id deptId,
            d.`name` deptName
        FROM
            `sys_dept` d
        WHERE
            d.is_deleted = 'N'
        <if test="hospitalId!=null">
            AND d.`hospital_id` = #{hospitalId}
        </if>
    </select>
    <select id="getDeptRegion" resultType="com.lantone.security.dto.GetDeptInfoDTO">
        SELECT
            d.id deptId,
            d.`name` deptName,
            d.`status`,
            IF(d.`status`=1,"启用","禁用") statusName,
            r.id regionId,
            r.`name` regionName,
            d.gmt_create gmtCreate
        FROM
        sys_dept d
        LEFT JOIN sys_region_dept rd ON d.id = rd.dept_id
        AND rd.is_deleted = 'N'
        LEFT JOIN sys_region r ON rd.region_id = r.id
        AND r.is_deleted = 'N'
        WHERE
        d.is_deleted = 'N'
        AND d.hospital_id = #{getHospitalListInfoVO.hospitalId}
        <if test="getHospitalListInfoVO.hospitalName !=null and getHospitalListInfoVO.hospitalName!=''">
            AND d.`name` LIKE concat('%',#{getHospitalListInfoVO.hospitalName},'%')
        </if>
        <if test="getHospitalListInfoVO.status !=null and getHospitalListInfoVO.status!=''">
            AND d.`status` = #{getHospitalListInfoVO.status}
        </if>
        <if test="getHospitalListInfoVO.status !=null and getHospitalListInfoVO.status!=''">
            AND r.id = #{getHospitalListInfoVO.regionId}
        </if>
    </select>
    <select id="getDepts" resultType="com.lantone.security.dto.GetDataAuthDeptDTO">
        SELECT
        d.id,
        d.hospital_id hospitalId,
        h.`name` hospitalName,
        d.`name`,
        2 detailType
        FROM
        sys_dept d
        LEFT JOIN sys_hospital h ON h.id = d.hospital_id
        AND h.is_deleted = 'N'
        WHERE
        d.is_deleted = 'N'
        AND d.`status` = 1
        <if test="deptIds !=null and deptIds.size!=0">
            AND d.id IN
            <foreach collection="deptIds" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
    </select>
    <select id="getDoctors" resultType="com.lantone.security.dto.GetDataAuthDoctorDTO">
        SELECT
            doctor.id,
            doctor.`name`,
            d.id deptId,
            d.`name` deptName,
            d.hospital_id hospitalId,
            3 detailType
        FROM
            sys_doctor doctor,
            sys_dept_doctor dd,
            sys_dept d
        WHERE
            doctor.is_deleted = 'N'
        AND dd.is_deleted = 'N'
        AND d.is_deleted = 'N'
        AND doctor.id = dd.doctor_id
        AND dd.dept_id = d.id
        <if test="doctorIds !=null and doctorIds.size!=0">
            AND doctor.id IN
            <foreach collection="doctorIds" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
    </select>
</mapper>