|
|
@@ -32,6 +32,7 @@ import com.ylx.massage.service.TWxUserService;
|
|
|
import com.ylx.massage.utils.OrderNumberGenerator;
|
|
|
import com.ylx.order.domain.OrderStatusFlow;
|
|
|
import com.ylx.order.domain.TOrder;
|
|
|
+import com.ylx.order.domain.dto.OrderCancleDTO;
|
|
|
import com.ylx.order.domain.dto.OrderDateQueryDTO;
|
|
|
import com.ylx.order.domain.dto.OrderSubmitDTO;
|
|
|
import com.ylx.order.domain.dto.OrderUpdateStatusDTO;
|
|
|
@@ -75,6 +76,9 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("M月d日");
|
|
|
private final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
|
|
|
+
|
|
|
+ /** 仅允许取消的状态:待付款(0) */
|
|
|
+ private static final List<Integer> ALLOWED_CANCEL_STATUS = Collections.singletonList(0);
|
|
|
@Resource
|
|
|
private ProjectService projectService;
|
|
|
@Resource
|
|
|
@@ -752,4 +756,48 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
}
|
|
|
return vo;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int cancelOrder(OrderCancleDTO dto) {
|
|
|
+ // 1. 查询订单
|
|
|
+ TOrder order = this.baseMapper.selectById(dto.getId());
|
|
|
+ if (order == null) {
|
|
|
+ throw new IllegalArgumentException("订单不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 权限校验:仅限订单所属用户取消
|
|
|
+ Long currentUserId = SecurityUtils.getUserId();
|
|
|
+ if (!order.getUserId().equals(currentUserId)) {
|
|
|
+ throw new RuntimeException("您无权操作此订单");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 状态校验:只有待付款(0)可以取消
|
|
|
+ if (!OrderStatusEnum.PENDING_PAYMENT.getCode().equals(order.getStatus())) {
|
|
|
+ throw new RuntimeException("只有待付款订单可以取消,当前状态无法取消");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 重复取消校验(可选)
|
|
|
+ if (OrderStatusEnum.CANCELLED.getCode().equals(order.getStatus())) {
|
|
|
+ throw new RuntimeException("订单已取消,请勿重复操作");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 执行取消更新
|
|
|
+ TOrder updateOrder = new TOrder();
|
|
|
+ updateOrder.setId(dto.getId());
|
|
|
+ updateOrder.setStatus(OrderStatusEnum.CANCELLED.getCode());
|
|
|
+ updateOrder.setCancelledReason(dto.getCancelledReason());
|
|
|
+ updateOrder.setCancelledTime(LocalDateTime.now());
|
|
|
+ updateOrder.setUpdateTime(new Date());
|
|
|
+
|
|
|
+ int rows = this.baseMapper.updateById(updateOrder);
|
|
|
+ if (rows <= 0) {
|
|
|
+ throw new RuntimeException("取消失败,请稍后重试");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 6. 待付款订单取消无需退款(因为未支付),但如需记录日志等可留扩展
|
|
|
+ log.info("订单取消成功,订单ID:{},原因:{}", dto.getId(), dto.getCancelledReason());
|
|
|
+
|
|
|
+ return rows;
|
|
|
+ }
|
|
|
}
|