|
@@ -2,22 +2,25 @@ package com.emato.biz.service.impl;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
-import cn.hutool.http.HttpException;
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
import cn.hutool.http.HttpRequest;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.emato.biz.config.OmsConfig;
|
|
import com.emato.biz.config.OmsConfig;
|
|
|
|
+import com.emato.biz.domain.warehouse.Page;
|
|
import com.emato.biz.domain.warehouse.PullInveQueryVO;
|
|
import com.emato.biz.domain.warehouse.PullInveQueryVO;
|
|
import com.emato.biz.mapper.warehouse.WbInveMngMapper;
|
|
import com.emato.biz.mapper.warehouse.WbInveMngMapper;
|
|
|
|
+import com.emato.biz.util.JsonUtil;
|
|
|
|
+import com.emato.biz.util.OmsDataUtil;
|
|
import com.emato.common.core.domain.AjaxResult;
|
|
import com.emato.common.core.domain.AjaxResult;
|
|
import com.emato.common.exception.ServiceException;
|
|
import com.emato.common.exception.ServiceException;
|
|
import com.emato.common.utils.DateUtils;
|
|
import com.emato.common.utils.DateUtils;
|
|
import com.emato.common.utils.oms.JacksonUtils;
|
|
import com.emato.common.utils.oms.JacksonUtils;
|
|
-import com.emato.common.utils.oms.OkHttpUtils;
|
|
|
|
import com.emato.common.utils.oms.request.OmsSign;
|
|
import com.emato.common.utils.oms.request.OmsSign;
|
|
-import okhttp3.Request;
|
|
|
|
import org.apache.http.HttpStatus;
|
|
import org.apache.http.HttpStatus;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
@@ -27,6 +30,7 @@ import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import com.emato.biz.mapper.warehouse.OWbInveMngMapper;
|
|
import com.emato.biz.mapper.warehouse.OWbInveMngMapper;
|
|
@@ -53,6 +57,8 @@ public class OWbInveMngServiceImpl implements IOWbInveMngService
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(OWbInveMngServiceImpl.class);
|
|
private static final Logger log = LoggerFactory.getLogger(OWbInveMngServiceImpl.class);
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private ThreadPoolExecutor threadPoolExecutor;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 根据条件拉取 OMS 数据
|
|
* 根据条件拉取 OMS 数据
|
|
@@ -62,43 +68,51 @@ public class OWbInveMngServiceImpl implements IOWbInveMngService
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
public AjaxResult pullInveMng(PullInveQueryVO queryVO) {
|
|
public AjaxResult pullInveMng(PullInveQueryVO queryVO) {
|
|
- // 构建请求报文
|
|
|
|
- // 初始化请求 oms 参数 Map
|
|
|
|
- Map<String, String> reqOmsPram = new TreeMap<>();
|
|
|
|
- queryVO.setThirdMerchId("tmb770630141444423680");
|
|
|
|
- // 请求 oms 数据
|
|
|
|
- reqOmsPram.put("data", JacksonUtils.toJsonStr(queryVO));
|
|
|
|
- // 请求 oms 商户号
|
|
|
|
- reqOmsPram.put("merchId", OmsConfig.getMerchSn());
|
|
|
|
- log.debug("===== 请求 oms 商户号 ====> {}", OmsConfig.getMerchSn());
|
|
|
|
- // 请求 oms 时间戳
|
|
|
|
- String timestamp = System.currentTimeMillis()/1000 + "";
|
|
|
|
- reqOmsPram.put("timestamp", timestamp);
|
|
|
|
- log.debug("===== 请求 oms 时间戳 ====> {}", timestamp);
|
|
|
|
- // 请求 oms 签名
|
|
|
|
- String sign = OmsSign.sign(reqOmsPram, OmsConfig.getMd5Salt());
|
|
|
|
- reqOmsPram.put("sign", sign);
|
|
|
|
- // oms 请求地址
|
|
|
|
- String url = OmsConfig.getQueryMngInveUrl();
|
|
|
|
- log.debug("===== 请求 oms 地址 ====> {}", url);
|
|
|
|
- // 请求 oms 报文
|
|
|
|
- String reqBody = JacksonUtils.toJsonStr(reqOmsPram);
|
|
|
|
- log.debug("===== 请求 oms 报文 ====> {}", reqBody);
|
|
|
|
-
|
|
|
|
- // oms 返回数据
|
|
|
|
- String body = null;
|
|
|
|
- try {
|
|
|
|
- body = HttpRequest.post(url)
|
|
|
|
- .body(JacksonUtils.toJsonStr(reqOmsPram))
|
|
|
|
- .execute()
|
|
|
|
- .body();
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new ServiceException("请求 OMS 异常!");
|
|
|
|
- }
|
|
|
|
- log.debug("OMS 响应信息 => {}", body);
|
|
|
|
|
|
+ // 声明处理数量
|
|
|
|
+ AtomicInteger rows = new AtomicInteger(0);
|
|
|
|
+ // 声明页数
|
|
|
|
+ int page = 1;
|
|
|
|
+ queryVO.setPage(page);
|
|
|
|
+ queryVO.setLimit(10);
|
|
|
|
+
|
|
|
|
+ // 查询 OMS 分页数据
|
|
|
|
+ Optional<Page<OWbInveMng>> firstPageOpt =
|
|
|
|
+ OmsDataUtil.fetchOmsPageData(queryVO, OmsConfig.getQueryMngInveUrl(), OWbInveMng.class);
|
|
|
|
+
|
|
|
|
+ // 如果有数据
|
|
|
|
+ firstPageOpt.ifPresent(firstPage -> {
|
|
|
|
+ // 如果有数据,开始批量处理
|
|
|
|
+ firstPage.getList().forEach(obj -> log.debug("拉取的 sku => {}", obj.getSku()));
|
|
|
|
+ // 插入数据库
|
|
|
|
+ firstPage.getList().stream().forEach(obj -> {
|
|
|
|
+ // 插入数据库
|
|
|
|
+ wbInveMngMapper.saveOrUpdate(obj);
|
|
|
|
+ // 处理数据条数加 1
|
|
|
|
+ rows.getAndIncrement();
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ List<CompletableFuture<Void>> tasks = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ for(int pageNum = 2; pageNum <= firstPage.getTotalPage(); pageNum++) {
|
|
|
|
+ log.debug("当前拉取的页数 => {}", pageNum);
|
|
|
|
+ queryVO.setPage(pageNum);
|
|
|
|
+ // 分页拉取远程数据
|
|
|
|
+ Optional<Page<OWbInveMng>> pageOpt = OmsDataUtil.fetchOmsPageData(queryVO, OmsConfig.getQueryMngInveUrl(), OWbInveMng.class);
|
|
|
|
+ pageOpt.get().getList().forEach(obj -> {
|
|
|
|
+ wbInveMngMapper.saveOrUpdate(obj);
|
|
|
|
+ rows.getAndIncrement();
|
|
|
|
+ });
|
|
|
|
+ // 查询 OMS 数据
|
|
|
|
+ /*CompletableFuture<Void> task = CompletableFuture.runAsync(() -> {
|
|
|
|
+ }, threadPoolExecutor);
|
|
|
|
+ // 将任务添加到列表中
|
|
|
|
+ tasks.add(task);*/
|
|
|
|
+ }
|
|
|
|
+ CompletableFuture.allOf(tasks.toArray(new CompletableFuture[0])).join();
|
|
|
|
|
|
|
|
+ });
|
|
|
|
|
|
- // 获取响应,入库
|
|
|
|
|
|
+ log.debug("拉取库存成功,拉取数据条数 => {}", rows.get());
|
|
return AjaxResult.success("拉取库存成功!");
|
|
return AjaxResult.success("拉取库存成功!");
|
|
}
|
|
}
|
|
|
|
|
|
@@ -303,4 +317,63 @@ public class OWbInveMngServiceImpl implements IOWbInveMngService
|
|
return jsonResult;
|
|
return jsonResult;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 拉取 OMS 库存数据
|
|
|
|
+ *
|
|
|
|
+ * @param queryVO 请求 OMS 数据
|
|
|
|
+ * @param url 请求 OMS 地址
|
|
|
|
+ * @param clazz 返回的分页数据类
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private <T> Optional<Page<T>> fetchOmsPageData(PullInveQueryVO queryVO,
|
|
|
|
+ String url,
|
|
|
|
+ Class<T> clazz) {
|
|
|
|
+ // 构建请求报文
|
|
|
|
+ // 初始化请求 oms 参数 Map
|
|
|
|
+ Map<String, String> reqOmsPram = new TreeMap<>();
|
|
|
|
+ // 请求 oms 数据
|
|
|
|
+ reqOmsPram.put("===== data ====> {}", JacksonUtils.toJsonStr(queryVO));
|
|
|
|
+ // 请求 oms 商户号
|
|
|
|
+ reqOmsPram.put("merchId", OmsConfig.getMerchSn());
|
|
|
|
+ log.debug("===== 请求 oms 商户号 ====> {}", OmsConfig.getMerchSn());
|
|
|
|
+ // 请求 oms 时间戳
|
|
|
|
+ String timestamp = System.currentTimeMillis()/1000 + "";
|
|
|
|
+ reqOmsPram.put("timestamp", timestamp);
|
|
|
|
+ log.debug("===== 请求 oms 时间戳 ====> {}", timestamp);
|
|
|
|
+ // 请求 oms 签名
|
|
|
|
+ String sign = OmsSign.sign(reqOmsPram, OmsConfig.getMd5Salt());
|
|
|
|
+ reqOmsPram.put("sign", sign);
|
|
|
|
+ // oms 请求地址
|
|
|
|
+ log.debug("===== 请求 oms 地址 ====> {}", url);
|
|
|
|
+ // 请求 oms 报文
|
|
|
|
+ String reqBody = JacksonUtils.toJsonStr(reqOmsPram);
|
|
|
|
+ log.debug("===== 请求 oms 报文 ====> {}", reqBody);
|
|
|
|
+
|
|
|
|
+ // oms 返回数据
|
|
|
|
+ String body = null;
|
|
|
|
+ try {
|
|
|
|
+ body = HttpRequest.post(url)
|
|
|
|
+ .body(JacksonUtils.toJsonStr(reqOmsPram))
|
|
|
|
+ .execute()
|
|
|
|
+ .body();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new ServiceException("请求 OMS 异常!");
|
|
|
|
+ }
|
|
|
|
+ log.debug("OMS 响应信息 => {}", body);
|
|
|
|
+
|
|
|
|
+ // 解析返回的数据
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(body);
|
|
|
|
+ String code = jsonObject.getString("code");
|
|
|
|
+
|
|
|
|
+ if ("200".equals(code)){
|
|
|
|
+ Page<T> page = JsonUtil.parsePage(jsonObject, clazz);
|
|
|
|
+ if (!page.getList().isEmpty())
|
|
|
|
+ // 如果 page 有值就返回
|
|
|
|
+ return Optional.of(page);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return Optional.empty();
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|