|
|
@@ -910,68 +910,100 @@ public class LawyerConsultationOrderServiceImpl extends ServiceImpl<LawyerConsul
|
|
|
|
|
|
/**
|
|
|
* 取消律师咨询订单
|
|
|
+ * <p>
|
|
|
+ * 取消订单前会进行以下校验和处理:
|
|
|
+ * 1. 参数校验:订单ID不能为空
|
|
|
+ * 2. 订单存在性校验:订单必须存在
|
|
|
+ * 3. 订单状态校验:已取消或已完成的订单不允许再次取消
|
|
|
+ * 4. 如果订单是待支付状态,会取消Redis中的订单支付超时计时器
|
|
|
+ * 5. 根据订单状态更新为相应状态:
|
|
|
+ * - 待接单状态:更新为已退款状态
|
|
|
+ * - 待支付状态:更新为已取消状态
|
|
|
+ * </p>
|
|
|
*
|
|
|
* @param id 订单ID
|
|
|
- * @return 是否取消成功
|
|
|
+ * @return 是否取消成功,true表示成功,false表示失败
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public boolean cancelOrder(String id) {
|
|
|
- // 参数校验
|
|
|
+ log.info("开始取消订单,订单ID={}", id);
|
|
|
+
|
|
|
+ // 1. 参数校验
|
|
|
if (!StringUtils.hasText(id)) {
|
|
|
log.warn("取消订单失败:订单ID为空");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 查询订单信息
|
|
|
+ // 2. 查询订单信息
|
|
|
LawyerConsultationOrder order = consultationOrderMapper.selectById(id);
|
|
|
if (order == null) {
|
|
|
log.warn("取消订单失败:订单不存在,订单ID={}", id);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 检查订单状态:已完成或已取消的订单不允许再次取消
|
|
|
+ // 3. 订单状态校验
|
|
|
Integer orderStatus = order.getOrderStatus();
|
|
|
+ String orderNumber = order.getOrderNumber();
|
|
|
Integer cancelStatus = LawyerStatusEnum.CANCEL.getStatus();
|
|
|
+ Integer completedStatus = LawyerStatusEnum.COMPLETE.getStatus();
|
|
|
+ Integer waitAcceptStatus = LawyerStatusEnum.WAIT_ACCEPT.getStatus();
|
|
|
+ Integer waitPayStatus = LawyerStatusEnum.WAIT_PAY.getStatus();
|
|
|
+ Integer refundedStatus = LawyerStatusEnum.REFUNDED.getStatus();
|
|
|
+
|
|
|
+ // 3.1 检查订单是否已取消
|
|
|
if (cancelStatus.equals(orderStatus)) {
|
|
|
- log.warn("取消订单失败:订单已取消,订单ID={}, 订单编号={}", id, order.getOrderNumber());
|
|
|
+ log.warn("取消订单失败:订单已取消,订单ID={}, 订单编号={}", id, orderNumber);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 如果订单已完成,不允许取消
|
|
|
- Integer completedStatus = LawyerStatusEnum.COMPLETE.getStatus(); // 3:已完成
|
|
|
+ // 3.2 检查订单是否已完成
|
|
|
if (completedStatus.equals(orderStatus)) {
|
|
|
- log.warn("取消订单失败:订单已完成,不允许取消,订单ID={}, 订单编号={}", id, order.getOrderNumber());
|
|
|
+ log.warn("取消订单失败:订单已完成,不允许取消,订单ID={}, 订单编号={}", id, orderNumber);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 如果订单是待支付状态,取消Redis中的订单支付超时计时
|
|
|
- Integer waitPayStatus = LawyerStatusEnum.WAIT_PAY.getStatus(); // 0:待支付
|
|
|
- if (waitPayStatus.equals(orderStatus) && order.getOrderNumber() != null) {
|
|
|
- orderExpirationService.cancelOrderPaymentTimeout(order.getOrderNumber());
|
|
|
- log.info("已取消订单支付超时计时,订单编号={}", order.getOrderNumber());
|
|
|
+ // 4. 如果订单是待支付状态,取消Redis中的订单支付超时计时器
|
|
|
+ if (waitPayStatus.equals(orderStatus) && StringUtils.hasText(orderNumber)) {
|
|
|
+ try {
|
|
|
+ orderExpirationService.cancelOrderPaymentTimeout(orderNumber);
|
|
|
+ log.info("已取消订单支付超时计时器,订单编号={}", orderNumber);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("取消订单支付超时计时器失败,订单编号={}", orderNumber, e);
|
|
|
+ // 继续执行取消订单操作,不因取消计时器失败而中断
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 更新订单状态为已取消
|
|
|
- LambdaUpdateWrapper<LawyerConsultationOrder> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
-
|
|
|
- if (order.getPaymentStatus() == 1) {
|
|
|
- updateWrapper.eq(LawyerConsultationOrder::getId, id)
|
|
|
- .set(LawyerConsultationOrder::getOrderStatus, LawyerStatusEnum.REFUNDED.getStatus())
|
|
|
- .set(LawyerConsultationOrder::getUpdatedTime, new Date());
|
|
|
- }else if(order.getPaymentStatus() == 0){
|
|
|
- updateWrapper.eq(LawyerConsultationOrder::getId, id)
|
|
|
- .set(LawyerConsultationOrder::getOrderStatus, cancelStatus)
|
|
|
- .set(LawyerConsultationOrder::getUpdatedTime, new Date());
|
|
|
+ // 5. 根据订单状态更新为相应状态
|
|
|
+ Integer targetStatus = null;
|
|
|
+ if (waitAcceptStatus.equals(orderStatus)) {
|
|
|
+ // 待接单状态:更新为已退款状态
|
|
|
+ targetStatus = refundedStatus;
|
|
|
+ } else if (waitPayStatus.equals(orderStatus)) {
|
|
|
+ // 待支付状态:更新为已取消状态
|
|
|
+ targetStatus = cancelStatus;
|
|
|
+ } else {
|
|
|
+ log.warn("取消订单失败:订单状态不允许取消,订单ID={}, 订单编号={}, 订单状态={}",
|
|
|
+ id, orderNumber, orderStatus);
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
+ // 6. 更新订单状态
|
|
|
+ LambdaUpdateWrapper<LawyerConsultationOrder> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ updateWrapper.eq(LawyerConsultationOrder::getId, id)
|
|
|
+ .set(LawyerConsultationOrder::getOrderStatus, targetStatus)
|
|
|
+ .set(LawyerConsultationOrder::getUpdatedTime, new Date());
|
|
|
+
|
|
|
int updateCount = consultationOrderMapper.update(null, updateWrapper);
|
|
|
boolean success = updateCount > 0;
|
|
|
|
|
|
+ // 7. 记录操作结果
|
|
|
if (success) {
|
|
|
- log.info("取消订单成功,订单ID={}, 订单编号={}, 原状态={}", id, order.getOrderNumber(), orderStatus);
|
|
|
+ log.info("取消订单成功,订单ID={}, 订单编号={}, 原状态={}, 新状态={}",
|
|
|
+ id, orderNumber, orderStatus, targetStatus);
|
|
|
} else {
|
|
|
- log.error("取消订单失败:更新数据库失败,订单ID={}, 订单编号={}", id, order.getOrderNumber());
|
|
|
+ log.error("取消订单失败:更新数据库失败,订单ID={}, 订单编号={}, 原状态={}",
|
|
|
+ id, orderNumber, orderStatus);
|
|
|
}
|
|
|
|
|
|
return success;
|