获取优惠卷

This commit is contained in:
2026-06-27 20:12:11 +08:00
parent 9e4f32a47c
commit 9b94911319

View File

@@ -7,6 +7,8 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.rj.dto.hxr.HxrMoneyCouponApiContext;
import com.rj.dto.hxr.HxrUserLoginApiContext;
import com.rj.entity.LbThirdIntegrationConfig;
@@ -211,8 +213,24 @@ public class LbUserCouponServiceImpl
return null;
}
int pageLimit = recentDays > 0 ? recentDays : 90;
String uri = couponApiBaseUrl + (couponApiBaseUrl.contains("?") ? "&" : "?") + "limit=" + pageLimit;
// 每页条数使用配置值
int pageSize = ctx.pageLimit() > 0 ? ctx.pageLimit() : 10;
// 总共需要获取的条数
int totalNeeded = recentDays > 0 ? recentDays : 90;
List<JsonNode> allItems = new ArrayList<>();
int page = 1;
int fetched = 0;
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(60))
.followRedirects(HttpClient.Redirect.NORMAL)
.build();
while (fetched < totalNeeded) {
int limit = Math.min(pageSize, totalNeeded - fetched);
String uri = couponApiBaseUrl + (couponApiBaseUrl.contains("?") ? "&" : "?")
+ "cate=2&type=1&page=" + page + "&limit=" + limit;
long timestamp = System.currentTimeMillis() / 1000;
String noncestr = randomNoncestr();
@@ -221,11 +239,6 @@ public class LbUserCouponServiceImpl
signParams.put("noncestr", noncestr);
String sign = HxrGoodsSignUtil.computeSign(signParams, ctx.appStr().trim());
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(60))
.followRedirects(HttpClient.Redirect.NORMAL)
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.timeout(Duration.ofSeconds(120))
@@ -245,9 +258,52 @@ public class LbUserCouponServiceImpl
int status = response.statusCode();
if (status < 200 || status >= 300) {
log.warn("优惠券API HTTP {} bodyPrefix={}", status, abbreviate(response.body(), 400));
break;
}
JsonNode root = JSON.readTree(response.body());
int code = root.path("code").asInt(-1);
if (code != 0) {
log.warn("优惠券API返回错误 code={} msg={}", code, root.path("msg").asText(""));
break;
}
JsonNode listNode = root.path("data").path("list");
if (listNode.isMissingNode() || !listNode.isArray() || listNode.size() == 0) {
log.info("优惠券API返回空列表 page={}", page);
break;
}
for (JsonNode item : listNode) {
if (fetched >= totalNeeded) {
break;
}
allItems.add(item);
fetched++;
}
// 如果返回的数据少于请求的limit说明没有更多数据了
if (listNode.size() < limit) {
break;
}
page++;
}
if (allItems.isEmpty()) {
return null;
}
return response.body();
// 构建合并后的JSON
ObjectNode resultNode = JSON.createObjectNode();
resultNode.put("code", 0);
ObjectNode dataNode = resultNode.putObject("data");
ArrayNode listArray = dataNode.putArray("list");
for (JsonNode item : allItems) {
listArray.add(item);
}
return JSON.writeValueAsString(resultNode);
}
private int parseAndSaveCoupons(String couponJson, LbUser user, String tenantId) throws Exception {