WishController.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package com.etotem.cfc.controller.wish;
  2. import com.etotem.cfc.common.Result;
  3. import com.etotem.cfc.dto.CreateWishDTO;
  4. import com.etotem.cfc.dto.WishDTO;
  5. import com.etotem.cfc.service.WishService;
  6. import io.swagger.v3.oas.annotations.Operation;
  7. import io.swagger.v3.oas.annotations.tags.Tag;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.web.bind.annotation.*;
  10. import javax.annotation.Resource;
  11. import java.util.List;
  12. import java.util.Map;
  13. /**
  14. * 心愿控制器
  15. */
  16. @Slf4j
  17. @Tag(name = "心愿管理", description = "心愿创建、定价、兑换、兑现等接口")
  18. @RestController
  19. @RequestMapping("/api/wishes")
  20. public class WishController {
  21. @Resource
  22. private WishService wishService;
  23. // ========== 创建心愿 ==========
  24. @Operation(summary = "创建心愿")
  25. @PostMapping("/create")
  26. public Result<Long> createWish(
  27. @RequestAttribute("userId") Long userId,
  28. @RequestAttribute("role") String role,
  29. @RequestBody CreateWishDTO dto) {
  30. try {
  31. Long wishId;
  32. if ("child".equals(role)) {
  33. wishId = wishService.createByChild(userId, dto);
  34. } else {
  35. wishId = wishService.createByParent(userId, dto);
  36. }
  37. return Result.success(wishId);
  38. } catch (RuntimeException e) {
  39. log.error("创建心愿失败", e);
  40. return Result.error(e.getMessage());
  41. }
  42. }
  43. // ========== 定价流程 ==========
  44. @Operation(summary = "家长定价")
  45. @PutMapping("/{id}/set-price")
  46. public Result<Boolean> setPrice(
  47. @PathVariable Long id,
  48. @RequestAttribute("userId") Long userId,
  49. @RequestBody Map<String, Object> body) {
  50. try {
  51. Integer pointsRequired = (Integer) body.get("pointsRequired");
  52. if (pointsRequired == null || pointsRequired <= 0) {
  53. return Result.error("请输入有效的积分值");
  54. }
  55. boolean success = wishService.setPrice(id, userId, pointsRequired);
  56. if (!success) {
  57. return Result.error("定价失败,请检查心愿状态");
  58. }
  59. return Result.success(true);
  60. } catch (Exception e) {
  61. log.error("定价失败", e);
  62. return Result.error("定价失败: " + e.getMessage());
  63. }
  64. }
  65. @Operation(summary = "家长拒绝心愿")
  66. @PutMapping("/{id}/reject")
  67. public Result<Boolean> rejectWish(
  68. @PathVariable Long id,
  69. @RequestAttribute("userId") Long userId,
  70. @RequestBody Map<String, String> body) {
  71. try {
  72. String reason = body.get("reason");
  73. boolean success = wishService.rejectWish(id, userId, reason);
  74. if (!success) {
  75. return Result.error("拒绝失败,请检查心愿状态");
  76. }
  77. return Result.success(true);
  78. } catch (Exception e) {
  79. log.error("拒绝心愿失败", e);
  80. return Result.error("拒绝失败: " + e.getMessage());
  81. }
  82. }
  83. // ========== 兑换流程 ==========
  84. @Operation(summary = "孩子申请兑换")
  85. @PostMapping("/{id}/exchange")
  86. public Result<Boolean> requestExchange(
  87. @PathVariable Long id,
  88. @RequestAttribute("userId") Long userId) {
  89. try {
  90. boolean success = wishService.requestExchange(id, userId);
  91. if (!success) {
  92. return Result.error("申请兑换失败,请检查心愿状态");
  93. }
  94. return Result.success(true);
  95. } catch (RuntimeException e) {
  96. log.error("申请兑换失败", e);
  97. return Result.error(e.getMessage());
  98. }
  99. }
  100. @Operation(summary = "家长审批兑换")
  101. @PutMapping("/{id}/approve-exchange")
  102. public Result<Boolean> approveExchange(
  103. @PathVariable Long id,
  104. @RequestAttribute("userId") Long userId,
  105. @RequestBody Map<String, Object> body) {
  106. try {
  107. Boolean approved = (Boolean) body.get("approved");
  108. String reason = (String) body.get("reason"); // 拒绝原因
  109. if (approved == null) {
  110. return Result.error("请指定审批结果");
  111. }
  112. boolean success = wishService.approveExchange(id, userId, approved);
  113. if (!success) {
  114. return Result.error("审批失败,请检查心愿状态");
  115. }
  116. return Result.success(true);
  117. } catch (RuntimeException e) {
  118. log.error("审批兑换失败", e);
  119. return Result.error(e.getMessage());
  120. }
  121. }
  122. // ========== 兑现流程 ==========
  123. @Operation(summary = "家长标记已购买")
  124. @PutMapping("/{id}/fulfill")
  125. public Result<Boolean> markFulfilled(
  126. @PathVariable Long id,
  127. @RequestAttribute("userId") Long userId) {
  128. try {
  129. boolean success = wishService.markFulfilled(id, userId);
  130. if (!success) {
  131. return Result.error("标记失败,请检查心愿状态");
  132. }
  133. return Result.success(true);
  134. } catch (Exception e) {
  135. log.error("标记已购买失败", e);
  136. return Result.error("标记失败: " + e.getMessage());
  137. }
  138. }
  139. @Operation(summary = "孩子确认收货")
  140. @PutMapping("/{id}/confirm")
  141. public Result<Boolean> confirmReceived(
  142. @PathVariable Long id,
  143. @RequestAttribute("userId") Long userId) {
  144. try {
  145. boolean success = wishService.confirmReceived(id, userId);
  146. if (!success) {
  147. return Result.error("确认失败,请检查心愿状态");
  148. }
  149. return Result.success(true);
  150. } catch (Exception e) {
  151. log.error("确认收货失败", e);
  152. return Result.error("确认失败: " + e.getMessage());
  153. }
  154. }
  155. // ========== 取消心愿 ==========
  156. @Operation(summary = "取消心愿")
  157. @DeleteMapping("/{id}")
  158. public Result<Boolean> cancelWish(
  159. @PathVariable Long id,
  160. @RequestAttribute("userId") Long userId,
  161. @RequestBody(required = false) Map<String, String> body) {
  162. try {
  163. String reason = body != null ? body.get("reason") : null;
  164. boolean success = wishService.cancelWish(id, userId, reason);
  165. if (!success) {
  166. return Result.error("取消失败,请检查心愿状态");
  167. }
  168. return Result.success(true);
  169. } catch (Exception e) {
  170. log.error("取消心愿失败", e);
  171. return Result.error("取消失败: " + e.getMessage());
  172. }
  173. }
  174. // ========== 查询接口 ==========
  175. @Operation(summary = "获取心愿列表")
  176. @PostMapping("/list")
  177. public Result<List<WishDTO>> getWishes(
  178. @RequestAttribute("userId") Long userId,
  179. @RequestAttribute("role") String role,
  180. @RequestParam(required = false) Long childId,
  181. @RequestParam(required = false) Long familyId,
  182. @RequestParam(required = false) String status) {
  183. try {
  184. List<WishDTO> wishes;
  185. if ("child".equals(role)) {
  186. // 孩子只能看自己的心愿
  187. wishes = wishService.getChildWishes(userId);
  188. } else if (childId != null) {
  189. // 家长查看指定孩子的心愿
  190. wishes = wishService.getChildWishes(childId);
  191. } else if (familyId != null) {
  192. // 家长查看家庭所有心愿
  193. wishes = wishService.getFamilyWishes(familyId);
  194. } else {
  195. return Result.error("请指定查询条件(childId或familyId)");
  196. }
  197. return Result.success(wishes);
  198. } catch (Exception e) {
  199. log.error("获取心愿列表失败", e);
  200. return Result.error("获取失败: " + e.getMessage());
  201. }
  202. }
  203. @Operation(summary = "获取待处理列表(家长)")
  204. @PostMapping("/pending")
  205. public Result<List<WishDTO>> getPendingWishes(
  206. @RequestAttribute("userId") Long userId,
  207. @RequestBody Map<String, Object> body) {
  208. try {
  209. Long familyId = body.get("familyId") != null ? Long.valueOf(body.get("familyId").toString()) : null;
  210. if (familyId == null) {
  211. return Result.error("请指定家庭ID");
  212. }
  213. List<WishDTO> wishes = wishService.getParentPendingWishes(familyId);
  214. return Result.success(wishes);
  215. } catch (Exception e) {
  216. log.error("获取待处理列表失败", e);
  217. return Result.error("获取失败: " + e.getMessage());
  218. }
  219. }
  220. @Operation(summary = "获取心愿详情")
  221. @PostMapping("/{id}")
  222. public Result<WishDTO> getWishById(@PathVariable Long id) {
  223. try {
  224. WishDTO wish = wishService.getWishById(id);
  225. if (wish == null) {
  226. return Result.error("心愿不存在");
  227. }
  228. return Result.success(wish);
  229. } catch (Exception e) {
  230. log.error("获取心愿详情失败", e);
  231. return Result.error("获取失败: " + e.getMessage());
  232. }
  233. }
  234. }