Offline user when power change

This commit is contained in:
2023-12-01 17:21:42 +08:00
parent 1ad38bf2a8
commit 95ea00e643
9 changed files with 128 additions and 54 deletions

View File

@@ -18,9 +18,13 @@ interface UserMapper : BaseMapper<User> {
fun selectPage(page: IPage<Long>, searchType: String, searchValue: String?, searchRegex: Boolean): IPage<Long> fun selectPage(page: IPage<Long>, searchType: String, searchValue: String?, searchRegex: Boolean): IPage<Long>
fun selectListWithRoleInfoByIds(userIds: List<Long>): List<User>? fun selectListWithRoleInfoByIds(userIds: List<Long>): List<User>
fun selectOneWithRoleInfoById(id: Long): User? fun selectOneWithRoleInfoById(id: Long): User?
fun selectListWithInfo(): List<User> fun selectListWithInfo(): List<User>
fun selectIdsWithRoleIds(roleIds: List<Long>): List<Long>
fun selectIdsWithGroupIds(groupIds: List<Long>): List<Long>
} }

View File

@@ -14,7 +14,7 @@ import jakarta.validation.constraints.NotNull
data class GroupUpdateParam( data class GroupUpdateParam(
@Schema(description = "用户组 ID") @Schema(description = "用户组 ID")
@field:NotNull(message = "ID can not be null") @field:NotNull(message = "ID can not be null")
val id: Long, val id: Long?,
@Schema(description = "用户组名称") @Schema(description = "用户组名称")
@field:NotBlank(message = "Name can not be blank") @field:NotBlank(message = "Name can not be blank")

View File

@@ -13,7 +13,7 @@ import jakarta.validation.constraints.NotNull
data class RoleChangeStatusParam( data class RoleChangeStatusParam(
@Schema(description = "角色 ID") @Schema(description = "角色 ID")
@field:NotNull(message = "Role id can not be null") @field:NotNull(message = "Role id can not be null")
val id: Long, val id: Long?,
@Schema(description = "启用", allowableValues = ["true", "false"], defaultValue = "true") @Schema(description = "启用", allowableValues = ["true", "false"], defaultValue = "true")
val enable: Boolean = true val enable: Boolean = true

View File

@@ -14,7 +14,7 @@ import jakarta.validation.constraints.NotNull
data class RoleUpdateParam( data class RoleUpdateParam(
@Schema(description = "角色 ID") @Schema(description = "角色 ID")
@field:NotNull(message = "Role id can not be null") @field:NotNull(message = "Role id can not be null")
val id: Long, val id: Long?,
@Schema(description = "角色名称") @Schema(description = "角色名称")
@field:NotBlank(message = "Name can not be blank") @field:NotBlank(message = "Name can not be blank")

View File

@@ -35,4 +35,8 @@ interface IUserService : IService<User> {
fun deleteOne(id: Long) fun deleteOne(id: Long)
fun delete(userDeleteParam: UserDeleteParam) fun delete(userDeleteParam: UserDeleteParam)
fun selectIdsWithRoleIds(roleIds: List<Long>): List<Long>
fun selectIdsWithGroupIds(groupIds: List<Long>): List<Long>
} }

View File

@@ -12,7 +12,10 @@ import top.fatweb.api.mapper.permission.GroupMapper
import top.fatweb.api.param.permission.group.* import top.fatweb.api.param.permission.group.*
import top.fatweb.api.service.permission.IGroupService import top.fatweb.api.service.permission.IGroupService
import top.fatweb.api.service.permission.IRoleGroupService import top.fatweb.api.service.permission.IRoleGroupService
import top.fatweb.api.service.permission.IUserService
import top.fatweb.api.util.PageUtil import top.fatweb.api.util.PageUtil
import top.fatweb.api.util.RedisUtil
import top.fatweb.api.util.WebUtil
import top.fatweb.api.vo.PageVo import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.permission.base.GroupVo import top.fatweb.api.vo.permission.base.GroupVo
import top.fatweb.api.vo.permission.GroupWithRoleVo import top.fatweb.api.vo.permission.GroupWithRoleVo
@@ -25,7 +28,9 @@ import top.fatweb.api.vo.permission.GroupWithRoleVo
*/ */
@Service @Service
class GroupServiceImpl( class GroupServiceImpl(
private val roleGroupService: IRoleGroupService private val redisUtil: RedisUtil,
private val roleGroupService: IRoleGroupService,
private val userService: IUserService
) : ServiceImpl<GroupMapper, Group>(), IGroupService { ) : ServiceImpl<GroupMapper, Group>(), IGroupService {
override fun getPage(groupGetParam: GroupGetParam?): PageVo<GroupWithRoleVo> { override fun getPage(groupGetParam: GroupGetParam?): PageVo<GroupWithRoleVo> {
val groupIdsPage = Page<Long>(groupGetParam?.currentPage ?: 1, groupGetParam?.pageSize ?: 20) val groupIdsPage = Page<Long>(groupGetParam?.currentPage ?: 1, groupGetParam?.pageSize ?: 20)
@@ -107,11 +112,19 @@ class GroupServiceImpl(
}) })
} }
groupUpdateParam.id?.let { offlineUser(it) }
return GroupConverter.groupToGroupVo(group) return GroupConverter.groupToGroupVo(group)
} }
override fun changeStatus(groupChangeStatusParam: GroupChangeStatusParam): Boolean { override fun changeStatus(groupChangeStatusParam: GroupChangeStatusParam): Boolean {
return updateById(GroupConverter.groupChangeStatusParamToGroup(groupChangeStatusParam)) updateById(GroupConverter.groupChangeStatusParamToGroup(groupChangeStatusParam)).let {
if (it && !groupChangeStatusParam.enable) {
groupChangeStatusParam.id?.let { id -> offlineUser(id) }
}
return it
}
} }
@Transactional @Transactional
@@ -123,5 +136,11 @@ class GroupServiceImpl(
override fun delete(groupDeleteParam: GroupDeleteParam) { override fun delete(groupDeleteParam: GroupDeleteParam) {
baseMapper.deleteBatchIds(groupDeleteParam.ids) baseMapper.deleteBatchIds(groupDeleteParam.ids)
roleGroupService.remove(KtQueryWrapper(RoleGroup()).`in`(RoleGroup::groupId, groupDeleteParam.ids)) roleGroupService.remove(KtQueryWrapper(RoleGroup()).`in`(RoleGroup::groupId, groupDeleteParam.ids))
offlineUser(*groupDeleteParam.ids.toLongArray())
}
private fun offlineUser(vararg groupIds: Long) {
val userIds = userService.selectIdsWithGroupIds(groupIds.toList())
WebUtil.offlineUser(redisUtil, *userIds.toLongArray())
} }
} }

View File

@@ -10,11 +10,10 @@ import top.fatweb.api.entity.permission.PowerRole
import top.fatweb.api.entity.permission.Role import top.fatweb.api.entity.permission.Role
import top.fatweb.api.mapper.permission.RoleMapper import top.fatweb.api.mapper.permission.RoleMapper
import top.fatweb.api.param.permission.role.* import top.fatweb.api.param.permission.role.*
import top.fatweb.api.service.permission.IFuncService import top.fatweb.api.service.permission.*
import top.fatweb.api.service.permission.IMenuService
import top.fatweb.api.service.permission.IPowerRoleService
import top.fatweb.api.service.permission.IRoleService
import top.fatweb.api.util.PageUtil import top.fatweb.api.util.PageUtil
import top.fatweb.api.util.RedisUtil
import top.fatweb.api.util.WebUtil
import top.fatweb.api.vo.PageVo import top.fatweb.api.vo.PageVo
import top.fatweb.api.vo.permission.base.RoleVo import top.fatweb.api.vo.permission.base.RoleVo
import top.fatweb.api.vo.permission.RoleWithPowerVo import top.fatweb.api.vo.permission.RoleWithPowerVo
@@ -27,9 +26,11 @@ import top.fatweb.api.vo.permission.RoleWithPowerVo
*/ */
@Service @Service
class RoleServiceImpl( class RoleServiceImpl(
private val redisUtil: RedisUtil,
private val powerRoleService: IPowerRoleService, private val powerRoleService: IPowerRoleService,
private val funcService: IFuncService, private val funcService: IFuncService,
private val menuService: IMenuService private val menuService: IMenuService,
private val userService: IUserService
) : ServiceImpl<RoleMapper, Role>(), IRoleService { ) : ServiceImpl<RoleMapper, Role>(), IRoleService {
override fun getPage(roleGetParam: RoleGetParam?): PageVo<RoleWithPowerVo> { override fun getPage(roleGetParam: RoleGetParam?): PageVo<RoleWithPowerVo> {
val roleIdsPage = Page<Long>(roleGetParam?.currentPage ?: 1, roleGetParam?.pageSize ?: 20) val roleIdsPage = Page<Long>(roleGetParam?.currentPage ?: 1, roleGetParam?.pageSize ?: 20)
@@ -118,11 +119,19 @@ class RoleServiceImpl(
}) })
} }
roleUpdateParam.id?.let { offlineUser(it) }
return RoleConverter.roleToRoleVo(role) return RoleConverter.roleToRoleVo(role)
} }
override fun changeStatus(roleChangeStatusParam: RoleChangeStatusParam): Boolean { override fun changeStatus(roleChangeStatusParam: RoleChangeStatusParam): Boolean {
return updateById(RoleConverter.roleChangeStatusParamToRole(roleChangeStatusParam)) updateById(RoleConverter.roleChangeStatusParamToRole(roleChangeStatusParam)).let {
if (it && !roleChangeStatusParam.enable) {
roleChangeStatusParam.id?.let { id -> offlineUser(id) }
}
return it
}
} }
@Transactional @Transactional
@@ -134,6 +143,7 @@ class RoleServiceImpl(
override fun delete(roleDeleteParam: RoleDeleteParam) { override fun delete(roleDeleteParam: RoleDeleteParam) {
baseMapper.deleteBatchIds(roleDeleteParam.ids) baseMapper.deleteBatchIds(roleDeleteParam.ids)
powerRoleService.remove(KtQueryWrapper(PowerRole()).`in`(PowerRole::roleId, roleDeleteParam.ids)) powerRoleService.remove(KtQueryWrapper(PowerRole()).`in`(PowerRole::roleId, roleDeleteParam.ids))
offlineUser(*roleDeleteParam.ids.toLongArray())
} }
private fun getFullPowerIds(powerIds: List<Long>): Set<Long> { private fun getFullPowerIds(powerIds: List<Long>): Set<Long> {
@@ -161,4 +171,9 @@ class RoleServiceImpl(
getMenuParent(it, parentIds) getMenuParent(it, parentIds)
} }
} }
private fun offlineUser(vararg roleIds: Long) {
val userIds = userService.selectIdsWithRoleIds(roleIds.toList())
WebUtil.offlineUser(redisUtil, *userIds.toLongArray())
}
} }

