| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package com.etotem.cfc.controller.stats;
- import com.etotem.cfc.common.Result;
- import com.etotem.cfc.entity.User;
- import com.etotem.cfc.mapper.ChildMapper;
- import com.etotem.cfc.mapper.UserMapper;
- import com.etotem.cfc.service.TaskStatsService;
- import io.swagger.v3.oas.annotations.Operation;
- import io.swagger.v3.oas.annotations.tags.Tag;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Map;
- @Tag(name = "任务统计", description = "任务完成率分析")
- @RestController
- @RequestMapping("/api/stats")
- public class StatsController {
- @Resource
- private TaskStatsService taskStatsService;
- @Resource
- private UserMapper userMapper;
- @Resource
- private ChildMapper childMapper;
- @Operation(summary = "孩子任务完成率")
- @PostMapping("/child-completion")
- public Result<Map<String, Object>> childCompletion(
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, Object> params) {
- User user = userMapper.selectById(userId);
- if (user == null) return Result.error("用户不存在");
- Long childId = params.get("childId") != null ? Long.valueOf(params.get("childId").toString()) : null;
- Date startDate = parseDate(params.get("startDate"));
- Date endDate = parseDate(params.get("endDate"));
- Map<String, Object> stats = taskStatsService.getChildStats(childId, startDate, endDate);
- return Result.success(stats);
- }
- @Operation(summary = "家庭任务完成率")
- @PostMapping("/family-completion")
- public Result<Map<String, Object>> familyCompletion(
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, Object> params) {
- User user = userMapper.selectById(userId);
- if (user == null) return Result.error("用户不存在");
- if (user.getFamilyId() == null) return Result.error("未关联家庭");
- Date startDate = parseDate(params.get("startDate"));
- Date endDate = parseDate(params.get("endDate"));
- Map<String, Object> stats = taskStatsService.getFamilyStats(user.getFamilyId(), startDate, endDate);
- return Result.success(stats);
- }
- private Date parseDate(Object value) {
- if (value == null) return null;
- try {
- return new SimpleDateFormat("yyyy-MM-dd").parse(value.toString());
- } catch (ParseException e) {
- return null;
- }
- }
- }
|