|
|
@@ -0,0 +1,186 @@
|
|
|
+package com.zhongshu.card.server.core.service.org;
|
|
|
+
|
|
|
+import com.github.microservice.auth.client.content.ResultState;
|
|
|
+import com.github.microservice.auth.client.model.RoleGroupModel;
|
|
|
+import com.github.microservice.components.data.base.util.PageEntityUtil;
|
|
|
+import com.zhongshu.card.client.model.org.*;
|
|
|
+import com.zhongshu.card.client.ret.ResultContent;
|
|
|
+import com.zhongshu.card.client.ret.ResultMessage;
|
|
|
+import com.zhongshu.card.client.utils.type.RoleType;
|
|
|
+import com.zhongshu.card.server.core.dao.org.RoleDao;
|
|
|
+import com.zhongshu.card.server.core.dataConfig.CardDefault;
|
|
|
+import com.zhongshu.card.server.core.domain.org.Organization;
|
|
|
+import com.zhongshu.card.server.core.domain.org.Role;
|
|
|
+import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
+import com.zhongshu.card.server.core.util.BeanUtils;
|
|
|
+import com.zhongshu.card.server.core.util.DateUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author TRX
|
|
|
+ * @date 2024/6/5
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class RoleService extends SuperService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RoleDao roleDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ com.github.microservice.auth.client.service.RoleService roleService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加/编辑角色
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent addRole(RoleAddParam param) {
|
|
|
+ Assert.hasText(param.getName(), "name不能为空");
|
|
|
+ Assert.hasText(param.getCode(), "code不能为空");
|
|
|
+ String oid = param.getOid();
|
|
|
+ if (StringUtils.isEmpty(oid)) {
|
|
|
+ oid = getCurrentOid();
|
|
|
+ }
|
|
|
+ param.setOid(oid);
|
|
|
+ if (param.getSort() == null) {
|
|
|
+ param.setSort(1L);
|
|
|
+ }
|
|
|
+ Role nameRole = roleDao.findTopByNameAndOid(param.getName(), oid);
|
|
|
+ Role codeRole = roleDao.findTopByCodeAndOid(param.getCode(), oid);
|
|
|
+ if (StringUtils.isEmpty(param.getId())) {
|
|
|
+ param.setId(null);
|
|
|
+ // 添加
|
|
|
+ if (ObjectUtils.isNotEmpty(nameRole)) {
|
|
|
+ return ResultContent.buildFail(String.format("角色名称已存在:%s", param.getName()));
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isNotEmpty(codeRole)) {
|
|
|
+ return ResultContent.buildFail(String.format("角色名称已存在:%s", param.getCode()));
|
|
|
+ }
|
|
|
+
|
|
|
+ RoleGroupModel admin = new RoleGroupModel();
|
|
|
+ admin.setName(param.getName());
|
|
|
+ admin.setRemark("");
|
|
|
+ admin.setOrganizationId(oid);
|
|
|
+ com.github.microservice.auth.client.content.ResultContent<String> updateRoleGroupAdmin = roleService.updateRoleGroup(admin);
|
|
|
+ String groupId = updateRoleGroupAdmin.getContent();
|
|
|
+
|
|
|
+ com.github.microservice.auth.client.model.RoleModel roleModel = new com.github.microservice.auth.client.model.RoleModel();
|
|
|
+ roleModel.setOrganizationId(oid);
|
|
|
+ roleModel.setName(param.getName());
|
|
|
+ roleModel.setRemark("");
|
|
|
+ // 添加角色
|
|
|
+ com.github.microservice.auth.client.content.ResultContent<String> updateRole = roleService.updateRole(roleModel);
|
|
|
+ String roleId = updateRole.getContent();
|
|
|
+ // 把角色绑定到角色组
|
|
|
+ roleService.addRoleToRoleGroup(groupId, new String[]{updateRole.getContent()});
|
|
|
+
|
|
|
+ Role role = new Role();
|
|
|
+ BeanUtils.copyProperties(param, role);
|
|
|
+ role.setRoleGroupId(groupId);
|
|
|
+ role.setRoleType(RoleType.Custom);
|
|
|
+ role.setRoleId(roleId);
|
|
|
+ role.setCreateUserId(getCurrentUserId());
|
|
|
+ role.setIsAdmin(Boolean.FALSE);
|
|
|
+ roleDao.save(role);
|
|
|
+ } else {
|
|
|
+ // 编辑
|
|
|
+ Role role = roleDao.findTopById(param.getId());
|
|
|
+ if (ObjectUtils.isEmpty(role)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isNotEmpty(nameRole) && !nameRole.getId().equals(role.getId())) {
|
|
|
+ return ResultContent.buildFail(String.format("角色名称已存在:%s", param.getName()));
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isNotEmpty(codeRole) && !codeRole.getId().equals(role.getId())) {
|
|
|
+ return ResultContent.buildFail(String.format("角色编码已存在:%s", param.getCode()));
|
|
|
+ }
|
|
|
+ com.github.microservice.auth.client.content.ResultContent<RoleGroupModel> resultContent = roleService.getRoleGroup(role.getRoleGroupId());
|
|
|
+ if (resultContent.getState() == ResultState.Success) {
|
|
|
+ RoleGroupModel roleGroupModel = resultContent.getContent();
|
|
|
+ roleGroupModel.setName(param.getName());
|
|
|
+ roleService.updateRoleGroup(roleGroupModel);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(role.getRoleId())) {
|
|
|
+ com.github.microservice.auth.client.content.ResultContent<com.github.microservice.auth.client.model.RoleModel> roleModelResultContent = roleService.getRole(role.getRoleId());
|
|
|
+ if (roleModelResultContent.getState() == ResultState.Success) {
|
|
|
+ com.github.microservice.auth.client.model.RoleModel roleModel = roleModelResultContent.getContent();
|
|
|
+ roleModel.setName(param.getName());
|
|
|
+ roleService.updateRole(roleModel);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 编辑本地角色
|
|
|
+ BeanUtils.copyProperties(param, role);
|
|
|
+ roleDao.save(role);
|
|
|
+ }
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除角色
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent deleteRole(String id) {
|
|
|
+ Role role = roleDao.findTopById(id);
|
|
|
+ if (ObjectUtils.isEmpty(role)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
|
|
|
+ }
|
|
|
+ if (role.getIsAdmin() != null && role.getIsAdmin()) {
|
|
|
+ return ResultContent.buildFail(String.format("管理员角色不能删除"));
|
|
|
+ }
|
|
|
+ // 删除本地角色
|
|
|
+ roleDao.delete(role);
|
|
|
+ // 删除权限中心角色
|
|
|
+ roleService.removeRole(role.getRoleId());
|
|
|
+ roleService.removeRoleGroup(role.getRoleGroupId());
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 角色详情
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent<RoleModel> getRole(String id) {
|
|
|
+ Role role = roleDao.findTopById(id);
|
|
|
+ if (ObjectUtils.isEmpty(role)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
|
|
|
+ }
|
|
|
+ RoleModel roleModel = toModel(role);
|
|
|
+ return ResultContent.buildSuccess(roleModel);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 角色列表
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @param pageable
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent<Page<RoleModel>> page(RoleSearchParam param, Pageable pageable) {
|
|
|
+ Page<Role> page = roleDao.page(pageable, param);
|
|
|
+ return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
|
|
|
+ }
|
|
|
+
|
|
|
+ public RoleModel toModel(Role role) {
|
|
|
+ RoleModel roleModel = new RoleModel();
|
|
|
+ if (ObjectUtils.isNotEmpty(role)) {
|
|
|
+ BeanUtils.copyProperties(role, roleModel);
|
|
|
+ }
|
|
|
+ return roleModel;
|
|
|
+ }
|
|
|
+}
|