|
@@ -1,32 +1,120 @@
|
|
package com.backendsys.modules.system.service.impl;
|
|
package com.backendsys.modules.system.service.impl;
|
|
|
|
|
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
|
+import cn.hutool.core.map.MapUtil;
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
import com.backendsys.entity.System.SysUserRoleDTO;
|
|
import com.backendsys.entity.System.SysUserRoleDTO;
|
|
|
|
+import com.backendsys.exception.CustException;
|
|
import com.backendsys.modules.system.dao.SysUserRoleDao;
|
|
import com.backendsys.modules.system.dao.SysUserRoleDao;
|
|
-import com.backendsys.modules.system.entity.SysUser.SysUserInfo;
|
|
|
|
|
|
+import com.backendsys.modules.system.dao.SysUserRoleModuleRelationDao;
|
|
|
|
+import com.backendsys.modules.system.dao.SysUserRoleRelationDao;
|
|
import com.backendsys.modules.system.entity.SysUser.SysUserRole;
|
|
import com.backendsys.modules.system.entity.SysUser.SysUserRole;
|
|
|
|
+import com.backendsys.modules.system.entity.SysUser.SysUserRoleModuleRelation;
|
|
|
|
+import com.backendsys.modules.system.entity.SysUser.SysUserRoleRelation;
|
|
import com.backendsys.modules.system.service.SysUserRoleV2Service;
|
|
import com.backendsys.modules.system.service.SysUserRoleV2Service;
|
|
import com.backendsys.utils.response.PageEntity;
|
|
import com.backendsys.utils.response.PageEntity;
|
|
import com.backendsys.utils.response.PageInfoResult;
|
|
import com.backendsys.utils.response.PageInfoResult;
|
|
import com.backendsys.utils.v2.PageUtils;
|
|
import com.backendsys.utils.v2.PageUtils;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
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.List;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
public class SysUserRoleV2ServiceImpl extends ServiceImpl<SysUserRoleDao, SysUserRole> implements SysUserRoleV2Service {
|
|
public class SysUserRoleV2ServiceImpl extends ServiceImpl<SysUserRoleDao, SysUserRole> implements SysUserRoleV2Service {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private SysUserRoleDao sysUserRoleDao;
|
|
private SysUserRoleDao sysUserRoleDao;
|
|
|
|
+ @Autowired
|
|
|
|
+ private SysUserRoleRelationDao sysUserRoleRelationDao;
|
|
|
|
+ @Autowired
|
|
|
|
+ private SysUserRoleModuleRelationDao sysUserRoleModuleRelationDao;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public PageEntity getUserRole(SysUserRoleDTO sysUserRoleDTO) {
|
|
public PageEntity getUserRole(SysUserRoleDTO sysUserRoleDTO) {
|
|
- // 分页
|
|
|
|
- PageUtils.startPage();
|
|
|
|
|
|
+ PageUtils.startPage(); // 分页
|
|
List<SysUserRole> list = sysUserRoleDao.selectUserRoleList(sysUserRoleDTO);
|
|
List<SysUserRole> list = sysUserRoleDao.selectUserRoleList(sysUserRoleDTO);
|
|
return new PageInfoResult(list).toEntity();
|
|
return new PageInfoResult(list).toEntity();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public SysUserRole getUserRoleDetail(SysUserRoleDTO sysUserRoleDTO) {
|
|
|
|
+
|
|
|
|
+ Long role_id = sysUserRoleDTO.getRole_id();
|
|
|
|
+ SysUserRole roleDetail = sysUserRoleDao.selectById(role_id);
|
|
|
|
+ if (roleDetail == null) throw new CustException("系统用户角色不存在");
|
|
|
|
+
|
|
|
|
+ // 查询绑定人数
|
|
|
|
+ LambdaQueryWrapper<SysUserRoleRelation> queryWrapper = new LambdaQueryWrapper<SysUserRoleRelation>().eq(SysUserRoleRelation::getRole_id, role_id);
|
|
|
|
+ Long count = sysUserRoleRelationDao.selectCount(queryWrapper);
|
|
|
|
+ roleDetail.setUser_count(Convert.toInt(count));
|
|
|
|
+
|
|
|
|
+ return roleDetail;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional
|
|
|
|
+ public Map<String, Object> insertUserRole(SysUserRoleDTO sysUserRoleDTO) {
|
|
|
|
+
|
|
|
|
+ // 判断 modules 是否重复
|
|
|
|
+ List<String> modulesIds = sysUserRoleDTO.getModules();
|
|
|
|
+ if (CollUtil.isNotEmpty(modulesIds) && modulesIds.size() != CollUtil.distinct(modulesIds).size()) {
|
|
|
|
+ throw new CustException("模块ID出现重复值");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 创建角色
|
|
|
|
+ sysUserRoleDao.insertUserRole(sysUserRoleDTO);
|
|
|
|
+ // 创建角色与模块关系
|
|
|
|
+ sysUserRoleDao.insertUserRoleModuleRelation(sysUserRoleDTO);
|
|
|
|
+
|
|
|
|
+ return Map.of("role_id", sysUserRoleDTO.getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional
|
|
|
|
+ public Map<String, Object> updateUserRole(SysUserRoleDTO sysUserRoleDTO) {
|
|
|
|
+
|
|
|
|
+ // 判断 modules 是否重复
|
|
|
|
+ List<String> modulesIds = sysUserRoleDTO.getModules();
|
|
|
|
+ if (CollUtil.isNotEmpty(modulesIds) && modulesIds.size() != CollUtil.distinct(modulesIds).size()) {
|
|
|
|
+ throw new CustException("模块ID出现重复值");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ SysUserRole detail = sysUserRoleDao.selectById(sysUserRoleDTO.getRole_id());
|
|
|
|
+ if (detail == null) throw new CustException("系统用户角色不存在");
|
|
|
|
+
|
|
|
|
+ // 更新角色
|
|
|
|
+ sysUserRoleDao.updateUserRole(sysUserRoleDTO);
|
|
|
|
+ // 更新角色与模块关系 (需要先删除全部,再重新插入)
|
|
|
|
+ sysUserRoleDao.updateUserRoleModuleRelation(sysUserRoleDTO);
|
|
|
|
+
|
|
|
|
+ return Map.of("role_id", sysUserRoleDTO.getRole_id());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional
|
|
|
|
+ public Map<String, Object> deleteUserRole(SysUserRoleDTO sysUserRoleDTO) {
|
|
|
|
+
|
|
|
|
+ Long role_id = sysUserRoleDTO.getRole_id();
|
|
|
|
+
|
|
|
|
+ SysUserRole roleDetail = getUserRoleDetail(sysUserRoleDTO);
|
|
|
|
+ if (roleDetail == null) throw new CustException("系统用户角色不存在");
|
|
|
|
+
|
|
|
|
+ Integer user_count = roleDetail.getUser_count();
|
|
|
|
+ if (user_count > 0) throw new CustException("角色已关联 " + user_count + " 个用户,请解除关联后再删除");
|
|
|
|
+
|
|
|
|
+ // 删除角色
|
|
|
|
+ sysUserRoleDao.delete(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRole_id, role_id));
|
|
|
|
+ // 删除角色与模块关系
|
|
|
|
+ sysUserRoleModuleRelationDao.delete(new LambdaQueryWrapper<SysUserRoleModuleRelation>().eq(SysUserRoleModuleRelation::getRole_id, role_id));
|
|
|
|
+
|
|
|
|
+ return Map.of("role_id", sysUserRoleDTO.getRole_id());
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|