|
|
@@ -0,0 +1,244 @@
|
|
|
+package com.etotem.cfc.controller.admin;
|
|
|
+
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.entity.SupplyHierarchy;
|
|
|
+import com.etotem.cfc.entity.SupplyHierarchyChangeRequest;
|
|
|
+import com.etotem.cfc.entity.SupplySystemMember;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.SupplySystemMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.etotem.cfc.service.SupplyHierarchyService;
|
|
|
+import com.etotem.cfc.service.SupplySystemService;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/admin/supply-hierarchy")
|
|
|
+public class SupplyHierarchyController {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SupplyHierarchyService supplyHierarchyService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SupplySystemService supplySystemService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SupplySystemMemberMapper supplySystemMemberMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ @PostMapping("/set")
|
|
|
+ public Result<Void> setHierarchy(@RequestBody Map<String, Object> params) {
|
|
|
+ try {
|
|
|
+ Long systemId = params.get("systemId") != null ? ((Number) params.get("systemId")).longValue() : null;
|
|
|
+ Long parentId = params.get("parentId") != null ? ((Number) params.get("parentId")).longValue() : null;
|
|
|
+ Long childId = params.get("childId") != null ? ((Number) params.get("childId")).longValue() : null;
|
|
|
+ if (systemId == null || parentId == null || childId == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ supplyHierarchyService.setHierarchy(systemId, parentId, childId);
|
|
|
+ return Result.success();
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return Result.error(e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return Result.error("设置失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/remove")
|
|
|
+ public Result<Void> removeRelation(@RequestBody Map<String, Object> params) {
|
|
|
+ try {
|
|
|
+ Long systemId = params.get("systemId") != null ? ((Number) params.get("systemId")).longValue() : null;
|
|
|
+ Long childId = params.get("childId") != null ? ((Number) params.get("childId")).longValue() : null;
|
|
|
+ if (systemId == null || childId == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ supplyHierarchyService.removeRelation(systemId, childId);
|
|
|
+ return Result.success();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return Result.error("移除失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/direct-children")
|
|
|
+ public Result<List<User>> getDirectChildren(@RequestBody Map<String, Object> params) {
|
|
|
+ Long systemId = params.get("systemId") != null ? ((Number) params.get("systemId")).longValue() : null;
|
|
|
+ Long parentId = params.get("parentId") != null ? ((Number) params.get("parentId")).longValue() : null;
|
|
|
+ if (systemId == null || parentId == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ List<User> children = supplyHierarchyService.getDirectChildren(systemId, parentId);
|
|
|
+ return Result.success(children);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/all-children")
|
|
|
+ public Result<List<User>> getAllChildren(@RequestBody Map<String, Object> params) {
|
|
|
+ Long systemId = params.get("systemId") != null ? ((Number) params.get("systemId")).longValue() : null;
|
|
|
+ Long parentId = params.get("parentId") != null ? ((Number) params.get("parentId")).longValue() : null;
|
|
|
+ if (systemId == null || parentId == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ List<User> children = supplyHierarchyService.getAllChildren(systemId, parentId);
|
|
|
+ return Result.success(children);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/parent")
|
|
|
+ public Result<User> getParent(@RequestBody Map<String, Object> params) {
|
|
|
+ Long systemId = params.get("systemId") != null ? ((Number) params.get("systemId")).longValue() : null;
|
|
|
+ Long userId = params.get("userId") != null ? ((Number) params.get("userId")).longValue() : null;
|
|
|
+ if (systemId == null || userId == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ User parent = supplyHierarchyService.getParent(systemId, userId);
|
|
|
+ return Result.success(parent);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/parent-chain")
|
|
|
+ public Result<List<User>> getParentChain(@RequestBody Map<String, Object> params) {
|
|
|
+ Long systemId = params.get("systemId") != null ? ((Number) params.get("systemId")).longValue() : null;
|
|
|
+ Long userId = params.get("userId") != null ? ((Number) params.get("userId")).longValue() : null;
|
|
|
+ if (systemId == null || userId == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ List<User> chain = supplyHierarchyService.getParentChain(systemId, userId);
|
|
|
+ return Result.success(chain);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/tree")
|
|
|
+ public Result<List<Map<String, Object>>> getTree(@RequestBody Map<String, Object> params) {
|
|
|
+ Long systemId = params.get("systemId") != null ? ((Number) params.get("systemId")).longValue() : null;
|
|
|
+ if (systemId == null) {
|
|
|
+ return Result.error("systemId 不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<SupplySystemMember> members = supplySystemService.memberList(systemId);
|
|
|
+ Map<Long, User> userMap = new HashMap<>();
|
|
|
+ for (SupplySystemMember m : members) {
|
|
|
+ User u = userMapper.selectById(m.getUserId());
|
|
|
+ if (u != null) {
|
|
|
+ userMap.put(u.getId(), u);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<SupplyHierarchy> allRelations = supplyHierarchyService.list(
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<SupplyHierarchy>()
|
|
|
+ .eq(SupplyHierarchy::getSystemId, systemId)
|
|
|
+ .eq(SupplyHierarchy::getStatus, "active"));
|
|
|
+
|
|
|
+ Map<Long, List<SupplyHierarchy>> childrenMap = new HashMap<>();
|
|
|
+ for (SupplyHierarchy r : allRelations) {
|
|
|
+ childrenMap.computeIfAbsent(r.getParentId(), k -> new ArrayList<>()).add(r);
|
|
|
+ }
|
|
|
+
|
|
|
+ SupplySystemMember admin = members.stream().filter(m -> "admin".equals(m.getRole())).findFirst().orElse(null);
|
|
|
+ if (admin == null) {
|
|
|
+ return Result.success(new ArrayList<>());
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Map<String, Object>> root = buildTree(admin.getUserId(), childrenMap, userMap, new HashSet<>());
|
|
|
+ return Result.success(root);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Map<String, Object>> buildTree(Long parentId, Map<Long, List<SupplyHierarchy>> childrenMap,
|
|
|
+ Map<Long, User> userMap, HashSet<Long> visited) {
|
|
|
+ List<Map<String, Object>> result = new ArrayList<>();
|
|
|
+ if (visited.contains(parentId)) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ visited.add(parentId);
|
|
|
+
|
|
|
+ List<SupplyHierarchy> children = childrenMap.get(parentId);
|
|
|
+ if (children == null || children.isEmpty()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (SupplyHierarchy h : children) {
|
|
|
+ User childUser = userMap.get(h.getChildId());
|
|
|
+ if (childUser == null) continue;
|
|
|
+
|
|
|
+ Map<String, Object> node = new HashMap<>();
|
|
|
+ node.put("userId", childUser.getId());
|
|
|
+ node.put("nickname", childUser.getNickname());
|
|
|
+ node.put("realName", childUser.getRealName());
|
|
|
+ node.put("phone", childUser.getPhone());
|
|
|
+ node.put("level", h.getLevel());
|
|
|
+ node.put("children", buildTree(h.getChildId(), childrenMap, userMap, visited));
|
|
|
+ result.add(node);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/my")
|
|
|
+ public Result<Map<String, Object>> getMyRelation(@RequestBody Map<String, Object> params) {
|
|
|
+ Long systemId = params.get("systemId") != null ? ((Number) params.get("systemId")).longValue() : null;
|
|
|
+ Long userId = params.get("userId") != null ? ((Number) params.get("userId")).longValue() : null;
|
|
|
+ if (systemId == null || userId == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("parent", supplyHierarchyService.getParent(systemId, userId));
|
|
|
+ result.put("directChildren", supplyHierarchyService.getDirectChildren(systemId, userId));
|
|
|
+ result.put("allChildren", supplyHierarchyService.getAllChildren(systemId, userId));
|
|
|
+ result.put("parentChain", supplyHierarchyService.getParentChain(systemId, userId));
|
|
|
+ return Result.success(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/change-requests")
|
|
|
+ public Result<List<SupplyHierarchyChangeRequest>> getChangeRequests(@RequestBody Map<String, Object> params) {
|
|
|
+ Long systemId = params.get("systemId") != null ? ((Number) params.get("systemId")).longValue() : null;
|
|
|
+ String status = (String) params.get("status");
|
|
|
+ if (systemId == null) {
|
|
|
+ return Result.error("systemId 不能为空");
|
|
|
+ }
|
|
|
+ List<SupplyHierarchyChangeRequest> requests = supplyHierarchyService.getChangeRequests(systemId, status);
|
|
|
+ return Result.success(requests);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/change/apply")
|
|
|
+ public Result<Void> applyChange(@RequestBody Map<String, Object> params) {
|
|
|
+ try {
|
|
|
+ Long systemId = params.get("systemId") != null ? ((Number) params.get("systemId")).longValue() : null;
|
|
|
+ Long targetParentId = params.get("targetParentId") != null ? ((Number) params.get("targetParentId")).longValue() : null;
|
|
|
+ String reason = (String) params.get("reason");
|
|
|
+ if (systemId == null || targetParentId == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ supplyHierarchyService.applyChange(systemId, targetParentId, targetParentId, reason);
|
|
|
+ return Result.success();
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return Result.error(e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return Result.error("申请失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/change/review")
|
|
|
+ public Result<Void> reviewChange(@RequestBody Map<String, Object> params) {
|
|
|
+ try {
|
|
|
+ Long requestId = params.get("requestId") != null ? ((Number) params.get("requestId")).longValue() : null;
|
|
|
+ String status = (String) params.get("status");
|
|
|
+ String rejectReason = (String) params.get("rejectReason");
|
|
|
+ if (requestId == null || status == null) {
|
|
|
+ return Result.error("参数不完整");
|
|
|
+ }
|
|
|
+ supplyHierarchyService.reviewChange(requestId, status, rejectReason, null);
|
|
|
+ return Result.success();
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return Result.error(e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return Result.error("审核失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|