AdminBadgeController.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package com.train.controller.admin;
  2. import com.train.common.Result;
  3. import com.train.service.BadgeService;
  4. import io.swagger.v3.oas.annotations.Operation;
  5. import io.swagger.v3.oas.annotations.tags.Tag;
  6. import org.springframework.web.bind.annotation.*;
  7. import javax.annotation.Resource;
  8. import java.util.Map;
  9. @Tag(name = "管理端-徽章", description = "徽章手动颁发")
  10. @RestController
  11. @RequestMapping("/api/admin/badge")
  12. public class AdminBadgeController {
  13. @Resource
  14. private BadgeService badgeService;
  15. @Operation(summary = "手动颁发徽章")
  16. @PostMapping("/grant")
  17. public Result<?> grant(@RequestBody Map<String, Object> body,
  18. @org.springframework.web.bind.annotation.RequestAttribute("adminId") Long adminId) {
  19. Object uidObj = body.get("uid");
  20. if (uidObj == null) {
  21. return Result.error("学员ID不能为空");
  22. }
  23. Long uid;
  24. try {
  25. uid = Long.valueOf(uidObj.toString());
  26. } catch (NumberFormatException e) {
  27. return Result.error("学员ID不合法");
  28. }
  29. String badgeKey = body.get("badgeKey") == null ? null : body.get("badgeKey").toString().trim();
  30. if (badgeKey == null || badgeKey.isEmpty()) {
  31. return Result.error("徽章键不能为空");
  32. }
  33. String remark = body.get("remark") == null ? null : body.get("remark").toString();
  34. boolean ok = badgeService.grantBadge(uid, badgeKey, adminId, remark);
  35. if (!ok) {
  36. return Result.error("徽章键不存在或用户不存在");
  37. }
  38. return Result.success();
  39. }
  40. }