fix(外观专利): 整批品牌查询失败的文案按真因分档(BK B95-02)

现网 16890 侧成片刷「解密失败: Malformed UTF-8 data」,交付表却一律写
「品牌库上游整体不可用」——上游其实有应答,坏的是应答体解密,
运维照「上游挂了」去查永远查不到东西。

- 新增 AppearancePatentBrandFailureText:批内 reason 全同形才分档
  (全解密→解密链路故障、全不可达→服务不可达、混形/空→通用文案),
  三条文案都保留「商标查询失败」前缀(状态列与 isBrandQueryFailed 靠它判失败)。
- AppearancePatentLlmClient 的 brandAggregateFailure 收成一行委派;
  删掉只被测试调用的 boolean buildTitleRisk 重载(String 版保留兼容)。
- 用例 +5:整批解密/整批不可达/混形与空 reason/三条文案互不相同且同前缀。
This commit is contained in:
2026-11-11 20:01:36 +08:00
parent 691b29da7e
commit a17d8edc9e
3 changed files with 176 additions and 29 deletions
@@ -0,0 +1,86 @@
package com.nanri.aiimage.modules.appearancepatent.client;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
import java.util.ArrayList;
import java.util.List;
/**
* 整批品牌查询全失败时,标题列该写哪句人话。
*
* <p>2026-11-11 现网(BACKLOG B95-02):16890 侧 {@code 解密失败: Malformed UTF-8 data} 成片刷,
* 交付表却一律写「品牌库上游整体不可用」——上游其实**有应答**,坏的是应答体解密,运维照着
* 「上游挂了」去查永远查不到东西。真因不同,指向的动作不同,所以按批内 reason 分档。
*
* <p>三条文案都必须以「商标查询失败」开头:{@code AppearancePatentLlmClient#isBrandQueryFailed}
* 与状态列都靠这个前缀判定失败,措辞可以变,前缀不能变。
*
* <p>单独成类而不是塞回客户端:{@code AppearancePatentLlmClient} 已在可读性棘轮的
* 「超长文件点名」账上,只许减。
*/
final class AppearancePatentBrandFailureText {
/** 分不出真因时的通用文案(保底,不是默认)。 */
static final String UPSTREAM_UNAVAILABLE =
"商标查询失败:品牌库上游整体不可用(本批待查词无一返回结果,非本行数据问题)";
/** 批内真因统一是「拿回了包但解不开」。 */
static final String DECRYPT_BROKEN =
"商标查询失败:品牌库返回内容无法解密(服务侧解密链路故障,非本行数据问题)";
/** 批内真因统一是「连接根本没建立」。 */
static final String SERVICE_UNREACHABLE =
"商标查询失败:品牌检测服务不可达(连接未建立,非本行数据问题)";
private AppearancePatentBrandFailureText() {
}
/**
* 整批待查词是否无一返回可用结果;是则给出该批的行级文案,否则 null(逐行写「商标查询失败」)。
*
* <p>判据刻意保守:只要有任何一行拿到过侵权命中、或有任何一行查成功过,就返回 null。
* 没发过查询的行(LLM 已判「无」、或标题为空)不参与判定,否则一批全是"无需查"的行
* 会被误判成上游故障。
*/
static String aggregate(List<BrandCheckClient.BrandCheckBatchResult> brandChecks) {
boolean anyChecked = false;
List<String> reasons = new ArrayList<>();
for (BrandCheckClient.BrandCheckBatchResult check : brandChecks) {
if (check == null || check.brands() == null || check.brands().isEmpty()) {
continue;
}
anyChecked = true;
// 命中过侵权就说明上游至少在返回结果,那种行优先给真实结论,不升级成整批口径
if (check.hasFailedData() || !check.hasQueryFailedData()) {
return null;
}
reasons.add(normalize(check.reason()));
}
return anyChecked ? classify(reasons) : null;
}
/** 批内 reason 全同形才分档;空 reason 或混形一律落回通用文案(不猜)。 */
static String classify(List<String> reasons) {
if (reasons == null || reasons.isEmpty()) {
return UPSTREAM_UNAVAILABLE;
}
boolean allDecrypt = true;
boolean allUnreachable = true;
for (String reason : reasons) {
String text = normalize(reason);
if (text.isEmpty()) {
return UPSTREAM_UNAVAILABLE;
}
allDecrypt &= text.contains("解密");
allUnreachable &= text.contains("不可达") || text.contains("连接未建立");
}
if (allDecrypt) {
return DECRYPT_BROKEN;
}
return allUnreachable ? SERVICE_UNREACHABLE : UPSTREAM_UNAVAILABLE;
}
private static String normalize(String value) {
return value == null ? "" : value.trim();
}
}
@@ -256,9 +256,9 @@ public class AppearancePatentLlmClient {
*/
private void applyLlmRowsInOneBatch(List<AppearancePatentResultRowDto> llmRows, List<LlmResult> llmResults) {
List<BrandCheckClient.BrandCheckBatchResult> brandChecks = brandChecksFor(llmResults);
boolean brandUpstreamUnavailable = brandUpstreamUnavailable(brandChecks);
String aggregateFailure = brandAggregateFailure(brandChecks);
for (int k = 0; k < llmRows.size(); k++) {
applyResult(llmRows.get(k), llmResults.get(k), brandChecks.get(k), brandUpstreamUnavailable);
applyResult(llmRows.get(k), llmResults.get(k), brandChecks.get(k), aggregateFailure);
}
}
@@ -1076,10 +1076,10 @@ public class AppearancePatentLlmClient {
resolved.add(result);
}
List<BrandCheckClient.BrandCheckBatchResult> brandChecks = brandChecksFor(resolved);
boolean brandUpstreamUnavailable = brandUpstreamUnavailable(brandChecks);
String aggregateFailure = brandAggregateFailure(brandChecks);
for (int i = 0; i < rows.size(); i++) {
AppearancePatentResultRowDto row = copy(rows.get(i));
applyResult(row, resolved.get(i), brandChecks.get(i), brandUpstreamUnavailable);
applyResult(row, resolved.get(i), brandChecks.get(i), aggregateFailure);
merged.add(row);
}
return merged;
@@ -1134,37 +1134,24 @@ public class AppearancePatentLlmClient {
}
/**
* 整批待查词是否无一返回可用结果——用来把「上游整体挂了」和「这个词查不了」分开写。
*
* <p>判据刻意保守:只要有任何一行拿到过侵权命中、或有任何一行查成功过,就不算整体不可用,
* 仍按逐行的「商标查询失败」写。没发过查询的行(LLM 已判「无」、或标题为空)不参与判定,
* 否则一批全是"无需查"的行会被误判成上游故障。
* 整批待查词是否无一返回可用结果——是则给出该批的行级文案,否则 null(逐行写「商标查询失败」)。
* 分档与判据都在 {@link AppearancePatentBrandFailureText}(本文件只许减,见可读性棘轮)。
*/
private static boolean brandUpstreamUnavailable(
private String brandAggregateFailure(
List<BrandCheckClient.BrandCheckBatchResult> brandChecks) {
boolean anyChecked = false;
for (BrandCheckClient.BrandCheckBatchResult check : brandChecks) {
if (check == null || check.brands() == null || check.brands().isEmpty()) {
continue;
}
anyChecked = true;
if (check.hasFailedData() || !check.hasQueryFailedData()) {
return false;
}
}
return anyChecked;
return AppearancePatentBrandFailureText.aggregate(brandChecks);
}
void applyResult(AppearancePatentResultRowDto row, LlmResult result,
BrandCheckClient.BrandCheckBatchResult brandCheck,
boolean brandUpstreamUnavailable) {
String brandAggregateFailure) {
if (row == null || result == null) {
return;
}
if (isNoTitleRisk(result)) {
row.setTitleRisk(NO_INFRINGEMENT);
} else if (shouldCheckBrand(result)) {
row.setTitleRisk(buildTitleRisk(result.title(), brandCheck, brandUpstreamUnavailable));
row.setTitleRisk(buildTitleRisk(result.title(), brandCheck, brandAggregateFailure));
} else {
row.setTitleRisk("");
}
@@ -1196,11 +1183,15 @@ public class AppearancePatentLlmClient {
}
String buildTitleRisk(String llmTitle, BrandCheckClient.BrandCheckBatchResult brandCheck) {
return buildTitleRisk(llmTitle, brandCheck, false);
return buildTitleRisk(llmTitle, brandCheck, (String) null);
}
/**
* @param brandAggregateFailure 整批全失败时的行级文案(见 {@link AppearancePatentBrandFailureText});
* null=不是整批口径,逐行写「商标查询失败」
*/
String buildTitleRisk(String llmTitle, BrandCheckClient.BrandCheckBatchResult brandCheck,
boolean brandUpstreamUnavailable) {
String brandAggregateFailure) {
List<String> brands = brandCheck == null ? List.of() : brandCheck.brands();
if (brands == null || brands.isEmpty()) {
return firstNonBlank(llmTitle, "");
@@ -1210,7 +1201,7 @@ public class AppearancePatentLlmClient {
}
if (brandCheck.hasQueryFailedData()) {
// 命中过侵权就说明上游至少在返回结果,那种行优先给真实结论,不升级成整批口径。
return brandUpstreamUnavailable ? BRAND_UPSTREAM_UNAVAILABLE : BRAND_QUERY_FAILED;
return brandAggregateFailure == null ? BRAND_QUERY_FAILED : brandAggregateFailure;
}
return NO_INFRINGEMENT;
}