<?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.kakarote.admin.mapper.AdminUserMapper">
    <select id="findByUsername" resultType="java.util.HashMap" parameterType="java.lang.String">
        select a.*,(SELECT name FROM wk_admin_dept WHERE dept_id = a.dept_id) as deptName,(SELECT realname FROM wk_admin_user WHERE user_id=a.parent_id and is_del = 0) as parentName
        FROM wk_admin_user as a
        where a.username = #{username} and a.is_del = 0
    </select>
    <select id="queryUserList" resultType="com.kakarote.admin.entity.VO.AdminUserVO">
        select a.realname,a.username,a.user_id,a.sex,a.mobile,a.email,e.name as
        deptName,a.status,a.create_time,a.dept_id,a.img,
        a.post,a.parent_id,
        (select b.realname from wk_admin_user as b where b.user_id = a.parent_id) as
        parentName
        from wk_admin_user as a
        left join wk_admin_dept as e on a.dept_id = e.dept_id
        <if test="data.roleId != null">
            left join wk_admin_user_role as g on a.user_id = g.user_id
        </if>
        where 1 = 1 and a.is_del = 0
        <if test="data.realname != null and data.realname != ''">
            and (a.realname like concat('%', #{data.realname}, '%') or a.mobile like concat('%',#{data.realname}, '%'))
        </if>
        <if test="data.deptIdList.size()>0">
            and a.dept_id in
            <foreach item="item" collection="data.deptIdList" separator="," open="(" close=")" index="">
                #{item}
            </foreach>
        </if>
        <if test="data.status != null">
            and a.status = #{data.status}
        </if>
        <choose>
            <when test="data.label != null and data.label==1">
                AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) &lt;= date(a.create_time)
            </when>
            <when test="data.label != null and data.label==2">
                and a.status='2'
            </when>
            <when test="data.label != null and data.label==3">
                and a.status = '0'
            </when>
        </choose>
        <if test="data.roleId != null">
            and g.role_id=#{data.roleId}
        </if>
        <choose>
            <when test="data.deptOwnerUserId != null">
                order by a.user_id = ${data.deptOwnerUserId} desc,status desc
            </when>
            <when test="data.deptId == null and data.userId != null">
                order by a.user_id = ${data.userId} desc,status desc
            </when>
            <otherwise>
                order by status desc
            </otherwise>
        </choose>
    </select>
    <select id="queryListName" resultType="com.kakarote.admin.entity.VO.UserBookVO">
        select a.*,
        case
        when LENGTH( tmp_char) > 1 &amp;&amp; tmp_loc > 0 &amp;&amp; tmp_loc &lt; 24
        THEN (ELT(tmp_loc,'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z'))
        ELSE UPPER(tmp_char) end
        as initial
        from (
        select au.realname,au.username,au.img,au.mobile,au.post,ad.name as deptName
        ,au.dept_id,au.user_id,au.status as user_status,
        case
        when (select count(1) from wk_admin_attention where be_user_id = au.user_id and
        attention_user_id = #{data.userId}) > 0 then 1 ELSE 0 end as status,
        INTERVAL(CONV(HEX(CONVERT(LEFT(realname,1) USING GBK)),16,10),
        0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,0xC8F6,0xCBFA,0xCDDA
        ,0xCEF4,0xD1B9,0xD4D1) as tmp_loc,
        LEFT(realname,1) as tmp_char
        from wk_admin_user as au
        LEFT JOIN wk_admin_dept as ad on au.dept_id = ad.dept_id
        where au.is_del = 0
        ) as a
        where user_status = 1
        <if test="data.deptId != null">
            and dept_id = #{data.deptId}
        </if>
        <if test="data.search != null and data.search != ''">
            and (realname like concat('%', #{data.search}, '%') or username like concat('%', #{data.search}, '%'))
        </if>
        <if test="data.status != null">
            and status = #{data.status}
        </if>
        ORDER BY initial
        <choose>
            <when test="data.initial != null and data.initial==2">
                desc
            </when>
            <otherwise>
                asc
            </otherwise>
        </choose>
    </select>
    <select id="querySimpleUserByDeptId" resultType="com.kakarote.admin.entity.VO.HrmSimpleUserVO">
        select user_id,realname,img,post from wk_admin_user where  dept_id = #{deptId} and status != 0 and is_del = 0
    </select>
    <select id="querySimpleUserByDeptIdAndExamine" resultType="com.kakarote.admin.entity.VO.HrmSimpleUserVO">
        select user_id,realname,img,post from wk_admin_user where  dept_id = #{deptId} and status = 1 and is_del = 0
    </select>
    <select id="queryLoginUserInfo" resultType="com.kakarote.core.entity.UserInfo">
        select a.*,(SELECT name FROM wk_admin_dept WHERE dept_id = a.dept_id) as deptName,
        (SELECT realname FROM wk_admin_user WHERE user_id=a.parent_id) as parentName
        FROM wk_admin_user as a
        where a.user_id = #{userId} and a.is_del = 0
    </select>
    <select id="countUserByLabel" resultType="java.lang.Integer">
        select count(1)
        from wk_admin_user as a
        where 1 = 1
        <if test="status != null">
            and a.status = #{status}
        </if>
        <choose>
            <when test="label != null and label==1">
                AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) &lt;= date(a.create_time)
            </when>
            <when test="label != null and label==2">
                and a.status='2'
            </when>
            <when test="label != null and label==3">
                and a.status = '0'
            </when>
        </choose>
    </select>
</mapper>
