|
@@ -1,12 +1,33 @@
|
|
package com.winhc.repal.service.impl;
|
|
package com.winhc.repal.service.impl;
|
|
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.winhc.common.enums.CodeMsg;
|
|
|
|
+import com.winhc.common.exception.CommonException;
|
|
|
|
+import com.winhc.repal.entity.RepalGroupMember;
|
|
import com.winhc.repal.entity.RepalGroupRole;
|
|
import com.winhc.repal.entity.RepalGroupRole;
|
|
|
|
+import com.winhc.repal.entity.RepalRolePermission;
|
|
|
|
+import com.winhc.repal.enums.DeletedStatusEnum;
|
|
|
|
+import com.winhc.repal.enums.RoleTypeEnum;
|
|
|
|
+import com.winhc.repal.model.dto.MemberRoleDTO;
|
|
|
|
+import com.winhc.repal.model.vo.GroupRoleVO;
|
|
|
|
+import com.winhc.repal.model.vo.RolePermissionVO;
|
|
import com.winhc.repal.repository.RepalGroupRoleMapper;
|
|
import com.winhc.repal.repository.RepalGroupRoleMapper;
|
|
|
|
+import com.winhc.repal.service.RepalGroupMemberService;
|
|
import com.winhc.repal.service.RepalGroupRoleService;
|
|
import com.winhc.repal.service.RepalGroupRoleService;
|
|
|
|
+import com.winhc.repal.service.RepalRolePermissionService;
|
|
|
|
+import com.winhc.repal.util.UserContextUtil;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Objects;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @description RepalGroupRole 接口实现类
|
|
* @description RepalGroupRole 接口实现类
|
|
@@ -17,8 +38,83 @@ import java.util.List;
|
|
public class RepalGroupRoleServiceImpl extends ServiceImpl<RepalGroupRoleMapper, RepalGroupRole> implements RepalGroupRoleService {
|
|
public class RepalGroupRoleServiceImpl extends ServiceImpl<RepalGroupRoleMapper, RepalGroupRole> implements RepalGroupRoleService {
|
|
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private RepalRolePermissionService repalRolePermissionService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RepalGroupMemberService repalGroupMemberService;
|
|
|
|
+
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public List<RepalGroupRole> getRoleByIdList(List<Long> idList) {
|
|
public List<RepalGroupRole> getRoleByIdList(List<Long> idList) {
|
|
return this.listByIds(idList);
|
|
return this.listByIds(idList);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public Boolean addRole(MemberRoleDTO dto) {
|
|
|
|
+ long groupId = repalGroupMemberService.getUserGroupId(UserContextUtil.getUser().getUserId());
|
|
|
|
+ // 添加角色
|
|
|
|
+ RepalGroupRole repalGroupRole = new RepalGroupRole();
|
|
|
|
+ repalGroupRole.setRepalGroupId(groupId);
|
|
|
|
+ repalGroupRole.setRoleType(RoleTypeEnum.CUSTOMER.getCode());
|
|
|
|
+ repalGroupRole.setRoleName(dto.getRoleName());
|
|
|
|
+ repalGroupRole.setRepalGroupId(groupId);
|
|
|
|
+ this.save(repalGroupRole);
|
|
|
|
+ // 为角色添加权限
|
|
|
|
+ repalRolePermissionService.addRolePermission(repalGroupRole.getId(), dto.getPermissionCodeList());
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<GroupRoleVO> getGroupRoleList() {
|
|
|
|
+ long userId = UserContextUtil.getUser().getUserId();
|
|
|
|
+ List<RepalGroupRole> groupRoleList = this.list(Wrappers.lambdaQuery(RepalGroupRole.class)
|
|
|
|
+ .in(RepalGroupRole::getRepalGroupId, Stream.of(0, repalGroupMemberService.getUserGroupId(userId)).collect(Collectors.toList()))
|
|
|
|
+ .eq(RepalGroupRole::getDeleted, DeletedStatusEnum.NORMAL.getCode())
|
|
|
|
+ .orderByDesc(RepalGroupRole::getId));
|
|
|
|
+ return groupRoleList.stream().map(role -> {
|
|
|
|
+ GroupRoleVO vo = new GroupRoleVO();
|
|
|
|
+ vo.setRoleId(role.getId());
|
|
|
|
+ vo.setRoleType(role.getRoleType());
|
|
|
|
+ vo.setRoleName(role.getRoleName());
|
|
|
|
+ return vo;
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<RolePermissionVO> getPermissionList(Long roleId) {
|
|
|
|
+ List<RepalRolePermission> permissionList = repalRolePermissionService.getRolePermissionList(roleId);
|
|
|
|
+ if (CollUtil.isEmpty(permissionList)) {
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
+ }
|
|
|
|
+ return permissionList.stream().map(permission -> {
|
|
|
|
+ RolePermissionVO vo = new RolePermissionVO();
|
|
|
|
+ vo.setPermissionName(permission.getPermissionName());
|
|
|
|
+ vo.setPermissionCode(permission.getPermissionCode());
|
|
|
|
+ return vo;
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public Boolean deleteRoleById(Long roleId) {
|
|
|
|
+ long userId = UserContextUtil.getUser().getUserId();
|
|
|
|
+ RepalGroupRole repalGroupRole = this.getById(roleId);
|
|
|
|
+ if (RoleTypeEnum.SYSTEM.getCode().equals(repalGroupRole.getRoleType())) {
|
|
|
|
+ throw new CommonException(CodeMsg.FAILED, "系统角色无法删除");
|
|
|
|
+ }
|
|
|
|
+ if (!repalGroupRole.getRepalGroupId().equals(repalGroupMemberService.getUserGroupId(userId))) {
|
|
|
|
+ throw new CommonException(CodeMsg.FAILED, "该角色不属于该组织");
|
|
|
|
+ }
|
|
|
|
+ if (repalGroupMemberService.count(Wrappers.lambdaQuery(RepalGroupMember.class)
|
|
|
|
+ .eq(RepalGroupMember::getRoleId, roleId)
|
|
|
|
+ .eq(RepalGroupMember::getDeleted, DeletedStatusEnum.NORMAL.getCode())) > 0) {
|
|
|
|
+ throw new CommonException(CodeMsg.FAILED, "有成员为该角色,无法删除,请先改变该成员的角色后再删除");
|
|
|
|
+ }
|
|
|
|
+ this.update(Wrappers.lambdaUpdate(RepalGroupRole.class)
|
|
|
|
+ .eq(RepalGroupRole::getId, roleId)
|
|
|
|
+ .set(RepalGroupRole::getDeleted, DeletedStatusEnum.DELETE.getCode()));
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
}
|
|
}
|