<?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.qmth.themis.business.dao.TBUserMapper">

    <select id="userQuery" resultType="com.qmth.themis.business.dto.response.TBUserDto">
        select
            t.id,
            t.name,
            t.loginName,
            t.orgId,
            t.orgName,
            t.roleNameStr,
            t.enable,
            t.mobileNumber,
            t.roleCodeStr,
            if(t.updateName is not null, t.updateName, t.createName) as updateName,
            if(t.updateTime is not null, t.updateTime, t.createTime) as updateTime
            from
            (
            select
                tbu.org_id as orgId,tbu.id, tbu.mobile_number as mobileNumber, tbu.login_name as loginName, tbu.name, group_concat(tbr.role_code) as roleCodeStr, group_concat(tbr.role_name) as roleNameStr, tbu.enable,(
            select
            t.name
            from
            t_b_org t
            where
            t.id = tbu.org_id) as orgName,(
            select
            t.name
            from
            t_b_user t
            where
            t.id = tbu.create_id) as createName, tbu.create_time as createTime, (
            select
            t.name
            from
            t_b_user t
            where
            t.id = tbu.update_id) as updateName, tbu.update_time as updateTime
            from
            t_b_user tbu
            left join t_b_user_role tbur on
            tbur.user_id = tbu.id
            left join t_b_role tbr on
            tbr.role_code = tbur.role_code
            <where>
                <if test="id != null and id != ''">
                    and tbu.id = #{id}
                </if>
                <if test="loginName != null and loginName != ''">
                    and tbu.login_name like concat('%', #{loginName}, '%')
                </if>
                <if test="name != null and name != ''">
                    and tbu.name like concat('%', #{name}, '%')
                </if>
                <if test="enable != null and enable != '' or enable == 0">
                    and tbu.enable = #{enable}
                </if>
            </where> group by tbu.org_id,tbu.id,tbu.mobile_number,tbu.login_name,tbu.name,tbu.enable) t
            <where>
                t.roleCodeStr <![CDATA[ <> ]]> 'SUPER_ADMIN'
                <if test="role != null and role != ''">
                    and t.roleCodeStr like concat('%', #{role}, '%')
                </if>
            </where>
             order by t.name
    </select>
</mapper>
