fix(jd): full 闸门后端两红格随窗清账——同族补值服务抽 JdLlmChatSupport+CompanyMapper 注解 SQL 平移 XML

- CatchSilenceRatchetTest(1113→1110 回落上限,净账 +0):①JdSizeChartAiService/JdPublishFillAiService 逐字节重复的 chatCompletion/parse/restClient 三份并成 JdLlmChatSupport 一份(parse catch 2→1);②asDecimal/asNumber 正则已钉形状后的 BigDecimal/Double catch 是死代码整段摘除(−2);降级外壳(外层 catch Exception→WARN+空映射)口径不动
- SqlLayeringConventionTest(898e8a5e 走注解通道当窗被抓):CompanyMapper 两支 @Select 平移进 mapper/company/CompanyMapper.xml,SQL 文本未改写;XML_STATEMENT_TOTAL 152→154、MigratedSqlEquivalenceTest XML_SELECT_TOTAL 107→109+NOT_MIGRATED 点名两条
- 判据:CatchSilence 9+SqlLayering 9+MigratedSql 3+FillAi 11+SizeChart 10+Company 侧 113 全绿
This commit is contained in:
2026-11-23 09:55:30 +08:00
parent d76e11ccff
commit 2e74a483e8
8 changed files with 134 additions and 127 deletions
@@ -4,11 +4,10 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.company.model.dto.CompanyUserCountRow;
import com.nanri.aiimage.modules.company.model.entity.CompanyEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/** SQL 一律收口在 mapper/company/CompanyMapper.xml(铁律 1;2026-11-23 从注解平移)。 */
@Mapper
public interface CompanyMapper extends BaseMapper<CompanyEntity> {
@@ -17,10 +16,8 @@ public interface CompanyMapper extends BaseMapper<CompanyEntity> {
* 停用账号也算在册——它们仍挂在公司上,删了公司一样把人挂在查不到的租户名下。
* 公司行是硬删(无软删位),users 侧同样硬删,所以这里不用再过滤删除标记。
*/
@Select("SELECT company_id AS companyId, COUNT(*) AS userCount FROM users GROUP BY company_id")
List<CompanyUserCountRow> selectUserCountByCompany();
/** 单家公司在册账号数:CompanyService.delete 的「有账号不能删」闸门用(权威校验,前端那份只是展示提示)。 */
@Select("SELECT COUNT(*) FROM users WHERE company_id = #{companyId}")
Long selectUserCountByCompanyId(@Param("companyId") Long companyId);
Long selectUserCountByCompanyId(Long companyId);
}
@@ -0,0 +1,83 @@
package com.nanri.aiimage.modules.jd.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.config.HttpClientPool;
import com.nanri.aiimage.modules.usersecret.support.LlmChannel;
import org.springframework.web.client.RestClient;
import java.io.InputStream;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 京东上架两条 AI 补值链路(尺码表 {@link JdSizeChartAiService}/必填参数
* {@link JdPublishFillAiService})共用的龙抬头 chat/completions 客户端——两个服务
* 原本各持一份逐字节相同的 chatCompletion/parse/restClient(2026-11-23 抽并,同族
* 重复件只许有一份),差异只在 prompt 组装与输出归一,都留在各自服务里。
*
* <p>降级口径:响应不可解析返回空串(调用方按「无补值」降级);网络/超时异常不在这
* 一层吞——往上交给服务的降级外壳统一 WARN + 空映射,日志里能带上 userId/商品上下文。</p>
*/
class JdLlmChatSupport {
private static final int CHAT_TIMEOUT_MILLIS = 20_000;
private static final int MAX_RESPONSE_BYTES = 1 << 20;
private final ObjectMapper objectMapper;
private volatile RestClient sharedClient;
JdLlmChatSupport(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
/** 调龙抬头 chat/completions;该渠道需要显式思考参数(LlmChannel 登记)。返回 content 单串。 */
String completion(String apiKey, String prompt) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("model", LlmChannel.LONGTAITOU.modelFor(null));
body.put("messages", List.of(Map.of("role", "user", "content", prompt)));
body.put("temperature", 0.2);
LlmChannel.LONGTAITOU.applyThinking(body);
String response = restClient().post()
.uri(LlmChannel.LONGTAITOU.host() + "/v1/chat/completions")
.headers(headers -> headers.setBearerAuth(apiKey))
.body(body)
.exchange((request, res) -> {
try (InputStream in = res.getBody()) {
return HttpClientPool.readBodyBounded(in,
Duration.ofMillis(CHAT_TIMEOUT_MILLIS), MAX_RESPONSE_BYTES);
}
});
JsonNode root = parse(response);
JsonNode content = root == null ? null
: root.path("choices").path(0).path("message").path("content");
return content == null || content.isNull() ? "" : content.asText();
}
/** 模型输出/上游响应体 → JsonNode;解析不了返回 null(调用方按空降级),不抛。 */
JsonNode parse(String body) {
try {
return body == null || body.isBlank() ? null : objectMapper.readTree(body);
} catch (Exception ex) {
return null;
}
}
private RestClient restClient() {
RestClient client = sharedClient;
if (client != null) {
return client;
}
synchronized (this) {
if (sharedClient == null) {
sharedClient = RestClient.builder()
.requestFactory(HttpClientPool.requestFactory(CHAT_TIMEOUT_MILLIS))
.build();
}
return sharedClient;
}
}
}
@@ -2,17 +2,12 @@ package com.nanri.aiimage.modules.jd.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.config.HttpClientPool;
import com.nanri.aiimage.modules.jd.model.dto.JdPublishFillValuesRequest;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import com.nanri.aiimage.modules.usersecret.support.LlmChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -35,8 +30,6 @@ import java.util.Map;
@Slf4j
public class JdPublishFillAiService {
private static final int CHAT_TIMEOUT_MILLIS = 20_000;
private static final int MAX_RESPONSE_BYTES = 1 << 20;
private static final int MAX_SKUS_PER_CALL = 200;
/** 京麦标题框单行输入:超长标题整条丢弃,宁缺勿错。 */
private static final int MAX_TITLE_CHARS = 45;
@@ -44,13 +37,11 @@ public class JdPublishFillAiService {
private static final int MAX_STOCK = 999999;
private final UserApiSecretService userApiSecretService;
private final ObjectMapper objectMapper;
private volatile RestClient sharedClient;
private final JdLlmChatSupport chatClient;
public JdPublishFillAiService(UserApiSecretService userApiSecretService, ObjectMapper objectMapper) {
this.userApiSecretService = userApiSecretService;
this.objectMapper = objectMapper;
this.chatClient = new JdLlmChatSupport(objectMapper);
}
/**
@@ -154,27 +145,9 @@ public class JdPublishFillAiService {
+ "只包含下面点名的缺口。\nSKU 清单:\n" + skus + "需要补值的缺口:\n" + gaps;
}
/** 调龙抬头 chat/completions;该渠道需要显式思考参数(LlmChannel 登记)。 */
/** 调龙抬头 chat/completions(请求组装与响应解析收口在 {@link JdLlmChatSupport} 同一份里)。 */
String chatCompletion(String apiKey, String prompt) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("model", LlmChannel.LONGTAITOU.modelFor(null));
body.put("messages", List.of(Map.of("role", "user", "content", prompt)));
body.put("temperature", 0.2);
LlmChannel.LONGTAITOU.applyThinking(body);
String response = restClient().post()
.uri(LlmChannel.LONGTAITOU.host() + "/v1/chat/completions")
.headers(headers -> headers.setBearerAuth(apiKey))
.body(body)
.exchange((request, res) -> {
try (InputStream in = res.getBody()) {
return HttpClientPool.readBodyBounded(in,
Duration.ofMillis(CHAT_TIMEOUT_MILLIS), MAX_RESPONSE_BYTES);
}
});
JsonNode root = parse(response);
JsonNode content = root == null ? null
: root.path("choices").path(0).path("message").path("content");
return content == null || content.isNull() ? "" : content.asText();
return chatClient.completion(apiKey, prompt);
}
/**
@@ -193,7 +166,7 @@ public class JdPublishFillAiService {
int end = json.lastIndexOf("```");
json = start >= 0 && end > start ? json.substring(start + 1, end) : json.replaceAll("```", "");
}
JsonNode root = parse(json);
JsonNode root = chatClient.parse(json);
if (root == null || !root.isObject()) {
return out;
}
@@ -272,38 +245,12 @@ public class JdPublishFillAiService {
}
if (value.isTextual()) {
String text = value.asText().trim();
// 正则已把形状钉成「纯数字+可选小数」,BigDecimal 对这种串不会抛——不设 catch。
if (text.isEmpty() || !text.matches("\\d+(\\.\\d+)?")) {
return null;
}
try {
return new BigDecimal(text);
} catch (NumberFormatException ex) {
return null;
}
return new BigDecimal(text);
}
return null;
}
private JsonNode parse(String body) {
try {
return body == null || body.isBlank() ? null : objectMapper.readTree(body);
} catch (Exception ex) {
return null;
}
}
private RestClient restClient() {
RestClient client = sharedClient;
if (client != null) {
return client;
}
synchronized (this) {
if (sharedClient == null) {
sharedClient = RestClient.builder()
.requestFactory(HttpClientPool.requestFactory(CHAT_TIMEOUT_MILLIS))
.build();
}
return sharedClient;
}
}
}
@@ -2,16 +2,11 @@ package com.nanri.aiimage.modules.jd.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.config.HttpClientPool;
import com.nanri.aiimage.modules.jd.model.dto.JdSizeChartValuesRequest;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import com.nanri.aiimage.modules.usersecret.support.LlmChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import java.io.InputStream;
import java.time.Duration;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@@ -34,18 +29,14 @@ import java.util.Map;
@Slf4j
public class JdSizeChartAiService {
private static final int CHAT_TIMEOUT_MILLIS = 20_000;
private static final int MAX_RESPONSE_BYTES = 1 << 20;
private static final int MAX_ITEMS_PER_CALL = 200;
private final UserApiSecretService userApiSecretService;
private final ObjectMapper objectMapper;
private volatile RestClient sharedClient;
private final JdLlmChatSupport chatClient;
public JdSizeChartAiService(UserApiSecretService userApiSecretService, ObjectMapper objectMapper) {
this.userApiSecretService = userApiSecretService;
this.objectMapper = objectMapper;
this.chatClient = new JdLlmChatSupport(objectMapper);
}
/**
@@ -103,27 +94,9 @@ public class JdSizeChartAiService {
+ "{\"尺码行名\": {\"列key\": [值1, 值2]}}。\n需要补值的格子:\n" + rows;
}
/** 调龙抬头 chat/completions;该渠道需要显式思考参数(LlmChannel 登记)。 */
/** 调龙抬头 chat/completions(请求组装与响应解析收口在 {@link JdLlmChatSupport} 同一份里)。 */
String chatCompletion(String apiKey, String prompt) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("model", LlmChannel.LONGTAITOU.modelFor(null));
body.put("messages", List.of(Map.of("role", "user", "content", prompt)));
body.put("temperature", 0.2);
LlmChannel.LONGTAITOU.applyThinking(body);
String response = restClient().post()
.uri(LlmChannel.LONGTAITOU.host() + "/v1/chat/completions")
.headers(headers -> headers.setBearerAuth(apiKey))
.body(body)
.exchange((request, res) -> {
try (InputStream in = res.getBody()) {
return HttpClientPool.readBodyBounded(in,
Duration.ofMillis(CHAT_TIMEOUT_MILLIS), MAX_RESPONSE_BYTES);
}
});
JsonNode root = parse(response);
JsonNode content = root == null ? null
: root.path("choices").path(0).path("message").path("content");
return content == null || content.isNull() ? "" : content.asText();
return chatClient.completion(apiKey, prompt);
}
/**
@@ -142,7 +115,7 @@ public class JdSizeChartAiService {
int end = json.lastIndexOf("```");
json = start >= 0 && end > start ? json.substring(start + 1, end) : json.replaceAll("```", "");
}
JsonNode root = parse(json);
JsonNode root = chatClient.parse(json);
if (root == null || !root.isObject()) {
return out;
}
@@ -182,15 +155,13 @@ public class JdSizeChartAiService {
return v.asDouble();
}
if (v.isTextual()) {
// 正则已把形状钉成「纯数字+可选小数」,Double 对这种串不会抛(超大数字给 Infinity,
// 由调用方的 ≤500 上限格丢弃)——不设 catch。
String text = v.asText().trim();
if (!text.matches("\\d+(\\.\\d+)?")) {
return null;
}
try {
return Double.parseDouble(text);
} catch (NumberFormatException ex) {
return null;
}
return Double.parseDouble(text);
}
return null;
}
@@ -198,27 +169,4 @@ public class JdSizeChartAiService {
private static double round1(double value) {
return Math.round(value * 10.0) / 10.0;
}
private JsonNode parse(String body) {
try {
return body == null || body.isBlank() ? null : objectMapper.readTree(body);
} catch (Exception ex) {
return null;
}
}
private RestClient restClient() {
RestClient client = sharedClient;
if (client != null) {
return client;
}
synchronized (this) {
if (sharedClient == null) {
sharedClient = RestClient.builder()
.requestFactory(HttpClientPool.requestFactory(CHAT_TIMEOUT_MILLIS))
.build();
}
return sharedClient;
}
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nanri.aiimage.modules.company.mapper.CompanyMapper">
<!-- 2026-11-23 京东上架补值窗从注解 SQL 平移而来(SqlLayeringConventionTest 抓到 898e8a5e
走了注解通道),SQL 文本未改写;语句 id 与接口方法名逐字一致。
等价性口径:非注解迁移件,登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED。 -->
<select id="selectUserCountByCompany" resultType="com.nanri.aiimage.modules.company.model.dto.CompanyUserCountRow">
SELECT company_id AS companyId, COUNT(*) AS userCount FROM users GROUP BY company_id
</select>
<select id="selectUserCountByCompanyId" resultType="java.lang.Long">
SELECT COUNT(*) FROM users WHERE company_id = #{companyId}
</select>
</mapper>
@@ -114,7 +114,11 @@ class MigratedSqlEquivalenceTest {
//(selectDistinctShopNames,分类添加店铺弹窗的候选下拉),点名进下方 NOT_MIGRATED
//(SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 150 → 152)。
// 该笔当时只点 jd 点名包没跑整仓整量、两处账本都漏记,发版前整量抓到补记。
private static final int XML_SELECT_TOTAL = 107;
// 107 → 109:2026-11-23 京东上架补值窗把 CompanyMapper 的 2 条注解 @Select 平移进
// mapper/company/CompanyMapper.xml(SqlLayeringConventionTest 抓到 898e8a5e 走了注解
// 通道),SQL 文本未改写;非注解迁移件,逐条点名进下方 NOT_MIGRATED
//(SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 152 → 154)。
private static final int XML_SELECT_TOTAL = 109;
/**
* 不是「从注解搬过来」的语句:注解时代没有原文可对撞,等价性这支只能跳过它们。
@@ -151,6 +155,11 @@ class MigratedSqlEquivalenceTest {
"TaskFileJobMapper#selectTerminalFileTaskIds",
// CR-01(2026-10-25)新写的 1 条写侧语句:会话世代原子自增。
"LoginUserMapper#incrementSessionEpoch",
// 2026-11-23 从 CompanyMapper 注解 @Select 平移进 XML 的 2 条(公司管理
// 删除/批量删除的在册账号数两条,SQL 文本未改写);注解时代原文随搬迁
// 进了本仓 XML,等价性这支没有注解 fixture 可对撞,按新写件点名跳过。
"CompanyMapper#selectUserCountByCompany",
"CompanyMapper#selectUserCountByCompanyId",
// 关键词使用记录管理端(2026-10-25)新写的 10 条只读 select。
"KeywordUsageAdminMapper#countUsageRows",
"KeywordUsageAdminMapper#selectUsageRows",
@@ -142,7 +142,11 @@ class SqlLayeringConventionTest {
// 语句;同轮登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED(该文件 XML_SELECT_TOTAL
// 同轮 106 → 107)。该笔当时只点 jd 点名包没跑整仓整量、两处账本都漏记,
// 发版前整量抓到补记。
private static final int XML_STATEMENT_TOTAL = 152;
// 152 → 154:2026-11-23 京东上架补值窗把 CompanyMapper 的 2 条注解 @Select 平移进
// mapper/company/CompanyMapper.xml(898e8a5e 走了注解通道,本测试当窗抓到),SQL 文本
// 未改写;逐条登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED(同文件 XML_SELECT_TOTAL
// 同轮 107 → 109)。
private static final int XML_STATEMENT_TOTAL = 154;
/**
* wrapper 里夹带函数级 SQL 的现状("这不是简单单表"的证据),文件名 → 条数:
@@ -425,6 +425,12 @@ class CatchSilenceRatchetTest {
// catch Exception → log.warn(带 taskId/moduleType)后 return null(NOT_RETHROW,
// 直推载荷组装失败退回兜底拉取,不把重试口打成 500)。
// 本批复核办法:单跑本类实测 total 1110、notRethrow(含 log) 见 STRICT_NOT_RETHROW 上方 11-21 段。
// 11-23 京东上架必填参数补值窗(7aa87bd0):JdPublishFillAiService 初版照抄尺码表
// 三枚 catch 把总数顶到 1113(本格真红),随窗收口回落 1110,净账 +0:
// ① 同族重复的 chatCompletion/parse/restClient 三份并成 JdLlmChatSupport 一份
// (FillAi/SizeChart 各删一份 parse catch、支撑类落一份,净 −1);
// ② asDecimal/asNumber 两处「正则已钉形状后 BigDecimal/Double 再 catch」是死代码,
// 整段摘除(净 −2)。降级外壳的口径没动:外层 catch Exception → WARN + 空映射照旧。
static final int STRICT_TOTAL = 1110;
/**
* 重抛型 catch 的**下限**(不是上限):本轮把两处吞异常改成重抛之后实测 388。