| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package com.train.controller;
- import com.train.common.Result;
- import com.train.service.AttendanceService;
- import io.swagger.v3.oas.annotations.Operation;
- import io.swagger.v3.oas.annotations.tags.Tag;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestAttribute;
- 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.List;
- import java.util.Map;
- /**
- * 培训签到 Controller(学员侧)
- *
- * - scan ★:反扫签到码,幂等(同场次重复扫码不重复入账)
- * - 统一 @PostMapping;必须本人(@RequestAttribute("userId"))
- */
- @Tag(name = "培训签到", description = "扫码签到(幂等)/ 我的场次 / 我的签到记录")
- @RestController
- @RequestMapping("/api/attendance")
- public class AttendanceController {
- @Resource
- private AttendanceService attendanceService;
- /**
- * 反扫签到
- * body: {code}
- * 出参: {attendanceId, cohortId, sessionId, hours(分钟), category, status, attendedAt}
- */
- @Operation(summary = "扫码签到(幂等)")
- @PostMapping("/scan")
- public Result<Map<String, Object>> scan(
- @RequestAttribute("userId") Long userId,
- @RequestBody(required = false) Map<String, Object> body) {
- String code = body == null || body.get("code") == null ? null : body.get("code").toString().trim();
- return attendanceService.scan(userId, code);
- }
- /**
- * 我的场次列表(按 session 归并,含学时快照)。
- */
- @Operation(summary = "我的场次列表")
- @PostMapping("/mySessions")
- public Result<List<Map<String, Object>>> mySessions(@RequestAttribute("userId") Long userId) {
- return attendanceService.mySessions(userId);
- }
- /**
- * 我的签到记录(可按批次过滤)
- * body: {cohortId?}
- */
- @Operation(summary = "我的签到记录")
- @PostMapping("/myAttendance")
- public Result<List<Map<String, Object>>> myAttendance(
- @RequestAttribute("userId") Long userId,
- @RequestBody(required = false) Map<String, Object> body) {
- Long cohortId = body == null || body.get("cohortId") == null ? null : toLong(body.get("cohortId"));
- return attendanceService.myAttendance(userId, cohortId);
- }
- /**
- * 发签到码(讲师/机构组织者侧;★幂等返回)
- * body: {sessionId, sessionHours?(分钟), category?(theory/practice/other), note?}
- * 权限(Service 内三重校验):isAdmin | 任一 active 机构管理者 | 该批次主讲讲师本人
- */
- @Operation(summary = "发签到码(讲师/组织者)")
- @PostMapping("/session/code")
- public Result<Map<String, Object>> sessionCode(
- @RequestAttribute(value = "userId", required = false) Long userId,
- @RequestAttribute(value = "adminId", required = false) Long adminId,
- @RequestAttribute(value = "isAdmin", required = false) Boolean isAdmin,
- @RequestBody(required = false) Map<String, Object> body) {
- String sessionId = body == null || body.get("sessionId") == null ? null : body.get("sessionId").toString().trim();
- Integer hours = toInt(body == null ? null : body.get("sessionHours"));
- String category = body == null || body.get("category") == null ? null : body.get("category").toString().trim();
- String note = body == null || body.get("note") == null ? null : body.get("note").toString().trim();
- // user token 注入 userId;admin token 注入 adminId;admin 直接放行
- Long uid = userId != null ? userId : adminId;
- return attendanceService.issueCode(isAdmin, uid, sessionId, hours, category, note);
- }
- /**
- * 轮换签到码(讲师/机构组织者侧)
- * body: {sessionId, sessionHours?(分钟), category?(theory/practice/other), note?}
- */
- @Operation(summary = "轮换签到码(讲师/组织者)")
- @PostMapping("/session/rotate")
- public Result<Map<String, Object>> sessionRotate(
- @RequestAttribute(value = "userId", required = false) Long userId,
- @RequestAttribute(value = "adminId", required = false) Long adminId,
- @RequestAttribute(value = "isAdmin", required = false) Boolean isAdmin,
- @RequestBody(required = false) Map<String, Object> body) {
- String sessionId = body == null || body.get("sessionId") == null ? null : body.get("sessionId").toString().trim();
- Integer hours = toInt(body == null ? null : body.get("sessionHours"));
- String category = body == null || body.get("category") == null ? null : body.get("category").toString().trim();
- String note = body == null || body.get("note") == null ? null : body.get("note").toString().trim();
- Long uid = userId != null ? userId : adminId;
- return attendanceService.rotateCode(isAdmin, uid, sessionId, hours, category, note);
- }
- private Long toLong(Object v) {
- if (v == null) {
- return null;
- }
- try {
- return Long.valueOf(v.toString().trim());
- } catch (NumberFormatException e) {
- return null;
- }
- }
- private Integer toInt(Object v) {
- if (v == null) {
- return null;
- }
- try {
- return Integer.valueOf(v.toString().trim());
- } catch (NumberFormatException e) {
- return null;
- }
- }
- }
|