【8.1.0】【user-org】更新同步绑定角色业务

pull/60/head
fengshuonan 2024-01-18 18:46:56 +08:00
parent ef8be70758
commit 3dd43b8d23
9 changed files with 210 additions and 0 deletions

View File

@ -33,6 +33,16 @@ public interface SysUserOrgServiceApi {
*/
List<UserOrgDTO> getUserOrgList(Long userId, boolean getTotalOrg);
/**
* id
*
* @param userId id
* @param getTotalOrg true-false-
* @author fengshuonan
* @since 2024-01-18 17:48
*/
List<Long> getUserOrgIdList(Long userId, boolean getTotalOrg);
/**
* id
*
@ -79,4 +89,15 @@ public interface SysUserOrgServiceApi {
*/
Set<Long> getOrgUserIdList(Set<Long> orgIdList);
/**
*
*
* @param userId id
* @param excludeOrgId id
* @param statusFlag 1-2-
* @author fengshuonan
* @since 2024-01-18 17:20
*/
void updateOtherOrgStatus(Long userId, Long excludeOrgId, Integer statusFlag);
}

View File

@ -0,0 +1,46 @@
package cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request;
import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.NewUserRoleBindItem;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
*
*
* @author fengshuonan
* @since 2024-01-18 17:12
*/
@Data
public class SyncBindRequest {
/**
* id
*/
@ChineseDescription("用户id")
@NotNull(message = "用户id不能为空")
private Long userId;
/**
* id
*/
@ChineseDescription("用户所属机构id")
@NotNull(message = "用户所属机构id不能为空")
private Long orgId;
/**
* true-
*/
@ChineseDescription("用户所属机构的状态true-启用")
@NotNull(message = "用户所属机构的状态不能为空")
private Boolean statusFlag;
/**
*
*/
@ChineseDescription("角色绑定的详情")
private List<NewUserRoleBindItem> roleBindItemList;
}

View File

@ -10,6 +10,7 @@ import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.NewUserRoleBindRespon
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.DeleteRequest;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.RoleControlRequest;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.StatusControlRequest;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.SyncBindRequest;
import cn.stylefeng.roses.kernel.sys.modular.user.pojo.request.SysUserOrgRequest;
import cn.stylefeng.roses.kernel.sys.modular.user.service.SysRoleAssignService;
import cn.stylefeng.roses.kernel.sys.modular.user.service.SysUserOrgService;
@ -121,4 +122,16 @@ public class UserRoleAssignPageController {
return new SuccessResponseData<>();
}
/**
*
*
* @author fengshuonan
* @since 2024-01-18 15:47
*/
@PostResource(name = "同步到其他公司绑定信息", path = "/sysRoleAssign/syncOtherOrgStatusAndBusinessRole")
public ResponseData<?> syncOtherOrgStatusAndBusinessRole(@RequestBody @Validated SyncBindRequest syncBindRequest) {
sysRoleAssignService.syncOtherOrgStatusAndBusinessRole(syncBindRequest);
return new SuccessResponseData<>();
}
}

View File

@ -4,6 +4,7 @@ import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.NewUserRoleBindRespon
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.DeleteRequest;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.RoleControlRequest;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.StatusControlRequest;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.SyncBindRequest;
import cn.stylefeng.roses.kernel.sys.modular.user.pojo.request.SysUserOrgRequest;
import java.util.List;
@ -64,4 +65,12 @@ public interface SysRoleAssignService {
*/
void disableAllOrgStatus(SysUserOrgRequest sysUserOrgRequest);
/**
*
*
* @author fengshuonan
* @since 2024-01-18 17:16
*/
void syncOtherOrgStatusAndBusinessRole(SyncBindRequest syncBindRequest);
}

View File

@ -1,10 +1,13 @@
package cn.stylefeng.roses.kernel.sys.modular.user.service;
import cn.stylefeng.roses.kernel.sys.api.SysUserRoleServiceApi;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.NewUserRoleBindItem;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
import cn.stylefeng.roses.kernel.sys.modular.user.pojo.request.SysUserRoleRequest;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
*
*
@ -45,4 +48,15 @@ public interface SysUserRoleService extends IService<SysUserRole>, SysUserRoleSe
*/
void addBusinessAndCompanyBindRole(SysUserRole sysUserRole);
/**
*
*
* @param userId id
* @param excludeOrgId id
* @param currentOrgBindRoleList
* @author fengshuonan
* @since 2024-01-18 17:41
*/
void updateOtherOrgBusinessRole(Long userId, Long excludeOrgId, List<NewUserRoleBindItem> currentOrgBindRoleList);
}

