xin
2025-09-22 6681b64ff05626c6f10ad69e49a87c3969b72bb8
oying-system/src/main/java/com/oying/modules/sh/service/impl/OrderServiceImpl.java
@@ -6,6 +6,7 @@
import com.oying.modules.hwc.service.SwiftPassService;
import com.oying.modules.pc.product.domain.Product;
import com.oying.modules.pc.product.domain.enums.ProductStatusEnum;
import com.oying.modules.pc.product.service.ProductInventoryService;
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;
@@ -54,7 +55,11 @@
    private final RedisUtils redisUtils;
    private final StoreService storeService;
    private final OrderOperationLogService operationLogService;
    private final static String DESCRIBE = "哦应:";
    private final ProductInventoryService productInventoryService;
    private static final String DESCRIBE = "哦应:";
    private static final String ORDER_KEY = "oying:order";
    private static final String ORDER_STORE_KEY = "oying:order:store";
    private static final String ORDER_CODE = "ORDER";
    @Override
    public PageResult<Order> queryAll(OrderQueryCriteria criteria, Page<Object> page) {
@@ -91,13 +96,13 @@
        if (!(address.getDistance().compareTo(BigDecimal.valueOf(store.getRadius())) <= 0)) {
            throw new BadRequestException("超出配送范围");
        }
        String orderNum = redisUtils.generateOrderSn(GenerateEnum.ORDER.getKey());
        // 总金额
        BigDecimal amount = BigDecimal.ZERO;
        // 订单号
        String orderNum = redisUtils.generateSn(ORDER_KEY, GenerateEnum.ORDER.getKey());
        // 商品快照
        List<OrderProductSnapshot> snapshots = new ArrayList<>();
        for (ProductInfo productInfo : info.getProducts()) {
            Product product = productInfo.getProduct();
            productInventoryService.decreaseStock(product.getProductId(), productInfo.getCount());
            OrderProductSnapshot snapshot = new OrderProductSnapshot();
            snapshot.setOrderNum(orderNum);
            snapshot.setStoreId(submit.getStoreId());
@@ -112,7 +117,6 @@
            snapshot.setOriginalPrice(product.getPrice());
            snapshot.setPaidPrice(product.getPrice());
            BigDecimal decimal = BigDecimalUtils.multiply(product.getPrice(), productInfo.getCount());
            amount = BigDecimalUtils.add(amount, decimal);
            snapshot.setActuallyPayPrice(decimal);
            snapshot.setPayState(PayStateEnum.NOTPAY.getKey());
            snapshots.add(snapshot);
@@ -128,13 +132,14 @@
        // 订单信息
        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() : "");
        order.setOrderTime(submit.getDateTime());
        order.setSendPrice(store.getDeliveryFee());
        order.setSendType(SendTypeEnum.LY.getKey());
        order.setPackagingPrice(info.getPackagingPrice());
        order.setUserId(SecurityUtils.getCurrentUserId());
        order.setUsername(SecurityUtils.getCurrentUsername());
        order.setStoreId(submit.getStoreId());
@@ -144,14 +149,14 @@
        order.setStoreLongitude(BigDecimal.valueOf(store.getLongitude()));
        order.setStoreLatitude(BigDecimal.valueOf(store.getLatitude()));
        order.setOrderDescribe(DESCRIBE + submit.getStoreId());
        order.setOriginalPrice(amount);
        order.setPaidPrice(amount);
        order.setActuallyPayPrice(amount);
        order.setOriginalPrice(info.getAmount());
        order.setPaidPrice(BigDecimalUtils.subtract(info.getAmount(), info.getPromotionAmount()));
        order.setActuallyPayPrice(info.getPayAmount());
        order.setOpenid(openid);
        order.setPayType(submit.getPayType().getKey());
        order.setExpireTime(expire);
        order.setOpenid(openid);
        Long total = BigDecimalUtils.yuanToCents(amount);
        Long total = BigDecimalUtils.yuanToCents(info.getAmount());
        if (total > 0) {
            HwcResponse response = swiftPassService.pay(StringUtils.getIp(request), total.toString(), expire,
                    DESCRIBE + submit.getStoreId(), openid, orderNum, submit.getPayType());
@@ -172,7 +177,6 @@
        }
        OrderAddressSnapshot addressSnapshot = getOrderAddressSnapshot(orderNum, address);
        addressSnapshotService.save(addressSnapshot);
        orderMapper.insert(order);
@@ -212,6 +216,7 @@
        }
        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) {
@@ -236,6 +241,7 @@
            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());
@@ -243,9 +249,15 @@
        OrderInfo info = new OrderInfo();
        info.setStore(store);
        info.setProducts(products);
        info.setPromotionAmount(BigDecimal.ZERO);
        info.setDeliveryPrice(store.getDeliveryFee());
        BigDecimal packagingPrice = BigDecimalUtils.multiply(store.getPackagingFee(), count);
        info.setPackagingPrice(packagingPrice);
        amount = BigDecimalUtils.add(amount, packagingPrice);
        amount = BigDecimalUtils.add(amount, store.getDeliveryFee());
        info.setAmount(amount);
        info.setPayAmount(amount);
        info.setPromotionAmount(BigDecimal.ZERO);
        info.setUserAddresses(userAddressService.queryUserAddress(store.getLongitude(), store.getLatitude()));
        return info;
    }
@@ -261,7 +273,13 @@
    public OrderResponse getByOrderNum(String orderNum) {
        return new OrderResponse(orderMapper.getByOrderNum(orderNum),
                addressSnapshotService.queryByOrderNum(orderNum),
                operationLogService.getByOrderNum(orderNum));
                operationLogService.getByOrderNum(orderNum, ORDER_CODE));
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Order queryByOrderNum(String orderNum) {
        return orderMapper.getByOrderNum(orderNum);
    }
    @Override
@@ -278,11 +296,11 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancel(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("不能修改他人订单");
@@ -295,7 +313,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);
@@ -323,6 +341,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);
    }