Bladeren bron

订单信息

唐tag 1 jaar geleden
bovenliggende
commit
7cdb345019

+ 42 - 0
src/main/java/com/zswl/dataservicestarter/dao/DataServiceOrderInfoDao.java

@@ -2,6 +2,9 @@ package com.zswl.dataservicestarter.dao;
 
 import com.zswl.dataservicestarter.dao.base.MongoDao;
 import com.zswl.dataservicestarter.domain.DataServiceOrderInfo;
+import com.zswl.dataservicestarter.type.DataServiceOrderType;
+
+import java.util.List;
 
 /**
  * 订单信息Dao
@@ -13,4 +16,43 @@ public interface DataServiceOrderInfoDao extends MongoDao<DataServiceOrderInfo>
     DataServiceOrderInfo findTopByHashCode(int hashCode);
 
     DataServiceOrderInfo findTopByOrderNo(String orderNo);
+
+    /**
+     * 查询未同步到商铺的数据
+     *
+     * @param orderType
+     * @param isSync2Shop
+     * @return
+     */
+    List<DataServiceOrderInfo> findTop100ByOrderTypeAndIsSync2ShopOrderByCreateTimeAsc(DataServiceOrderType orderType, Boolean isSync2Shop);
+
+    /**
+     * 统计未同步到商铺的数据
+     *
+     * @param orderType
+     * @param isSync2Shop
+     * @return
+     */
+    long countByOrderTypeAndIsSync2Shop(DataServiceOrderType orderType, Boolean isSync2Shop);
+
+    /**
+     * 查询未同步到平台的数据
+     *
+     * @param orderType
+     * @param isSync2Platform
+     * @return
+     */
+    List<DataServiceOrderInfo> findTop100ByOrderTypeAndIsSync2PlatformOrderByCreateTimeAsc(DataServiceOrderType orderType, Boolean isSync2Platform);
+
+    /**
+     * 统计未同步到平台的数据
+     *
+     * @param orderType
+     * @param isSync2Platform
+     * @return
+     */
+    long countByOrderTypeAndIsSync2Platform(DataServiceOrderType orderType, Boolean isSync2Platform);
+
+    List<DataServiceOrderInfo> findByOrderNoIn(List<String> orderNos);
+
 }

+ 1 - 1
src/main/java/com/zswl/dataservicestarter/domain/DataServiceOrderInfo.java

@@ -30,7 +30,7 @@ public class DataServiceOrderInfo extends DataServiceSuperEntity {
     private Integer hashCode;
 
     /**
-     * 订单类型 平台  商铺
+     * 订单类型 平台:需要同步到商铺  商铺:需要同步到平台
      */
     private DataServiceOrderType orderType;
 

+ 26 - 0
src/main/java/com/zswl/dataservicestarter/model/SyncDataModel.java

@@ -0,0 +1,26 @@
+package com.zswl.dataservicestarter.model;
+
+import com.zswl.dataservicestarter.domain.DataServiceOrderInfo;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+/**
+ * @author TRX
+ * @date 2024/3/27
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class SyncDataModel {
+    /**
+     * 数据列表
+     */
+    List<DataServiceOrderInfoModel> list;
+    /**
+     * 总数
+     */
+    long totalElements = 0;
+}

+ 135 - 5
src/main/java/com/zswl/dataservicestarter/service/DataServiceOrderInfoService.java

@@ -9,18 +9,27 @@ import com.zswl.dataservicestarter.domain.DataServicePaymentInfo;
 import com.zswl.dataservicestarter.model.DataServiceGoodsModel;
 import com.zswl.dataservicestarter.model.DataServiceOrderInfoModel;
 import com.zswl.dataservicestarter.model.DataServicePaymentInfoModel;
+import com.zswl.dataservicestarter.model.SyncDataModel;
 import com.zswl.dataservicestarter.type.DataServiceOrderType;
+import com.zswl.dataservicestarter.type.PlatFormNewDataEvent;
+import com.zswl.dataservicestarter.type.ShopNewDataEvent;
 import com.zswl.dataservicestarter.utils.CommonUtil;
 import com.zswl.dataservicestarter.utils.result.ResultContent;
