解决同步订单时,每次只同步固定90条的问题
This commit is contained in:
@@ -49,7 +49,17 @@ public class HxrAdminOrderSelectService {
|
|||||||
*/
|
*/
|
||||||
public Optional<HxrOrderSelectResponse> fetchOrderSelect(
|
public Optional<HxrOrderSelectResponse> fetchOrderSelect(
|
||||||
String buyTimeStart, String buyTimeEnd, Integer isResell) throws Exception {
|
String buyTimeStart, String buyTimeEnd, Integer isResell) throws Exception {
|
||||||
return fetchOrderSelectInternal(buildOrderSelectUrl(buyTimeStart, buyTimeEnd, isResell));
|
return fetchOrderSelect(buyTimeStart, buyTimeEnd, isResell, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按购买时间区间、转卖状态及页码拉取订单列表。
|
||||||
|
*
|
||||||
|
* @param page 页码,从 1 开始;与配置 URL 中的 {@code limit} 配合分页
|
||||||
|
*/
|
||||||
|
public Optional<HxrOrderSelectResponse> fetchOrderSelect(
|
||||||
|
String buyTimeStart, String buyTimeEnd, Integer isResell, int page) throws Exception {
|
||||||
|
return fetchOrderSelectInternal(buildOrderSelectUrl(buyTimeStart, buyTimeEnd, isResell, page));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<HxrOrderSelectResponse> fetchOrderSelectInternal(String requestUri) throws Exception {
|
private Optional<HxrOrderSelectResponse> fetchOrderSelectInternal(String requestUri) throws Exception {
|
||||||
@@ -94,13 +104,14 @@ public class HxrAdminOrderSelectService {
|
|||||||
/**
|
/**
|
||||||
* 在 {@link HxrAdminProperties#getOrderSelectUrl()} 上覆盖 {@code buy_time[0]}、{@code buy_time[1]}、{@code is_resell}。
|
* 在 {@link HxrAdminProperties#getOrderSelectUrl()} 上覆盖 {@code buy_time[0]}、{@code buy_time[1]}、{@code is_resell}。
|
||||||
*/
|
*/
|
||||||
private String buildOrderSelectUrl(String buyTimeStart, String buyTimeEnd, Integer isResell) {
|
private String buildOrderSelectUrl(String buyTimeStart, String buyTimeEnd, Integer isResell, int page) {
|
||||||
UriComponentsBuilder b = UriComponentsBuilder.fromUriString(properties.getOrderSelectUrl());
|
UriComponentsBuilder b = UriComponentsBuilder.fromUriString(properties.getOrderSelectUrl());
|
||||||
b.replaceQueryParam("buy_time[0]", buyTimeStart == null ? "" : buyTimeStart);
|
b.replaceQueryParam("buy_time[0]", buyTimeStart == null ? "" : buyTimeStart);
|
||||||
b.replaceQueryParam("buy_time[1]", buyTimeEnd == null ? "" : buyTimeEnd);
|
b.replaceQueryParam("buy_time[1]", buyTimeEnd == null ? "" : buyTimeEnd);
|
||||||
if (isResell != null) {
|
if (isResell != null) {
|
||||||
b.replaceQueryParam("is_resell", isResell);
|
b.replaceQueryParam("is_resell", isResell);
|
||||||
}
|
}
|
||||||
|
b.replaceQueryParam("page", Math.max(1, page));
|
||||||
return b.build().encode().toUriString();
|
return b.build().encode().toUriString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ import java.util.Optional;
|
|||||||
@Service
|
@Service
|
||||||
public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrderRow> implements ILbOrderRowService {
|
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
|
@Autowired
|
||||||
private HxrAdminOrderSelectService hxrAdminOrderSelectService;
|
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 tid = tenantId.trim();
|
||||||
String rowDataType = resolveSyncDataType(dataType, isResell);
|
String rowDataType = resolveSyncDataType(dataType, isResell);
|
||||||
BigDecimal todayTotalMoneySum = parseMoney(body.allMoney());
|
HxrOrderSelectResponse firstBody = null;
|
||||||
int todayOrderCount = body.count();
|
int page = 1;
|
||||||
int todayUnresellCount = countUnresell(rows);
|
int totalSynced = 0;
|
||||||
List<LbOrderRow> entities = new ArrayList<>(rows.size());
|
while (true) {
|
||||||
for (HxrOrderRow row : rows) {
|
Optional<HxrOrderSelectResponse> opt =
|
||||||
entities.add(toLbOrderRow(
|
hxrAdminOrderSelectService.fetchOrderSelect(
|
||||||
row, tid, rowDataType, todayTotalMoneySum, todayOrderCount, todayUnresellCount));
|
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);
|
if (totalSynced == 0) {
|
||||||
// boolean ok = this.saveBatch(entities);
|
result.put("success", true);
|
||||||
result.put("success", ok);
|
result.put("message", "接口成功,时间范围内无订单数据");
|
||||||
result.put("message", ok ? "同步完成" : "批量保存失败");
|
result.put("synced", 0);
|
||||||
result.put("synced", ok ? entities.size() : 0);
|
result.put("remoteCount", firstBody != null ? firstBody.count() : 0);
|
||||||
result.put("remoteCount", body.count());
|
result.put("pages", page);
|
||||||
result.put("allMoney", body.allMoney());
|
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;
|
return result;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
result.put("success", false);
|
result.put("success", false);
|
||||||
|
|||||||
Reference in New Issue
Block a user