StatsController.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.etotem.cfc.controller.stats;
  2. import com.etotem.cfc.common.Result;
  3. import com.etotem.cfc.entity.User;
  4. import com.etotem.cfc.mapper.ChildMapper;
  5. import com.etotem.cfc.mapper.UserMapper;
  6. import com.etotem.cfc.service.TaskStatsService;
  7. import io.swagger.v3.oas.annotations.Operation;
  8. import io.swagger.v3.oas.annotations.tags.Tag;
  9. import org.springframework.web.bind.annotation.*;
  10. import javax.annotation.Resource;
  11. import java.text.ParseException;
  12. import java.text.SimpleDateFormat;
  13. import java.util.Date;
  14. import java.util.Map;
  15. @Tag(name = "任务统计", description = "任务完成率分析")
  16. @RestController
  17. @RequestMapping("/api/stats")
  18. public class StatsController {
  19. @Resource
  20. private TaskStatsService taskStatsService;
  21. @Resource
  22. private UserMapper userMapper;
  23. @Resource
  24. private ChildMapper childMapper;
  25. @Operation(summary = "孩子任务完成率")
  26. @PostMapping("/child-completion")
  27. public Result<Map<String, Object>> childCompletion(
  28. @RequestAttribute("userId") Long userId,
  29. @RequestBody Map<String, Object> params) {
  30. User user = userMapper.selectById(userId);
  31. if (user == null) return Result.error("用户不存在");
  32. Long childId = params.get("childId") != null ? Long.valueOf(params.get("childId").toString()) : null;
  33. Date startDate = parseDate(params.get("startDate"));
  34. Date endDate = parseDate(params.get("endDate"));
  35. Map<String, Object> stats = taskStatsService.getChildStats(childId, startDate, endDate);
  36. return Result.success(stats);
  37. }
  38. @Operation(summary = "家庭任务完成率")
  39. @PostMapping("/family-completion")
  40. public Result<Map<String, Object>> familyCompletion(
  41. @RequestAttribute("userId") Long userId,
  42. @RequestBody Map<String, Object> params) {
  43. User user = userMapper.selectById(userId);
  44. if (user == null) return Result.error("用户不存在");
  45. if (user.getFamilyId() == null) return Result.error("未关联家庭");
  46. Date startDate = parseDate(params.get("startDate"));
  47. Date endDate = parseDate(params.get("endDate"));
  48. Map<String, Object> stats = taskStatsService.getFamilyStats(user.getFamilyId(), startDate, endDate);
  49. return Result.success(stats);
  50. }
  51. private Date parseDate(Object value) {
  52. if (value == null) return null;
  53. try {
  54. return new SimpleDateFormat("yyyy-MM-dd").parse(value.toString());
  55. } catch (ParseException e) {
  56. return null;
  57. }
  58. }
  59. }