<?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.simuwang.base.mapper.system.SysUserMapper">
    <select id="listBindRoles" resultType="com.simuwang.base.common.support.dos.OnlyIdNameDO">
        select t.role_id    as id,
               t.role_name  as name
        from sys_role t
                 join sys_user_role t1 on t.role_id = t1.role_id
                 join sys_user t2 on t1.user_id = t2.user_id and t2.isvalid = 1 and t2.status = 1
        where t2.user_id = #{userId}
          and t.isvalid = 1 and t.status = 1
        order by t.role_id
    </select>

    <select id="allRoles" resultType="com.simuwang.base.common.support.dos.OnlyIdNameDO">
        select t.role_id    as id,
               t.role_name  as name
        from sys_role t
        where t.isvalid = 1 and t.status = 1
        order by t.role_id
    </select>

    <select id="selectRoleByUserId" resultType="com.simuwang.base.pojo.dos.sys.SysRoleDO">
        select t.role_id    as roleId,
               t.role_name  as roleName,
               t.role_key   as roleKey,
               t.role_sort  as roleSort,
               t.status     as status,
               t.data_scope as dataScope,
               t.remark,
               t.creatorid  as creatorId,
               t.createtime as createTime,
               t.updaterid  as updaterId,
               t.updatetime as updateTime
        from sys_role t
                 join sys_user_role t1 on t.role_id = t1.role_id
                 join sys_user t2 on t1.user_id = t2.user_id and t2.isvalid = 1 and t2.status = 1
        where t2.user_id = #{userId}
          and t.isvalid = 1 and t.status = 1
        order by t.role_id
    </select>

    <select id="selectAllRole" resultType="com.simuwang.base.pojo.dos.sys.SysRoleDO">
        select t.role_id    as roleId,
               t.role_name  as roleName,
               t.role_key   as roleKey,
               t.role_sort  as roleSort,
               t.status     as status,
               t.data_scope as dataScope,
               t.remark,
               t.creatorid  as creatorId,
               t.createtime as createTime,
               t.updaterid  as updaterId,
               t.updatetime as updateTime
        from sys_role t
        where t.isvalid = 1 and t.status = 1
        order by t.role_id
    </select>

    <select id="selectMenuByUserId" resultType="com.simuwang.base.pojo.dos.sys.SysMenuDO">
        select sm.menu_id    as menuId,
               sm.menu_name  as menuName,
               sm.parent_id  as parentId,
               sm.perms      as perms,
               sm.icon       as icon,
               sm.order_num  as orderNum,
               sm.path       as path,
               sm.component  as component,
               sm.query      as query,
               sm.is_frame   as isFrame,
               sm.is_cache   as isCache,
               sm.menu_type  as menuType,
               sm.visible    as visible,
               sm.status     as status,
               sm.remark,
               sm.creatorid  as creatorId,
               sm.createtime as createTime,
               sm.updaterid  as updaterId,
               sm.updatetime as updateTime
        from sys_menu sm
                 join sys_role_menu srm on sm.menu_id = srm.menu_id
                 join sys_role sr on sr.role_id = srm.role_id and sr.isvalid = 1 and sr.status = 1
                 join sys_user_role t1 on sr.role_id = t1.role_id
                 join sys_user t2 on t1.user_id = t2.user_id and t2.isvalid = 1 and t2.status = 1
        where t2.user_id = #{userId}
          and sm.isvalid = 1 and sm.status = 1
        order by sm.menu_id
    </select>

    <select id="selectAllMenu" resultType="com.simuwang.base.pojo.dos.sys.SysMenuDO">
        select sm.menu_id    as menuId,
               sm.menu_name  as menuName,
               sm.parent_id  as parentId,
               sm.perms      as perms,
               sm.icon       as icon,
               sm.order_num  as orderNum,
               sm.path       as path,
               sm.component  as component,
               sm.query      as query,
               sm.is_frame   as isFrame,
               sm.is_cache   as isCache,
               sm.menu_type  as menuType,
               sm.visible    as visible,
               sm.status     as status,
               sm.remark,
               sm.creatorid  as creatorId,
               sm.createtime as createTime,
               sm.updaterid  as updaterId,
               sm.updatetime as updateTime
        from sys_menu sm
        where sm.isvalid = 1 and sm.status = 1
        order by sm.menu_id
    </select>

    <select id="roleUserPage" resultType="com.simuwang.base.pojo.dos.sys.SysRoleUserDO"
            parameterType="com.simuwang.base.pojo.dto.sys.RoleUserQuery">
        select t.role_id    as roleId,
               t.role_name  as roleName,
               t.role_key   as roleKey,
               t.role_sort  as roleSort,
               t.status     as status,
               t.data_scope as dataScope,
               t.remark,
               t.creatorid  as creatorId,
               t.createtime as createTime,
               t.updaterid  as updaterId,
               t.updatetime as updateTime,
               t2.user_id   as userId,
               t2.user_name as userName,
               t2.email,
               t2.phonenumber,
               t2.status    as userStatus
        from sys_role t
                 join sys_user_role t1 on t.role_id = t1.role_id
                 join sys_user t2 on t1.user_id = t2.user_id and t2.isvalid = 1
        where t.isvalid = 1
        <if test="keyword != null and keyword != ''">
            and (
                t.role_name like concat('%', #{keyword}, '%')
                or
                t.role_key like concat('%', #{keyword}, '%')
                or
                t2.user_name like concat('%', #{keyword}, '%')
                or
                t2.email like concat('%', #{keyword}, '%')
                or
                t2.phonenumber like concat('%', #{keyword}, '%')
            )
        </if>
        <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
            and t.createtime between #{beginTime} and #{endTime}
        </if>
        <choose>
            <when test="orderItems != null and !orderItems.isEmpty()">
                order by
                <foreach collection="orderItems" item="item" separator=",">
                    ${item.column} ${item.asc}
                </foreach>
            </when>
            <otherwise>order by t.createtime</otherwise>
        </choose>
        limit #{offset}, #{pageSize}
    </select>

    <select id="countRoleUser" parameterType="com.simuwang.base.pojo.dto.sys.RoleUserQuery">
        select count(t.role_id)
        from sys_role t
                 join sys_user_role t1 on t.role_id = t1.role_id
                 join sys_user t2 on t1.user_id = t2.user_id and t2.isvalid = 1
        where t.isvalid = 1
        <if test="keyword != null and keyword != ''">
            and (
            t.role_name like concat('%', #{keyword}, '%')
            or
            t.role_key like concat('%', #{keyword}, '%')
            or
            t2.user_name like concat('%', #{keyword}, '%')
            or
            t2.email like concat('%', #{keyword}, '%')
            or
            t2.phonenumber like concat('%', #{keyword}, '%')
            )
        </if>
        <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
            and t.createtime between #{beginTime} and #{endTime}
        </if>
    </select>

    <delete id="delUserRoleByUserId" parameterType="int">
        delete from sys_user_role where user_id =#{userId}
    </delete>

    <insert id="batchUserRoles">
        insert into sys_user_role(user_id, role_id) values
        <foreach collection="roleIds" item="roleId" separator=",">
            (#{userId}, #{roleId})
        </foreach>
    </insert>
</mapper>