<?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.auth.dao.base.mapper.v1.UserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.auth.dao.base.entity.UserEntity">
        <id column="id" property="id"/>
        <id column="username" property="username"/>
        <id column="nickname" property="nickname"/>
        <id column="email" property="email"/>
        <id column="phone" property="phone"/>
        <id column="password" property="password"/>
        <id column="avatar" property="avatar"/>
        <id column="status" property="status"/>
        <id column="is_deleted" property="isDeleted"/>
        <id column="salt" property="salt"/>
        <id column="gender" property="gender"/>
        <id column="birthday" property="birthday"/>
        <id column="introduction" property="introduction"/>
        <id column="last_login_ip" property="lastLoginIp"/>
        <id column="last_login_region" property="lastLoginRegion"/>
        <id column="last_login_time" property="lastLoginTime"/>
        <id column="dept_id" property="deptId"/>
        <id column="post_id" property="postId"/>
        <id column="create_time" property="createTime"/>
        <id column="update_time" property="updateTime"/>
        <id column="create_user" property="createUser"/>
        <id column="update_user" property="updateUser"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, username,nickname,email,phone,password,avatar,status,is_deleted,salt,gender,birthday,introduction,last_login_ip,last_login_region,last_login_time,dept_id,post_id, create_time, update_time, create_user, update_user
    </sql>

    <!-- 分页查询用户信息内容 -->
    <select id="selectListByPage" resultType="com.auth.model.base.vo.UserVo">
        select
        base.*,
        create_user.username as create_username,
        update_user.username as update_username
        from sys_user base
        left join sys_user create_user on create_user.id = base.create_user
        left join sys_user update_user on update_user.id = base.update_user
        <where>
            base.is_deleted = 0
            <if test="dto.username != null and dto.username != ''">
                and base.username like CONCAT('%',#{dto.username},'%')
            </if>
            <if test="dto.nickname != null and dto.nickname != ''">
                and base.nickname like CONCAT('%',#{dto.nickname},'%')
            </if>
            <if test="dto.email != null and dto.email != ''">
                and base.email like CONCAT('%',#{dto.email},'%')
            </if>
            <if test="dto.phone != null and dto.phone != ''">
                and base.phone like CONCAT('%',#{dto.phone},'%')
            </if>
            <if test="dto.password != null and dto.password != ''">
                and base.password like CONCAT('%',#{dto.password},'%')
            </if>
            <if test="dto.avatar != null and dto.avatar != ''">
                and base.avatar like CONCAT('%',#{dto.avatar},'%')
            </if>
            <if test="dto.status != null and dto.status != ''">
                and base.status like CONCAT('%',#{dto.status},'%')
            </if>
            <if test="dto.isDeleted != null and dto.isDeleted != ''">
                and base.is_deleted like CONCAT('%',#{dto.isDeleted},'%')
            </if>
            <if test="dto.salt != null and dto.salt != ''">
                and base.salt like CONCAT('%',#{dto.salt},'%')
            </if>
            <if test="dto.gender != null and dto.gender != ''">
                and base.gender like CONCAT('%',#{dto.gender},'%')
            </if>
            <if test="dto.birthday != null and dto.birthday != ''">
                and base.birthday like CONCAT('%',#{dto.birthday},'%')
            </if>
            <if test="dto.introduction != null and dto.introduction != ''">
                and base.introduction like CONCAT('%',#{dto.introduction},'%')
            </if>
            <if test="dto.lastLoginIp != null and dto.lastLoginIp != ''">
                and base.last_login_ip like CONCAT('%',#{dto.lastLoginIp},'%')
            </if>
            <if test="dto.lastLoginRegion != null and dto.lastLoginRegion != ''">
                and base.last_login_region like CONCAT('%',#{dto.lastLoginRegion},'%')
            </if>
            <if test="dto.lastLoginTime != null and dto.lastLoginTime != ''">
                and base.last_login_time like CONCAT('%',#{dto.lastLoginTime},'%')
            </if>
            <if test="dto.deptId != null and dto.deptId != ''">
                and base.dept_id like CONCAT('%',#{dto.deptId},'%')
            </if>
            <if test="dto.postId != null and dto.postId != ''">
                and base.post_id like CONCAT('%',#{dto.postId},'%')
            </if>
        </where>
    </select>

    <!-- 根据用户id查询用户角色和权限信息 -->
    <select id="selectUserRolePermissionByUsername" resultType="com.auth.dao.base.view.UserRolePermission">
        select *
        from v_user_role_permission vurp
        where vurp.user_id = #{userId}
          and vurp.is_deleted = 0
          and vurp.status = 1
    </select>

    <!-- 根据用户id查找该用户的角色内容 -->
    <select id="selectRolesByUserId" resultType="com.auth.dao.base.entity.RoleEntity">
        SELECT tr.*
        FROM sys_user_role tur
        JOIN sys_role tr ON tur.role_id = tr.id
        <where>
            <if test="userId != null">
                tur.user_id = #{userId}
            </if>
        </where>
    </select>

    <!-- 根据用户id查找当前用户的权限 -->
    <select id="selectPermissionByUserId" resultType="com.auth.dao.base.entity.PermissionEntity">
        SELECT DISTINCT p.*
        FROM sys_permission p
                 JOIN sys_role_permission rp ON p.id = rp.permission_id
                 JOIN sys_user_role ur ON rp.role_id = ur.role_id
        WHERE ur.user_id = #{userId}
    </select>

    <!-- 根据用户名查询用户信息 -->
    <select id="selectByUsername" resultType="com.auth.dao.base.entity.UserEntity">
        select
        <include refid="Base_Column_List"/>
        from sys_user
        where username = #{username} and is_deleted = 0 and status = 1
    </select>

</mapper>