View File

@ -1,16 +1,20 @@
package cn.stylefeng.roses.kernel.sys.modular.user.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.sys.api.SysRoleServiceApi;
import cn.stylefeng.roses.kernel.sys.api.enums.role.RoleTypeEnum;
import cn.stylefeng.roses.kernel.sys.api.pojo.role.SysRoleDTO;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.UserOrgDTO;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.NewUserRoleBindItem;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.NewUserRoleBindResponse;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.UserRoleDTO;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.DeleteRequest;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.RoleControlRequest;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.StatusControlRequest;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.request.SyncBindRequest;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserOrg;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
import cn.stylefeng.roses.kernel.sys.modular.user.enums.SysUserOrgExceptionEnum;
@ -23,6 +27,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@ -53,6 +58,11 @@ public class SysRoleAssignServiceImpl implements SysRoleAssignService {
// 1. 获取用户的所有绑定的组织机构信息
List<UserOrgDTO> userOrgList = sysUserOrgService.getUserOrgList(userId, true);
// 用户没绑定机构,直接返回
if (ObjectUtil.isEmpty(userOrgList)) {
return new ArrayList<>();
}
// 2. 组装NewUserRoleBindResponse
List<NewUserRoleBindResponse> baseResponse = RoleAssignFactory.createBaseResponse(userOrgList);
@ -148,4 +158,21 @@ public class SysRoleAssignServiceImpl implements SysRoleAssignService {
sysUserOrgService.disableAllOrgStatus(sysUserOrgRequest.getUserId());
}
@Override
public void syncOtherOrgStatusAndBusinessRole(SyncBindRequest syncBindRequest) {
// 1. 获取当前指定公司的同步状态
Boolean statusFlag = syncBindRequest.getStatusFlag();
// 2. 更新用户所属其他组织机构的状态
sysUserOrgService.updateOtherOrgStatus(syncBindRequest.getUserId(), syncBindRequest.getOrgId(), statusFlag ? StatusEnum.ENABLE.getCode() : StatusEnum.DISABLE.getCode());
// 3. 筛选出当前公司的业务角色
List<NewUserRoleBindItem> currentOrgBindRoleList = syncBindRequest.getRoleBindItemList().stream().filter(i -> i.getRoleType().equals(RoleTypeEnum.BUSINESS_ROLE.getCode()))
.filter(NewUserRoleBindItem::getCheckedFlag).collect(Collectors.toList());
// 4. 更新用户所属其他组织机构的业务角色
sysUserRoleService.updateOtherOrgBusinessRole(syncBindRequest.getUserId(), syncBindRequest.getOrgId(), currentOrgBindRoleList);
}
}

View File

