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

View File

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