1. 拆分接口
This commit is contained in:
2026-03-23 11:04:01 +08:00
parent a57e208e7a
commit 65dfe51f96
2 changed files with 173 additions and 148 deletions

View File

@@ -86,7 +86,7 @@ public class APPMallController {
*/
@PostMapping("/create-order")
@ApiOperationSupport(order = 4)
@Operation(summary = "下单", description = "传入CreateOrderRequest")
@Operation(summary = "下单", description = "传入CreateMallOrderRequest")
public R<CreateOrderResponse> createOrder(@Valid @RequestBody CreateMallOrderRequest request) {
return R.data(orderService.createMallOrder(request));
}

View File

@@ -165,8 +165,9 @@ public class CommonUserOrderServiceImpl extends ServiceImpl<CommonUserOrderMappe
if (!request.getIsOfflinePayment()) {
discount = BigDecimal.valueOf(new Random().nextInt(99) + 1, 2);
}
return switch (BusinessModule.fromCodeIgnoreCaseOrNull(orderType)) {
case SELF_OPERATED_MALL -> {
if (BusinessModule.fromCodeIgnoreCaseOrNull(orderType).equals(BusinessModule.SELF_OPERATED_MALL)){
throw new ServiceException("无效订单");
}
if (request.getConsigneePhone() == null){
throw new ServiceException("请填写联系电话");
}
@@ -188,132 +189,156 @@ public class CommonUserOrderServiceImpl extends ServiceImpl<CommonUserOrderMappe
order.setPaymentType(request.getIsOfflinePayment() ? request.getPaymentType() : null);
order.setCreateTime(new Date());
this.save(order);
yield new CreateOrderResponse(String.valueOf(order.getId()), actualAmount, totalAmount, discount);
}
case TRAVEL_GROUP -> {
Long productId;
try {
productId = Long.valueOf(request.getItemCode());
} catch (NumberFormatException e) {
throw new ServiceException("商品ID格式非法");
}
if (request.getPaymentType() != 1 && request.getPaymentType() != 2){
throw new ServiceException("请选择正确的支付方式");
}
List<Long> travelerIds = request.getTourInfoIds();
if (CollectionUtils.isEmpty(travelerIds)) {
throw new ServiceException("请至少选择一名出行人");
}
if (request.getTourStartDate() == null || request.getTourEndDate() == null){
throw new ServiceException("请选择出行时间");
}
if (request.getTourStartDate().getTime() >= request.getTourEndDate().getTime()){
throw new ServiceException("开始时间不能大于结束时间");
}
TourProductEntity tourProduct = tourProductService.getById(productId);
if (tourProduct == null || tourProduct.getStatus() != 1) { // 假设 1=上架
throw new ServiceException("该旅游产品已下架或不存在");
}
int selectedCount = travelerIds.size();
// 使用 Optional 处理数据库中可能为 NULL 的最大/最小人数限制
int minPeople = Optional.ofNullable(tourProduct.getMinPeople()).orElse(1);
int maxPeople = Optional.ofNullable(tourProduct.getMaxPeople()).orElse(9999);
if (selectedCount < minPeople || selectedCount > maxPeople) {
throw new ServiceException(String.format("出行人数超出限制:最少 %d 人,最多 %d 人 (当前选择 %d 人)",
minPeople, maxPeople, selectedCount));
}
// 3. 【核心安全】查询出行人信息 (增加 userId 校验,防止越权)
// 原代码未校验 userId,恶意用户可遍历 ID 盗用他人信息下单
List<CommonTourInfoEntity> travelerList = commonTourInfoService.list(
Wrappers.lambdaQuery(CommonTourInfoEntity.class)
.in(CommonTourInfoEntity::getId, travelerIds)
.eq(CommonTourInfoEntity::getUserId, AuthUtil.getUserId())
.orderByDesc(CommonTourInfoEntity::getId)
);
// 校验是否所有请求的 ID 都成功查出(防止传入不存在的 ID 或他人的 ID)
if (travelerList.size() != selectedCount) {
throw new ServiceException("部分出行人信息无效或无权选择,请重新选择");
}
// 4. 【金额计算】安全累加与证件校验
BigDecimal totalAmount = BigDecimal.ZERO;
// 提前获取价格并兜底为 0,防止数据库脏数据导致 NPE
BigDecimal adultPrice = Optional.ofNullable(tourProduct.getAdultPrice()).orElse(BigDecimal.ZERO);
BigDecimal childPrice = Optional.ofNullable(tourProduct.getChildPrice()).orElse(BigDecimal.ZERO);
BigDecimal elderPrice = Optional.ofNullable(tourProduct.getBasePrice()).orElse(BigDecimal.ZERO);
for (CommonTourInfoEntity traveler : travelerList) {
Integer type = Optional.ofNullable(traveler.getTravelerType()).orElse(1); // 默认成人
// 累加金额
totalAmount = switch (type) {
case 1 -> totalAmount.add(adultPrice);
case 2 -> totalAmount.add(childPrice);
case 3 -> totalAmount.add(elderPrice);
default -> totalAmount.add(adultPrice); // 未知类型按成人算
};
// 证件合规性校验 (提取为独立方法,保持主流程清晰)
validateCertificates(traveler, tourProduct);
}
BigDecimal actualAmount = totalAmount.subtract(discount);
if (actualAmount.compareTo(BigDecimal.ZERO) < 0) {
actualAmount = BigDecimal.ZERO; // 防止实付金额为负
}
CommonUserOrderEntity order = AppCommonConvert.INSTANCE.convertTourProductToOrder(request, tourProduct);
order.setConsigneePhone(travelerList.get(0).getContactPhone());
order.setConsignee(travelerList.get(0).getUserName());
order.setUserId(AuthUtil.getUserId());
order.setOrderType(BusinessModule.TRAVEL_GROUP.getCode());
order.setTotalAmount(totalAmount);
order.setActualAmount(actualAmount);
order.setOrderType(orderType);
order.setPaymentType(request.getPaymentType());
order.setCreateTime(new Date());
this.save(order);
if (order.getId() == null) {
throw new ServiceException("订单创建失败:无法生成订单号");
}
// 7. 【构建订单】详情表 (快照)
TourOrderDetailEntity detail = new TourOrderDetailEntity();
detail.setOrderId(order.getId());
detail.setUserId(AuthUtil.getUserId());
detail.setStartDate(request.getTourStartDate());
detail.setEndDate(request.getTourEndDate());
detail.setRemark(request.getRemark());
detail.setContractStatus(0);
// 转换快照数据
detail.setTravelerDetail(AppCommonConvert.INSTANCE.convertTourInfoListToOrderDetail(travelerList));
detail.setProductSnapshot(AppCommonConvert.INSTANCE.convertTourProductListToOrderDetail(List.of(tourProduct)));
tourOrderDetailService.save(detail);
yield new CreateOrderResponse(String.valueOf(order.getId()), actualAmount, totalAmount, discount);
}
default -> throw new IllegalArgumentException("不支持的订单类型: " + request.getOrderType());
};
return new CreateOrderResponse(String.valueOf(order.getId()), actualAmount, totalAmount, discount);
// return switch (BusinessModule.fromCodeIgnoreCaseOrNull(orderType)) {
// case SELF_OPERATED_MALL -> {
// if (request.getConsigneePhone() == null){
// throw new ServiceException("请填写联系电话");
// }
//
// if (request.getConsignee() == null){
// throw new ServiceException("请填写联系人");
// }
//
// MallProductEntity product = productService.getById(Long.valueOf(request.getItemCode()));
// Assert.notNull(product, "无效商品");
// CommonUserOrderEntity order = AppCommonConvert.INSTANCE.convertMallProductToOrder(request, product);
// BigDecimal totalAmount = product.getRetailPrice().multiply(BigDecimal.valueOf(request.getQuantity())).setScale(2, RoundingMode.DOWN);
// BigDecimal actualAmount = totalAmount.subtract(discount);
// order.setOrderType(BusinessModule.SELF_OPERATED_MALL.getCode());
// order.setUserId(AuthUtil.getUserId());
// order.setTotalAmount(totalAmount);
// order.setActualAmount(actualAmount);
// order.setOrderType(orderType);
// order.setPaymentType(request.getIsOfflinePayment() ? request.getPaymentType() : null);
// order.setCreateTime(new Date());
// this.save(order);
// yield
// }
// case TRAVEL_GROUP -> {
//
// Long productId;
// try {
// productId = Long.valueOf(request.getItemCode());
// } catch (NumberFormatException e) {
// throw new ServiceException("商品ID格式非法");
// }
//
// if (request.getPaymentType() != 1 && request.getPaymentType() != 2){
// throw new ServiceException("请选择正确的支付方式");
// }
//
// List<Long> travelerIds = request.getTourInfoIds();
// if (CollectionUtils.isEmpty(travelerIds)) {
// throw new ServiceException("请至少选择一名出行人");
// }
//
// if (request.getTourStartDate() == null || request.getTourEndDate() == null){
// throw new ServiceException("请选择出行时间");
// }
//
// if (request.getTourStartDate().getTime() >= request.getTourEndDate().getTime()){
// throw new ServiceException("开始时间不能大于结束时间");
// }
//
// TourProductEntity tourProduct = tourProductService.getById(productId);
// if (tourProduct == null || tourProduct.getStatus() != 1) { // 假设 1=上架
// throw new ServiceException("该旅游产品已下架或不存在");
// }
//
//
//
//
// int selectedCount = travelerIds.size();
// // 使用 Optional 处理数据库中可能为 NULL 的最大/最小人数限制
// int minPeople = Optional.ofNullable(tourProduct.getMinPeople()).orElse(1);
// int maxPeople = Optional.ofNullable(tourProduct.getMaxPeople()).orElse(9999);
//
// if (selectedCount < minPeople || selectedCount > maxPeople) {
// throw new ServiceException(String.format("出行人数超出限制:最少 %d 人,最多 %d 人 (当前选择 %d 人)",
// minPeople, maxPeople, selectedCount));
// }
//
// // 3. 【核心安全】查询出行人信息 (增加 userId 校验,防止越权)
// // 原代码未校验 userId,恶意用户可遍历 ID 盗用他人信息下单
// List<CommonTourInfoEntity> travelerList = commonTourInfoService.list(
// Wrappers.lambdaQuery(CommonTourInfoEntity.class)
// .in(CommonTourInfoEntity::getId, travelerIds)
// .eq(CommonTourInfoEntity::getUserId, AuthUtil.getUserId())
// .orderByDesc(CommonTourInfoEntity::getId)
// );
//
// // 校验是否所有请求的 ID 都成功查出(防止传入不存在的 ID 或他人的 ID)
// if (travelerList.size() != selectedCount) {
// throw new ServiceException("部分出行人信息无效或无权选择,请重新选择");
// }
//
//
// // 4. 【金额计算】安全累加与证件校验
// BigDecimal totalAmount = BigDecimal.ZERO;
// // 提前获取价格并兜底为 0,防止数据库脏数据导致 NPE
// BigDecimal adultPrice = Optional.ofNullable(tourProduct.getAdultPrice()).orElse(BigDecimal.ZERO);
// BigDecimal childPrice = Optional.ofNullable(tourProduct.getChildPrice()).orElse(BigDecimal.ZERO);
// BigDecimal elderPrice = Optional.ofNullable(tourProduct.getBasePrice()).orElse(BigDecimal.ZERO);
//
// for (CommonTourInfoEntity traveler : travelerList) {
// Integer type = Optional.ofNullable(traveler.getTravelerType()).orElse(1); // 默认成人
//
// // 累加金额
// totalAmount = switch (type) {
// case 1 -> totalAmount.add(adultPrice);
// case 2 -> totalAmount.add(childPrice);
// case 3 -> totalAmount.add(elderPrice);
// default -> totalAmount.add(adultPrice); // 未知类型按成人算
// };
//
// // 证件合规性校验 (提取为独立方法,保持主流程清晰)
// validateCertificates(traveler, tourProduct);
// }
//
//
//
// BigDecimal actualAmount = totalAmount.subtract(discount);
// if (actualAmount.compareTo(BigDecimal.ZERO) < 0) {
// actualAmount = BigDecimal.ZERO; // 防止实付金额为负
// }
//
//
// CommonUserOrderEntity order = AppCommonConvert.INSTANCE.convertTourProductToOrder(request, tourProduct);
// order.setConsigneePhone(travelerList.get(0).getContactPhone());
// order.setConsignee(travelerList.get(0).getUserName());
// order.setUserId(AuthUtil.getUserId());
// order.setOrderType(BusinessModule.TRAVEL_GROUP.getCode());
// order.setTotalAmount(totalAmount);
// order.setActualAmount(actualAmount);
// order.setOrderType(orderType);
// order.setPaymentType(request.getPaymentType());
// order.setCreateTime(new Date());
//
// this.save(order);
// if (order.getId() == null) {
// throw new ServiceException("订单创建失败:无法生成订单号");
// }
//
// // 7. 【构建订单】详情表 (快照)
// TourOrderDetailEntity detail = new TourOrderDetailEntity();
// detail.setOrderId(order.getId());
// detail.setUserId(AuthUtil.getUserId());
// detail.setStartDate(request.getTourStartDate());
// detail.setEndDate(request.getTourEndDate());
// detail.setRemark(request.getRemark());
// detail.setContractStatus(0);
//
// // 转换快照数据
// detail.setTravelerDetail(AppCommonConvert.INSTANCE.convertTourInfoListToOrderDetail(travelerList));
// detail.setProductSnapshot(AppCommonConvert.INSTANCE.convertTourProductListToOrderDetail(List.of(tourProduct)));
//
// tourOrderDetailService.save(detail);
//
// yield new CreateOrderResponse(String.valueOf(order.getId()), actualAmount, totalAmount, discount);
// }
// default -> throw new IllegalArgumentException("不支持的订单类型: " + request.getOrderType());
// };
}
@Override