| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- package com.etotem.cfc.controller.wish;
- import com.etotem.cfc.common.Result;
- import com.etotem.cfc.dto.CreateWishDTO;
- import com.etotem.cfc.dto.WishDTO;
- import com.etotem.cfc.service.WishService;
- import io.swagger.v3.oas.annotations.Operation;
- import io.swagger.v3.oas.annotations.tags.Tag;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import java.util.List;
- import java.util.Map;
- /**
- * 心愿控制器
- */
- @Slf4j
- @Tag(name = "心愿管理", description = "心愿创建、定价、兑换、兑现等接口")
- @RestController
- @RequestMapping("/api/wishes")
- public class WishController {
- @Resource
- private WishService wishService;
- // ========== 创建心愿 ==========
- @Operation(summary = "创建心愿")
- @PostMapping("/create")
- public Result<Long> createWish(
- @RequestAttribute("userId") Long userId,
- @RequestAttribute("role") String role,
- @RequestBody CreateWishDTO dto) {
- try {
- Long wishId;
- if ("child".equals(role)) {
- wishId = wishService.createByChild(userId, dto);
- } else {
- wishId = wishService.createByParent(userId, dto);
- }
- return Result.success(wishId);
- } catch (RuntimeException e) {
- log.error("创建心愿失败", e);
- return Result.error(e.getMessage());
- }
- }
- // ========== 定价流程 ==========
- @Operation(summary = "家长定价")
- @PutMapping("/{id}/set-price")
- public Result<Boolean> setPrice(
- @PathVariable Long id,
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, Object> body) {
- try {
- Integer pointsRequired = (Integer) body.get("pointsRequired");
- if (pointsRequired == null || pointsRequired <= 0) {
- return Result.error("请输入有效的积分值");
- }
- boolean success = wishService.setPrice(id, userId, pointsRequired);
- if (!success) {
- return Result.error("定价失败,请检查心愿状态");
- }
- return Result.success(true);
- } catch (Exception e) {
- log.error("定价失败", e);
- return Result.error("定价失败: " + e.getMessage());
- }
- }
- @Operation(summary = "家长拒绝心愿")
- @PutMapping("/{id}/reject")
- public Result<Boolean> rejectWish(
- @PathVariable Long id,
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, String> body) {
- try {
- String reason = body.get("reason");
- boolean success = wishService.rejectWish(id, userId, reason);
- if (!success) {
- return Result.error("拒绝失败,请检查心愿状态");
- }
- return Result.success(true);
- } catch (Exception e) {
- log.error("拒绝心愿失败", e);
- return Result.error("拒绝失败: " + e.getMessage());
- }
- }
- // ========== 兑换流程 ==========
- @Operation(summary = "孩子申请兑换")
- @PostMapping("/{id}/exchange")
- public Result<Boolean> requestExchange(
- @PathVariable Long id,
- @RequestAttribute("userId") Long userId) {
- try {
- boolean success = wishService.requestExchange(id, userId);
- if (!success) {
- return Result.error("申请兑换失败,请检查心愿状态");
- }
- return Result.success(true);
- } catch (RuntimeException e) {
- log.error("申请兑换失败", e);
- return Result.error(e.getMessage());
- }
- }
- @Operation(summary = "家长审批兑换")
- @PutMapping("/{id}/approve-exchange")
- public Result<Boolean> approveExchange(
- @PathVariable Long id,
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, Object> body) {
- try {
- Boolean approved = (Boolean) body.get("approved");
- String reason = (String) body.get("reason"); // 拒绝原因
- if (approved == null) {
- return Result.error("请指定审批结果");
- }
- boolean success = wishService.approveExchange(id, userId, approved);
- if (!success) {
- return Result.error("审批失败,请检查心愿状态");
- }
- return Result.success(true);
- } catch (RuntimeException e) {
- log.error("审批兑换失败", e);
- return Result.error(e.getMessage());
- }
- }
- // ========== 兑现流程 ==========
- @Operation(summary = "家长标记已购买")
- @PutMapping("/{id}/fulfill")
- public Result<Boolean> markFulfilled(
- @PathVariable Long id,
- @RequestAttribute("userId") Long userId) {
- try {
- boolean success = wishService.markFulfilled(id, userId);
- if (!success) {
- return Result.error("标记失败,请检查心愿状态");
- }
- return Result.success(true);
- } catch (Exception e) {
- log.error("标记已购买失败", e);
- return Result.error("标记失败: " + e.getMessage());
- }
- }
- @Operation(summary = "孩子确认收货")
- @PutMapping("/{id}/confirm")
- public Result<Boolean> confirmReceived(
- @PathVariable Long id,
- @RequestAttribute("userId") Long userId) {
- try {
- boolean success = wishService.confirmReceived(id, userId);
- if (!success) {
- return Result.error("确认失败,请检查心愿状态");
- }
- return Result.success(true);
- } catch (Exception e) {
- log.error("确认收货失败", e);
- return Result.error("确认失败: " + e.getMessage());
- }
- }
- // ========== 取消心愿 ==========
- @Operation(summary = "取消心愿")
- @DeleteMapping("/{id}")
- public Result<Boolean> cancelWish(
- @PathVariable Long id,
- @RequestAttribute("userId") Long userId,
- @RequestBody(required = false) Map<String, String> body) {
- try {
- String reason = body != null ? body.get("reason") : null;
- boolean success = wishService.cancelWish(id, userId, reason);
- if (!success) {
- return Result.error("取消失败,请检查心愿状态");
- }
- return Result.success(true);
- } catch (Exception e) {
- log.error("取消心愿失败", e);
- return Result.error("取消失败: " + e.getMessage());
- }
- }
- // ========== 查询接口 ==========
- @Operation(summary = "获取心愿列表")
- @PostMapping("/list")
- public Result<List<WishDTO>> getWishes(
- @RequestAttribute("userId") Long userId,
- @RequestAttribute("role") String role,
- @RequestParam(required = false) Long childId,
- @RequestParam(required = false) Long familyId,
- @RequestParam(required = false) String status) {
- try {
- List<WishDTO> wishes;
- if ("child".equals(role)) {
- // 孩子只能看自己的心愿
- wishes = wishService.getChildWishes(userId);
- } else if (childId != null) {
- // 家长查看指定孩子的心愿
- wishes = wishService.getChildWishes(childId);
- } else if (familyId != null) {
- // 家长查看家庭所有心愿
- wishes = wishService.getFamilyWishes(familyId);
- } else {
- return Result.error("请指定查询条件(childId或familyId)");
- }
- return Result.success(wishes);
- } catch (Exception e) {
- log.error("获取心愿列表失败", e);
- return Result.error("获取失败: " + e.getMessage());
- }
- }
- @Operation(summary = "获取待处理列表(家长)")
- @PostMapping("/pending")
- public Result<List<WishDTO>> getPendingWishes(
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, Object> body) {
- try {
- Long familyId = body.get("familyId") != null ? Long.valueOf(body.get("familyId").toString()) : null;
- if (familyId == null) {
- return Result.error("请指定家庭ID");
- }
- List<WishDTO> wishes = wishService.getParentPendingWishes(familyId);
- return Result.success(wishes);
- } catch (Exception e) {
- log.error("获取待处理列表失败", e);
- return Result.error("获取失败: " + e.getMessage());
- }
- }
- @Operation(summary = "获取心愿详情")
- @PostMapping("/{id}")
- public Result<WishDTO> getWishById(@PathVariable Long id) {
- try {
- WishDTO wish = wishService.getWishById(id);
- if (wish == null) {
- return Result.error("心愿不存在");
- }
- return Result.success(wish);
- } catch (Exception e) {
- log.error("获取心愿详情失败", e);
- return Result.error("获取失败: " + e.getMessage());
- }
- }
- }
|