+import lombok.SneakyThrows;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 /**
  * 订单信息 管理
@@ -40,6 +49,9 @@ public class DataServiceOrderInfoService {
     @Autowired
     DataServicePaymentInfoDao paymentInfoDao;
 
+    @Autowired
+    ApplicationContext applicationContext;
+
     /**
      * 从平台同步订单信息到 数据服务
      *
@@ -119,9 +131,69 @@ public class DataServiceOrderInfoService {
 
         orderInfoDao.save(orderInfo);
 
+        // 发送需要商铺同步数据的事件
+        this.sendShopEventData();
         return ResultContent.buildSuccess(hasCode);
     }
 
+    /**
+     * 得到未同步到平台的数据  同步成功后调用 markSynced2Platform
+     *
+     * @return
+     */
+    public ResultContent<SyncDataModel> getTop100NotSyncPlatformDatas() {
+        SyncDataModel model = new SyncDataModel();
+        // 查询来源是商铺的数据  未同步
+        List<DataServiceOrderInfo> orderInfos = orderInfoDao.findTop100ByOrderTypeAndIsSync2ShopOrderByCreateTimeAsc(DataServiceOrderType.Shop, Boolean.FALSE);
+        long totalElements = orderInfoDao.countByOrderTypeAndIsSync2Shop(DataServiceOrderType.Shop, Boolean.FALSE);
+        List<DataServiceOrderInfoModel> list = new ArrayList<>();
+        if (ObjectUtils.isNotEmpty(orderInfos)) {
+            list = orderInfos.stream().map(this::toModel).collect(Collectors.toList());
+        }
+        model.setList(list);
+        model.setTotalElements(totalElements);
+        return ResultContent.buildSuccess(model);
+    }
+
+    /**
+     * 查询未同步到渠道/商铺的数据 同步后要调用 markSynced2Shop
+     *
+     * @return
+     */
+    public ResultContent<SyncDataModel> getTop100NotSyncShopDatas() {
+        SyncDataModel model = new SyncDataModel();
+        // 查询来源是平台的数据 未同步到商铺
+        List<DataServiceOrderInfo> orderInfos = orderInfoDao.findTop100ByOrderTypeAndIsSync2PlatformOrderByCreateTimeAsc(DataServiceOrderType.Platform, Boolean.FALSE);
+        long totalElements = orderInfoDao.countByOrderTypeAndIsSync2Platform(DataServiceOrderType.Platform, Boolean.FALSE);
+        List<DataServiceOrderInfoModel> list = new ArrayList<>();
+        if (ObjectUtils.isNotEmpty(orderInfos)) {
+            list = orderInfos.stream().map(this::toModel).collect(Collectors.toList());
+        }
+        model.setList(list);
+        model.setTotalElements(totalElements);
+        return ResultContent.buildSuccess(model);
+    }
+
+    public ResultContent sendPlatformEventData() {
+        PlatFormNewDataEvent testEvent = new PlatFormNewDataEvent(this);
+        applicationContext.publishEvent(testEvent);
+        return ResultContent.buildSuccess();
+    }
+
+    public ResultContent sendShopEventData() {
+        ShopNewDataEvent testEvent = new ShopNewDataEvent(this);
+        applicationContext.publishEvent(testEvent);
+        return ResultContent.buildSuccess();
+    }
+
+    @EventListener(classes = PlatFormNewDataEvent.class)
+    @Async
+    @SneakyThrows
+    public void test(PlatFormNewDataEvent testEvent) {
+        TimeUnit.SECONDS.sleep(10);
+        System.out.println("消息来了");
+    }
+
     /**
      * 根据订单号查询订单信息
      *
@@ -137,6 +209,7 @@ public class DataServiceOrderInfoService {
         return ResultContent.buildSuccess(orderInfo);
     }
 
+
     /**
      * 更加HashCode验证支付信息是否存在
      *
@@ -152,14 +225,71 @@ public class DataServiceOrderInfoService {
     }
 
     /**
-     * 编辑信息是否同步到平台
+     * 编辑已同步到 商铺
      *
-     * @param dataId
+     * @param orderNos
      * @return
      */
