ParentController.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.zxyj.controller.family;
  2. import com.zxyj.common.Result;
  3. import com.zxyj.dto.CreateRewardDTO;
  4. import com.zxyj.dto.RewardDTO;
  5. import com.zxyj.entity.User;
  6. import com.zxyj.service.RewardService;
  7. import com.zxyj.service.UserService;
  8. import javax.annotation.Resource;
  9. import org.springframework.web.bind.annotation.*;
  10. import java.util.List;
  11. import java.util.Map;
  12. @RestController
  13. @RequestMapping("/api/parent")
  14. public class ParentController {
  15. @Resource
  16. private RewardService rewardService;
  17. @Resource
  18. private UserService userService;
  19. /**
  20. * 家长心愿单 - 创建心愿(直接进入可用列表)
  21. */
  22. @PostMapping("/wishlist")
  23. public Result<Long> createParentWishlist(@RequestAttribute("userId") Long userId,
  24. @RequestBody CreateRewardDTO dto) {
  25. // 家长直接创建奖励,状态为available
  26. Long rewardId = rewardService.createReward(userId, dto);
  27. return Result.success(rewardId);
  28. }
  29. /**
  30. * 家长心愿单 - 获取列表(所有家庭成员的心愿)
  31. */
  32. @PostMapping("/wishlist")
  33. public Result<List<RewardDTO>> getParentWishlist(@RequestAttribute("userId") Long userId) {
  34. User user = userService.getUserInfo(userId);
  35. if (user == null || user.getFamilyId() == null) {
  36. return Result.error("用户无家庭信息");
  37. }
  38. // 获取家庭所有可用的奖励(包括孩子创建的待审批心愿)
  39. List<RewardDTO> rewards = rewardService.getFamilyRewardLibrary(user.getFamilyId());
  40. return Result.success(rewards);
  41. }
  42. /**
  43. * 家长心愿单 - 获取家庭心愿列表(路由兼容)
  44. */
  45. @PostMapping("/wishlist/family")
  46. public Result<List<RewardDTO>> getFamilyWishlist(@RequestAttribute("userId") Long userId) {
  47. User user = userService.getUserInfo(userId);
  48. if (user == null || user.getFamilyId() == null) {
  49. return Result.error("用户无家庭信息");
  50. }
  51. List<RewardDTO> rewards = rewardService.getFamilyRewardLibrary(user.getFamilyId());
  52. return Result.success(rewards);
  53. }
  54. /**
  55. * 家长心愿单 - 审批孩子的心愿并填写积分
  56. */
  57. @PostMapping("/wishlist/{id}/approve")
  58. public Result<Boolean> approveWishlist(@PathVariable Long id,
  59. @RequestAttribute("userId") Long userId,
  60. @RequestBody Map<String, Object> params) {
  61. Integer pointsRequired = params.get("pointsRequired") != null ? (Integer) params.get("pointsRequired") : null;
  62. if (pointsRequired == null || pointsRequired <= 0) {
  63. return Result.error("请填写有效的积分");
  64. }
  65. boolean success = rewardService.approveWishlist(id, userId, pointsRequired);
  66. return Result.success(success);
  67. }
  68. /**
  69. * 家长心愿单 - 兑换家长的心愿
  70. */
  71. @PostMapping("/wishlist/{id}/exchange")
  72. public Result<Map<String, Object>> exchangeParentWishlist(@PathVariable Long id,
  73. @RequestAttribute("userId") Long userId) {
  74. // 家长兑换自己的心愿,直接扣积分
  75. User user = userService.getUserInfo(userId);
  76. if (user == null) {
  77. return Result.error("用户不存在");
  78. }
  79. // 家长积分在user表
  80. if (user.getTotalPoints() == null) {
  81. user.setTotalPoints(0);
  82. }
  83. // TODO: 实现家长兑换逻辑(暂时返回成功)
  84. Map<String, Object> result = new java.util.HashMap<>();
  85. result.put("success", true);
  86. result.put("newBalance", user.getTotalPoints());
  87. return Result.success(result);
  88. }
  89. }