From 7b46d1d38540333a59f7bba25af9d5aeabbc16bd Mon Sep 17 00:00:00 2001
From: xin <1099200748@qq.com>
Date: Tue, 15 Jul 2025 15:39:26 +0800
Subject: [PATCH] 配送类型

---
 oying-system/src/main/java/com/oying/modules/sh/service/impl/OrderServiceImpl.java |  107 ++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 81 insertions(+), 26 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 c185dc3..84a8c76 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
@@ -9,24 +9,20 @@
 import com.oying.modules.pc.product.service.ProductService;
 import com.oying.modules.pc.store.domain.Store;
 import com.oying.modules.pc.store.service.StoreService;
-import com.oying.modules.sh.domain.Order;
-import com.oying.modules.sh.domain.OrderProductSnapshot;
+import com.oying.modules.sh.domain.*;
 import com.oying.modules.sh.domain.request.GeneratorOrder;
 import com.oying.modules.sh.domain.request.ProductOrder;
+import com.oying.modules.sh.domain.request.StatusOrder;
 import com.oying.modules.sh.domain.request.SubmitOrder;
 import com.oying.modules.sh.domain.vo.OrderInfo;
+import com.oying.modules.sh.domain.vo.OrderResponse;
 import com.oying.modules.sh.domain.vo.ProductInfo;
-import com.oying.modules.sh.service.OrderProductSnapshotService;
-import com.oying.modules.sh.service.UserAddressService;
+import com.oying.modules.sh.service.*;
 import com.oying.utils.*;
-import com.oying.utils.enums.GenerateEnum;
-import com.oying.utils.enums.OrderStatusEnum;
-import com.oying.utils.enums.PayStateEnum;
-import com.oying.utils.enums.PayTypeEnum;
+import com.oying.utils.enums.*;
 import lombok.RequiredArgsConstructor;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.oying.modules.sh.service.OrderService;
 import com.oying.modules.sh.domain.dto.OrderQueryCriteria;
 import com.oying.modules.sh.mapper.OrderMapper;
 import org.springframework.stereotype.Service;
@@ -53,25 +49,34 @@
     private final ProductService productService;
     private final SwiftPassService swiftPassService;
     private final OrderProductSnapshotService productSnapshotService;
+    private final OrderAddressSnapshotService addressSnapshotService;
     private final RedisUtils redisUtils;
     private final StoreService storeService;
