| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package com.zxyj.controller;
- import com.zxyj.common.Result;
- import com.zxyj.entity.MiniGame;
- import com.zxyj.entity.User;
- import com.zxyj.entity.Child;
- import com.zxyj.mapper.UserMapper;
- import com.zxyj.mapper.ChildMapper;
- import com.zxyj.service.MiniGameService;
- import io.swagger.v3.oas.annotations.Operation;
- import io.swagger.v3.oas.annotations.tags.Tag;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import java.util.List;
- import java.util.Map;
- @Tag(name = "小游戏管理", description = "小游戏相关接口")
- @RestController
- @RequestMapping("/api/mini-game")
- public class MiniGameController {
- @Autowired
- private MiniGameService miniGameService;
- @Autowired
- private UserMapper userMapper;
- @Autowired
- private ChildMapper childMapper;
- /**
- * 获取启用的小游戏列表
- */
- @Operation(summary = "获取小游戏列表")
- @GetMapping("/list")
- public Result<List<MiniGame>> getGameList() {
- List<MiniGame> games = miniGameService.getEnabledGames();
- return Result.success(games);
- }
- /**
- * 根据游戏代码获取游戏详情
- */
- @Operation(summary = "获取游戏详情")
- @GetMapping("/{gameCode}")
- public Result<MiniGame> getGameByCode(@PathVariable String gameCode) {
- MiniGame game = miniGameService.getGameByCode(gameCode);
- if (game == null) {
- return Result.error("游戏不存在");
- }
- return Result.success(game);
- }
- /**
- * 获取所有小游戏(管理端)
- */
- @Operation(summary = "获取所有小游戏(管理端)")
- @GetMapping("/all")
- public Result<List<MiniGame>> getAllGames() {
- List<MiniGame> games = miniGameService.getAllGames();
- return Result.success(games);
- }
- /**
- * 更新游戏状态
- */
- @Operation(summary = "更新游戏状态")
- @PutMapping("/{id}/status")
- public Result<Boolean> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
- boolean success = miniGameService.updateGameStatus(id, status);
- return Result.success(success);
- }
- /**
- * 完成任务并获得积分
- */
- @Operation(summary = "游戏完成任务")
- @PostMapping("/complete")
- public Result<Map<String, Object>> completeGame(
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, Object> body) {
- Long childId = Long.valueOf(body.get("childId").toString());
- String gameCode = (String) body.get("gameCode");
- Integer completionTime = body.get("completionTime") != null ?
- Integer.valueOf(body.get("completionTime").toString()) : null;
- Integer score = body.get("score") != null ?
- Integer.valueOf(body.get("score").toString()) : null;
- // 权限校验:验证用户是否有权限操作该孩子
- User user = userMapper.selectById(userId);
- if (user == null) {
- return Result.error("用户不存在");
- }
- Child child = childMapper.selectById(childId);
- if (child == null) {
- return Result.error("孩子不存在");
- }
- // 验证孩子是否属于用户的家庭
- if (!child.getFamilyId().equals(user.getFamilyId())) {
- return Result.error("无权限操作该孩子数据");
- }
- try {
- Map<String, Object> result = miniGameService.completeGame(childId, gameCode, completionTime, score);
- return Result.success(result);
- } catch (Exception e) {
- return Result.error(e.getMessage());
- }
- }
- }
|