AdminAttendanceController.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.train.controller.admin;
  2. import com.train.common.Result;
  3. import com.train.service.AttendanceService;
  4. import io.swagger.v3.oas.annotations.Operation;
  5. import io.swagger.v3.oas.annotations.tags.Tag;
  6. import org.springframework.web.bind.annotation.PostMapping;
  7. import org.springframework.web.bind.annotation.RequestAttribute;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import javax.annotation.Resource;
  12. import java.util.Map;
  13. /**
  14. * 管理端-培训签到 Controller(发码 / 轮换 / 待生效列表)。
  15. *
  16. * 权限(Service 层经 RoleGuard 校验 isAdmin):全部端点仅 admin 可访问。
  17. * - issueCode 幂等返回:同 session active 码未过期则复用
  18. * - rotateCode 强制轮换
  19. * - 学时字段一律「分钟」整数;code 有效期可配 app.attendance.code-valid-minutes(默认 5 分钟)
  20. */
  21. @Tag(name = "管理端-培训签到", description = "发码 / 轮换 / 待生效签到列表")
  22. @RestController
  23. @RequestMapping("/api/admin/attendance")
  24. public class AdminAttendanceController {
  25. @Resource
  26. private AttendanceService attendanceService;
  27. /**
  28. * 发码(幂等返回)
  29. * body: {sessionId, sessionHours?(分钟), category?(theory/practice/other)}
  30. */
  31. @Operation(summary = "发签到码(幂等)")
  32. @PostMapping("/issueCode")
  33. public Result<Map<String, Object>> issueCode(
  34. @RequestAttribute(value = "adminId", required = false) Long adminId,
  35. @RequestAttribute(value = "isAdmin", required = false) Boolean isAdmin,
  36. @RequestBody(required = false) Map<String, Object> body) {
  37. String sessionId = body == null || body.get("sessionId") == null ? null : body.get("sessionId").toString().trim();
  38. Integer hours = toInt(body == null ? null : body.get("sessionHours"));
  39. String category = body == null || body.get("category") == null ? null : body.get("category").toString().trim();
  40. String note = body == null || body.get("note") == null ? null : body.get("note").toString().trim();
  41. return attendanceService.issueCode(isAdmin, adminId, sessionId, hours, category, note);
  42. }
  43. /**
  44. * 轮换码
  45. * body: {sessionId, sessionHours?(分钟), category?(theory/practice/other)}
  46. */
  47. @Operation(summary = "轮换签到码")
  48. @PostMapping("/rotateCode")
  49. public Result<Map<String, Object>> rotateCode(
  50. @RequestAttribute(value = "adminId", required = false) Long adminId,
  51. @RequestAttribute(value = "isAdmin", required = false) Boolean isAdmin,
  52. @RequestBody(required = false) Map<String, Object> body) {
  53. String sessionId = body == null || body.get("sessionId") == null ? null : body.get("sessionId").toString().trim();
  54. Integer hours = toInt(body == null ? null : body.get("sessionHours"));
  55. String category = body == null || body.get("category") == null ? null : body.get("category").toString().trim();
  56. String note = body == null || body.get("note") == null ? null : body.get("note").toString().trim();
  57. return attendanceService.rotateCode(isAdmin, adminId, sessionId, hours, category, note);
  58. }
  59. /**
  60. * 待生效签到列表(真分页)
  61. * body: {cohortId?, page?, size?}
  62. */
  63. @Operation(summary = "待生效签到列表(分页)")
  64. @PostMapping("/pending")
  65. public Result<Map<String, Object>> pending(
  66. @RequestAttribute(value = "isAdmin", required = false) Boolean isAdmin,
  67. @RequestBody(required = false) Map<String, Object> body) {
  68. Long cohortId = toLong(body == null ? null : body.get("cohortId"));
  69. Integer page = toInt(body == null ? null : body.get("page"));
  70. Integer size = toInt(body == null ? null : body.get("size"));
  71. return attendanceService.pendingAttendance(isAdmin, cohortId, page, size);
  72. }
  73. private Long toLong(Object v) {
  74. if (v == null) {
  75. return null;
  76. }
  77. try {
  78. return Long.valueOf(v.toString().trim());
  79. } catch (NumberFormatException e) {
  80. return null;
  81. }
  82. }
  83. private Integer toInt(Object v) {
  84. if (v == null) {
  85. return null;
  86. }
  87. try {
  88. return Integer.valueOf(v.toString().trim());
  89. } catch (NumberFormatException e) {
  90. return null;
  91. }
  92. }
  93. }