@ -226,6 +226,22 @@ public class SysUserOrgServiceImpl extends ServiceImpl<SysUserOrgMapper, SysUser
return userOrgDTOS;
}
@Override
public List<Long> getUserOrgIdList(Long userId, boolean getTotalOrg) {
LambdaQueryWrapper<SysUserOrg> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysUserOrg::getUserId, userId);
if (!getTotalOrg) {
queryWrapper.eq(SysUserOrg::getStatusFlag, StatusEnum.ENABLE.getCode());
}
queryWrapper.select(SysUserOrg::getOrgId);
List<SysUserOrg> sysUserOrgList = this.list(queryWrapper);
if (ObjectUtil.isEmpty(sysUserOrgList)) {
return new ArrayList<>();
} else {
return sysUserOrgList.stream().map(SysUserOrg::getOrgId).collect(Collectors.toList());
}
}
@Override
public List<Long> getOrgUserIdList(Long orgId, Boolean containSubOrgFlag) {
@ -305,6 +321,17 @@ public class SysUserOrgServiceImpl extends ServiceImpl<SysUserOrgMapper, SysUser
return list.stream().map(SysUserOrg::getUserId).collect(Collectors.toSet());
}
@Override
public void updateOtherOrgStatus(Long userId, Long excludeOrgId, Integer statusFlag) {
LambdaUpdateWrapper<SysUserOrg> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(SysUserOrg::getUserId, userId);
if (ObjectUtil.isNotEmpty(excludeOrgId)) {
updateWrapper.ne(SysUserOrg::getOrgId, excludeOrgId);
}
updateWrapper.set(SysUserOrg::getStatusFlag, statusFlag);
this.update(updateWrapper);
}
/**
*
*

View File

@ -13,11 +13,13 @@ import cn.stylefeng.roses.kernel.sys.api.callback.RemoveRoleCallbackApi;
import cn.stylefeng.roses.kernel.sys.api.callback.RemoveUserCallbackApi;
import cn.stylefeng.roses.kernel.sys.api.constants.SysConstants;
import cn.stylefeng.roses.kernel.sys.api.enums.role.RoleTypeEnum;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.NewUserRoleBindItem;
import cn.stylefeng.roses.kernel.sys.api.pojo.user.newrole.UserRoleDTO;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
import cn.stylefeng.roses.kernel.sys.modular.user.enums.SysUserExceptionEnum;
import cn.stylefeng.roses.kernel.sys.modular.user.mapper.SysUserRoleMapper;
import cn.stylefeng.roses.kernel.sys.modular.user.pojo.request.SysUserRoleRequest;
import cn.stylefeng.roses.kernel.sys.modular.user.service.SysUserOrgService;
import cn.stylefeng.roses.kernel.sys.modular.user.service.SysUserRoleService;
import cn.stylefeng.roses.kernel.sys.modular.user.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@ -55,6 +57,9 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
@Resource
private SysRoleLimitServiceApi sysRoleLimitServiceApi;
@Resource
private SysUserOrgService sysUserOrgService;
@Override
@Transactional(rollbackFor = Exception.class)
public void bindRoles(SysUserRoleRequest sysUserRoleRequest) {
@ -289,6 +294,51 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
this.save(sysUserRole);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateOtherOrgBusinessRole(Long userId, Long excludeOrgId, List<NewUserRoleBindItem> currentOrgBindRoleList) {
// 1. 清空用户在其他公司的业务角色
LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysUserRole::getUserId, userId);
queryWrapper.eq(SysUserRole::getRoleType, RoleTypeEnum.BUSINESS_ROLE.getCode());
queryWrapper.ne(SysUserRole::getRoleOrgId, excludeOrgId);
this.remove(queryWrapper);
// 清空角色缓存
userRoleCache.remove(userId.toString());
// 2. 获取用户有几个机构
List<Long> userOrgIdList = sysUserOrgService.getUserOrgIdList(userId, true);
if (ObjectUtil.isEmpty(userOrgIdList)) {
return;
}
List<Long> excludeAfterOrgIds = userOrgIdList.stream().filter(orgId -> !orgId.equals(excludeOrgId)).collect(Collectors.toList());
if (ObjectUtil.isEmpty(excludeAfterOrgIds)) {
return;
}
// 3. 给这些机构,绑定上当前公司的业务角色
if (ObjectUtil.isEmpty(currentOrgBindRoleList)) {
return;
}
ArrayList<SysUserRole> sysUserRoles = new ArrayList<>();
for (Long userOrgId : excludeAfterOrgIds) {
for (NewUserRoleBindItem newUserRoleBindItem : currentOrgBindRoleList) {
SysUserRole sysUserRole = new SysUserRole();
sysUserRole.setUserId(userId);
sysUserRole.setRoleId(newUserRoleBindItem.getRoleId());
sysUserRole.setRoleType(RoleTypeEnum.BUSINESS_ROLE.getCode());
sysUserRole.setRoleOrgId(userOrgId);
sysUserRoles.add(sysUserRole);
}
}
this.saveBatch(sysUserRoles);
// 清空角色缓存
userRoleCache.remove(userId.toString());
}
/**
*
*

View File

@ -349,6 +349,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
@Override
public List<SysRoleDTO> getBusinessRoleAndCompanyRole(List<Long> companyIdList) {
if (ObjectUtil.isEmpty(companyIdList)) {
return new ArrayList<>();
}
LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysRoleLambdaQueryWrapper.eq(SysRole::getRoleType, RoleTypeEnum.BUSINESS_ROLE.getCode());
sysRoleLambdaQueryWrapper.or().nested(i -> i.eq(SysRole::getRoleType, RoleTypeEnum.COMPANY_ROLE.getCode()).and(j -> j.in(SysRole::getRoleCompanyId, companyIdList)));