<?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.diagbot.mapper.SysUserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.SysUser">
        <id column="id" property="id"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="creator" property="creator"/>
        <result column="modifier" property="modifier"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="linkman" property="linkman"/>
        <result column="type" property="type"/>
        <result column="locked" property="locked"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <select id="userPage" resultType="com.diagbot.dto.SysUserQueryDTO"
            parameterType="com.diagbot.vo.SysUserQueryVO">
        SELECT
        t3.userId,
        t3.username AS userName,
        t3.linkman AS linkman,
        t6.deptName AS deptName,
        t10.roleName AS roleName,
        t3.`status` AS `status`,
        t3.locked
        FROM
        (
        SELECT
        t1.id AS userId,
        t1.username AS userName,
        t1.linkman AS linkman,
        t1.`status` AS `status`,
        t2.hospital_id AS hospitalId,
        t1.locked
        FROM
        sys_user t1,
        sys_user_hospital t2
        WHERE
        t1.is_deleted = 'N'
        AND t2.is_deleted = 'N'
        AND t2.user_id = t1.id
        AND t2.hospital_id = #{hospitalId}
        ) t3
        <choose>
            <when test="deptId !=null and deptId != ''">
                INNER JOIN (
                SELECT DISTINCT
                t4.user_id AS userId,
                t4.hospital_id AS hospitalId,
                group_concat(t5.dept_name order by t5.dept_name ASC) AS deptName
                FROM
                sys_user_dept t4,
                bas_dept_info t5
                WHERE
                t4.is_deleted = 'N'
                AND t4.dept_id = t5.dept_id
                AND t4.hospital_id = t5.hospital_id
                AND t4.hospital_id = #{hospitalId}
                AND t4.dept_id = #{deptId}
                GROUP BY
                t4.user_id
                ) t6 ON t3.userId = t6.userId
            </when>
            <otherwise>
                LEFT JOIN (
                SELECT DISTINCT
                t4.user_id AS userId,
                t4.hospital_id AS hospitalId,
                group_concat(t5.dept_name order by t5.dept_name ASC) AS deptName
                FROM
                sys_user_dept t4,
                bas_dept_info t5
                WHERE
                t4.is_deleted = 'N'
                AND t4.dept_id = t5.dept_id
                AND t4.hospital_id = t5.hospital_id
                AND t4.hospital_id = #{hospitalId}
                GROUP BY
                t4.user_id
                ) t6 ON t3.userId = t6.userId
            </otherwise>
        </choose>
        <choose>
            <when test="roleId !=null">
                INNER JOIN (
                SELECT
                t8.user_id AS userId,
                group_concat(t9.`name` order by t9.id ASC) AS roleName,
                min(t9.id) as roleId
                FROM
                sys_user_role t8,
                sys_role t9
                WHERE
                t8.is_deleted = 'N'
                AND t9.is_deleted = 'N'
                AND t8.role_id = t9.id
                AND t8.role_id = #{roleId}
                GROUP BY
                t8.user_id
                ) t10 ON t10.userId = t3.userId
            </when>
            <otherwise>
                LEFT JOIN (
                SELECT
                t8.user_id AS userId,
                group_concat(t9.`name` order by t9.id ASC) AS roleName,
                min(t9.id) as roleId
                FROM
                sys_user_role t8,
                sys_role t9
                WHERE
                t8.is_deleted = 'N'
                AND t9.is_deleted = 'N'
                AND t8.role_id = t9.id
                GROUP BY
                t8.user_id
                ) t10 ON t10.userId = t3.userId
            </otherwise>
        </choose>
        WHERE
        IFNULL(t10.roleId,0) != -1
        <if test="linkman != null and linkman != ''">
            and t3.linkman like CONCAT('%',#{linkman},'%')
        </if>
        ORDER BY
        t3.locked
    </select>
</mapper>
