|
@@ -0,0 +1,119 @@
|
|
|
+package com.sd.wln.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.ListUtil;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.ruoyi.common.constant.StatusConstant;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.framework.mybatis.holder.LogicHolder;
|
|
|
+import com.sd.business.entity.order.po.OrderInfo;
|
|
|
+import com.sd.business.service.order.OrderInfoService;
|
|
|
+import com.sd.framework.util.TransactionUtil;
|
|
|
+import com.sd.wln.context.OrderContext;
|
|
|
+import com.sd.wln.entity.ResynchronizationDto;
|
|
|
+import com.sd.wln.service.OrderHandleService;
|
|
|
+import com.sd.wln.service.WlnOrderService;
|
|
|
+import com.sd.wln.util.WlnUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class OrderHandleServiceImpl implements OrderHandleService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrderInfoService orderInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WlnOrderService wlnOrderService;
|
|
|
+
|
|
|
+ @DSTransactional
|
|
|
+ @Override
|
|
|
+ public synchronized void resynchronization(ResynchronizationDto dto) {
|
|
|
+ JSONObject wlnOrder;
|
|
|
+ try {
|
|
|
+ wlnOrder = WlnUtil.getOrderByWlnCode(dto.getWlnCode());
|
|
|
+ } catch (ServiceException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("订单同步失败", e);
|
|
|
+ throw new ServiceException("订单同步失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ LogicHolder.setLogicHolder(true);
|
|
|
+ String uid = wlnOrder.getString("uid");
|
|
|
+ OrderInfo orderInfo = orderInfoService.getOne(q -> q.eq(OrderInfo::getWlnUid, uid));
|
|
|
+ if (orderInfo != null) {
|
|
|
+ if (Objects.equals(orderInfo.getDelFlag(), StatusConstant.NOT_DELETED)) {
|
|
|
+ throw new ServiceException("订单未被删除,无法重新同步");
|
|
|
+ }
|
|
|
+ orderInfoService.delete(orderInfo.getId());
|
|
|
+ }
|
|
|
+ LogicHolder.clear();
|
|
|
+
|
|
|
+ String storageCode = wlnOrder.getString("storage_code");
|
|
|
+ OrderContext context = new OrderContext(storageCode, Collections.singletonList(wlnOrder));
|
|
|
+ OrderInfo order = wlnOrderService.createOrder(context, wlnOrder);
|
|
|
+ wlnOrderService.addOrder(context, wlnOrder, order);
|
|
|
+ wlnOrderService.saveOrUpdateOrder(context);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void bathSyncOrder() {
|
|
|
+ List<OrderInfo> list = orderInfoService.list(q -> q.in(OrderInfo::getWlnStatus, 0, 1, 14));
|
|
|
+ Map<String, OrderInfo> orderInfoMap = list.stream().collect(Collectors.toMap(OrderInfo::getWlnCode, Function.identity()));
|
|
|
+ Map<String, List<OrderInfo>> map = list.stream().collect(Collectors.groupingBy(OrderInfo::getWlnStorageCode));
|
|
|
+
|
|
|
+ for (String warehouseCode : map.keySet()) {
|
|
|
+ List<JSONObject> wlnOrderList = new ArrayList<>();
|
|
|
+
|
|
|
+ // list切分,万里牛最多只能查询200条
|
|
|
+ List<List<OrderInfo>> split = ListUtil.split(map.get(warehouseCode), 200);
|
|
|
+ try {
|
|
|
+ for (List<OrderInfo> item : split) {
|
|
|
+ String wlnCodeListStr = item.stream().map(OrderInfo::getWlnCode).collect(Collectors.joining(","));
|
|
|
+ wlnOrderList.addAll(WlnUtil.getOrderListByWlnCodeList(wlnCodeListStr));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("订单手动同步失败", e);
|
|
|
+ throw new ServiceException("订单同步失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (wlnOrderList.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 初始化订单上下文
|
|
|
+ OrderContext context = new OrderContext(warehouseCode, wlnOrderList);
|
|
|
+
|
|
|
+ for (JSONObject wlnOrder : wlnOrderList) {
|
|
|
+ OrderInfo orderInfo = orderInfoMap.get(wlnOrder.getString("trade_no"));
|
|
|
+ if (Objects.equals(wlnOrder.getLong("modify_time"), orderInfo.getWlnModifyTimestamp())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ wlnOrderService.updateOrder(context, wlnOrder, orderInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存新增、修改的订单
|
|
|
+ try {
|
|
|
+ TransactionUtil.execute(() -> {
|
|
|
+ // 保存或更新数据
|
|
|
+ wlnOrderService.saveOrUpdateOrder(context);
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("同步订单失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|