|
|
@ -5,6 +5,7 @@ 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; |
|
|
@ -17,7 +18,10 @@ 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 |
|
|
@ -39,6 +43,8 @@ public class RoleServiceImpl implements RoleService { |
|
|
|
private AdminService adminService; |
|
|
|
@Autowired |
|
|
|
private GeneralService generalService; |
|
|
|
@Autowired |
|
|
|
private MenuMapper menuMapper; |
|
|
|
|
|
|
|
//新增角色 |
|
|
|
@Override |
|
|
@ -96,6 +102,89 @@ public class RoleServiceImpl implements RoleService { |
|
|
|
return Result.success("创建成功"); |
|
|
|
} |
|
|
|
|
|
|
|
//新增角色 |
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public Result addRole2(RoleVo roleVo) { |
|
|
|
//校验角色 |
|
|
|
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("权限为空"); |
|
|
|
} 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 |
|
|
|