package com.emotion.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.emotion.common.PageResult; import com.emotion.common.Result; import com.emotion.dto.request.PageRequest; import com.emotion.dto.response.BaseResponse; import com.emotion.entity.UserStats; import com.emotion.service.UserStatsService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.stream.Collectors; /** * 用户统计控制器 * * @author emotion-museum * @date 2025-07-23 */ @RestController @RequestMapping("/user-stats") public class UserStatsController { @Autowired private UserStatsService userStatsService; private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); /** * 分页查询用户统计 */ @GetMapping("/page") public Result> getPage(@Validated PageRequest request) { IPage page = userStatsService.getPage(request); List responses = page.getRecords().stream() .map(this::convertToResponse) .collect(Collectors.toList()); PageResult pageResult = new PageResult<>(); pageResult.setCurrent(page.getCurrent()); pageResult.setSize(page.getSize()); pageResult.setTotal(page.getTotal()); pageResult.setPages(page.getPages()); pageResult.setRecords(responses); return Result.success(pageResult); } /** * 根据用户ID获取统计信息 */ @GetMapping("/user/{userId}") public Result getByUserId(@PathVariable String userId) { UserStats stats = userStatsService.getByUserId(userId); if (stats == null) { return Result.notFound("用户统计不存在"); } return Result.success(convertToResponse(stats)); } /** * 根据用户ID和统计类型获取统计信息 */ @GetMapping("/user/{userId}/type/{statsType}") public Result getByUserIdAndStatsType(@PathVariable String userId, @PathVariable String statsType) { UserStats stats = userStatsService.getByUserIdAndStatsType(userId, statsType); if (stats == null) { return Result.notFound("用户统计不存在"); } return Result.success(convertToResponse(stats)); } /** * 根据统计类型查询统计信息 */ @GetMapping("/type/{statsType}") public Result> getByStatsType(@PathVariable String statsType) { List statsList = userStatsService.getByStatsType(statsType); List responses = statsList.stream() .map(this::convertToResponse) .collect(Collectors.toList()); return Result.success(responses); } /** * 查询用户的所有统计类型 */ @GetMapping("/user/{userId}/all") public Result> getAllStatsByUserId(@PathVariable String userId) { List statsList = userStatsService.getAllStatsByUserId(userId); List responses = statsList.stream() .map(this::convertToResponse) .collect(Collectors.toList()); return Result.success(responses); } /** * 查询排名前N的用户统计 */ @GetMapping("/type/{statsType}/top") public Result> getTopUsersByStatsType(@PathVariable String statsType, @RequestParam(defaultValue = "10") Integer limit) { List statsList = userStatsService.getTopUsersByStatsType(statsType, limit); List responses = statsList.stream() .map(this::convertToResponse) .collect(Collectors.toList()); return Result.success(responses); } /** * 查询用户在指定统计类型中的排名 */ @GetMapping("/user/{userId}/type/{statsType}/rank") public Result getUserRankByStatsType(@PathVariable String userId, @PathVariable String statsType) { Long rank = userStatsService.getUserRankByStatsType(userId, statsType); return Result.success(rank); } /** * 创建或更新用户统计 */ @PostMapping public Result createOrUpdate(@RequestBody @Validated UserStatsCreateRequest request) { UserStats stats = userStatsService.createOrUpdateUserStats( request.getUserId(), request.getStatsType(), request.getValue(), request.getPeriod() ); return Result.success(convertToResponse(stats)); } /** * 更新用户统计值 */ @PutMapping("/user/{userId}/type/{statsType}") public Result updateStatsValue(@PathVariable String userId, @PathVariable String statsType, @RequestParam Double value) { boolean updated = userStatsService.updateStatsValue(userId, statsType, value); if (!updated) { return Result.error("更新失败"); } return Result.success(); } /** * 增加用户统计值 */ @PutMapping("/user/{userId}/type/{statsType}/increment") public Result incrementStatsValue(@PathVariable String userId, @PathVariable String statsType, @RequestParam Double increment) { boolean updated = userStatsService.incrementStatsValue(userId, statsType, increment); if (!updated) { return Result.error("增加失败"); } return Result.success(); } /** * 重新计算用户统计 */ @PutMapping("/user/{userId}/recalculate") public Result recalculateUserStats(@PathVariable String userId) { boolean recalculated = userStatsService.recalculateUserStats(userId); if (!recalculated) { return Result.error("重新计算失败"); } return Result.success(); } /** * 重新计算所有用户统计 */ @PutMapping("/recalculate-all") public Result recalculateAllUserStats() { boolean recalculated = userStatsService.recalculateAllUserStats(); if (!recalculated) { return Result.error("重新计算失败"); } return Result.success(); } /** * 查询平均统计值 */ @GetMapping("/type/{statsType}/avg") public Result getAvgValueByStatsType(@PathVariable String statsType) { Double avgValue = userStatsService.getAvgValueByStatsType(statsType); return Result.success(avgValue); } /** * 查询最大统计值 */ @GetMapping("/type/{statsType}/max") public Result getMaxValueByStatsType(@PathVariable String statsType) { Double maxValue = userStatsService.getMaxValueByStatsType(statsType); return Result.success(maxValue); } /** * 查询最小统计值 */ @GetMapping("/type/{statsType}/min") public Result getMinValueByStatsType(@PathVariable String statsType) { Double minValue = userStatsService.getMinValueByStatsType(statsType); return Result.success(minValue); } /** * 删除过期的统计数据 */ @DeleteMapping("/expired") public Result deleteExpiredStats(@RequestParam(defaultValue = "30") Integer days) { boolean deleted = userStatsService.deleteExpiredStats(days); if (!deleted) { return Result.error("删除失败"); } return Result.success(); } /** * 转换为响应对象 */ private UserStatsResponse convertToResponse(UserStats stats) { UserStatsResponse response = new UserStatsResponse(); BeanUtils.copyProperties(stats, response); response.setId(stats.getId()); if (stats.getCreateTime() != null) { response.setCreateTime(stats.getCreateTime().format(DATE_TIME_FORMATTER)); } if (stats.getUpdateTime() != null) { response.setUpdateTime(stats.getUpdateTime().format(DATE_TIME_FORMATTER)); } return response; } /** * 用户统计创建请求 */ @lombok.Data public static class UserStatsCreateRequest { @NotBlank(message = "用户ID不能为空") private String userId; @NotBlank(message = "统计类型不能为空") private String statsType; @NotNull(message = "统计值不能为空") private Double value; private String period; } /** * 用户统计响应类 */ @lombok.Data @lombok.EqualsAndHashCode(callSuper = true) public static class UserStatsResponse extends BaseResponse { private String userId; private String statsType; private Double value; private String period; } }