|
|
@@ -110,60 +110,32 @@ public class ProductSupplyServiceImpl implements ProductSupplyService {
|
|
|
return Result.fail("获取token失败");
|
|
|
}
|
|
|
|
|
|
- int page = 1;
|
|
|
int pageSize = 50;
|
|
|
- int total = 0;
|
|
|
- boolean isFirstPage = true;
|
|
|
|
|
|
try {
|
|
|
- while (true) {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("page", page);
|
|
|
- params.put("pageSize", pageSize);
|
|
|
- //1-倒序 2-升序
|
|
|
- params.put("create_sort", 1);
|
|
|
-
|
|
|
- String resp = HttpUtil.restTemplatePost(
|
|
|
- prefixUrl + "/supplyapi/app/product/storage/getMyStorageIdsList",
|
|
|
- params,
|
|
|
- headers
|
|
|
- );
|
|
|
-
|
|
|
- JSONObject respJson = JSONObject.parseObject(resp);
|
|
|
- if (respJson.getIntValue("code") != 0) {
|
|
|
- log.error("syncToQiyun: 调用选品库列表接口失败,page={},msg={}", page, respJson.getString("msg"));
|
|
|
- return Result.fail("调用选品库列表接口失败: " + respJson.getString("msg"));
|
|
|
- }
|
|
|
-
|
|
|
- JSONObject data = respJson.getJSONObject("data");
|
|
|
- JSONArray array = data.getJSONArray("list");
|
|
|
-
|
|
|
- if (isFirstPage) {
|
|
|
- total = data.getIntValue("total");
|
|
|
- isFirstPage = false;
|
|
|
- }
|
|
|
-
|
|
|
- if (array != null && !array.isEmpty()) {
|
|
|
- if (!sendKafkaMessageWithRetry(array.toString())) {
|
|
|
- log.error("syncToQiyun: Kafka发送失败,page={},已重试3次", page);
|
|
|
- return Result.fail("Kafka发送失败,第" + page + "页数据发送失败");
|
|
|
- }
|
|
|
- log.info("syncToQiyun: 成功发送第{}页数据,共{}条", page, array.size());
|
|
|
- }
|
|
|
-
|
|
|
- int currentPage = data.getIntValue("page");
|
|
|
- int currentPageSize = data.getIntValue("pageSize");
|
|
|
- int nextPage = currentPage + 1;
|
|
|
+ // 查询第一页,获取总数并发送
|
|
|
+ JSONObject firstPageData = fetchStoragePage(1, pageSize, headers);
|
|
|
+ if (firstPageData == null) {
|
|
|
+ return Result.fail("调用选品库列表接口失败");
|
|
|
+ }
|
|
|
|
|
|
- if (nextPage * currentPageSize > total) {
|
|
|
- break;
|
|
|
- }
|
|
|
+ int total = firstPageData.getIntValue("total");
|
|
|
+ JSONArray array = firstPageData.getJSONArray("list");
|
|
|
+ if (array != null && !array.isEmpty()) {
|
|
|
+ sendKafkaMessageWithRetry(array.toString());
|
|
|
+ log.info("syncToQiyun: 成功发送第1页数据,共{}条", array.size());
|
|
|
+ }
|
|
|
|
|
|
- page = nextPage;
|
|
|
+ // 发送异步任务消息,由KafkaConsumer并发处理剩余分页
|
|
|
+ if (total > (array == null ? 0 : array.size())) {
|
|
|
+ JSONObject taskMsg = new JSONObject();
|
|
|
+ taskMsg.put("total", total);
|
|
|
+ taskMsg.put("pageSize", pageSize);
|
|
|
+ kafkaProducer.sendMessage("sync.qiyun.remain", taskMsg.toJSONString());
|
|
|
}
|
|
|
|
|
|
- log.info("syncToQiyun: 所有商品数据同步完成,总计{}条", total);
|
|
|
- return Result.success(true);
|
|
|
+ log.info("syncToQiyun: 同步任务已提交,总计{}条", total);
|
|
|
+ return Result.success("同步任务已提交,共" + total + "条数据将异步处理");
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
log.error("syncToQiyun: 执行异常", e);
|
|
|
@@ -171,7 +143,31 @@ public class ProductSupplyServiceImpl implements ProductSupplyService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private boolean sendKafkaMessageWithRetry(String message) {
|
|
|
+ /**
|
|
|
+ * 查询选品库指定分页
|
|
|
+ */
|
|
|
+ public JSONObject fetchStoragePage(int page, int pageSize, Map<String, String> headers) {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("page", page);
|
|
|
+ params.put("pageSize", pageSize);
|
|
|
+ params.put("create_sort", 1);
|
|
|
+
|
|
|
+ String resp = HttpUtil.restTemplatePost(
|
|
|
+ prefixUrl + "/supplyapi/app/product/storage/getMyStorageIdsList",
|
|
|
+ params,
|
|
|
+ headers
|
|
|
+ );
|
|
|
+
|
|
|
+ JSONObject respJson = JSONObject.parseObject(resp);
|
|
|
+ if (respJson == null || respJson.getIntValue("code") != 0) {
|
|
|
+ log.error("fetchStoragePage: 调用选品库列表接口失败,page={},msg={}", page,
|
|
|
+ respJson != null ? respJson.getString("msg") : "null");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return respJson.getJSONObject("data");
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean sendKafkaMessageWithRetry(String message) {
|
|
|
int maxRetries = 3;
|
|
|
|
|
|
for (int retryCount = 0; retryCount < maxRetries; retryCount++) {
|