解决同步订单时,每次只同步固定90条的问题
This commit is contained in:
@@ -23,6 +23,9 @@ import java.util.Optional;
|
||||
@Service
|
||||
public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrderRow> implements ILbOrderRowService {
|
||||
|
||||
/** 与 hxr.admin.order-select-url 中 {@code limit} 一致,用于判断是否还有下一页 */
|
||||
private static final int HXR_ORDER_PAGE_SIZE = 90;
|
||||
|
||||
@Autowired
|
||||
private HxrAdminOrderSelectService hxrAdminOrderSelectService;
|
||||
|
||||
@@ -208,43 +211,86 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
|
||||
}
|
||||
|
||||
|
||||
Optional<HxrOrderSelectResponse> opt =
|
||||
hxrAdminOrderSelectService.fetchOrderSelect(buyTimeStart, buyTimeEnd, isResell);
|
||||
if (opt.isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "未拉取到订单(请检查 hxr.admin cookie/phpsid、网络或后台返回)");
|
||||
result.put("synced", 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
HxrOrderSelectResponse body = opt.get();
|
||||
List<HxrOrderRow> rows = body.data();
|
||||
if (rows == null || rows.isEmpty()) {
|
||||
result.put("success", true);
|
||||
result.put("message", "接口成功,本页无订单数据");
|
||||
result.put("synced", 0);
|
||||
result.put("remoteCount", body.count());
|
||||
return result;
|
||||
}
|
||||
|
||||
String tid = tenantId.trim();
|
||||
String rowDataType = resolveSyncDataType(dataType, isResell);
|
||||
BigDecimal todayTotalMoneySum = parseMoney(body.allMoney());
|
||||
int todayOrderCount = body.count();
|
||||
int todayUnresellCount = countUnresell(rows);
|
||||
List<LbOrderRow> entities = new ArrayList<>(rows.size());
|
||||
for (HxrOrderRow row : rows) {
|
||||
entities.add(toLbOrderRow(
|
||||
row, tid, rowDataType, todayTotalMoneySum, todayOrderCount, todayUnresellCount));
|
||||
HxrOrderSelectResponse firstBody = null;
|
||||
int page = 1;
|
||||
int totalSynced = 0;
|
||||
while (true) {
|
||||
Optional<HxrOrderSelectResponse> opt =
|
||||
hxrAdminOrderSelectService.fetchOrderSelect(
|
||||
buyTimeStart, buyTimeEnd, isResell, page);
|
||||
if (opt.isEmpty()) {
|
||||
if (page == 1) {
|
||||
result.put("success", false);
|
||||
result.put("message", "未拉取到订单(请检查 hxr.admin cookie/phpsid、网络或后台返回)");
|
||||
result.put("synced", 0);
|
||||
return result;
|
||||
}
|
||||
result.put("success", false);
|
||||
result.put("message", "第 " + page + " 页拉取失败,已成功同步前 "
|
||||
+ (page - 1) + " 页共 " + totalSynced + " 条");
|
||||
result.put("synced", totalSynced);
|
||||
if (firstBody != null) {
|
||||
result.put("remoteCount", firstBody.count());
|
||||
result.put("allMoney", firstBody.allMoney());
|
||||
}
|
||||
result.put("pages", page - 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
HxrOrderSelectResponse body = opt.get();
|
||||
if (firstBody == null) {
|
||||
firstBody = body;
|
||||
}
|
||||
List<HxrOrderRow> rows = body.data();
|
||||
if (rows == null || rows.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
|
||||
BigDecimal todayTotalMoneySum = parseMoney(firstBody.allMoney());
|
||||
int todayOrderCount = firstBody.count();
|
||||
int todayUnresellCount = countUnresell(rows);
|
||||
List<LbOrderRow> entities = new ArrayList<>(rows.size());
|
||||
for (HxrOrderRow row : rows) {
|
||||
entities.add(toLbOrderRow(
|
||||
row, tid, rowDataType, todayTotalMoneySum, todayOrderCount, todayUnresellCount));
|
||||
}
|
||||
|
||||
boolean ok = this.saveOrUpdateBatch(entities);
|
||||
if (!ok) {
|
||||
result.put("success", false);
|
||||
result.put("message", "第 " + page + " 页保存失败,已成功同步前 "
|
||||
+ (page - 1) + " 页共 " + totalSynced + " 条");
|
||||
result.put("synced", totalSynced);
|
||||
result.put("remoteCount", firstBody.count());
|
||||
result.put("allMoney", firstBody.allMoney());
|
||||
result.put("pages", page - 1);
|
||||
return result;
|
||||
}
|
||||
totalSynced += entities.size();
|
||||
|
||||
if (rows.size() < HXR_ORDER_PAGE_SIZE ) {
|
||||
break;
|
||||
}
|
||||
page++;
|
||||
}
|
||||
|
||||
boolean ok = this.saveOrUpdateBatch(entities);
|
||||
// boolean ok = this.saveBatch(entities);
|
||||
result.put("success", ok);
|
||||
result.put("message", ok ? "同步完成" : "批量保存失败");
|
||||
result.put("synced", ok ? entities.size() : 0);
|
||||
result.put("remoteCount", body.count());
|
||||
result.put("allMoney", body.allMoney());
|
||||
if (totalSynced == 0) {
|
||||
result.put("success", true);
|
||||
result.put("message", "接口成功,时间范围内无订单数据");
|
||||
result.put("synced", 0);
|
||||
result.put("remoteCount", firstBody != null ? firstBody.count() : 0);
|
||||
result.put("pages", page);
|
||||
return result;
|
||||
}
|
||||
|
||||
result.put("success", true);
|
||||
result.put("message", "同步完成");
|
||||
result.put("synced", totalSynced);
|
||||
result.put("remoteCount", firstBody.count());
|
||||
result.put("allMoney", firstBody.allMoney());
|
||||
result.put("pages", page);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
|
||||
Reference in New Issue
Block a user