| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package com.etotem.cfc.controller.assessment;
- import com.etotem.cfc.common.Result;
- import com.etotem.cfc.entity.AssessmentOrder;
- import com.etotem.cfc.entity.AssessmentAppointment;
- import com.etotem.cfc.service.AssessmentOrderService;
- import com.etotem.cfc.service.AssessmentAppointmentService;
- import io.swagger.v3.oas.annotations.Operation;
- import io.swagger.v3.oas.annotations.tags.Tag;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import java.util.*;
- import java.util.stream.Collectors;
- @Tag(name = "评估订单", description = "评估订单与支付")
- @RestController
- @RequestMapping("/api/assessment/order")
- public class AssessmentOrderController {
- @Resource
- private AssessmentOrderService orderService;
- @Resource
- private AssessmentAppointmentService appointmentService;
- @Resource
- private com.etotem.cfc.mapper.FamilyMapper familyMapper;
- @Operation(summary = "创建评估订单")
- @PostMapping("/create")
- public Result<Map<String, Object>> createOrder(
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, Object> body) {
- Long familyId = Long.valueOf(body.get("familyId").toString());
- Long childId = body.get("childId") != null ? Long.valueOf(body.get("childId").toString()) : null;
- Long guideId = null;
- if (body.get("guideId") != null) {
- guideId = Long.valueOf(body.get("guideId").toString());
- }
- // 未指定规划师时,尝试使用家庭绑定的规划师
- if (guideId == null) {
- com.etotem.cfc.entity.Family family = familyMapper.selectById(familyId);
- if (family != null && family.getTeacherId() != null) {
- guideId = family.getTeacherId();
- }
- }
- Long packageId = Long.valueOf(body.get("packageId").toString());
- String guideName = body.get("guideName") != null ? body.get("guideName").toString() : null;
- String packageName = body.get("packageName") != null ? body.get("packageName").toString() : null;
- Long totalPrice = body.get("totalPrice") != null ? Long.valueOf(body.get("totalPrice").toString()) : 0L;
- Long discountAmount = body.get("discountAmount") != null ? Long.valueOf(body.get("discountAmount").toString()) : 0L;
- AssessmentOrder order = orderService.createOrder(
- familyId, userId, childId, guideId, packageId,
- guideName, packageName, totalPrice, discountAmount);
- Map<String, Object> result = new HashMap<>();
- result.put("orderNo", order.getOrderNo());
- result.put("actualPrice", order.getActualPrice());
- result.put("status", order.getStatus());
- return Result.success(result);
- }
- @Operation(summary = "获取订单详情")
- @PostMapping("/detail")
- public Result<AssessmentOrder> getOrderDetail(
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, Object> body) {
- String orderNo = body.get("orderNo") != null ? body.get("orderNo").toString() : null;
- if (orderNo == null) return Result.error("订单号不能为空");
- AssessmentOrder order = orderService.getByOrderNo(orderNo);
- if (order == null) return Result.error("订单不存在");
- if (!order.getUserId().equals(userId)) return Result.error("无权查看");
- return Result.success(order);
- }
- @Operation(summary = "我的订单列表")
- @PostMapping("/my-orders")
- public Result<List<AssessmentOrder>> getMyOrders(@RequestAttribute("userId") Long userId) {
- List<AssessmentOrder> orders = orderService.getByUserId(userId);
- return Result.success(orders);
- }
- @Operation(summary = "支付订单")
- @PostMapping("/pay")
- public Result<Map<String, Object>> payOrder(
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, Object> body) {
- String orderNo = body.get("orderNo") != null ? body.get("orderNo").toString() : null;
- String payType = body.get("payType") != null ? body.get("payType").toString() : "wechat";
- if (orderNo == null) return Result.error("订单号不能为空");
- boolean success = orderService.payOrder(orderNo, payType);
- if (!success) return Result.error("支付失败");
- // 支付成功后确认预约
- AssessmentOrder order = orderService.getByOrderNo(orderNo);
- if (order != null && order.getChildId() != null) {
- appointmentService.confirmAppointmentByOrder(order);
- }
- Map<String, Object> result = new HashMap<>();
- result.put("orderNo", orderNo);
- result.put("payTime", new Date());
- result.put("status", "paid");
- return Result.success(result);
- }
- @Operation(summary = "取消订单")
- @PostMapping("/cancel")
- public Result<Boolean> cancelOrder(
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, Object> body) {
- String orderNo = body.get("orderNo") != null ? body.get("orderNo").toString() : null;
- if (orderNo == null) return Result.error("订单号不能为空");
- AssessmentOrder order = orderService.getByOrderNo(orderNo);
- if (order == null) return Result.error("订单不存在");
- if (!order.getUserId().equals(userId)) return Result.error("无权操作");
- boolean success = orderService.cancelOrder(orderNo);
- if (!success) return Result.error("取消失败,仅待支付订单可取消");
- return Result.success(true);
- }
- @Operation(summary = "退款订单")
- @PostMapping("/refund")
- public Result<Boolean> refundOrder(
- @RequestAttribute("userId") Long userId,
- @RequestBody Map<String, Object> body) {
- String orderNo = body.get("orderNo") != null ? body.get("orderNo").toString() : null;
- if (orderNo == null) return Result.error("订单号不能为空");
- AssessmentOrder order = orderService.getByOrderNo(orderNo);
- if (order == null) return Result.error("订单不存在");
- if (!order.getUserId().equals(userId)) return Result.error("无权操作");
- boolean success = orderService.refundOrder(orderNo);
- if (!success) return Result.error("退款失败,仅已支付订单可退款");
- return Result.success(true);
- }
- }
|