+    private final OrderOperationLogService operationLogService;
     private final static String DESCRIBE = "哦应:";
 
     @Override
     public PageResult<Order> queryAll(OrderQueryCriteria criteria, Page<Object> page) {
-        return PageUtil.toPage(orderMapper.findAll(criteria, page));
+        criteria.setOffset(page.offset());
+        List<Order> list = orderMapper.findAll(criteria, criteria.getBlurry());
+        Long total = orderMapper.countAll(criteria, criteria.getBlurry());
+        return PageUtil.toPage(list, total);
     }
 
     @Override
     public List<Order> queryAll(OrderQueryCriteria criteria) {
-        return orderMapper.findAll(criteria);
+        return orderMapper.findAll(criteria, criteria.getBlurry());
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
     public synchronized Order submitOrder(SubmitOrder submit, HttpServletRequest request) {
-        if (!submit.getPayType().equals(PayTypeEnum.HWC)) {
-            throw new BadRequestException("支付类型暂未开放");
+        switch (submit.getPayType()) {
+            case HWC:
+            case HWC2:
+                break;
+            default:
+                throw new BadRequestException("支付类型暂未开放");
         }
         String orderNum = redisUtils.generateOrderSn(GenerateEnum.ORDER.getKey());
         // 总金额
@@ -82,7 +87,6 @@
         OrderInfo info = generatorOrder(generator);
         // 商品快照
         List<OrderProductSnapshot> snapshots = new ArrayList<>();
-
         for (ProductInfo productInfo : info.getProducts()) {
             Product product = productInfo.getProduct();
             OrderProductSnapshot snapshot = new OrderProductSnapshot();
@@ -112,14 +116,24 @@
         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.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.setUserId(SecurityUtils.getCurrentUserId());
         order.setUsername(SecurityUtils.getCurrentUsername());
-        Store store = storeService.getMerchantStore(submit.getStoreId());
         order.setStoreId(submit.getStoreId());
         order.setStoreName(store.getStoreName());
         order.setStoreLogo(store.getLogoImageUrl());
@@ -150,9 +164,35 @@
             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);
+
+        addressSnapshotService.save(addressSnapshot);
         orderMapper.insert(order);
         productSnapshotService.saveBatch(snapshots);
+
+        order.setProductSnapshots(new HashSet<>(snapshots));
+        OrderResponse response = new OrderResponse(order, addressSnapshot, null);
+
+        operationLogService.create(response, OrderStatusEnum.ZERO);
         return order;
+    }
+
+    private static OrderAddressSnapshot getOrderAddressSnapshot(String orderNum, UserAddress address) {
+        OrderAddressSnapshot snapshot = new OrderAddressSnapshot();
+        snapshot.setOrderNum(orderNum);
+        snapshot.setReceiverName(address.getReceiverName());
+        snapshot.setReceiverPhone(address.getReceiverPhone());
+        snapshot.setProvince(address.getProvince() != null ? address.getProvince() : "");
+        snapshot.setCity(address.getCity() != null ? address.getCity() : "");
+        snapshot.setDistrict(address.getDistrict() != null ? address.getDistrict() : "");
+        snapshot.setStreet(address.getStreet() != null ? address.getStreet() : "");
+        snapshot.setShortAddress(address.getShortAddress());
+        snapshot.setDetail(address.getDetail());
+        snapshot.setLongitude(address.getLongitude());
+        snapshot.setLatitude(address.getLatitude());
+        snapshot.setTag(address.getTag());
+        return snapshot;
     }
 
     @Override
@@ -165,7 +205,7 @@
             if (product == null) {
                 throw new BadRequestException("商品不存在");
             }
-            if (product.getStoreId().equals(criteria.getStoreId())) {
+            if (!product.getStoreId().equals(criteria.getStoreId())) {
                 throw new BadRequestException("不支持跨门店下单");
             }
             if (!product.getStatus().equals(ProductStatusEnum.AVAILABLE.getValue())) {
@@ -196,13 +236,22 @@
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public Order getByOrderNum(String orderNum) {
-        return orderMapper.getByOrderNum(orderNum);
+    public void statusOrder(StatusOrder statusOrder) {
+        operationLog(getByOrderNum(statusOrder.getOrderNum()), statusOrder.getStatus());
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public void paySuccess(Order order) {
+    public OrderResponse getByOrderNum(String orderNum) {
+        return new OrderResponse(orderMapper.getByOrderNum(orderNum),
+                addressSnapshotService.queryByOrderNum(orderNum),
+                operationLogService.getByOrderNum(orderNum));
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void operationLog(OrderResponse order, OrderStatusEnum stateEnum) {
+        operationLogService.create(order, stateEnum);
     }
 
     @Override
@@ -214,31 +263,37 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public void closeOrder(String orderNum) {
-        Order order = getByOrderNum(orderNum);
+        OrderResponse response = getByOrderNum(orderNum);
+        Order order = response.getOrder();
         if (order == null) {
             throw new BadRequestException("订单不存在");
+        }
+        if (!SecurityUtils.getCurrentUserId().equals(order.getUserId())) {
+            throw new BadRequestException("不能修改他人订单");
+        }
+        if (!order.getPayState().equals(PayStateEnum.NOTPAY.getKey())) {
+            throw new BadRequestException(PayStateEnum.getValue(order.getPayState()));
         }
         PayTypeEnum anEnum = PayTypeEnum.find(order.getPayType());
         switch (anEnum) {
             case HWC:
             case HWC2:
                 JSONObject object = swiftPassService.query(orderNum, anEnum);
-                if (object.getString("trade_state").equals(PayStateEnum.SUCCESS.getKey())) {
-                    throw new BadRequestException("订单已支付");
-                }
-                if (object.getString("trade_state").equals(PayStateEnum.CLOSED.getKey())) {
-                    throw new BadRequestException("订单已关闭");
+                if (object.getString("trade_state").equals(PayStateEnum.NOTPAY.getKey())) {
+                    throw new BadRequestException(PayStateEnum.getValue(order.getPayState()));
                 }
                 swiftPassService.closeOrder(orderNum, anEnum);
                 break;
         }
-        orderMapper.updateCloseStatus(orderNum, PayStateEnum.CLOSED.getKey());
+        operationLogService.create(response, OrderStatusEnum.NINE);
+        orderMapper.updateCloseStatus(orderNum, PayStateEnum.CLOSED.getKey(), PayStateEnum.CLOSED.getValue());
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
     public void create(Order resources) {
         orderMapper.insert(resources);
+        throw new BadRequestException("未开放");
     }
 
     @Override

--
Gitblit v1.9.3