|
@@ -1,221 +0,0 @@
|
|
|
-package cn.cslg.permission.service.associate;
|
|
|
-
|
|
|
-import cn.cslg.permission.common.model.vo.DataVO;
|
|
|
-import cn.cslg.permission.common.model.vo.FunctionVO;
|
|
|
-import cn.cslg.permission.common.model.vo.RoleVO;
|
|
|
-import cn.cslg.permission.common.model.vo.TenantVO;
|
|
|
-import cn.cslg.permission.common.utils.DataUtils;
|
|
|
-import cn.cslg.permission.domain.Function;
|
|
|
-import cn.cslg.permission.domain.Role;
|
|
|
-import cn.cslg.permission.domain.Tenant;
|
|
|
-import cn.cslg.permission.domain.associate.AssoRoleFunctionData;
|
|
|
-import cn.cslg.permission.domain.associate.AssoTenantFunction;
|
|
|
-import cn.cslg.permission.mapper.associate.AssoTenantFunctionMapper;
|
|
|
-import cn.cslg.permission.service.FunctionService;
|
|
|
-import cn.cslg.permission.service.RoleService;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import lombok.RequiredArgsConstructor;
|
|
|
-import org.springframework.context.annotation.Lazy;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
-
|
|
|
-import java.util.*;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author 沈永艺
|
|
|
- * @date 2022-8-29
|
|
|
- * @description 关联类 功能对应租户 Service 层
|
|
|
- */
|
|
|
-
|
|
|
-@Service
|
|
|
-@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
|
-public class TenantFunctionService extends ServiceImpl<AssoTenantFunctionMapper, AssoTenantFunction> {
|
|
|
- private final AssoTenantFunctionMapper assoTenantFunctionMapper;
|
|
|
- private final FunctionService functionService;
|
|
|
- private final RoleFunctionDataService roleFunctionDataService;
|
|
|
- private final RoleService roleService;
|
|
|
-
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public void add(TenantVO tenantVO, Tenant tenant) {
|
|
|
- try {
|
|
|
- List<AssoTenantFunction> tfList = new ArrayList<>();
|
|
|
- tenantVO.getFunctions().forEach(item -> {
|
|
|
- AssoTenantFunction assoTenantFunction = new AssoTenantFunction();
|
|
|
- if (item.split(",").length > 1) {
|
|
|
- assoTenantFunction
|
|
|
- .setTenantId(tenant.getId())
|
|
|
- .setFunctionModifyPath(item)
|
|
|
- .setFunctionId(Integer.valueOf(item.split(",")[item.split(",").length - 1]));
|
|
|
- }
|
|
|
- tfList.add(assoTenantFunction);
|
|
|
- });
|
|
|
- if (tfList.size() != 0) {
|
|
|
- this.saveBatch(tfList);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public void delete(Integer tenantId, Integer functionId, List<Integer> functionIds) {
|
|
|
- try {
|
|
|
- LambdaQueryWrapper<AssoTenantFunction> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
- if (functionIds != null) {
|
|
|
- queryWrapper
|
|
|
- .in(AssoTenantFunction::getFunctionId, functionIds);
|
|
|
- } else {
|
|
|
- if (tenantId != null) {
|
|
|
- queryWrapper.eq(AssoTenantFunction::getTenantId, tenantId);
|
|
|
- } else if (functionId != null) {
|
|
|
- queryWrapper
|
|
|
- .eq(AssoTenantFunction::getFunctionId, functionId);
|
|
|
- }
|
|
|
- }
|
|
|
- this.remove(queryWrapper);
|
|
|
- } catch (Exception e) {
|
|
|
- TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public void updateRoleFunctionData(TenantVO tenantVO, Tenant tenant) {
|
|
|
- try {
|
|
|
- //查询角色ID下的有哪些功能ID
|
|
|
- LambdaQueryWrapper<AssoTenantFunction> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
- lambdaQueryWrapper.eq(AssoTenantFunction::getTenantId, tenantVO.getId());
|
|
|
- List<AssoTenantFunction> tfList = this.list(lambdaQueryWrapper);
|
|
|
- //获取已有功能List
|
|
|
- List<Integer> assoList = tfList.stream().map(AssoTenantFunction::getFunctionId).collect(Collectors.toList());
|
|
|
-
|
|
|
- //解析前台传入数据有哪些功能ID
|
|
|
- List<Integer> paramList = new ArrayList<>();
|
|
|
- List<Map<String, Object>> mapList = new ArrayList<>();
|
|
|
- tenantVO.getFunctions().forEach(item -> {
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
- map.put(item.split(",")[(item.split(",").length - 1)], item);
|
|
|
- mapList.add(map);
|
|
|
- paramList.add(Integer.valueOf(item.split(",")[(item.split(",").length - 1)]));
|
|
|
- });
|
|
|
-
|
|
|
- processDataByDiff(tenantVO, tenant, assoList, paramList, mapList);
|
|
|
- } catch (Exception e) {
|
|
|
- TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private void processDataByDiff(TenantVO tenantVO, Tenant tenant, List<Integer> assoList, List<Integer> paramList, List<Map<String, Object>> mapList) {
|
|
|
- Map<String, List<Integer>> compare = DataUtils.compareTwoList(assoList, paramList);
|
|
|
- List<Integer> reduce = compare.get("reduce");
|
|
|
- List<Integer> change = compare.get("change");
|
|
|
- if (reduce.size() > 0) {
|
|
|
- LambdaQueryWrapper<AssoTenantFunction> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
|
|
|
- lambdaQueryWrapper1
|
|
|
- .in(AssoTenantFunction::getFunctionId, reduce)
|
|
|
- .eq(AssoTenantFunction::getTenantId, tenantVO.getId());
|
|
|
- this.remove(lambdaQueryWrapper1);
|
|
|
-
|
|
|
- List<Integer> roleIds = roleService.list(new LambdaQueryWrapper<Role>().eq(Role::getTenantId, tenantVO.getId())).stream().map(Role::getId).collect(Collectors.toList());
|
|
|
- if (roleIds.size() > 0) {
|
|
|
- roleFunctionDataService.delete(null, null, reduce, roleIds);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- //移除该租户下角色的 租户已经没有的功能
|
|
|
- LambdaQueryWrapper<Role> queryWrapperRole = new LambdaQueryWrapper<>();
|
|
|
- queryWrapperRole.eq(Role::getTenantId, tenant.getId());
|
|
|
- List<Role> roles = roleService.list(queryWrapperRole);
|
|
|
- List<Integer> Ids = new ArrayList<>();
|
|
|
- roles.forEach((item -> {
|
|
|
- Ids.add(item.getId());
|
|
|
- }));
|
|
|
- LambdaQueryWrapper<AssoRoleFunctionData> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
- queryWrapper.in(AssoRoleFunctionData::getFunctionId, reduce).in(AssoRoleFunctionData::getRoleId, Ids);
|
|
|
- roleFunctionDataService.remove(queryWrapper);
|
|
|
- }
|
|
|
- if (change.size() > 0) {
|
|
|
- change.forEach(item -> {
|
|
|
- AssoTenantFunction assoTenantFunction = assoTenantFunctionMapper.getOneById(item, tenantVO.getId());
|
|
|
-
|
|
|
- if (assoTenantFunction != null && assoTenantFunction.getIsDelete() == 1) {
|
|
|
- assoTenantFunctionMapper.updateIsDeleteById(assoTenantFunction.getId());
|
|
|
- } else {
|
|
|
- final String[] modifyPath = new String[1];
|
|
|
- mapList.forEach(x -> {
|
|
|
- if (x.containsKey(item.toString())) {
|
|
|
- modifyPath[0] = (String) x.get(item.toString());
|
|
|
- }
|
|
|
- });
|
|
|
- tenantVO.setFunctions(Collections.singletonList(modifyPath[0]));
|
|
|
-
|
|
|
- this.add(tenantVO, tenant);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- //给该租户下的管理员添加功能
|
|
|
- LambdaQueryWrapper<Role> queryWrapperMaster = new LambdaQueryWrapper<>();
|
|
|
- queryWrapperMaster.eq(Role::getTenantId, tenant.getId()).eq(Role::getRoleType, 2);
|
|
|
- List<Role> roleList = roleService.list(queryWrapperMaster);
|
|
|
- if (roleList != null && roleList.size() != 0) {
|
|
|
- Integer roleId = roleList.get(0).getId();
|
|
|
- RoleVO roleVO = new RoleVO();
|
|
|
- roleVO.setId(roleId);
|
|
|
- List<RoleVO.Permission> permissions = new ArrayList<>();
|
|
|
- change.forEach(item -> {
|
|
|
- RoleVO.Permission permission = new RoleVO.Permission();
|
|
|
- permission.setFunctionId(item);
|
|
|
- DataVO dataVO = new DataVO();
|
|
|
- dataVO.setId(0);
|
|
|
- permission.setData(dataVO);
|
|
|
- permissions.add(permission);
|
|
|
-
|
|
|
- });
|
|
|
- roleVO.setPermissionData(permissions);
|
|
|
- roleFunctionDataService.add(roleVO);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public List<String> getFunctionModifyPath(Integer tenantId) {
|
|
|
- List<String> functionModifyPathList = new ArrayList<>();
|
|
|
- LambdaQueryWrapper<AssoTenantFunction> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
- lambdaQueryWrapper.eq(AssoTenantFunction::getTenantId, tenantId);
|
|
|
- List<AssoTenantFunction> rfdList = this.list(lambdaQueryWrapper);
|
|
|
- for (AssoTenantFunction assoTenantFunction : rfdList) {
|
|
|
- functionModifyPathList.add(assoTenantFunction.getFunctionModifyPath());
|
|
|
- }
|
|
|
- return functionModifyPathList;
|
|
|
- }
|
|
|
-
|
|
|
- public List<FunctionVO> getFunction(Integer tenantId) {
|
|
|
- LambdaQueryWrapper<AssoTenantFunction> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
- queryWrapper.eq(AssoTenantFunction::getTenantId, tenantId);
|
|
|
- List<AssoTenantFunction> tfList = this.list(queryWrapper);
|
|
|
- List<FunctionVO> functionVOList = new ArrayList<>();
|
|
|
- if (!tfList.isEmpty()) {
|
|
|
- List<Integer> idList = tfList.stream().map(AssoTenantFunction::getFunctionId).collect(Collectors.toList());
|
|
|
- List<Function> functionList = functionService.listByIds(idList);
|
|
|
- for (Function function : functionList) {
|
|
|
- FunctionVO functionVO = new FunctionVO();
|
|
|
- functionVO
|
|
|
- .setId(function.getId())
|
|
|
- .setName(function.getFunctionName())
|
|
|
- .setDescribe(function.getFunctionDescription());
|
|
|
- functionVOList.add(functionVO);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return functionVOList;
|
|
|
- }
|
|
|
-
|
|
|
-}
|