You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
8.5 KiB
274 lines
8.5 KiB
package com.example.demo.serviceImpl;
|
|
|
|
import com.example.demo.domain.entity.Role;
|
|
import com.example.demo.domain.vo.ConsumeUser;
|
|
import com.example.demo.domain.vo.MenuVo;
|
|
import com.example.demo.domain.vo.Result;
|
|
import com.example.demo.domain.vo.RoleVo;
|
|
import com.example.demo.mapper.MenuMapper;
|
|
import com.example.demo.mapper.RefundMapper;
|
|
import com.example.demo.mapper.RoleMapper;
|
|
import com.example.demo.service.AdminService;
|
|
import com.example.demo.service.GeneralService;
|
|
import com.example.demo.service.MenuService;
|
|
import com.example.demo.service.RoleService;
|
|
import com.github.pagehelper.PageHelper;
|
|
import com.github.pagehelper.PageInfo;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
/**
|
|
* @program: gold-java
|
|
* @ClassName RoleServiceImpl
|
|
* @description:
|
|
* @author: Double
|
|
* @create: 2025−07-15 11:30
|
|
* @Version 1.0
|
|
**/
|
|
|
|
@Service
|
|
public class RoleServiceImpl implements RoleService {
|
|
|
|
@Autowired
|
|
private RoleMapper roleMapper;
|
|
@Autowired
|
|
private MenuService menuService;
|
|
@Autowired
|
|
private AdminService adminService;
|
|
@Autowired
|
|
private GeneralService generalService;
|
|
@Autowired
|
|
private MenuMapper menuMapper;
|
|
|
|
//新增角色
|
|
@Override
|
|
@Transactional
|
|
public Result addRole(RoleVo roleVo) {
|
|
roleVo.setRoleName(roleVo.getRoleName().trim());
|
|
// 对角色名进行去空格处理后判断是否为空
|
|
if (roleVo.getRoleName() == null || roleVo.getRoleName().isEmpty()) {
|
|
return Result.error("角色名为空");
|
|
}
|
|
// 新增校验:角色名长度需在 2 - 20 之间
|
|
if (roleVo.getRoleName().length() < 2 || roleVo.getRoleName().length() > 20) {
|
|
return Result.error("角色名长度需在2-20个字符之间");
|
|
}
|
|
if (roleMapper.selectByRoleName(roleVo.getRoleName()) != null) {
|
|
return Result.error("角色名重复");
|
|
}
|
|
|
|
|
|
if (roleVo.getPriority() == null) {
|
|
return Result.error("优先级为空");
|
|
}
|
|
if (roleVo.getPriority() <= 0 || roleVo.getPriority() > 999) {
|
|
return Result.error("优先级不在1-999");
|
|
}
|
|
if (roleVo.getMenuIds() == null || roleVo.getMenuIds().isEmpty()) {
|
|
return Result.error("权限为空");
|
|
}
|
|
if (roleVo.getMarket() == null || roleVo.getMarket().isEmpty()) {
|
|
return Result.error("所属市场为空");
|
|
}
|
|
if (roleVo.getFatherId() != null) {
|
|
if (roleVo.getFatherId() != 2) {
|
|
for (Integer menuId : roleVo.getMenuIds()) {
|
|
if (menuId == 9) {
|
|
return Result.error("上级角色非管理员用户不能赋予权限管理");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
|
|
roleMapper.addRole(roleVo);
|
|
Role role = roleMapper.selectByRoleName(roleVo.getRoleName());
|
|
if (role == null) {
|
|
return Result.error("角色添加失败");
|
|
} else {
|
|
roleVo.setId(role.getId());
|
|
}
|
|
menuService.addPermission(roleVo);
|
|
} catch (Exception e) {
|
|
return Result.error("添加失败");
|
|
}
|
|
|
|
return Result.success("创建成功");
|
|
}
|
|
|
|
//新增角色
|
|
@Override
|
|
@Transactional
|
|
public Result addRole2(RoleVo roleVo) {
|
|
//校验角色
|
|
// 对角色名进行去空格处理后判断是否为空
|
|
if (roleVo.getRoleName() == null || roleVo.getRoleName().trim().isEmpty()) {
|
|
return Result.error("角色名为空");
|
|
}
|
|
// 新增校验:角色名长度需在 2 - 20 之间
|
|
if (roleVo.getRoleName().length() < 2 || roleVo.getRoleName().length() > 20) {
|
|
return Result.error("角色名长度需在2-20个字符之间");
|
|
}
|
|
if (roleMapper.selectByRoleName(roleVo.getRoleName()) != null) {
|
|
return Result.error("角色名重复");
|
|
}
|
|
|
|
|
|
if (roleVo.getPriority() == null) {
|
|
return Result.error("优先级为空");
|
|
}
|
|
if (roleVo.getPriority() <= 0 || roleVo.getPriority() > 999) {
|
|
return Result.error("优先级不在1-999");
|
|
}
|
|
if (roleVo.getMenuIds() == null || roleVo.getMenuIds().isEmpty()) {
|
|
return Result.error("权限为空");
|
|
} else {
|
|
List<Integer> menuIds = new ArrayList<>(roleVo.getMenuIds()); // 创建副本避免修改原始列表
|
|
Set<Integer> processedIds = new HashSet<>(menuIds); // 使用Set快速检查存在性
|
|
|
|
// 处理每个菜单ID及其所有父菜单
|
|
for (int i = 0; i < menuIds.size(); i++) {
|
|
Integer currentId = menuIds.get(i);
|
|
|
|
// ID为1不处理
|
|
if (currentId == 1) continue;
|
|
|
|
// 获取父菜单ID
|
|
Integer fatherId = menuMapper.selectFatherId(currentId);
|
|
|
|
// 递归添加所有父菜单
|
|
while (fatherId != null) {
|
|
if (!processedIds.contains(fatherId)) {
|
|
menuIds.add(fatherId);
|
|
processedIds.add(fatherId);
|
|
}
|
|
// 继续向上查找父菜单的父菜单
|
|
fatherId = menuMapper.selectFatherId(fatherId);
|
|
}
|
|
}
|
|
roleVo.setMenuIds(menuIds);
|
|
}
|
|
|
|
if (roleVo.getMarket() == null || roleVo.getMarket().isEmpty()) {
|
|
return Result.error("所属市场为空");
|
|
}
|
|
if (roleVo.getFatherId() != null) {
|
|
if (roleVo.getFatherId() != 2) {
|
|
for (Integer menuId : roleVo.getMenuIds()) {
|
|
if (menuId == 9) {
|
|
return Result.error("上级角色非管理员用户不能赋予权限管理");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
|
|
roleMapper.addRole(roleVo);
|
|
Role role = roleMapper.selectByRoleName(roleVo.getRoleName());
|
|
if (role == null) {
|
|
return Result.error("角色添加失败");
|
|
} else {
|
|
roleVo.setId(role.getId());
|
|
}
|
|
menuService.addPermission(roleVo);
|
|
System.out.println(roleVo.getMenuIds());
|
|
} catch (Exception e) {
|
|
return Result.error("添加失败");
|
|
}
|
|
|
|
return Result.success("创建成功");
|
|
}
|
|
|
|
//删除角色
|
|
@Override
|
|
@Transactional
|
|
public Result deleteRole(RoleVo roleVo) {
|
|
if (roleVo.getId() == null) {
|
|
return Result.error("角色id为空");
|
|
}
|
|
if (roleVo.getId() == 2) {
|
|
return Result.error("管理员不允许删除");
|
|
}
|
|
if (roleVo.getId() == 1) {
|
|
return Result.error("无用户不允许删除");
|
|
}
|
|
try {
|
|
roleMapper.deleteRoleMenu(roleVo);
|
|
roleMapper.deleteRole(roleVo);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return Result.error("删除失败");
|
|
}
|
|
return Result.success("删除成功");
|
|
}
|
|
|
|
@Override
|
|
public RoleVo selectFather(Integer id) {
|
|
|
|
return roleMapper.selectFather(id);
|
|
}
|
|
|
|
//查找全部角色
|
|
@Override
|
|
public List<RoleVo> selectAllRole() {
|
|
String account = adminService.getCurrentAdminAccount();
|
|
List<String> markets = generalService.getAdminMarket(account);
|
|
return roleMapper.selectAllRole(markets);
|
|
}
|
|
|
|
@Override
|
|
public List<RoleVo> selectSubRole(Integer id) {
|
|
return roleMapper.selectByFatherId(id);
|
|
}
|
|
|
|
//分页查找角色
|
|
@Override
|
|
public PageInfo<RoleVo> selectBy(Integer pageNum, Integer pageSize, RoleVo roleVo) {
|
|
String account = adminService.getCurrentAdminAccount();
|
|
List<String> markets = generalService.getAdminMarket(account);
|
|
roleVo.setMarkets(markets);
|
|
PageHelper.startPage(pageNum, pageSize);
|
|
List<RoleVo> roleVos = roleMapper.selectBy(roleVo);
|
|
// 遍历 roleVos 列表
|
|
for (RoleVo role : roleVos) {
|
|
// 获取每个角色的权限树
|
|
List<MenuVo> permissionTree = menuService.getPermissionTree(role.getId());
|
|
// 将权限树设置到角色对象中
|
|
role.setTree(permissionTree);
|
|
}
|
|
return new PageInfo<>(roleVos);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|