refactor: 删除无用接口、修改变量命名

This commit is contained in:
shiziyuan9527 2021-12-09 18:24:05 +08:00 committed by shiziyuan9527
parent 291116755c
commit 65d60bc182
3 changed files with 46 additions and 55 deletions

View File

@ -110,15 +110,9 @@ public class UserController {
userService.updateCurrentUserByResourceId(resourceId); userService.updateCurrentUserByResourceId(resourceId);
} }
@PostMapping("/switch/source/org/{sourceId}")
public UserDTO switchOrganization(@PathVariable(value = "sourceId") String sourceId) {
userService.switchUserRole("organization", sourceId);
return SessionUtils.getUser();
}
@PostMapping("/switch/source/ws/{sourceId}") @PostMapping("/switch/source/ws/{sourceId}")
public UserDTO switchWorkspace(@PathVariable(value = "sourceId") String sourceId) { public UserDTO switchWorkspace(@PathVariable(value = "sourceId") String sourceId) {
userService.switchUserRole("workspace", sourceId); userService.switchUserResource("workspace", sourceId);
return SessionUtils.getUser(); return SessionUtils.getUser();
} }

View File

@ -24,18 +24,18 @@ public class UserGroupService {
userRoleExample.createCriteria().andUserIdEqualTo(userId); userRoleExample.createCriteria().andUserIdEqualTo(userId);
List<UserGroup> userGroups = userGroupMapper.selectByExample(userRoleExample); List<UserGroup> userGroups = userGroupMapper.selectByExample(userRoleExample);
List<String> collect = userGroups.stream() List<String> collect = userGroups.stream()
.map(userRole -> userRole.getGroupId()) .map(UserGroup::getGroupId)
.distinct() .distinct()
.collect(Collectors.toList()); .collect(Collectors.toList());
for (int i = 0; i < collect.size(); i++) { for (String id : collect) {
Map<String, Object> map = new HashMap<>(2); Map<String, Object> map = new HashMap<>(2);
map.put("id", collect.get(i)); map.put("id", id);
map.put("ids", new ArrayList<>()); map.put("ids", new ArrayList<>());
for (int j = 0; j < userGroups.size(); j++) { for (UserGroup userGroup : userGroups) {
String role = userGroups.get(j).getGroupId(); String groupId = userGroup.getGroupId();
if (StringUtils.equals(role, collect.get(i))) { if (StringUtils.equals(groupId, id)) {
List ids = (List) map.get("ids"); List ids = (List) map.get("ids");
ids.add(userGroups.get(j).getSourceId()); ids.add(userGroup.getSourceId());
} }
} }
list.add(map); list.add(map);

View File

@ -107,22 +107,20 @@ public class UserService {
return extUserMapper.queryName(); return extUserMapper.queryName();
} }
public UserDTO insert(UserRequest user) { public UserDTO insert(UserRequest userRequest) {
checkUserParam(user); checkUserParam(userRequest);
// String id = userRequest.getId();
String id = user.getId(); User user = userMapper.selectByPrimaryKey(id);
User user1 = userMapper.selectByPrimaryKey(id); if (user != null) {
if (user1 != null) {
MSException.throwException(Translator.get("user_id_already_exists")); MSException.throwException(Translator.get("user_id_already_exists"));
} else { } else {
createUser(user); createUser(userRequest);
} }
List<Map<String, Object>> groups = userRequest.getGroups();
List<Map<String, Object>> groups = user.getGroups();
if (!groups.isEmpty()) { if (!groups.isEmpty()) {
insertUserGroup(groups, user.getId()); insertUserGroup(groups, userRequest.getId());
} }
return getUserDTO(user.getId()); return getUserDTO(userRequest.getId());
} }
public void insertUserGroup(List<Map<String, Object>> groups, String userId) { public void insertUserGroup(List<Map<String, Object>> groups, String userId) {
@ -385,14 +383,13 @@ public class UserService {
} }
} }
public void switchUserRole(String sign, String sourceId) { public void switchUserResource(String sign, String sourceId) {
SessionUser sessionUser = SessionUtils.getUser(); SessionUser sessionUser = SessionUtils.getUser();
// 获取最新UserDTO // 获取最新UserDTO
UserDTO user = getUserDTO(sessionUser.getId()); UserDTO user = getUserDTO(sessionUser.getId());
User newUser = new User(); User newUser = new User();
if (StringUtils.equals("workspace", sign)) { if (StringUtils.equals("workspace", sign)) {
Workspace workspace = workspaceMapper.selectByPrimaryKey(sourceId);
user.setLastWorkspaceId(sourceId); user.setLastWorkspaceId(sourceId);
List<Project> projects = getProjectListByWsAndUserId(sourceId); List<Project> projects = getProjectListByWsAndUserId(sourceId);
if (projects.size() > 0) { if (projects.size() > 0) {
@ -417,15 +414,13 @@ public class UserService {
userGroupExample.createCriteria().andUserIdEqualTo(useId); userGroupExample.createCriteria().andUserIdEqualTo(useId);
List<UserGroup> userGroups = userGroupMapper.selectByExample(userGroupExample); List<UserGroup> userGroups = userGroupMapper.selectByExample(userGroupExample);
List<Project> projectList = new ArrayList<>(); List<Project> projectList = new ArrayList<>();
userGroups.forEach(userGroup -> { userGroups.forEach(userGroup -> projects.forEach(project -> {
projects.forEach(project -> { if (StringUtils.equals(userGroup.getSourceId(), project.getId())) {
if (StringUtils.equals(userGroup.getSourceId(), project.getId())) { if (!projectList.contains(project)) {
if (!projectList.contains(project)) { projectList.add(project);
projectList.add(project);
}
} }
}); }
}); }));
return projectList; return projectList;
} }
@ -632,7 +627,7 @@ public class UserService {
.collect(Collectors.toList()); .collect(Collectors.toList());
if (workspaces.size() > 0) { if (workspaces.size() > 0) {
String wsId = workspaces.get(0).getSourceId(); String wsId = workspaces.get(0).getSourceId();
switchUserRole("workspace", wsId); switchUserResource("workspace", wsId);
} }
} else { } else {
UserGroup userGroup = project.stream().filter(p -> StringUtils.isNotBlank(p.getSourceId())) UserGroup userGroup = project.stream().filter(p -> StringUtils.isNotBlank(p.getSourceId()))
@ -952,7 +947,6 @@ public class UserService {
if (CollectionUtils.isEmpty(userGroups)) { if (CollectionUtils.isEmpty(userGroups)) {
return userGroupPermissionDTO; return userGroupPermissionDTO;
} }
// 设置 user_role
userGroupPermissionDTO.setUserGroups(userGroups); userGroupPermissionDTO.setUserGroups(userGroups);
List<String> groupId = userGroups.stream().map(UserGroup::getGroupId).collect(Collectors.toList()); List<String> groupId = userGroups.stream().map(UserGroup::getGroupId).collect(Collectors.toList());
@ -1088,24 +1082,27 @@ public class UserService {
} }
public void addProjectMember(AddMemberRequest request) { public void addProjectMember(AddMemberRequest request) {
if (!CollectionUtils.isEmpty(request.getUserIds())) { if (CollectionUtils.isEmpty(request.getUserIds())) {
for (String userId : request.getUserIds()) { LogUtil.info("add project member warning, request param user id list empty!");
UserGroupExample userGroupExample = new UserGroupExample(); return;
userGroupExample.createCriteria().andUserIdEqualTo(userId).andSourceIdEqualTo(request.getProjectId()); }
List<UserGroup> userGroups = userGroupMapper.selectByExample(userGroupExample);
if (userGroups.size() > 0) { for (String userId : request.getUserIds()) {
MSException.throwException(Translator.get("user_already_exists")); UserGroupExample userGroupExample = new UserGroupExample();
} else { userGroupExample.createCriteria().andUserIdEqualTo(userId).andSourceIdEqualTo(request.getProjectId());
for (String groupId : request.getGroupIds()) { List<UserGroup> userGroups = userGroupMapper.selectByExample(userGroupExample);
UserGroup userGroup = new UserGroup(); if (userGroups.size() > 0) {
userGroup.setGroupId(groupId); MSException.throwException(Translator.get("user_already_exists"));
userGroup.setSourceId(request.getProjectId()); } else {
userGroup.setUserId(userId); for (String groupId : request.getGroupIds()) {
userGroup.setId(UUID.randomUUID().toString()); UserGroup userGroup = new UserGroup();
userGroup.setUpdateTime(System.currentTimeMillis()); userGroup.setGroupId(groupId);
userGroup.setCreateTime(System.currentTimeMillis()); userGroup.setSourceId(request.getProjectId());
userGroupMapper.insertSelective(userGroup); userGroup.setUserId(userId);
} userGroup.setId(UUID.randomUUID().toString());
userGroup.setUpdateTime(System.currentTimeMillis());
userGroup.setCreateTime(System.currentTimeMillis());
userGroupMapper.insertSelective(userGroup);
} }
} }
} }