From 2e74a483e8ac53eacacee24b883a1fe68a08df95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Mon, 23 Nov 2026 09:55:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(jd):=20full=20=E9=97=B8=E9=97=A8=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=E4=B8=A4=E7=BA=A2=E6=A0=BC=E9=9A=8F=E7=AA=97=E6=B8=85?= =?UTF-8?q?=E8=B4=A6=E2=80=94=E2=80=94=E5=90=8C=E6=97=8F=E8=A1=A5=E5=80=BC?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E6=8A=BD=20JdLlmChatSupport=EF=BC=8BCompanyM?= =?UTF-8?q?apper=20=E6=B3=A8=E8=A7=A3=20SQL=20=E5=B9=B3=E7=A7=BB=20XML?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 全绿 --- .../modules/company/mapper/CompanyMapper.java | 7 +- .../modules/jd/service/JdLlmChatSupport.java | 83 +++++++++++++++++++ .../jd/service/JdPublishFillAiService.java | 67 ++------------- .../jd/service/JdSizeChartAiService.java | 68 ++------------- .../mapper/company/CompanyMapper.xml | 13 +++ .../aiimage/MigratedSqlEquivalenceTest.java | 11 ++- .../aiimage/SqlLayeringConventionTest.java | 6 +- .../config/CatchSilenceRatchetTest.java | 6 ++ 8 files changed, 134 insertions(+), 127 deletions(-) create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdLlmChatSupport.java create mode 100644 backend-java/src/main/resources/mapper/company/CompanyMapper.xml diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/company/mapper/CompanyMapper.java b/backend-java/src/main/java/com/nanri/aiimage/modules/company/mapper/CompanyMapper.java index cd5138584..56c8675f3 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/company/mapper/CompanyMapper.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/company/mapper/CompanyMapper.java @@ -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 { @@ -17,10 +16,8 @@ public interface CompanyMapper extends BaseMapper { * 停用账号也算在册——它们仍挂在公司上,删了公司一样把人挂在查不到的租户名下。 * 公司行是硬删(无软删位),users 侧同样硬删,所以这里不用再过滤删除标记。 */ - @Select("SELECT company_id AS companyId, COUNT(*) AS userCount FROM users GROUP BY company_id") List selectUserCountByCompany(); /** 单家公司在册账号数:CompanyService.delete 的「有账号不能删」闸门用(权威校验,前端那份只是展示提示)。 */ - @Select("SELECT COUNT(*) FROM users WHERE company_id = #{companyId}") - Long selectUserCountByCompanyId(@Param("companyId") Long companyId); + Long selectUserCountByCompanyId(Long companyId); } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdLlmChatSupport.java b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdLlmChatSupport.java new file mode 100644 index 000000000..ef5c2abbd --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdLlmChatSupport.java @@ -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 组装与输出归一,都留在各自服务里。 + * + *

降级口径:响应不可解析返回空串(调用方按「无补值」降级);网络/超时异常不在这 + * 一层吞——往上交给服务的降级外壳统一 WARN + 空映射,日志里能带上 userId/商品上下文。

+ */ +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 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; + } + } +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdPublishFillAiService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdPublishFillAiService.java index 3b0605d08..290818286 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdPublishFillAiService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdPublishFillAiService.java @@ -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 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; - } - } } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdSizeChartAiService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdSizeChartAiService.java index 9fd5b3ec0..7d8e4bd57 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdSizeChartAiService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdSizeChartAiService.java @@ -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 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; - } - } } diff --git a/backend-java/src/main/resources/mapper/company/CompanyMapper.xml b/backend-java/src/main/resources/mapper/company/CompanyMapper.xml new file mode 100644 index 000000000..c219e8a15 --- /dev/null +++ b/backend-java/src/main/resources/mapper/company/CompanyMapper.xml @@ -0,0 +1,13 @@ + + + + + + + diff --git a/backend-java/src/test/java/com/nanri/aiimage/MigratedSqlEquivalenceTest.java b/backend-java/src/test/java/com/nanri/aiimage/MigratedSqlEquivalenceTest.java index f9cc917ab..5ec6556a6 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/MigratedSqlEquivalenceTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/MigratedSqlEquivalenceTest.java @@ -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", diff --git a/backend-java/src/test/java/com/nanri/aiimage/SqlLayeringConventionTest.java b/backend-java/src/test/java/com/nanri/aiimage/SqlLayeringConventionTest.java index a2b3cddff..3ae4ddc1d 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/SqlLayeringConventionTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/SqlLayeringConventionTest.java @@ -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 的现状("这不是简单单表"的证据),文件名 → 条数: diff --git a/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java b/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java index 37f4a879c..97bdc1409 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java @@ -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。