-    public ResultContent markSyncPlatform(String dataId) {
-        int a = 100;
-        return ResultContent.buildSuccess();
+    public ResultContent<List<String>> markSynced2Shop(List<String> orderNos) {
+        List<String> syncedOrderNos = new ArrayList<>();
+        if (ObjectUtils.isNotEmpty(orderNos)) {
+            List<DataServiceOrderInfo> saveList = new ArrayList<>();
+            List<DataServiceOrderInfo> list = orderInfoDao.findByOrderNoIn(orderNos);
+            for (DataServiceOrderInfo orderInfo : list) {
+                // 未同步的
+                Boolean isSync2Shop = orderInfo.getIsSync2Shop();
+                if (isSync2Shop == null || !isSync2Shop) {
+                    orderInfo.setIsSync2Shop(Boolean.TRUE);
+                    orderInfo.setSyncTime(System.currentTimeMillis());
+                    saveList.add(orderInfo);
+                }
+            }
+            if (ObjectUtils.isNotEmpty(saveList)) {
+                syncedOrderNos = saveList.stream().map(it -> it.getOrderNo()).collect(Collectors.toList());
+            }
+        }
+        return ResultContent.buildSuccess(syncedOrderNos);
+    }
+
+    /**
+     * 编辑已同步到 商铺
+     *
+     * @param orderNos
+     * @return
+     */
+    public ResultContent<List<String>> markSynced2Platform(List<String> orderNos) {
+        List<String> syncedOrderNos = new ArrayList<>();
+        if (ObjectUtils.isNotEmpty(orderNos)) {
+            List<DataServiceOrderInfo> saveList = new ArrayList<>();
+            List<DataServiceOrderInfo> list = orderInfoDao.findByOrderNoIn(orderNos);
+            for (DataServiceOrderInfo orderInfo : list) {
+                // 未同步的
+                Boolean isSync2Platform = orderInfo.getIsSync2Platform();
+                if (isSync2Platform == null || !isSync2Platform) {
+                    orderInfo.setIsSync2Platform(Boolean.TRUE);
+                    orderInfo.setSyncTime(System.currentTimeMillis());
+                    saveList.add(orderInfo);
+                }
+            }
+            if (ObjectUtils.isNotEmpty(saveList)) {
+                syncedOrderNos = saveList.stream().map(it -> it.getOrderNo()).collect(Collectors.toList());
+            }
+        }
+        return ResultContent.buildSuccess(syncedOrderNos);
+    }
+
+    /**
+     * 订单信息 转Model
+     *
+     * @param orderInfo
+     * @return
+     */
+    public DataServiceOrderInfoModel toModel(DataServiceOrderInfo orderInfo) {
+        DataServiceOrderInfoModel model = new DataServiceOrderInfoModel();
+        if (ObjectUtils.isNotEmpty(orderInfo)) {
+            com.zswl.dataservicestarter.utils.BeanUtils.copyProperties(orderInfo, model);
+        }
+        return model;
     }
 
 }

+ 0 - 22
src/main/java/com/zswl/dataservicestarter/service/DataServicePaymentInfoService.java

@@ -1,22 +0,0 @@
-package com.zswl.dataservicestarter.service;
-
-import com.zswl.dataservicestarter.dao.DataServicePaymentInfoDao;
-import com.zswl.dataservicestarter.domain.DataServiceOrderInfo;
-import com.zswl.dataservicestarter.domain.DataServicePaymentInfo;
-import com.zswl.dataservicestarter.utils.result.ResultContent;
-import org.apache.commons.lang3.ObjectUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-/**
- * 支付信息管理
- *
- * @author TRX
- * @date 2024/3/25
- */
-@Service
-public class DataServicePaymentInfoService {
-    @Autowired
-    DataServicePaymentInfoDao paymentInfoDao;
-
-}

+ 18 - 0
src/main/java/com/zswl/dataservicestarter/type/PlatFormNewDataEvent.java

@@ -0,0 +1,18 @@
+package com.zswl.dataservicestarter.type;
+
+import lombok.Getter;
+import org.springframework.context.ApplicationEvent;
+
+/**
+ * 有平台需要同步的新数据
+ * 规则就是    平台同步过来的数据 需要同步到商铺
+ * 商铺得到的数据需要同步到 平台
+ *
+ * @Author TRX
+ * @Version: 1.0
+ */
+public class PlatFormNewDataEvent extends ApplicationEvent {
+    public PlatFormNewDataEvent(Object source) {
+        super(source);
+    }
+}

+ 15 - 0
src/main/java/com/zswl/dataservicestarter/type/ShopNewDataEvent.java

@@ -0,0 +1,15 @@
+package com.zswl.dataservicestarter.type;
+
+import org.springframework.context.ApplicationEvent;
+
+/**
+ * 有商铺需要同步的新数据
+ *
+ * @Author TRX
+ * @Version: 1.0
+ */
+public class ShopNewDataEvent extends ApplicationEvent {
+    public ShopNewDataEvent(Object source) {
+        super(source);
+    }
+}