From d6d06b62f76cb972bb220035401520e100cb1a35 Mon Sep 17 00:00:00 2001
From: xin <1099200748@qq.com>
Date: Tue, 19 Aug 2025 21:40:47 +0800
Subject: [PATCH] 取消订单校验优化
---
oying-system/src/main/java/com/oying/modules/sh/service/impl/OrderServiceImpl.java | 77 ++++++++++++++++++++++++++++----------
1 files changed, 57 insertions(+), 20 deletions(-)
diff --git a/oying-system/src/main/java/com/oying/modules/sh/service/impl/OrderServiceImpl.java b/oying-system/src/main/java/com/oying/modules/sh/service/impl/OrderServiceImpl.java
index 7a18e61..27ef9c0 100644
--- a/oying-system/src/main/java/com/oying/modules/sh/service/impl/OrderServiceImpl.java
+++ b/oying-system/src/main/java/com/oying/modules/sh/service/impl/OrderServiceImpl.java
@@ -8,6 +8,7 @@
import com.oying.modules.pc.product.domain.enums.ProductStatusEnum;
import com.oying.modules.pc.product.service.ProductService;
import com.oying.modules.pc.store.domain.Store;
+import com.oying.modules.pc.store.domain.enums.StoreStatusEnum;
import com.oying.modules.pc.store.service.StoreService;
import com.oying.modules.sh.domain.*;
import com.oying.modules.sh.domain.request.GeneratorOrder;
@@ -53,7 +54,9 @@
private final RedisUtils redisUtils;
private final StoreService storeService;
private final OrderOperationLogService operationLogService;
- private final static String DESCRIBE = "哦应:";
+ private static final String DESCRIBE = "哦应:";
+ private static final String ORDER_KEY = "oying:order";
+ private static final String ORDER_STORE_KEY = "oying:order:store";
@Override
public PageResult<Order> queryAll(OrderQueryCriteria criteria, Page<Object> page) {
@@ -78,13 +81,22 @@
default:
throw new BadRequestException("支付类型暂未开放");
}
- String orderNum = redisUtils.generateOrderSn(GenerateEnum.ORDER.getKey());
- // 总金额
- BigDecimal amount = BigDecimal.ZERO;
GeneratorOrder generator = new GeneratorOrder();
generator.setProducts(submit.getProducts());
generator.setStoreId(submit.getStoreId());
OrderInfo info = generatorOrder(generator);
+ Store store = info.getStore();
+ UserAddress address = userAddressService.getById(submit.getAddressId(), store.getLongitude(), store.getLatitude());
+ if (address == null) {
+ throw new BadRequestException("没找到用户地址");
+ }
+ if (!(address.getDistance().compareTo(BigDecimal.valueOf(store.getRadius())) <= 0)) {
+ throw new BadRequestException("超出配送范围");
+ }
+ // 订单号
+ String orderNum = redisUtils.generateSn(ORDER_KEY, GenerateEnum.ORDER.getKey());
+ // 总金额
+ BigDecimal amount = BigDecimal.ZERO;
// 商品快照
List<OrderProductSnapshot> snapshots = new ArrayList<>();
for (ProductInfo productInfo : info.getProducts()) {
@@ -116,16 +128,10 @@
if (openid == null || openid.isEmpty()) {
throw new BadRequestException("OPENID错误");
}
-
- // 门店信息
- Store store = storeService.getById(submit.getStoreId());
- if (amount.compareTo(store.getDeliveryMinimum()) >= 0) {
- throw new BadRequestException("起送金额:" + store.getDeliveryMinimum());
- }
// 订单信息
Order order = new Order();
order.setOrderNum(orderNum);
- order.setOrderStoreNum(redisUtils.generateOrderSn(Math.toIntExact(submit.getStoreId())).substring(orderNum.length() - 4));
+ order.setOrderStoreNum(redisUtils.generateSn(ORDER_STORE_KEY, Math.toIntExact(submit.getStoreId())).substring(orderNum.length() - 4));
order.setOrderStatus(OrderStatusEnum.ZERO.getKey());
order.setOrderStatusDescribe(OrderStatusEnum.ZERO.getValue());
order.setOrderRemark(submit.getRemark() != null ? submit.getRemark() : "");
@@ -167,9 +173,8 @@
order.setPayMessage(PayStateEnum.SUCCESS.getValue());
order.setPayTime(DateUtil.localDateTimeFormat(now.toLocalDateTime(), DateUtil.SDF_YMDHMS));
}
- UserAddress address = userAddressService.getById(submit.getAddressId());
- OrderAddressSnapshot addressSnapshot = getOrderAddressSnapshot(orderNum, address);
+ OrderAddressSnapshot addressSnapshot = getOrderAddressSnapshot(orderNum, address);
addressSnapshotService.save(addressSnapshot);
orderMapper.insert(order);
@@ -202,8 +207,14 @@
@Override
@Transactional(rollbackFor = Exception.class)
public synchronized OrderInfo generatorOrder(GeneratorOrder criteria) {
+ // 门店信息
+ Store store = storeService.getById(criteria.getStoreId());
+ if (!store.getBusinessStatus().equals(StoreStatusEnum.OPEN.getValue())) {
+ throw new BadRequestException("停止营业");
+ }
List<ProductInfo> products = new ArrayList<>();
BigDecimal amount = BigDecimal.ZERO;
+ int count = 0;
for (ProductOrder productOrder : criteria.getProducts()) {
Product product = productService.getById(productOrder.getProductId());
if (product == null) {
@@ -228,13 +239,20 @@
info.setCount(productOrder.getCount());
products.add(info);
amount = BigDecimalUtils.add(amount, BigDecimalUtils.multiply(product.getPrice(), productOrder.getCount()));
+ count += productOrder.getCount();
+ }
+ if (!(amount.compareTo(store.getDeliveryMinimum()) >= 0)) {
+ throw new BadRequestException("起送金额:" + store.getDeliveryMinimum());
}
OrderInfo info = new OrderInfo();
+ info.setStore(store);
info.setProducts(products);
info.setAmount(amount);
info.setPayAmount(amount);
info.setPromotionAmount(BigDecimal.ZERO);
- info.setUserAddresses(userAddressService.queryUserAddress());
+ info.setDeliveryPrice(store.getDeliveryFee());
+ info.setPackagingPrice(BigDecimalUtils.multiply(store.getPackagingFee(), count));
+ info.setUserAddresses(userAddressService.queryUserAddress(store.getLongitude(), store.getLatitude()));
return info;
}
@@ -249,13 +267,19 @@
public OrderResponse getByOrderNum(String orderNum) {
return new OrderResponse(orderMapper.getByOrderNum(orderNum),
addressSnapshotService.queryByOrderNum(orderNum),
- operationLogService.getByOrderNum(orderNum));
+ operationLogService.getByOrderNum(orderNum, null));
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public Order queryByOrderNum(String orderNum) {
+ return orderMapper.getByOrderNum(orderNum);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void operationLog(OrderResponse order, OrderStatusEnum stateEnum, String cardName) {
- operationLogService.create(order, stateEnum, null);
+ operationLogService.create(order, stateEnum, cardName);
}
@Override
@@ -266,11 +290,11 @@
@Override
@Transactional(rollbackFor = Exception.class)
- public void closeOrder(String orderNum) {
+ public synchronized void cancel(String orderNum) {
OrderResponse response = getByOrderNum(orderNum);
Order order = response.getOrder();
if (order == null) {
- throw new BadRequestException("订单不存在");
+ throw new BadRequestException("订单不存在!");
}
if (!SecurityUtils.getCurrentUserId().equals(order.getUserId())) {
throw new BadRequestException("不能修改他人订单");
@@ -283,7 +307,7 @@
case HWC:
case HWC2:
JSONObject object = swiftPassService.query(orderNum, anEnum);
- if (object.getString("trade_state").equals(PayStateEnum.NOTPAY.getKey())) {
+ if (!object.getString("trade_state").equals(PayStateEnum.NOTPAY.getKey())) {
throw new BadRequestException(PayStateEnum.getValue(order.getPayState()));
}
swiftPassService.closeOrder(orderNum, anEnum);
@@ -311,6 +335,19 @@
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteAll(List<Long> ids) {
+ Long userid = SecurityUtils.getCurrentUserId();
+ for (Long id : ids) {
+ Order order = getById(id);
+ if (order == null) {
+ throw new BadRequestException("订单不存在!");
+ }
+ if (!order.getUserId().equals(userid)) {
+ throw new BadRequestException("不能删除他人订单");
+ }
+ if (!order.getPayState().equals(PayStateEnum.NOTPAY.getKey())) {
+ throw new BadRequestException("只能删除未支付订单");
+ }
+ }
orderMapper.deleteBatchIds(ids);
}
@@ -331,7 +368,7 @@
map.put("消息", order.getPayMessage());
map.put("支付类型", order.getPayType());
map.put("支付时间", order.getPayTime());
- map.put("订单失效时间RFC3339", order.getExpireTime());
+ map.put("订单失效时间", order.getExpireTime());
map.put("openid", order.getOpenid());
map.put("APPID", order.getAppId());
map.put("创建人", order.getCreateBy());
--
Gitblit v1.9.3