소스 검색

feat: 管理端分配管理与停接开关接口

iwt 4 주 전
부모
커밋
0578c533c7

+ 37 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/admin/AdminButlerController.java

@@ -169,4 +169,41 @@ public class AdminButlerController {
         data.put("size", size);
         return Result.success(data);
     }
+
+    @PostMapping("/assignments")
+    public Result<Map<String, Object>> assignments(@RequestBody Map<String, Object> params,
+                                                    @RequestAttribute("role") String role) {
+        if (!"admin".equals(role) && !"finance".equals(role)) {
+            return Result.error("权限不足");
+        }
+        Integer pageNum = params.get("pageNum") != null ? Integer.valueOf(params.get("pageNum").toString()) : 1;
+        Integer pageSize = params.get("pageSize") != null ? Integer.valueOf(params.get("pageSize").toString()) : 10;
+        return butlerService.listAssignments(pageNum, pageSize);
+    }
+
+    @PostMapping("/unassign")
+    public Result<Void> unassign(@RequestBody Map<String, Object> params,
+                                  @RequestAttribute("role") String role) {
+        if (!"admin".equals(role) && !"finance".equals(role)) {
+            return Result.error("权限不足");
+        }
+        if (params.get("assignmentId") == null) {
+            return Result.error("缺少 assignmentId");
+        }
+        return butlerService.unassign(Long.valueOf(params.get("assignmentId").toString()));
+    }
+
+    @PostMapping("/toggle-accepting")
+    public Result<Void> toggleAccepting(@RequestBody Map<String, Object> params,
+                                         @RequestAttribute("role") String role) {
+        if (!"admin".equals(role) && !"finance".equals(role)) {
+            return Result.error("权限不足");
+        }
+        if (params.get("butlerUserId") == null || params.get("accepting") == null) {
+            return Result.error("缺少参数");
+        }
+        return butlerService.toggleAccepting(
+                Long.valueOf(params.get("butlerUserId").toString()),
+                Integer.valueOf(params.get("accepting").toString()));
+    }
 }

+ 71 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/ButlerService.java

@@ -494,4 +494,75 @@ public class ButlerService {
         summary.put("description", bp != null ? bp.getDescription() : "");
         return summary;
     }
+
+    // ==================== 管理端分配管理 ====================
+
+    /**
+     * 分配关系分页列表
+     */
+    public Result<Map<String, Object>> listAssignments(Integer pageNum, Integer pageSize) {
+        int pn = pageNum == null || pageNum < 1 ? 1 : pageNum;
+        int ps = pageSize == null || pageSize < 1 ? 10 : Math.min(pageSize, 100);
+        Page<ButlerAssignment> page = butlerAssignmentMapper.selectPage(new Page<>(pn, ps),
+                new LambdaQueryWrapper<ButlerAssignment>()
+                        .orderByDesc(ButlerAssignment::getAssignedAt));
+
+        List<Map<String, Object>> rows = new java.util.ArrayList<>();
+        for (ButlerAssignment a : page.getRecords()) {
+            Map<String, Object> row = new HashMap<>();
+            row.put("id", a.getId());
+            row.put("familyId", a.getFamilyId());
+            row.put("butlerUserId", a.getButlerUserId());
+            row.put("level", a.getLevel());
+            row.put("assignedAt", a.getAssignedAt());
+            User bu = userMapper.selectById(a.getButlerUserId());
+            row.put("butlerNickname", bu != null ? bu.getNickname() : "");
+            rows.add(row);
+        }
+        Map<String, Object> data = new HashMap<>();
+        data.put("total", page.getTotal());
+        data.put("records", rows);
+        return Result.success(data);
+    }
+
+    /**
+     * 强制解绑(物理删除分配行 + 回退管家计数)
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public Result<Void> unassign(Long assignmentId) {
+        ButlerAssignment assignment = butlerAssignmentMapper.selectById(assignmentId);
+        if (assignment == null) {
+            return Result.error("分配记录不存在");
+        }
+        butlerAssignmentMapper.deleteById(assignmentId);
+
+        LambdaUpdateWrapper<ButlerProfile> decr = new LambdaUpdateWrapper<>();
+        decr.eq(ButlerProfile::getUserId, assignment.getButlerUserId())
+            .setSql("member_count = GREATEST(member_count - 1, 0)");
+        butlerProfileMapper.update(null, decr);
+
+        log.info("Admin unassigned assignmentId={}, family={}, butler={}",
+                assignmentId, assignment.getFamilyId(), assignment.getButlerUserId());
+        return Result.success(null);
+    }
+
+    /**
+     * 管家停接/恢复接单开关(不影响存量绑定)
+     */
+    public Result<Void> toggleAccepting(Long butlerUserId, Integer accepting) {
+        if (accepting == null || (accepting != 0 && accepting != 1)) {
+            return Result.error("accepting 取值只能为 0 或 1");
+        }
+        ButlerProfile profile = butlerProfileMapper.selectOne(
+                new LambdaQueryWrapper<ButlerProfile>()
+                        .eq(ButlerProfile::getUserId, butlerUserId)
+                        .last("LIMIT 1"));
+        if (profile == null) {
+            return Result.error("管家不存在");
+        }
+        profile.setAccepting(accepting);
+        profile.setUpdatedAt(new Date());
+        butlerProfileMapper.updateById(profile);
+        return Result.success(null);
+    }
 }