<?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.ichangzuo.system.mapper.UserMapper">

    <!-- 用户分页列表 -->
    <select id="listPagedUsers" resultType="com.ichangzuo.system.model.bo.UserBO">
        SELECT
            user_id id,
            user_name username,
            nickname,
            phone mobile,
            sex gender,
            portrait avatar,
            state status,
            email,
            name,
            is_name,
            is_phone,
            authority,
            reg_time,
            login_time,
            login_num,
            delete_time,
            vip_time,
            integral
        FROM
            mc_user
        <where>
            <if test='queryParams.keywords!=null and queryParams.keywords.trim() neq ""'>
                AND (
                    user_name LIKE CONCAT('%',#{queryParams.keywords},'%')
                    OR nickname LIKE CONCAT('%',#{queryParams.keywords},'%')
                    OR name LIKE CONCAT('%',#{queryParams.keywords},'%')
                    OR email LIKE CONCAT('%',#{queryParams.keywords},'%')
                    OR phone LIKE CONCAT('%',#{queryParams.keywords},'%')
                    )
            </if>
            <if test="queryParams.status!=null">
                <choose>
                    <when test="queryParams.status >= 3">
                        AND state >= 3
                    </when>
                    <otherwise>
                        AND state = #{queryParams.status}
                    </otherwise>
                </choose>
            </if>
            <if test="queryParams.regTime != null and queryParams.regTime.size > 0">
                <if test="queryParams.regTime[0] != null and queryParams.regTime[0] != ''">
                    <bind name="startDate" value="queryParams.regTime[0]"/>
                    AND reg_time &gt;= #{startDate}
                </if>
                <if test="queryParams.regTime[1] != null and queryParams.regTime[1] != ''">
                    <bind name="endDate" value="queryParams.regTime[1]"/>
                    AND reg_time &lt;= #{endDate}
                </if>
            </if>
        </where>
<!--        GROUP BY-->
<!--            u.user_id-->
    </select>

    <!-- 用户表单信息映射 -->
    <resultMap id="UserFormMap" type="com.ichangzuo.system.model.form.UserForm">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="username" column="username" jdbcType="VARCHAR"/>
        <result property="nickname" column="nickname" jdbcType="VARCHAR"/>
        <result property="mobile" column="mobile" jdbcType="VARCHAR"/>
        <result property="gender" column="gender" jdbcType="TINYINT"/>
        <result property="avatar" column="avatar" jdbcType="VARCHAR"/>
        <result property="email" column="email" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="BOOLEAN"/>
<!--        <result property="deptId" column="dept_id" jdbcType="BIGINT"/>-->
        <collection
                property="roleIds"
                column="id"
                select="com.ichangzuo.system.mapper.UserRoleMapper.listRoleIdsByUserId" >
            <result column="role_id" />
        </collection>
    </resultMap>

    <!-- 根据用户ID获取用户详情 -->
    <select id="getUserFormData" resultMap="UserFormMap">
        SELECT
            user_id id,
            user_name username,
            nickname,
            phone mobile,
            sex gender,
            portrait avatar,
            email,
            state status
<!--            dept_id-->
        FROM
            mc_user
        WHERE
            user_id = #{userId} <!--AND is_deleted = 0-->
    </select>

    <!-- 用户认证信息映射 -->
    <resultMap id="UserAuthMap" type="com.ichangzuo.system.model.dto.UserAuthInfo">
        <id property="userId" column="userId" jdbcType="BIGINT"/>
        <result property="username" column="username" jdbcType="VARCHAR"/>
        <result property="password" column="password" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="BOOLEAN"/>
<!--        <result property="deptId" column="dept_id" jdbcType="BIGINT"></result>-->
        <collection property="roles" ofType="string" javaType="java.util.Set">
            <result column="code"/>
        </collection>
    </resultMap>

    <!-- 根据用户名获取认证信息 -->
    <select id="getUserAuthInfo" resultMap="UserAuthMap">
        SELECT
            u.user_id userId,
            u.user_name username,
            u.nickname,
            u.user_password password,
            u.state status,
            r.CODE
        FROM
            mc_user u
<!--                LEFT JOIN sys_user_role t2 ON t2.user_id = u.user_id-->
                LEFT JOIN sys_role r ON r.id = IF(u.authority &amp; 0x100, 2, IF(u.authority &amp; 0x10, 4, 3))
        WHERE
            u.user_name = #{username} <!--AND u.is_deleted=0-->
    </select>

    <!-- 获取用户导出列表 -->
    <select id="listExportUsers" resultType="com.ichangzuo.system.model.dto.UserExportDTO">
        SELECT
            u.user_name username,
            u.nickname,
            u.phone mobile,
            CASE u.sex
                WHEN 1 THEN '男'
                WHEN 2 THEN '女'
                ELSE '保密'
            END gender,
<!--            d.NAME AS dept_name,-->
            u.reg_time create_time
        FROM
            mc_user u
<!--                LEFT JOIN sys_dept d ON u.dept_id = d.id-->
        <where>
<!--            u.is_deleted = 0 AND u.username != 'root'-->
            <if test='keywords!=null and keywords.trim() neq ""'>
                AND (u.user_name LIKE CONCAT('%',#{keywords},'%')
                OR u.nickname LIKE CONCAT('%',#{keywords},'%')
                OR u.phone LIKE CONCAT('%',#{keywords},'%'))
            </if>
            <if test='status!=null'>
                AND u.state = #{status}
            </if>
<!--            <if test='deptId!=null'>-->
<!--                AND concat(',',concat(d.tree_path,',',d.id),',') like concat('%,',#{deptId},',%')-->
<!--            </if>-->
        </where>
        GROUP BY u.user_id
    </select>

    <!-- 根据用户ID获取用户详情 -->
    <select id="getUserProfile" resultType="com.ichangzuo.system.model.bo.UserBO">
        SELECT
            u.user_id id,
            u.user_name username,
            u.nickname,
            u.phone mobile,
            u.sex gender,
            u.portrait avatar,
            u.state status,
<!--            d.NAME AS deptName,-->
<!--            GROUP_CONCAT(r.NAME) AS roleNames,-->
            r.NAME roleNames,
            u.reg_time create_time
        FROM
            mc_user u
<!--                LEFT JOIN sys_dept d ON u.dept_id = d.id-->
<!--                LEFT JOIN sys_user_role sur ON u.user_id = sur.user_id-->
                LEFT JOIN sys_role r ON r.id = IF(u.authority &amp; 0x100, 2, IF(u.authority &amp; 0x10, 4, 3))
        WHERE
            u.user_id = #{userId} <!--AND u.is_deleted = 0-->
    </select>

</mapper>