View File

@@ -248,4 +248,8 @@ class UserServiceImpl(
WebUtil.offlineUser(redisUtil, *ids.toLongArray()) WebUtil.offlineUser(redisUtil, *ids.toLongArray())
} }
override fun selectIdsWithRoleIds(roleIds: List<Long>) = baseMapper.selectIdsWithRoleIds(roleIds)
override fun selectIdsWithGroupIds(groupIds: List<Long>) = baseMapper.selectIdsWithGroupIds(groupIds)
} }

View File

@@ -251,6 +251,34 @@
where t_user.deleted = 0 where t_user.deleted = 0
</select> </select>
<select id="selectIdsWithRoleIds" resultType="long">
select t_user.id
from t_user
left join (select * from t_user_role where deleted = 0) as tur on t_user.id = tur.user_id
left join (select * from t_role where deleted = 0) as tr on tr.id = tur.role_id
<where>
t_user.deleted = 0
<foreach collection="roleIds" item="item" index="index" open="and tr.id in (" separator="," close=")"
nullable="true">
#{item}
</foreach>
</where>
</select>
<select id="selectIdsWithGroupIds" resultType="long">
select t_user.id
from t_user
left join (select * from t_user_group where deleted = 0) as tug on t_user.id = tug.user_id
left join (select * from t_group where deleted = 0) as tg on tg.id = tug.group_id
<where>
t_user.deleted = 0
<foreach collection="groupIds" item="item" index="index" open="and tg.id in (" separator="," close=")"
nullable="true">
#{item}
</foreach>
</where>
</select>
<resultMap id="userBaseMap" type="user"> <resultMap id="userBaseMap" type="user">
<id property="id" column="user_id"/> <id property="id" column="user_id"/>
<result property="username" column="user_username"/> <result property="username" column="user_username"/>