|
@@ -6,11 +6,19 @@ import java.util.stream.Collectors;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.gihon.common.entity.GihonButton;
|
|
|
import com.gihon.common.entity.GihonRole;
|
|
|
-import com.gihon.common.entity.GihonUserRole;
|
|
|
-import com.gihon.sso.mapper.GihonRoleMapper;
|
|
|
+import com.gihon.common.entity.GihonRoleButton;
|
|
|
+import com.gihon.common.entity.vo.RoleButtonReq;
|
|
|
+import com.gihon.common.entity.vo.RoleListReq;
|
|
|
+import com.gihon.common.exception.BusinessException;
|
|
|
+import com.gihon.common.mapper.GihonRoleMapper;
|
|
|
|
|
|
/**
|
|
|
* 直接继承没有多实现处理
|
|
@@ -24,17 +32,128 @@ public class GihonRoleService extends ServiceImpl<GihonRoleMapper, GihonRole> {
|
|
|
@Autowired
|
|
|
private GihonUserRoleService gihonUserRoleService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private GihonRoleButtonService gihonRoleButtonService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GihonButtonService gihonButtonService;
|
|
|
+ @Autowired
|
|
|
+ private GihonMenuService gihonMenuService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户的角色列表
|
|
|
+ */
|
|
|
public List<GihonRole> getRoleList(Long userId) {
|
|
|
- List<GihonUserRole> roleList = gihonUserRoleService.roleList(userId);
|
|
|
+ List<Long> roleList = gihonUserRoleService.roleListByUserId(userId);
|
|
|
if (roleList.isEmpty()) {
|
|
|
return new ArrayList<>(0);
|
|
|
}
|
|
|
- return this.lambdaQuery()
|
|
|
- .in(GihonRole::getId, roleList.stream().map(ur -> ur.getRoleId()).collect(Collectors.toList())).list();
|
|
|
+ return this.getRoleListByIds(roleList);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按照角色IDS获取角色列表
|
|
|
+ */
|
|
|
public List<GihonRole> getRoleListByIds(List<Long> ids) {
|
|
|
return this.lambdaQuery().in(GihonRole::getId, ids).list();
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 前端分页获取角色列表
|
|
|
+ */
|
|
|
+ public IPage<GihonRole> getRolelistPage(RoleListReq companyListReq) {
|
|
|
+ Page<GihonRole> page = new Page<GihonRole>(companyListReq.getPageIndex(), companyListReq.getPageSize());
|
|
|
+ return this.lambdaQuery()
|
|
|
+ .like(StringUtils.hasText(companyListReq.getRoleName()), GihonRole::getRoleName,
|
|
|
+ companyListReq.getRoleName())
|
|
|
+ .like(StringUtils.hasText(companyListReq.getRoleCode()), GihonRole::getRoleCode,
|
|
|
+ companyListReq.getRoleCode())
|
|
|
+ .page(page);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 新增角色
|
|
|
+ */
|
|
|
+ public boolean saveRole(GihonRole role) {
|
|
|
+ int count = this.lambdaQuery().eq(GihonRole::getRoleName, role.getRoleName()).or()
|
|
|
+ .eq(GihonRole::getRoleCode, role.getRoleCode()).count();
|
|
|
+ if (count > 0) {
|
|
|
+ throw new BusinessException("角色名称或编码重复");
|
|
|
+ }
|
|
|
+ return this.save(role);
|
|
|
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 更新角色
|
|
|
+ */
|
|
|
+ public boolean updateRole(GihonRole role) {
|
|
|
+ GihonRole roleOld = this.getById(role.getId());
|
|
|
+ if (roleOld == null) {
|
|
|
+ throw new BusinessException("角色不存在");
|
|
|
+ }
|
|
|
+ int count = this.lambdaQuery().ne(GihonRole::getId, role.getId()).and(p -> {
|
|
|
+ p.eq(GihonRole::getRoleName, role.getRoleName()).or().eq(GihonRole::getRoleCode, role.getRoleCode());
|
|
|
+ }).count();
|
|
|
+
|
|
|
+ if (count > 0) {
|
|
|
+ throw new BusinessException("角色名称或编码重复");
|
|
|
+ }
|
|
|
+ GihonRole roleUpdate = new GihonRole();
|
|
|
+ roleUpdate.setId(role.getId());
|
|
|
+ roleUpdate.setRoleName(role.getRoleName());
|
|
|
+ roleUpdate.setRoleCode(role.getRoleCode());
|
|
|
+ roleUpdate.setRoleDescription(role.getRoleDescription());
|
|
|
+ return this.updateById(roleUpdate);
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 移除一个角色
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public boolean removeRole(GihonRole role) {
|
|
|
+ GihonRole roleOld = this.getById(role.getId());
|
|
|
+ if (roleOld == null) {
|
|
|
+ throw new BusinessException("角色不存在");
|
|
|
+ }
|
|
|
+ // 异步通知
|
|
|
+ gihonRoleButtonService.removeButtonByRoleId(role.getId(),null);
|
|
|
+ gihonUserRoleService.removeRoleByUserId(role.getId());
|
|
|
+ return this.removeById(role.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取在某个模块下的按钮权限
|
|
|
+ */
|
|
|
+ public List<GihonButton> getButtonByRoleModule(Long roleId, Long moduleId) {
|
|
|
+ List<Long> menuIdList = gihonMenuService.getAllMenu(moduleId).stream().map(m -> m.getId())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ List<GihonButton> btnList = gihonButtonService.getMenuButton(menuIdList);
|
|
|
+ List<Long> btnCheckedList = gihonRoleButtonService.getButtonListByRole(roleId);
|
|
|
+ btnList.forEach(b -> b.setChecked(btnCheckedList.contains(b.getId())));
|
|
|
+ return btnList;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 设置角色在某个模块下的按钮权限
|
|
|
+ */
|
|
|
+ public boolean setButtonByRoleModule(RoleButtonReq req) {
|
|
|
+ List<Long> menuIdList = gihonMenuService.getAllMenu(req.getModuleId()).stream().map(m -> m.getId())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ List<Long> allbtnList = gihonButtonService.getMenuButton(menuIdList).stream().map(m -> m.getId())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ List<Long> btnoldList = gihonRoleButtonService.getButtonListByRole(req.getRoleId());
|
|
|
+ btnoldList.retainAll(allbtnList);//求交集,属于当前模块的角色配置
|
|
|
+ req.getButtonList().retainAll(allbtnList);
|
|
|
+ if(req.getButtonList().isEmpty()) {
|
|
|
+ return gihonRoleButtonService.removeButtonByRoleId(req.getRoleId(),btnoldList);
|
|
|
+ }else if(btnoldList.isEmpty()) {
|
|
|
+ return gihonRoleButtonService.insertRoleButton(req.getRoleId(), req.getButtonList());
|
|
|
+ }else {
|
|
|
+ List<Long> tempList = new ArrayList<>();
|
|
|
+ tempList.addAll(req.getButtonList());
|
|
|
+ tempList.removeAll(btnoldList);//新增
|
|
|
+ btnoldList.removeAll(req.getButtonList() );//需要删除的
|
|
|
+ gihonRoleButtonService.insertRoleButton(req.getRoleId(), tempList);
|
|
|
+ gihonRoleButtonService.removeButtonByRoleId(req.getRoleId(),btnoldList);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|