Compare commits

...

16 Commits

Author SHA1 Message Date
huangzd1997 e36d363c57 fix(common-tools): 资料下载改挂站点 /static/ 静态位
/materials/ 是不在发布白名单里的新目录,deploy_app_web.py 只上传
index.html+assets+/static/ 清单 ⇒ 上线即 404。改走既有 /static/(nginx 已配),
发布清单在 ops 仓 deploy_app_web.py 同步登记 4 个文件。
2026-10-23 23:38:03 +08:00
huangzd1997 a68e6d9252 fix(config): dedupe 文件槽位等待登记进 application.yml(隐式开关棘轮转绿) 2026-10-23 23:34:15 +08:00
huangzd1997 8fcf041a4e test(common-tools): 定价换算与五国ASIN页挂载冒烟测试
补两页的行为覆盖:换算四值与站点切换清空、资料下载/常用链接出口形态、
ASIN 规范化与域名随国家切换、位数不足报错并禁用打开按钮。
2026-10-23 23:08:23 +08:00
huangzd1997 a1c21068d5 refactor(common-tools): 关键词共享页四个弹窗拆成子组件,958→573 行
可读性棘轮用户端「新增超长文件」判红(新建页 958 行超 800 上限)——按只许减的口径
拆而不是点名放行:添加/词库统计/随机抽词/团队共享记录四个弹窗各自成组件
(keyword-share/*Dialog.vue,状态、请求、分页自持),页面只留筛选/统计卡/表格与
开关 ref。行为不变;随机抽词改为弹窗内 loading(原先是按钮文案态)。
vue-tsc 0 错;npm test 927 pass / mount 5 文件全过。
2026-10-23 23:05:05 +08:00
huangzd1997 de4465730f fix(common-tools): 关键词共享按后端真实返回接统计四段与日志字段
/stats-grouped 是四段各自 GROUP BY(对齐桌面版弹窗),前端原先按
「国家×一级类目」交叉表建模 ⇒ 弹窗恒为暂无数据;/logs 返回 userName/time
而表格读 operator/createdAt ⇒ 两列恒为占位符。改为 API 层映射 + 四段渲染,
日志去掉后端不产出的「说明」列。
2026-10-23 23:00:33 +08:00
huangzd1997 807ecaabe1 fix(common-tools): 资料下载锚点过 safeHttpUrl 出口守卫
safe-url-and-row-id 守卫要求动态 :href 一律过协议过滤,下载锚点补套
safeHttpUrl(与本页外链锚点同款写法)。
2026-10-23 22:41:27 +08:00
huangzd1997 b5d08ed77e feat(common-tools): 定价换算页补常用链接与资料下载区
链接清单与桌面版资料页同源(2026-10-23 实测可达),4 个资料文件随
public/materials/ 部署(目录名避开 .gitignore 的 downloads/ 规则)。
2026-10-23 22:38:06 +08:00
huangzd1997 eb6dd9bf44 feat(gating): 后管新增品牌Gating库管理页
菜单权限键 admin_brand_gating(admin_group_data 组),路由 asin-center/gating;
统计卡+五字段筛选+13 列表格+新增/删除/启停用/批量导入(粘文本 { text }
整段交后端,前端同款 parse_batch 规则只做行数预览);companyScoped=false
(biz_brand_gating 全平台共享表口径)。补 ops-cell 操作列宽登记与操作提示条。
2026-10-23 21:57:05 +08:00
huangzd1997 208583e14c feat(common-tools): 工具台新增常用工具分类,三个桌面工具网页化
- tool-catalog 最前插入「常用工具」组(brand_common_tools,23 工具·4 大分类),
  三工具各自权限键:pricing-calc / five-asin / keyword-share
- /pricing-calc:站点(英 GBP÷9、德法 EUR÷8)+ 买货价/运费 → 营业额/利润/
  利润率/换算汇率,公式与精度照抄桌面版,纯函数 node --test 钉住
- /five-asin:五国常量 + ASIN 10 位校验 → 亚马逊商品链接,window.open 与
  :href 出口全部过 safeHttpUrl(url-outlet 棘轮),邮编一键复制
- /keyword-share:筛选/四级类目级联/统计卡/表格分页 200/标记与取消使用/
  添加/删除/随机抽词/共享记录,对接 keywordshare 后端九端点
- 顶栏与 BrandTopBar 导航同步;router 注册三路由;补 test:mount 脚本
  (vitest mount 套件入口,与后管同口径)
2026-10-23 21:56:56 +08:00
huangzd1997 2c85f47c3b feat(brandgating): 品牌Gating库后端模块并入管理后台
直连主库既有 biz_brand_gating 表(原独立小网页后台 8090 下线的前置),
六端点挂 /api/admin/brand-gating,权限键 admin_brand_gating,走 admin
第一道门前缀守卫无需额外登记。列表/统计/新增/删除/启停用对齐桌面版
(停用记 disabled_at、恒刷 updated_at);批量导入 { text } 按桌面版
parse_batch 逐条同款解析,去重靠唯一键+批内四键预检。
SQL 账本 115→124、select 账本 78→86,8 条 select 点入 NOT_MIGRATED。
2026-10-23 21:56:45 +08:00
huangzd1997 2d408199d6 feat(keywordshare): 五国关键词共享后端模块(桌面工具网页化)
词本体直接读写主库既有 keyword_share 表(已是全量词库),使用状态
keyword_usage,留痕 keyword_add_log/keyword_delete_log;不建新表、无 SQLite。
九个端点挂 /api/keyword-share,权限键 keyword-share,companyId 服务端取。
列表排序对齐桌面版:已使用在前、按 used_time 升序;添加查重跳过+留痕+
自动标记已使用;随机抽词从未使用池。
SQL 账本 99→115(16 条新写语句)、select 账本 67→78(11 条),逐条点入
NOT_MIGRATED;ApiGuardCoverageAuditor METHOD_GUARDED_PATHS 登记
/api/keyword-share(9 端点全部 handler 内 requireConsolePageAccess 把门)。
FILTER_* 命名避开任务状态词表棘轮。
2026-10-23 21:54:52 +08:00
huangzd1997 6d9d4b98b2 feat(permission): 常用工具与品牌Gating库菜单权限落库
- V163:APP 一级分组 brand_common_tools(105,排前端工具之前)+三工具子菜单
  pricing-calc / five-asin / keyword-share,按公司复制树,全量账号默认授组行
  (读侧展开子孙,管理员可收组、可按工具单独授)
- V164:admin 菜单 admin_brand_gating(admin_group_data 组,sort 36),
  按公司复制树,不预授(管理员在菜单权限配置自行分配)
- PermissionMenuSchemaInitializer 两处默认种子同步登记
2026-10-23 21:54:39 +08:00
huangzd1997 09dfb01f9c feat(unreachable-cooldown): 连接层不可达客户端级冷却,四家判定下沉共用
BACKLOG B24(窗长 300s 已拍板):连续 2 次连接未建立即对该 host 进
冷却窗,窗内调用零探针快速失败(归因文案与调用内短路逐字一致);到期
后下一条真实调用兼作探针,失败重进、成功恢复。isConnectFailure 四处
副本(BrandCheckClient/AppearancePatentLlmClient/SimilarAsinLlmClient/
TaskOwnerForwardService)下沉 common/retry 共用。三家客户端接入同一
冷却表(两个 LLM 客户端共用 ai.t8star.org 一格,16890 独立一格),
窗长可配(0=关闭退回调用内短路)。另修 CollectDataBrandBatchFilter
首批预算竞态(1ms 预算下 GC 停顿误切首批,照 BrandCheckClient 首轮
不掐契约补 start>0 守卫)。变异验证:冷却禁用 6 例判红;定向 47/47 绿。
2026-10-23 20:58:32 +08:00
huangzd1997 6af19b51ba fix(similar-asin): LLM 连接层故障短路,行级写明服务不可达真因
BACKLOG B23:网关不可达时 invokeChat 原本对任何异常跑满 3 次阶梯,
批量失败再逐行回退把白等乘上行数。现对齐 BrandCheckClient /
AppearancePatentLlmClient 同款口径:连续 2 次连接未建立(读超时与
5xx 不计入且清零证据)即停,抛「LLM 服务不可达(连接未建立,非本行
数据问题)」。变异验证 23.4s 判红→收口 7.7s 绿;后端整量 4709 例
0 红 0 错 BUILD SUCCESS。
2026-10-23 19:47:46 +08:00
huangzd1997 b65b709700 fix(appearance-patent): LLM 连接层故障短路,行级写明服务不可达真因
invokeChat 的重试阶梯原先不区分故障层:网关根本连不上也把 llmRetryTimes
打满。照 BrandCheckClient 的 SERVICE_UNREACHABLE 同口径补短路——同一次
调用连续 2 次连接未建立(ConnectException/NoRouteToHost/UnknownHost,
顺 cause 链)且中间无一次成功 ⇒ 停止重试,行级原因写成「LLM 服务不可达
(连接未建立,非本行数据问题)」。读超时与 5xx 故意不算,非连接失败
清零证据。结论一字不变,差别只在耗时与归因文案。
新增 2 例互为非零对照:死端口必须在第 2 次连接失败处停(第 3 次不发);
连得上但 5xx 要跑满整个阶梯。
2026-10-23 18:00:34 +08:00
huangzd1997 70b3be63a9 fix(dedupe): 文件处理槽位等待加上限,等不到判失败并写明原因
原先 Semaphore.acquire() 无超时:一个文件卡在上游读取或 OSS 上传时,
同批与后续任务的每个文件都无限排在这 4 个槽位上——任务不落终态、
进度冻在已提交那格。改为 tryAcquire(默认 600s,可配
aiimage.dedupe.run.file-slot-wait-seconds),等不到把该文件判失败
并写明「是没排到槽位」,不再是静默续等。新增 2 例互为对照:
槽位占满必须到点返回 false 且不占槽位;有空槽位照常放行。
2026-10-23 18:00:17 +08:00
123 changed files with 8937 additions and 87 deletions
@@ -68,6 +68,10 @@ export const OPERATION_GUIDES: Record<string, OperationGuideData> = {
text: '店铺信息按分组管理。长商城名会自动缩略,悬停即可查看完整内容。',
steps: ['选择分组', '维护店铺信息', '筛选核对结果'],
},
admin_brand_gating: {
text: '维护不符合规则的品牌(品牌Gating/类目Gating)。添加后可用筛选快速回查;云端Gating库与前端排查软件同步同一份数据,排查软件命中后会自动在此入库。',
steps: ['新增或批量导入', '筛选回查', '停用或删除'],
},
admin_shop_data_crawl_tasks: {
text: '店铺数据任务按状态和时间筛选。批量操作前请核对已选任务。',
steps: ['筛选任务', '检查状态', '执行批量操作'],
@@ -0,0 +1,759 @@
<script setup lang="ts">
/** 品牌Gating库(biz_brand_gating 并入管理后台)· 自绘列表页,布局沿用 DedupeRegistryPage 的
* panel-box + 双行筛选 + 旧式表格 + OldPagination,另加统计卡与新增/批量导入弹窗。 */
import { computed, onMounted, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDateTime } from '@/utils/datetime'
import OldPagination from '@/components/OldPagination.vue'
import LoadFailureBar from '@/components/LoadFailureBar.vue'
import { showAdminFeedback } from '@/components/admin-feedback-ui'
import { loadFailureMessage } from '@/components/load-failure-model'
import { openAdminConfirm } from '@/components/admin-confirm'
import {
createGating,
deleteGating,
fetchGatingPage,
fetchGatingStats,
importGatingText,
updateGatingStatus,
} from './gating-api.ts'
import {
GATING_COUNTRIES,
GATING_DEFAULT_PAGE_SIZE,
GATING_PAGE_SIZES,
GATING_SOURCES,
GATING_TYPES,
createGatingFilterState,
gatingStatusLabel,
toGatingListParams,
type GatingFilterState,
type GatingItem,
type GatingStats,
} from './gating-model.ts'
import { gatingImportOutcomeText, parseGatingImportText } from './gating-import.ts'
const loading = ref(false)
const loadFailure = ref('')
const rows = ref<GatingItem[]>([])
const total = ref(0)
const page = ref(1)
const pageSize = ref(GATING_DEFAULT_PAGE_SIZE)
const filter = reactive(createGatingFilterState())
const stats = ref<GatingStats | null>(null)
const addVisible = ref(false)
const addSaving = ref(false)
const addForm = reactive({
brand: '',
asin: '',
country: '全部',
gatingType: '品牌gating',
source: 'MANUAL',
shopName: '',
remark: '',
})
const importVisible = ref(false)
const importRunning = ref(false)
const importText = ref('')
/** 导入前行数预览:同一份解析规则桌面版/后端共用,前端只做提示,不做拦截。 */
const importPreviewCount = computed(() => parseGatingImportText(importText.value).items.length)
const totalPages = computed(() => Math.max(1, Math.ceil(total.value / pageSize.value)))
async function loadStats(): Promise<void> {
try {
stats.value = await fetchGatingStats()
} catch {
// 统计卡失败不拦列表:留空展示 0,列表自己的失败条会说话。
stats.value = null
}
}
async function load(): Promise<void> {
loading.value = true
loadFailure.value = ''
try {
const result = await fetchGatingPage(toGatingListParams(filter, page.value, pageSize.value))
rows.value = result.rows
total.value = result.total
} catch (error) {
loadFailure.value = loadFailureMessage(error, 'Gating列表加载失败')
showAdminFeedback(loadFailure.value, 'error')
} finally {
loading.value = false
}
}
function apply(): void {
page.value = 1
void load()
}
function resetFilter(): void {
Object.assign(filter, createGatingFilterState())
apply()
}
function changePage(next: number): void {
if (next < 1 || next > totalPages.value) return
page.value = next
void load()
}
function changeSize(size: number): void {
pageSize.value = size
page.value = 1
void load()
}
function openAdd(): void {
addForm.brand = ''
addForm.asin = ''
addForm.country = '全部'
addForm.gatingType = '品牌gating'
addForm.source = 'MANUAL'
addForm.shopName = ''
addForm.remark = ''
addVisible.value = true
}
async function submitAdd(): Promise<void> {
const brand = addForm.brand.trim()
if (!brand) {
ElMessage.warning('请填写品牌')
return
}
addSaving.value = true
try {
await createGating({
brand,
asin: addForm.asin.trim(),
country: addForm.country,
gatingType: addForm.gatingType,
source: addForm.source,
shopName: addForm.shopName.trim(),
remark: addForm.remark.trim(),
})
addVisible.value = false
ElMessage.success('新增成功')
await Promise.all([load(), loadStats()])
} catch (error) {
showAdminFeedback(loadFailureMessage(error, '新增失败'), 'error')
} finally {
addSaving.value = false
}
}
function openImport(): void {
importText.value = ''
importVisible.value = true
}
async function submitImport(): Promise<void> {
const text = importText.value.trim()
if (!text) {
ElMessage.warning('请粘贴批量数据')
return
}
importRunning.value = true
try {
const result = await importGatingText(text)
importVisible.value = false
ElMessage.success(gatingImportOutcomeText(result.added, result.skipped))
await Promise.all([load(), loadStats()])
} catch (error) {
showAdminFeedback(loadFailureMessage(error, '导入失败'), 'error')
} finally {
importRunning.value = false
}
}
/** 启用/停用切换:停用需二次确认(解除后排查不再拦截),恢复直接执行。 */
async function toggleStatus(row: GatingItem): Promise<void> {
const next: 0 | 1 = row.status === 1 ? 0 : 1
if (next === 0) {
const confirmed = await openAdminConfirm({ message: `确认停用品牌「${row.brand}」?停用后排查自动跳过该品牌,不再拦截`, danger: true })
if (!confirmed) return
}
try {
await updateGatingStatus(row.id, next)
ElMessage.success(next === 1 ? '已恢复生效' : '已停用,排查不再拦截')
await Promise.all([load(), loadStats()])
} catch (error) {
showAdminFeedback(loadFailureMessage(error, '状态更新失败'), 'error')
}
}
async function removeRow(row: GatingItem): Promise<void> {
const confirmed = await openAdminConfirm({ message: `确认删除 ID=${row.id} 的Gating记录(品牌:${row.brand})?`, danger: true })
if (!confirmed) return
try {
await deleteGating(row.id)
ElMessage.success('已删除')
await Promise.all([load(), loadStats()])
} catch (error) {
showAdminFeedback(loadFailureMessage(error, '删除失败'), 'error')
}
}
onMounted(() => {
void load()
void loadStats()
})
</script>
<template>
<div class="gating-view">
<section class="panel-box">
<div class="gating-panel-head">
<h3>品牌Gating库</h3>
<div class="gating-head-actions">
<button class="btn" type="button" @click="openAdd">新增</button>
<button class="btn" type="button" @click="openImport">批量导入</button>
</div>
</div>
<div class="gating-cards">
<div class="stat-card">
<div class="stat-n">{{ stats?.total ?? 0 }}</div>
<div class="stat-t">Gating总记录</div>
</div>
<div class="stat-card stat-active">
<div class="stat-n">{{ stats?.active ?? 0 }}</div>
<div class="stat-t">生效中</div>
</div>
<div class="stat-card stat-disabled">
<div class="stat-n">{{ stats?.disabled ?? 0 }}</div>
<div class="stat-t">停用</div>
</div>
<div class="stat-card stat-brands">
<div class="stat-n">{{ stats?.brands ?? 0 }}</div>
<div class="stat-t">去重品牌数</div>
</div>
<div v-for="group in [
{ title: '按Gating类型', data: stats?.byType ?? [] },
{ title: '按国家', data: stats?.byCountry ?? [] },
{ title: '按来源', data: stats?.bySource ?? [] },
]" :key="group.title" class="stat-group">
<div class="stat-group-title">{{ group.title }}</div>
<div class="stat-group-body">
<span v-for="entry in group.data" :key="entry.name" class="stat-chip">
{{ entry.name }} <b>{{ entry.count }}</b>
</span>
<span v-if="!group.data.length" class="stat-chip stat-chip-empty">暂无</span>
</div>
</div>
</div>
<div class="gating-filter-row">
<div class="form-group">
<label>关键词</label>
<input v-model="filter.keyword" type="text" placeholder="品牌 / ASIN / 备注" @keyup.enter="apply" />
</div>
<div class="form-group">
<label>国家</label>
<select v-model="filter.country">
<option value="">全部国家</option>
<option v-for="country in GATING_COUNTRIES" :key="country" :value="country">{{ country }}</option>
</select>
</div>
<div class="form-group">
<label>Gating类型</label>
<select v-model="filter.gatingType">
<option value="">全部类型</option>
<option v-for="gatingType in GATING_TYPES" :key="gatingType" :value="gatingType">{{ gatingType }}</option>
</select>
</div>
<div class="form-group">
<label>状态</label>
<select v-model="filter.status">
<option value="">全部状态</option>
<option value="1">生效</option>
<option value="0">停用</option>
</select>
</div>
<div class="form-group">
<label>来源</label>
<select v-model="filter.source">
<option value="">全部来源</option>
<option v-for="source in GATING_SOURCES" :key="source" :value="source">{{ source }}</option>
</select>
</div>
<div class="gating-filter-actions">
<button class="btn" type="button" @click="apply">查询</button>
<button class="btn btn-secondary" type="button" @click="resetFilter">重置</button>
</div>
</div>
<LoadFailureBar v-if="loadFailure" :message="loadFailure" @retry="load" />
<div class="table-scroll gating-table-scroll">
<table>
<thead>
<tr>
<th style="width: 60px">ID</th>
<th>品牌</th>
<th>ASIN</th>
<th>国家</th>
<th>Gating类型</th>
<th style="width: 70px">状态</th>
<th style="width: 80px">来源</th>
<th>店铺</th>
<th>备注</th>
<th style="width: 150px">创建时间</th>
<th style="width: 150px">更新时间</th>
<th style="width: 150px">停用时间</th>
<th style="width: 140px">操作</th>
</tr>
</thead>
<tbody>
<template v-if="rows.length">
<tr v-for="row in rows" :key="row.id">
<td>{{ row.id }}</td>
<td class="brand-cell">{{ row.brand }}</td>
<td class="mono-cell">{{ row.asin || '-' }}</td>
<td>{{ row.country }}</td>
<td><span class="type-tag">{{ row.gatingType }}</span></td>
<td>
<span class="status-tag" :class="row.status === 1 ? 'status-on' : 'status-off'">
{{ gatingStatusLabel(row.status) }}
</span>
</td>
<td>{{ row.source === 'AUTO' ? '自动同步' : '手动' }}</td>
<td>{{ row.shopName || '-' }}</td>
<td :title="row.remark">{{ row.remark || '-' }}</td>
<td class="time-cell">{{ formatDateTime(row.createdAt) }}</td>
<td class="time-cell">{{ formatDateTime(row.updatedAt) }}</td>
<td class="time-cell">{{ formatDateTime(row.disabledAt) }}</td>
<td class="ops-cell">
<button class="btn btn-sm" type="button" @click="toggleStatus(row)">
{{ row.status === 1 ? '停用' : '恢复' }}
</button>
<button class="btn btn-sm btn-danger" type="button" @click="removeRow(row)">删除</button>
</td>
</tr>
</template>
<tr v-else-if="loading">
<td colspan="13" class="empty-tip">加载中...</td>
</tr>
<tr v-else>
<td colspan="13" class="empty-tip">暂无Gating记录</td>
</tr>
</tbody>
</table>
</div>
<OldPagination
:total="total"
:page="page"
:page-size="pageSize"
:sizes="[10, 20, 50]"
@change="changePage"
@size-change="changeSize"
/>
</section>
<div v-if="addVisible" class="modal-mask" @click.self="addVisible = false">
<div class="modal">
<h3>新增Gating品牌</h3>
<div class="form-group">
<label>品牌(必填)</label>
<input v-model="addForm.brand" type="text" placeholder="请输入品牌" @keyup.enter="submitAdd" />
</div>
<div class="form-group">
<label>国家</label>
<select v-model="addForm.country">
<option v-for="country in GATING_COUNTRIES" :key="country" :value="country">{{ country }}</option>
</select>
</div>
<div class="form-group">
<label>Gating类型</label>
<select v-model="addForm.gatingType">
<option v-for="gatingType in GATING_TYPES" :key="gatingType" :value="gatingType">{{ gatingType }}</option>
</select>
</div>
<div class="form-group">
<label>来源</label>
<select v-model="addForm.source">
<option v-for="source in GATING_SOURCES" :key="source" :value="source">{{ source }}</option>
</select>
</div>
<div class="form-group">
<label>ASIN(可选)</label>
<input v-model="addForm.asin" type="text" placeholder="请输入 ASIN" />
</div>
<div class="form-group">
<label>店铺(可选)</label>
<input v-model="addForm.shopName" type="text" placeholder="请输入店铺名" />
</div>
<div class="form-group">
<label>备注(可选)</label>
<input v-model="addForm.remark" type="text" placeholder="请输入备注" />
</div>
<div class="modal-actions">
<button class="btn" type="button" :disabled="addSaving" @click="submitAdd">保存</button>
<button class="btn btn-secondary" type="button" :disabled="addSaving" @click="addVisible = false">取消</button>
</div>
</div>
</div>
<div v-if="importVisible" class="modal-mask" @click.self="importVisible = false">
<div class="modal">
<h3>批量导入</h3>
<div class="form-group">
<label>每行一条:品牌[,国家[,类型[,ASIN[,备注]]]],支持逗号/分号/Tab 分隔;# 开头为注释行</label>
<textarea
v-model="importText"
class="import-textarea"
rows="10"
placeholder="示例:
tory burch,英国,品牌gating
jellycat,全部,品牌gating
crocs,德国,类目gating"
></textarea>
</div>
<div class="import-preview">将导入 {{ importPreviewCount }} 行(空行与注释行自动跳过)</div>
<div class="modal-actions">
<button class="btn" type="button" :disabled="importRunning" @click="submitImport">导入</button>
<button class="btn btn-secondary" type="button" :disabled="importRunning" @click="importVisible = false">取消</button>
</div>
</div>
</div>
</div>
</template>
<style scoped>
/* 布局与配色沿用去重总数据页(panel-box 蓝白末层样式)。 */
.gating-view {
font-family: inherit;
color: var(--c-text, #24384d);
display: flex;
flex-direction: column;
gap: 18px;
}
.panel-box {
width: 100%;
min-width: 0;
padding: 20px 22px 24px;
border: 1px solid #d8e3ee;
border-radius: 14px;
background: linear-gradient(145deg, #ffffff, #f9fbfd);
box-shadow: 0 1px 2px rgba(39, 67, 94, 0.04), 0 14px 30px -24px rgba(39, 67, 94, 0.28);
}
h3 {
margin: 0 0 16px;
font-size: 15px;
font-weight: 650;
color: #24384d;
letter-spacing: 0.2px;
}
.gating-panel-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.gating-panel-head h3 {
margin: 0;
}
.gating-head-actions {
display: flex;
gap: 10px;
}
.gating-cards {
display: flex;
gap: 12px;
flex-wrap: wrap;
align-items: stretch;
margin-bottom: 16px;
}
.stat-card {
flex: 0 0 132px;
padding: 12px 14px;
border: 1px solid #d8e3ee;
border-top: 3px solid #2dd4bf;
border-radius: 10px;
background: #ffffff;
}
.stat-card.stat-active { border-top-color: #34d399; }
.stat-card.stat-disabled { border-top-color: #9ca3af; }
.stat-card.stat-brands { border-top-color: #a78bfa; }
.stat-n {
font-size: 24px;
font-weight: 700;
color: #24384d;
}
.stat-t {
margin-top: 2px;
font-size: 12px;
color: #7a8194;
}
.stat-group {
flex: 1 1 200px;
min-width: 0;
padding: 10px 14px;
border: 1px solid #d8e3ee;
border-radius: 10px;
background: #ffffff;
}
.stat-group-title {
font-size: 12px;
font-weight: 600;
color: #5b6f83;
margin-bottom: 6px;
}
.stat-group-body {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.stat-chip {
padding: 2px 8px;
border-radius: 6px;
background: #edf4fa;
color: #4e6479;
font-size: 12px;
white-space: nowrap;
}
.stat-chip b {
color: #2f5d8b;
}
.stat-chip-empty {
color: #9baaba;
}
.gating-filter-row {
display: grid;
grid-template-columns: repeat(5, minmax(140px, 1fr)) auto;
gap: 12px;
align-items: end;
margin-bottom: 16px;
}
.gating-filter-row .form-group {
min-width: 0;
}
.gating-filter-actions {
display: flex;
gap: 10px;
align-items: center;
white-space: nowrap;
}
.form-group {
display: flex;
flex-direction: column;
gap: 7px;
margin-bottom: 0;
}
.form-group label {
color: #5b6f83;
font-size: 12.5px;
font-weight: 600;
}
.form-group input,
.form-group select,
.form-group textarea {
min-width: 0;
min-height: 42px;
padding: 8px 12px;
border: 1px solid #cbd9e6;
border-radius: 9px;
background: #f8fbfd;
color: #24384d;
font-size: 13.5px;
font-family: inherit;
color-scheme: light;
outline: none;
transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}
.form-group textarea {
min-height: 0;
resize: vertical;
line-height: 1.6;
}
.form-group input:hover,
.form-group select:hover,
.form-group textarea:hover {
border-color: #9fb7cd;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
background: #ffffff;
border-color: #5f85ad;
box-shadow: 0 0 0 3px rgba(95, 133, 173, 0.16);
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
min-height: 42px;
padding: 9px 18px;
border: 1px solid #4f78a5;
border-radius: 9px;
background: linear-gradient(135deg, #5f85ad, #4f78a5);
color: #ffffff;
font-family: inherit;
font-size: 13.5px;
cursor: pointer;
box-shadow: 0 8px 18px -14px rgba(79, 120, 165, 0.72);
transition: background 160ms ease, box-shadow 160ms ease, transform 120ms ease;
}
.btn:hover:not(:disabled) {
background: linear-gradient(135deg, #7094ba, #5d83ac);
box-shadow: 0 11px 22px -14px rgba(79, 120, 165, 0.8);
}
.btn:disabled {
cursor: not-allowed;
opacity: 0.6;
}
.btn-secondary {
background: #ffffff;
color: #5b6f83;
border-color: #c7d7e5;
}
.btn-secondary:hover:not(:disabled) {
color: #2f5d8b;
border-color: #95b1cb;
background: #edf5fb;
}
.btn-danger {
background: linear-gradient(135deg, #c06d77, #b35f6a);
border-color: #b35f6a;
}
.btn-danger:hover:not(:disabled) {
background: linear-gradient(135deg, #cb7c84, #b96570);
}
.btn-sm {
min-height: 36px;
padding: 7px 12px;
}
.table-scroll {
width: 100%;
min-width: 0;
overflow-x: auto;
border: 1px solid #dbe5ee;
border-radius: 10px;
background: #ffffff;
}
.table-scroll table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}
.gating-table-scroll > table {
min-width: 1360px;
}
.table-scroll th,
.table-scroll td {
padding: 10px 12px;
text-align: left;
font-size: 13.5px;
line-height: 1.5;
border-bottom: 1px solid #e0e8ef;
vertical-align: middle;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.table-scroll th {
background: #edf4fa;
color: #4e6479;
border-bottom-color: #d5e1eb;
font-size: 12.5px;
font-weight: 600;
letter-spacing: 0.4px;
}
.table-scroll tbody tr:hover td {
background: #f1f7fb;
}
.table-scroll tbody tr:last-child td {
border-bottom: 0;
}
.brand-cell {
font-weight: 650;
}
.mono-cell {
font-family: "Cascadia Mono", "SF Mono", Consolas, "Courier New", monospace;
font-size: 12.5px;
}
.time-cell {
color: #6b7280;
font-size: 12.5px;
}
.type-tag {
display: inline-block;
padding: 2px 8px;
border-radius: 6px;
background: #fef3c7;
color: #b45309;
font-size: 12px;
}
.status-tag {
display: inline-block;
padding: 2px 8px;
border-radius: 6px;
font-size: 12px;
}
.status-on {
background: #d1fae5;
color: #10b981;
}
.status-off {
background: #e5e7eb;
color: #9ca3af;
}
.ops-cell {
display: flex;
align-items: center;
gap: 8px;
white-space: nowrap;
text-align: right;
flex-wrap: wrap;
}
.empty-tip {
padding: 44px 24px;
text-align: center;
color: #8293a5;
font-size: 13.5px;
}
.modal-mask {
position: fixed;
inset: 0;
z-index: 3000;
display: flex;
align-items: center;
justify-content: center;
padding: 40px 20px;
background: rgba(45, 66, 86, 0.36);
backdrop-filter: blur(6px);
}
.modal {
display: flex;
flex-direction: column;
width: min(560px, 100%);
max-height: 84vh;
padding: 22px 24px;
border: 1px solid #c7d7e5;
border-radius: 14px;
background: #ffffff;
box-shadow: 0 22px 60px rgba(39, 67, 94, 0.2);
overflow-y: auto;
}
.modal .form-group {
margin-bottom: 14px;
}
.modal-actions {
display: flex;
gap: 10px;
margin-top: 16px;
}
.import-textarea {
height: 180px;
}
.import-preview {
font-size: 12.5px;
color: #5b6f83;
}
</style>
@@ -0,0 +1,75 @@
/** 品牌Gating库 API 适配:GET/POST/DELETE /api/admin/brand-gating(契约路径与字段不得擅改)。 */
import { http } from '@/api/http'
import { unwrap } from '@/api/envelope'
import {
parseGatingPage,
parseGatingStats,
toGatingItem,
type GatingItem,
type GatingPageResult,
type GatingStats,
} from './gating-model.ts'
export const GATING_ENDPOINT = '/api/admin/brand-gating'
/** 列表:GET /api/admin/brand-gating?page&size&keyword&country&gatingType&status&source → { total, rows }。 */
export async function fetchGatingPage(params: Record<string, string | number>): Promise<GatingPageResult> {
const { data } = await http.get<unknown>(GATING_ENDPOINT, { params })
return parseGatingPage(data)
}
/** 统计:GET /api/admin/brand-gating/stats → { total, active, disabled, brands, byType, byCountry, bySource }。 */
export async function fetchGatingStats(): Promise<GatingStats> {
const { data } = await http.get<unknown>(`${GATING_ENDPOINT}/stats`)
return parseGatingStats(data)
}
/** 单条新增字段(驼峰);source/shopName 可缺省由后端落默认。 */
export interface GatingCreatePayload {
brand: string
asin: string
country: string
gatingType: string
source: string
shopName: string
remark: string
}
/** 新增:POST /api/admin/brand-gating → { added }。 */
export async function createGating(payload: GatingCreatePayload): Promise<number> {
const { data } = await http.post<unknown>(GATING_ENDPOINT, payload)
const core = unwrap<Record<string, unknown>>(data)
const added = core && typeof core === 'object' ? Number((core as Record<string, unknown>).added) : NaN
return Number.isFinite(added) ? added : 0
}
/** 删除:DELETE /api/admin/brand-gating/{id} → { deleted }。 */
export async function deleteGating(id: number): Promise<void> {
const { data } = await http.delete<unknown>(`${GATING_ENDPOINT}/${id}`)
unwrap<unknown>(data)
}
/**
* 启用/停用:POST /api/admin/brand-gating/{id}/status body { status } → { updated }。
* 停用(status=0)由后端记 disabled_at=NOW(),恢复(status=1)清空 disabled_at。
*/
export async function updateGatingStatus(id: number, status: 0 | 1): Promise<void> {
const { data } = await http.post<unknown>(`${GATING_ENDPOINT}/${id}/status`, { status })
unwrap<unknown>(data)
}
/** 批量导入:POST /api/admin/brand-gating/import body { text } → { added, skipped }。原始文本整段交后端解析。 */
export async function importGatingText(text: string): Promise<{ added: number; skipped: number }> {
const { data } = await http.post<unknown>(`${GATING_ENDPOINT}/import`, { text })
const core = unwrap<Record<string, unknown>>(data)
const record = (core && typeof core === 'object' ? core : {}) as Record<string, unknown>
const added = Number(record.added)
const skipped = Number(record.skipped)
return {
added: Number.isFinite(added) ? added : 0,
skipped: Number.isFinite(skipped) ? skipped : 0,
}
}
/** 单条解析出口(供需要单取的场景复用,例如挂载测试构造数据)。 */
export { toGatingItem, type GatingItem }
@@ -0,0 +1,66 @@
/** 批量导入文本解析(照抄桌面版 gating_web_admin.py parse_batch 的行格式与跳过规则)。
* 纯逻辑、零依赖(tests/gating-import.test.ts 用 node --test 直连本文件)。
*
* 口径(与桌面版 L208-223 逐条对齐):
* - 按行拆分;每行去首尾空白与 BOM(\uFEFF);
* - 空行与以 # 开头的注释行跳过;
* - 行内按中英文逗号/分号/制表符([,\t;,;])切分,各段去空白后丢掉空段;
* - 字段序:品牌(必留)、国家(缺省「全部」)、Gating类型(缺省「品牌gating」)、ASIN(缺省空)、备注(缺省空);
* 第 5 列之后的段照桌面版丢弃。
*
* 前端只用它做导入前的行数预览;真正提交仍把原始文本整段交给后端
* (POST /api/admin/brand-gating/import { text },去重与入库由后端按同一份规则解析)。 */
export interface GatingImportItem {
brand: string
country: string
gatingType: string
asin: string
remark: string
}
export interface GatingImportParseResult {
items: GatingImportItem[]
/** 因空行/注释行/纯分隔符行被跳过的行数。 */
skippedLines: number
}
export const GATING_IMPORT_DEFAULT_COUNTRY = '全部'
export const GATING_IMPORT_DEFAULT_TYPE = '品牌gating'
/** 与桌面版一致的列分隔符:中英文逗号、中英文分号、制表符。 */
const FIELD_SPLIT_RE = /[,\t;,;]/
/** 解析批量导入文本;永不抛错(坏行计入 skippedLines)。 */
export function parseGatingImportText(text: string): GatingImportParseResult {
const items: GatingImportItem[] = []
let skippedLines = 0
const lines = String(text ?? '').split(/\r?\n/)
for (const rawLine of lines) {
const line = rawLine.trim().replace(/^\uFEFF/, '').trim()
if (!line || line.startsWith('#')) {
if (rawLine.trim()) skippedLines += 1
continue
}
const parts = line
.split(FIELD_SPLIT_RE)
.map((part) => part.trim())
.filter((part) => part.length > 0)
if (!parts.length) {
skippedLines += 1
continue
}
items.push({
brand: parts[0],
country: parts[1] ?? GATING_IMPORT_DEFAULT_COUNTRY,
gatingType: parts[2] ?? GATING_IMPORT_DEFAULT_TYPE,
asin: parts[3] ?? '',
remark: parts[4] ?? '',
})
}
return { items, skippedLines }
}
/** 导入完成反馈文案:新增 X 条,跳过重复 Y 条。 */
export function gatingImportOutcomeText(added: number, skipped: number): string {
return `批量导入完成:新增 ${added} 条,跳过重复 ${skipped} 条`
}
@@ -0,0 +1,183 @@
/** 品牌Gating库页面模型(biz_brand_gating 并入管理后台):类型、字典、筛选态与响应解析。
* 纯逻辑、零依赖(tests/gating-model.test.ts 用 node --test 直连本文件,别 import 带 @ 别名/axios 的模块)。 */
export interface GatingItem {
id: number
brand: string
asin: string
country: string
gatingType: string
/** 1=生效 0=停用。 */
status: number
source: string
shopName: string
remark: string
createdAt?: string
updatedAt?: string
disabledAt?: string
}
export interface GatingPageResult {
rows: GatingItem[]
total: number
}
/** 分组统计单行(GET stats 的 byType/byCountry/bySource)。 */
export interface GatingNameCount {
name: string
count: number
}
export interface GatingStats {
total: number
active: number
disabled: number
brands: number
byType: GatingNameCount[]
byCountry: GatingNameCount[]
bySource: GatingNameCount[]
}
export interface GatingFilterState {
/** 品牌/ASIN/备注 LIKE 关键词。 */
keyword: string
country: string
gatingType: string
/** '' = 全部;'1' 生效;'0' 停用。 */
status: string
source: string
}
/** 国家字典:与桌面版 gating_web_admin 下拉一致;'全部' 是表内真实取值(不限国家时用空串)。 */
export const GATING_COUNTRIES = ['全部', '英国', '德国', '法国', '西班牙', '意大利'] as const
export const GATING_TYPES = ['品牌gating', '类目gating', '账户不符合'] as const
export const GATING_SOURCES = ['AUTO', 'MANUAL'] as const
/** 每页条数档位(默认 10,可选 20/50)。 */
export const GATING_PAGE_SIZES = [10, 20, 50] as const
export const GATING_DEFAULT_PAGE_SIZE = 10
export function createGatingFilterState(): GatingFilterState {
return { keyword: '', country: '', gatingType: '', status: '', source: '' }
}
function text(value: unknown): string {
return typeof value === 'string' ? value.trim() : ''
}
function intOr(value: unknown, fallback: number): number {
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : fallback
}
/** 解析单条记录;缺 id 视为无效。兼容 camel 与 snake 字段名。 */
export function toGatingItem(raw: unknown): GatingItem | null {
if (!raw || typeof raw !== 'object') return null
const record = raw as Record<string, unknown>
const id = intOr(record.id, 0)
if (id <= 0) return null
const item: GatingItem = {
id,
brand: text(record.brand),
asin: text(record.asin),
country: text(record.country),
gatingType: text(record.gatingType ?? record.gating_type),
status: intOr(record.status, 0),
source: text(record.source),
shopName: text(record.shopName ?? record.shop_name),
remark: text(record.remark),
}
const createdAt = text(record.createdAt ?? record.created_at)
const updatedAt = text(record.updatedAt ?? record.updated_at)
const disabledAt = text(record.disabledAt ?? record.disabled_at)
if (createdAt) item.createdAt = createdAt
if (updatedAt) item.updatedAt = updatedAt
if (disabledAt) item.disabledAt = disabledAt
return item
}
/** 解析列表负载({ total, rows };rows 兼容 items 别名),空负载回空页。 */
export function parseGatingPage(payload: unknown): GatingPageResult {
const out: GatingPageResult = { rows: [], total: 0 }
if (!payload || typeof payload !== 'object') return out
const record = payload as Record<string, unknown>
const rawRows = Array.isArray(record.rows) ? record.rows : Array.isArray(record.items) ? record.items : []
out.rows = rawRows
.map((raw) => toGatingItem(raw))
.filter((item): item is GatingItem => item !== null)
out.total = intOr(record.total, out.rows.length)
return out
}
/** 分组统计单组:数组 [{name,count}] 或桌面版遗留 dict({品牌gating: 3})都收。 */
export function parseGatingNameCountList(raw: unknown): GatingNameCount[] {
if (Array.isArray(raw)) {
const out: GatingNameCount[] = []
for (const item of raw) {
if (!item || typeof item !== 'object') continue
const record = item as Record<string, unknown>
const name = text(record.name)
if (!name) continue
out.push({ name, count: intOr(record.count, 0) })
}
return out
}
if (raw && typeof raw === 'object') {
const out: GatingNameCount[] = []
for (const [name, count] of Object.entries(raw as Record<string, unknown>)) {
if (!name) continue
out.push({ name, count: intOr(count, 0) })
}
return out
}
return []
}
/** 解析统计负载;缺省字段回 0/空数组。 */
export function parseGatingStats(payload: unknown): GatingStats {
const out: GatingStats = {
total: 0,
active: 0,
disabled: 0,
brands: 0,
byType: [],
byCountry: [],
bySource: [],
}
if (!payload || typeof payload !== 'object') return out
const record = payload as Record<string, unknown>
out.total = intOr(record.total, 0)
out.active = intOr(record.active, 0)
out.disabled = intOr(record.disabled, 0)
out.brands = intOr(record.brands, 0)
out.byType = parseGatingNameCountList(record.byType ?? record.by_type)
out.byCountry = parseGatingNameCountList(record.byCountry ?? record.by_country)
out.bySource = parseGatingNameCountList(record.bySource ?? record.by_source)
return out
}
/** 筛选态 + 分页 → 列表查询参数(page/size/keyword/country/gatingType/status/source);空白值不下发。 */
export function toGatingListParams(
state: GatingFilterState,
page: number,
pageSize: number,
): Record<string, string | number> {
const params: Record<string, string | number> = {
page: page >= 1 ? Math.floor(page) : 1,
size: pageSize >= 1 ? Math.floor(pageSize) : GATING_DEFAULT_PAGE_SIZE,
}
const keyword = (state.keyword || '').trim()
const country = (state.country || '').trim()
const gatingType = (state.gatingType || '').trim()
const status = (state.status || '').trim()
const source = (state.source || '').trim()
if (keyword) params.keyword = keyword
if (country) params.country = country
if (gatingType) params.gatingType = gatingType
if (status === '1' || status === '0') params.status = status
if (source) params.source = source
return params
}
/** 状态 → 展示文案;非 1 一律按停用。 */
export function gatingStatusLabel(status: unknown): string {
return status === 1 ? '生效' : '停用'
}
+2
View File
@@ -40,6 +40,8 @@ export const adminPages: AdminPageDef[] = [
{ path: 'asin-center/query', menuKey: 'admin_query_asin', title: '查询ASIN', companyScoped: true, load: () => import('@/pages/asin/QueryAsinPage.vue') },
{ path: 'asin-center/categories', menuKey: 'admin_product_categories', title: '商品类目', companyScoped: false, load: () => import('@/pages/asin/ProductCategoryPage.vue') },
{ path: 'asin-center/skip-price', menuKey: 'admin_skip_price_asin', title: '最低价ASIN', companyScoped: true, load: () => import('@/pages/asin/SkipPricePage.vue') },
// 品牌Gating库(biz_brand_gating):全平台共享表(桌面版 gating_web_admin 同源数据),不参与公司 tab。
{ path: 'asin-center/gating', menuKey: 'admin_brand_gating', title: '品牌Gating库', companyScoped: false, load: () => import('@/pages/gating/GatingPage.vue') },
]
export function routeOf(page: AdminPageDef): RouteRecordRaw {
@@ -0,0 +1,58 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { gatingImportOutcomeText, parseGatingImportText } from '../src/pages/gating/gating-import.ts'
test('gating_import_single_field_defaults', () => {
// 桌面版口径:只给品牌 → 国家=全部、类型=品牌gating、ASIN/备注空。
const result = parseGatingImportText('jellycat')
assert.equal(result.items.length, 1)
assert.deepEqual(result.items[0], { brand: 'jellycat', country: '全部', gatingType: '品牌gating', asin: '', remark: '' })
assert.equal(result.skippedLines, 0)
})
test('gating_import_full_five_fields', () => {
const result = parseGatingImportText('tory burch,英国,品牌gating,B0ABC123,人为录入')
assert.deepEqual(result.items[0], { brand: 'tory burch', country: '英国', gatingType: '品牌gating', asin: 'B0ABC123', remark: '人为录入' })
})
test('gating_import_mixed_separators_and_extra_fields_dropped', () => {
// 桌面版分隔符集合 [,\t;,;];第 5 列之后的段丢弃。
const result = parseGatingImportText('crocs,德国;类目gating\tB0X\t备注\t多余段')
assert.deepEqual(result.items[0], { brand: 'crocs', country: '德国', gatingType: '类目gating', asin: 'B0X', remark: '备注' })
})
test('gating_import_skip_blank_comment_and_separator_only_lines', () => {
const text = [
'',
' ',
'# 这是注释行',
';;,', // 纯分隔符行 → 空段全被丢,整行跳过
'jellycat',
].join('\n')
const result = parseGatingImportText(text)
assert.equal(result.items.length, 1)
assert.equal(result.items[0]?.brand, 'jellycat')
assert.equal(result.skippedLines, 2) // 注释行 + 纯分隔符行;纯空白行不计数
})
test('gating_import_bom_and_crlf_and_empty_parts', () => {
const result = parseGatingImportText('\uFEFFcrocs,德国,,B0X\r\n,头部空段被丢')
assert.equal(result.items.length, 2)
assert.equal(result.items[0]?.brand, 'crocs')
assert.equal(result.items[0]?.country, '德国')
// 桌面版口径:空段在切分后就被丢弃,后续字段整体左移(B0X 落到类型位)——照抄不修正。
assert.equal(result.items[0]?.gatingType, 'B0X')
assert.equal(result.items[0]?.asin, '')
assert.equal(result.items[1]?.brand, '头部空段被丢')
assert.equal(result.items[1]?.country, '全部')
})
test('gating_import_empty_input_never_throws', () => {
assert.deepEqual(parseGatingImportText(''), { items: [], skippedLines: 0 })
// @ts-expect-error 纯函数要扛住脏输入(运行时防呆)。
assert.deepEqual(parseGatingImportText(null), { items: [], skippedLines: 0 })
})
test('gating_import_outcome_text', () => {
assert.equal(gatingImportOutcomeText(3, 2), '批量导入完成:新增 3 条,跳过重复 2 条')
})
@@ -0,0 +1,99 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import {
createGatingFilterState,
gatingStatusLabel,
parseGatingNameCountList,
parseGatingPage,
parseGatingStats,
toGatingItem,
toGatingListParams,
} from '../src/pages/gating/gating-model.ts'
test('gating_filter_normal_primary_path', () => {
// 完整筛选映射到契约查询参数(page/size/keyword/country/gatingType/status/source)。
const state = { ...createGatingFilterState(), keyword: 'jellycat', country: '英国', gatingType: '品牌gating', status: '1', source: 'MANUAL' }
const params = toGatingListParams(state, 3, 20)
assert.equal(params.page, 3)
assert.equal(params.size, 20)
assert.equal(params.keyword, 'jellycat')
assert.equal(params.country, '英国')
assert.equal(params.gatingType, '品牌gating')
assert.equal(params.status, '1')
assert.equal(params.source, 'MANUAL')
})
test('gating_filter_boundary_blank_dropped', () => {
// 空筛选只带分页;非法 status(非 '0'/'1')不下发。
const params = toGatingListParams({ ...createGatingFilterState(), status: 'all', keyword: ' ' }, 0, -5)
assert.deepEqual(params, { page: 1, size: 10 })
})
test('gating_item_parse_normal_and_snake_fallback', () => {
const camel = toGatingItem({
id: 7, brand: 'jellycat', asin: 'B0ABC123', country: '英国', gatingType: '品牌gating',
status: 1, source: 'AUTO', shopName: '店A', remark: 'r',
createdAt: '2026-01-02 03:04:05', updatedAt: '2026-01-03 03:04:05', disabledAt: null,
})
assert.ok(camel)
assert.equal(camel.gatingType, '品牌gating')
assert.equal(camel.shopName, '店A')
assert.equal(camel.createdAt, '2026-01-02 03:04:05')
assert.equal(camel.disabledAt, undefined)
const snake = toGatingItem({
id: 9, gating_type: '类目gating', shop_name: '店B',
created_at: '2026-02-01 00:00:00', updated_at: '2026-02-02 00:00:00', disabled_at: '2026-02-03 00:00:00',
})
assert.ok(snake)
assert.equal(snake.gatingType, '类目gating')
assert.equal(snake.shopName, '店B')
assert.equal(snake.disabledAt, '2026-02-03 00:00:00')
})
test('gating_item_parse_invalid_rows_dropped', () => {
assert.equal(toGatingItem(null), null)
assert.equal(toGatingItem({ brand: 'no-id' }), null)
const page = parseGatingPage({ total: 3, rows: [{ id: 1, brand: 'a' }, { brand: 'bad' }, 'junk', null] })
assert.equal(page.rows.length, 1)
assert.equal(page.rows[0]?.brand, 'a')
assert.equal(page.total, 3)
})
test('gating_page_parse_empty_and_items_alias', () => {
assert.deepEqual(parseGatingPage(null), { rows: [], total: 0 })
const page = parseGatingPage({ total: 1, items: [{ id: 2, brand: 'b' }] })
assert.equal(page.rows.length, 1)
assert.equal(page.rows[0]?.id, 2)
})
test('gating_stats_parse_array_and_dict_forms', () => {
// 契约数组形态。
const stats = parseGatingStats({
total: 10, active: 8, disabled: 2, brands: 5,
byType: [{ name: '品牌gating', count: 6 }, { name: '类目gating', count: 2 }],
byCountry: [{ name: '英国', count: 4 }],
bySource: [{ name: 'AUTO', count: 7 }],
})
assert.equal(stats.total, 10)
assert.equal(stats.brands, 5)
assert.equal(stats.byType[0]?.name, '品牌gating')
assert.equal(stats.bySource[0]?.count, 7)
// 桌面版遗留 dict 形态也要能收。
const legacy = parseGatingStats({ total: 1, by_type: { '品牌gating': 3 }, by_country: { '全部': 1 }, by_source: { MANUAL: 1 } })
assert.equal(legacy.byType[0]?.name, '品牌gating')
assert.equal(legacy.byType[0]?.count, 3)
assert.equal(legacy.byCountry[0]?.name, '全部')
})
test('gating_name_count_list_garbage_in_empty_out', () => {
assert.deepEqual(parseGatingNameCountList(null), [])
assert.deepEqual(parseGatingNameCountList('x'), [])
assert.deepEqual(parseGatingNameCountList([{ name: '', count: 1 }, { count: 2 }, 'junk']), [])
})
test('gating_status_label', () => {
assert.equal(gatingStatusLabel(1), '生效')
assert.equal(gatingStatusLabel(0), '停用')
assert.equal(gatingStatusLabel(undefined), '停用')
})
@@ -0,0 +1,138 @@
/**
* 品牌Gating库页面挂载测试:渲染(统计卡 + 行)与两次操作载荷断言(新增、恢复生效)。
* 跑法:`npm run test:mount`(vitest + jsdom,配置见 vitest.config.ts)。
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import GatingPage from '@/pages/gating/GatingPage.vue'
const gatingApi = vi.hoisted(() => ({
fetchGatingPage: vi.fn(),
fetchGatingStats: vi.fn(),
createGating: vi.fn(),
deleteGating: vi.fn(),
updateGatingStatus: vi.fn(),
importGatingText: vi.fn(),
}))
vi.mock('@/pages/gating/gating-api', () => gatingApi)
const listPayload = {
total: 2,
rows: [
{
id: 1,
brand: 'jellycat',
asin: 'B0ABC123',
country: '英国',
gatingType: '品牌gating',
status: 1,
source: 'AUTO',
shopName: '',
remark: '',
createdAt: '2026-01-02 03:04:05',
updatedAt: '2026-01-03 03:04:05',
},
{
id: 2,
brand: 'crocs',
asin: '',
country: '全部',
gatingType: '类目gating',
status: 0,
source: 'MANUAL',
shopName: '店A',
remark: '人为录入',
createdAt: '2026-02-01 00:00:00',
updatedAt: '2026-02-02 00:00:00',
disabledAt: '2026-02-03 00:00:00',
},
],
}
const statsPayload = {
total: 2,
active: 1,
disabled: 1,
brands: 2,
byType: [{ name: '品牌gating', count: 1 }],
byCountry: [{ name: '英国', count: 1 }],
bySource: [{ name: 'MANUAL', count: 1 }],
}
const findButton = (wrapper: VueWrapper, label: string) =>
wrapper.findAll('button').find((button) => button.text().trim() === label)
describe('GatingPage', () => {
beforeEach(() => {
vi.clearAllMocks()
gatingApi.fetchGatingPage.mockResolvedValue(listPayload)
gatingApi.fetchGatingStats.mockResolvedValue(statsPayload)
})
it('渲染统计卡、筛选区与表格行', async () => {
const wrapper = mount(GatingPage)
await flushPromises()
const text = wrapper.text()
expect(text).toContain('Gating总记录')
expect(text).toContain('去重品牌数')
expect(text).toContain('jellycat')
expect(text).toContain('crocs')
expect(text).toContain('生效')
expect(text).toContain('停用')
expect(text).toContain('按Gating类型')
expect(wrapper.findAll('tbody tr').length).toBeGreaterThanOrEqual(2)
expect(gatingApi.fetchGatingPage).toHaveBeenCalledTimes(1)
expect(gatingApi.fetchGatingStats).toHaveBeenCalledTimes(1)
})
it('新增弹窗提交后按驼峰字段发载荷', async () => {
const wrapper = mount(GatingPage)
await flushPromises()
gatingApi.createGating.mockResolvedValue(1)
await findButton(wrapper, '新增')!.trigger('click')
const modal = wrapper.get('.modal')
const inputs = modal.findAll('input[type="text"]')
await inputs[0]!.setValue('jellycat') // 品牌
await inputs[1]!.setValue('B0Z9X') // ASIN
await inputs[2]!.setValue('店A') // 店铺
await inputs[3]!.setValue('测试备注') // 备注
await findButton(modal, '保存')!.trigger('click')
await flushPromises()
expect(gatingApi.createGating).toHaveBeenCalledTimes(1)
expect(gatingApi.createGating).toHaveBeenCalledWith({
brand: 'jellycat',
asin: 'B0Z9X',
country: '全部',
gatingType: '品牌gating',
source: 'MANUAL',
shopName: '店A',
remark: '测试备注',
})
})
it('停用行点恢复 → POST status 载荷 status=1;品牌为空时新增被拦截', async () => {
const wrapper = mount(GatingPage)
await flushPromises()
const crocsRow = wrapper
.findAll('tbody tr')
.find((row) => row.text().includes('crocs'))!
await findButton(crocsRow, '恢复')!.trigger('click')
await flushPromises()
expect(gatingApi.updateGatingStatus).toHaveBeenCalledTimes(1)
expect(gatingApi.updateGatingStatus).toHaveBeenCalledWith(2, 1)
// 品牌必填:空品牌提交不发请求
await findButton(wrapper, '新增')!.trigger('click')
await findButton(wrapper.get('.modal'), '保存')!.trigger('click')
await flushPromises()
expect(gatingApi.createGating).not.toHaveBeenCalled()
})
})
@@ -36,6 +36,7 @@ const OPS_MIN_COL_PX: Record<string, number> = {
'src/pages/asin/ProductCategoryPage.vue': 138,
'src/pages/asin/QueryAsinPage.vue': 138,
'src/pages/asin/SkipPricePage.vue': 138,
'src/pages/gating/GatingPage.vue': 138,
'src/pages/records/DeviceLogsPage.vue': 138,
'src/pages/records/RecordsDigitalHumanVersionPage.vue': 287,
'src/pages/records/RecordsSoftwareVersionPage.vue': 77,
@@ -0,0 +1,103 @@
package com.nanri.aiimage.common.retry;
import org.springframework.stereotype.Component;
import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.UnknownHostException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
/**
* 连接层不可达的客户端级冷却表(按上游 host 分键,Spring 单例)。
*
* <p>背景(BACKLOG B24,2026-10-23 拍板):三家同型客户端({@code BrandCheckClient}/
* {@code AppearancePatentLlmClient}/{@code SimilarAsinLlmClient})的连接层短路计数
* 生命周期都只到单次调用,网关长断时每个新调用/分块仍要各自真发连接探针才知道"还是断的"。
* 本表把「连续 2 次连接未建立」的证据跨调用累计,达到阈值即对该 host 进入冷却窗
* (默认 300s,同仓两处先例:{@code pt_worker/proxy_pool.py} 与 16890 服务侧的钥匙冷却),
* 窗内调用方不发请求直接快速失败;到期后下一条真实调用兼作探针,失败即重进、成功即恢复。
*
* <p>调用方契约:{@link #isCoolingDown} 在发请求前问一次;连接层失败记
* {@link #recordConnectFailure}(返回 true 的那方打一条进入冷却的 warn);
* 连接成功或"连上了没答好"(读超时/5xx/解析失败)记 {@link #recordConnected} 清零证据。
* 窗长 {@code <= 0} 表示关闭冷却,全部方法退化为无操作——调用方退回各自的调用内短路行为。
*/
@Component
public class UnreachableCooldown {
/**
* 进入冷却的证据数:与三家客户端各自的调用内短路阈值同数字——一次 refused 可能是抖动,
* 两次同形状且中间没有任何一次成功,才是服务在掉。
*/
private static final int EVIDENCE_THRESHOLD = 2;
private final ConcurrentHashMap<String, State> states = new ConcurrentHashMap<>();
private static final class State {
final AtomicInteger evidence = new AtomicInteger();
final AtomicLong cooldownUntilMillis = new AtomicLong();
}
/**
* 只认连接层故障(顺着 cause 链找):连接被拒/无路由/主机名解不了。
* 读超时与 5xx 是"连上了没答好",故意不算——那类抖动重试是有意义的。
* 原 BrandCheckClient/AppearancePatentLlmClient/SimilarAsinLlmClient 各有一份同款,
* B24 下沉至此,四家(含 TaskOwnerForwardService)共用同一判定。
*/
public static boolean isConnectFailure(Throwable error) {
for (Throwable t = error; t != null && t != t.getCause(); t = t.getCause()) {
if (t instanceof ConnectException || t instanceof NoRouteToHostException
|| t instanceof UnknownHostException) {
return true;
}
}
return false;
}
/** 该 host 是否正处冷却窗内:true 时调用方不发请求,直接按既有不可达归因文案快速失败。 */
public boolean isCoolingDown(String host, long windowMillis) {
if (windowMillis <= 0) {
return false;
}
State state = states.get(normalizeHost(host));
return state != null && state.cooldownUntilMillis.get() > System.currentTimeMillis();
}
/**
* 连接层失败证据 +1;累计到阈值即对该 host 开(或重开)冷却窗。
*
* @return true 仅当"本次调用让冷却窗从无到有"(并发下只会有一个调用方拿到 true),
* 调用方据此打一条进入冷却的 warn,期中快失败不刷屏
*/
public boolean recordConnectFailure(String host, long windowMillis) {
if (windowMillis <= 0) {
return false;
}
State state = states.computeIfAbsent(normalizeHost(host), key -> new State());
if (state.evidence.incrementAndGet() < EVIDENCE_THRESHOLD) {
return false;
}
long now = System.currentTimeMillis();
long previous = state.cooldownUntilMillis.get();
if (previous > now) {
return false;
}
return state.cooldownUntilMillis.compareAndSet(previous, now + windowMillis);
}
/** 连接成功或"连上了没答好":证据清零(已开的冷却窗不受影响,等它自然到期)。 */
public void recordConnected(String host) {
State state = states.get(normalizeHost(host));
if (state != null) {
state.evidence.set(0);
}
}
/** host 归一:两个 LLM 客户端配的同一网关必须落进同一格,别因尾斜杠/大小写裂成两份。 */
static String normalizeHost(String host) {
String normalized = host == null ? "" : host.trim().toLowerCase();
return normalized.endsWith("/") ? normalized.substring(0, normalized.length() - 1) : normalized;
}
}
@@ -19,7 +19,6 @@ import org.springframework.web.client.RestClient;
import org.springframework.web.util.ContentCachingRequestWrapper;
import java.io.IOException;
import java.net.ConnectException;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
@@ -89,7 +88,7 @@ public class TaskOwnerForwardService {
.retrieve()
.toEntity(byte[].class);
} catch (ResourceAccessException accessError) {
if (!isConnectFailure(accessError)) {
if (!com.nanri.aiimage.common.retry.UnreachableCooldown.isConnectFailure(accessError)) {
throw accessError;
}
lastError = accessError;
@@ -103,17 +102,6 @@ public class TaskOwnerForwardService {
throw lastError;
}
private static boolean isConnectFailure(Throwable error) {
Throwable cursor = error;
while (cursor != null) {
if (cursor instanceof ConnectException) {
return true;
}
cursor = cursor.getCause();
}
return false;
}
private static void sleepQuietly(long millis) {
try {
Thread.sleep(millis);
@@ -101,7 +101,12 @@ public class ApiGuardCoverageAuditor implements SmartInitializingSingleton {
* 往里塞一条没设防的路径会红。
*/
static final Set<String> METHOD_GUARDED_PATHS = Set.of(
"/api/client/credentials");
"/api/client/credentials",
// 五国关键词共享(2026-10-23 keywordshare 模块):9 个端点全部在 handler 体内经
// requireKeywordShareAccess → adminAuthSupport.requireUser + PermissionMenuService
// .requireConsolePageAccess(operator, …, "keyword-share") 逐请求把门,且 company_id
// 一律服务端取、不收请求参数——第二道门已到位,只欠进 USER_TOOL_PREFIXES 第一道门。
"/api/keyword-share");
/**
* 待收欠账:数据按 user_id 归属、却既不进守卫名单也没有 require* 的前缀。
@@ -46,6 +46,13 @@ public class AppearancePatentProperties {
* 每行每个 LLM 请求的重试次数(含首次)
*/
private int llmRetryTimes = 3;
/**
* 连接层不可达的客户端级冷却窗长(0=关闭,退回调用内短路)。连续 2 次连接未建立即进窗,
* 窗内调用不发请求直接快速失败;到期后下一条真实调用兼作探针。拍板口径见 BACKLOG B24
* (300s 同仓两处先例:pt_worker 代理池与 16890 服务侧的钥匙冷却)。
*/
private int llmUnreachableCooldownMillis = 300000;
/**
* 单行最多被「轮次」发起多少次(一轮 = 一次批量派发;每轮内部另有 llmRetryTimes 次 HTTP 重试)。
* <=0 关闭本闸。
@@ -34,4 +34,11 @@ public class BrandCheckProperties {
private int totalTimeoutMillis = 90000;
private int connectTimeoutMillis = 10000;
private int readTimeoutMillis = 60000;
/**
* 连接层不可达的客户端级冷却窗长(0=关闭,退回批内短路)。连续 2 次连接未建立即进窗,
* 窗内整批品牌不发请求直接按服务不可达判失败;到期后下一批的真实请求兼作探针。
* 拍板口径见 BACKLOG B24(300s 同仓两处先例:pt_worker 代理池与 16890 服务侧的钥匙冷却)。
*/
private int unreachableCooldownMillis = 300000;
}
@@ -197,6 +197,13 @@ public class SimilarAsinProperties {
private int llmFirstAttemptReadTimeoutMillis = 300000;
private int llmRetryTimes = 3;
/**
* 连接层不可达的客户端级冷却窗长(0=关闭,退回调用内短路)。连续 2 次连接未建立即进窗,
* 窗内调用不发请求直接快速失败;到期后下一条真实调用兼作探针。拍板口径见 BACKLOG B24
* (300s 同仓两处先例:pt_worker 代理池与 16890 服务侧的钥匙冷却)。
*/
private int llmUnreachableCooldownMillis = 300000;
/** 批内行级并发上限:每行最多 2 次图片对比 + 1 次合规 + 3 次类目匹配。 */
private int llmRowConcurrency = 5;
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.appearancepatent.client;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.retry.LlmRetryBackoff;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.AppearancePatentProperties;
import com.nanri.aiimage.config.HttpClientPool;
import com.nanri.aiimage.metrics.ExternalCallMetricsRecorder;
@@ -33,6 +34,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -65,6 +67,15 @@ public class AppearancePatentLlmClient {
*/
static final String BRAND_UPSTREAM_UNAVAILABLE =
"商标查询失败:品牌库上游整体不可用(本批待查词无一返回结果,非本行数据问题)";
/**
* 连接层短路的证据数:同一次调用里连续这么多次「连接根本没建立」且中间没有一次成功,
* 就判定 LLM 网关不可达,立刻停掉重试阶梯。与 {@code BrandCheckClient} 的
* SERVICE_UNREACHABLE 同口径——2026-10-22 那条断链在 B 侧一窗白等 14,944 次、
* 判失败 7,667 次的教训:连接层故障重试救不回来,只会把同一笔失败摊满整段退避预算。
*/
private static final int LLM_CONNECT_FAILURE_EVIDENCE = 2;
/** 连接层判定不可达时交付给行级的原因文案:说清是运维故障,不是这行数据的问题。 */
static final String REASON_LLM_UNREACHABLE = "LLM 服务不可达(连接未建立,非本行数据问题)";
private static final String APPEARANCE_ANOMALY = "外观识别异常";
private static final String MISSING_ROW_DATA = "爬虫数据缺失";
@@ -145,6 +156,7 @@ public class AppearancePatentLlmClient {
private final ExternalCallMetricsRecorder externalCallMetrics;
private final BrandCheckClient brandCheckClient;
private final UserSecretUsageService userSecretUsageService;
private final UnreachableCooldown unreachableCooldown;
private volatile RestClient sharedRestClient;
/** 首次尝试专用客户端(更短读超时,快速失败);配置未生效时与 sharedRestClient 相同。 */
@@ -634,12 +646,43 @@ public class AppearancePatentLlmClient {
String apiKey,
String responseFormat) throws Exception {
int attempts = Math.max(1, properties.getLlmRetryTimes());
long cooldownMillis = Math.max(0, properties.getLlmUnreachableCooldownMillis());
String host = properties.getLlmHost();
if (unreachableCooldown.isCoolingDown(host, cooldownMillis)) {
// 冷却期快失败不发请求、不记密钥次数(B24)
log.debug("[appearance-patent] 客户端级冷却中,快速失败 model={} window={}ms", model, cooldownMillis);
throw new IllegalStateException(REASON_LLM_UNREACHABLE + ";lastErr=冷却中(连续连接未建立,暂不打上游)");
}
Exception lastFailure = null;
AtomicInteger connectFailures = new AtomicInteger();
for (int attempt = 1; attempt <= attempts; attempt++) {
try {
return invokeChatOnce(model, system, userText, images, apiKey, responseFormat, attempt);
String content = invokeChatOnce(model, system, userText, images, apiKey, responseFormat, attempt);
unreachableCooldown.recordConnected(host);
return content;
} catch (Exception ex) {
lastFailure = ex;
if (UnreachableCooldown.isConnectFailure(ex)) {
boolean entered = unreachableCooldown.recordConnectFailure(host, cooldownMillis);
if (entered) {
log.warn("[appearance-patent] 连接层证据累计 {} 次,进入客户端级冷却 {}ms host={}",
LLM_CONNECT_FAILURE_EVIDENCE, cooldownMillis, host);
}
if (connectFailures.incrementAndGet() >= LLM_CONNECT_FAILURE_EVIDENCE
|| unreachableCooldown.isCoolingDown(host, cooldownMillis)) {
log.warn("[appearance-patent] LLM_UNREACHABLE 连续 {} 次连接未建立,判定 LLM 服务不可达,"
+ "停止重试 attempt={} model={} err={}",
LLM_CONNECT_FAILURE_EVIDENCE, attempt, model, failureMessage(ex));
// 结论不变(这批拿不到 LLM 结论),差别只在耗时与归因文案:
// 行级拿到的是"网关没连上",而不是一句裸的连接异常
throw new IllegalStateException(
REASON_LLM_UNREACHABLE + ";lastErr=" + failureMessage(ex), ex);
}
} else {
// 连上了只是这次没答好(读超时/解析失败/5xx):连接层证据不作数
connectFailures.set(0);
unreachableCooldown.recordConnected(host);
}
if (attempt >= attempts) {
break;
}
@@ -652,6 +695,14 @@ public class AppearancePatentLlmClient {
throw lastFailure == null ? new IllegalStateException("LLM call failed") : lastFailure;
}
/**
* 连接层判定下沉 {@code UnreachableCooldown.isConnectFailure}(B24):判定口径与
* BrandCheckClient/SimilarAsinLlmClient 共用一份,不再各留包内副本。
*/
static boolean isConnectFailure(Throwable error) {
return UnreachableCooldown.isConnectFailure(error);
}
private String invokeChatOnce(String model,
String system,
String userText,
@@ -77,16 +77,19 @@ public class BrandCheckClient {
private final BrandCheckProperties properties;
private final com.nanri.aiimage.metrics.ExternalCallMetricsRecorder externalCallMetrics;
private final UserApiSecretService userApiSecretService;
private final com.nanri.aiimage.common.retry.UnreachableCooldown unreachableCooldown;
/** 提交线程上的拒绝标记:队列满时由拒绝策略置位,提交方据此立即降级(不新增 catch 出口)。 */
private final ThreadLocal<AtomicBoolean> rejectedSubmission = new ThreadLocal<>();
private volatile RestClient sharedRestClient;
public BrandCheckClient(BrandCheckProperties properties,
com.nanri.aiimage.metrics.ExternalCallMetricsRecorder externalCallMetrics,
UserApiSecretService userApiSecretService) {
UserApiSecretService userApiSecretService,
com.nanri.aiimage.common.retry.UnreachableCooldown unreachableCooldown) {
this.properties = properties;
this.externalCallMetrics = externalCallMetrics;
this.userApiSecretService = userApiSecretService;
this.unreachableCooldown = unreachableCooldown;
}
// 有界队列 + 拒绝即降级:大批量(几千品牌)检查时队列洪峰不无界堆内存。
@@ -267,6 +270,18 @@ public class BrandCheckClient {
}
return outcomes;
}
long cooldownMillis = Math.max(0, properties.getUnreachableCooldownMillis());
if (unreachableCooldown.isCoolingDown(properties.getBaseUrl(), cooldownMillis)) {
// 客户端级冷却(B24):上一批已确认连接层不可达,本批连首轮探针都不发。
// 结论与批内短路版逐字一致(每行仍判查询失败、reason 同文案),差别只是不再为
// "确认还是断的"每批各烧一轮连接探针;到期后下一批的真实请求兼作探针。
log.warn("[brand-check] SERVICE_UNREACHABLE 客户端级冷却中,整批不打上游 brands={} window={}ms",
brands.size(), cooldownMillis);
for (String brand : brands) {
outcomes.put(brand, new BrandCheckOutcome(List.of(), List.of(brand), REASON_SERVICE_UNREACHABLE));
}
return outcomes;
}
long firstAttemptDeadlineNanos = deadlineNanos == Long.MAX_VALUE
? Long.MAX_VALUE
: deadlineNanos + FIRST_ATTEMPT_QUEUE_GRACE_MILLIS * 1_000_000L;
@@ -363,6 +378,7 @@ public class BrandCheckClient {
lastFailure = ex;
response = null;
if (isConnectFailure(ex)) {
recordConnectFailure(ex);
if (connectFailures.incrementAndGet() >= CONNECT_FAILURE_EVIDENCE) {
ladderCompleted = false;
connectDown = true;
@@ -374,6 +390,7 @@ public class BrandCheckClient {
} else {
// 连上了只是这次没答好(读超时/解析失败):连接层证据不作数
connectFailures.set(0);
unreachableCooldown.recordConnected(properties.getBaseUrl());
}
if (attempt < attempts) {
long retryDelayMillis = retryDelayMillis(attempt);
@@ -385,6 +402,7 @@ public class BrandCheckClient {
}
// 拿到响应=连接是通的,证据清零,别跨到下一次真断连时凑数
connectFailures.set(0);
unreachableCooldown.recordConnected(properties.getBaseUrl());
if (response != null && nullToEmpty(response.getQueryFaildData()).isEmpty()) {
// 本次查询完成且无失败项:命中侵权(faild_data)属有效结论,不重试。
return new BrandCheckOutcome(nullToEmpty(response.getFaildData()), List.of(), "");
@@ -554,14 +572,21 @@ public class BrandCheckClient {
* <p>故意不含读超时与 5xx:那两种是"连上了但这次没答好",重试救得回来;把它们算进
* 不可达证据,就会把一次上游抖动误判成服务在掉、把同批其余品牌的重试一起掐掉。
*/
static boolean isConnectFailure(Throwable error) {
for (Throwable t = error; t != null && t != t.getCause(); t = t.getCause()) {
if (t instanceof ConnectException || t instanceof NoRouteToHostException
|| t instanceof UnknownHostException) {
return true;
}
/** 连接层失败记入客户端级冷却表:达到阈值的那方打一条进入冷却的 warn(B24)。 */
private void recordConnectFailure(Exception ex) {
long cooldownMillis = Math.max(0, properties.getUnreachableCooldownMillis());
if (unreachableCooldown.recordConnectFailure(properties.getBaseUrl(), cooldownMillis)) {
log.warn("[brand-check] SERVICE_UNREACHABLE 连接层证据累计 {} 次,进入客户端级冷却 {}ms host={}",
CONNECT_FAILURE_EVIDENCE, cooldownMillis, properties.getBaseUrl());
}
return false;
}
/**
* 连接层判定下沉 {@code UnreachableCooldown.isConnectFailure}(B24):判定口径与
* 两个 LLM 客户端共用一份,不再各留包内副本。
*/
static boolean isConnectFailure(Throwable error) {
return com.nanri.aiimage.common.retry.UnreachableCooldown.isConnectFailure(error);
}
/**
@@ -0,0 +1,118 @@
package com.nanri.aiimage.modules.brandgating.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingAddRequest;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingImportRequest;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingStatusRequest;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingAddResultVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingDeleteResultVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingImportResultVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingPageVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingStatsVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingStatusResultVo;
import com.nanri.aiimage.modules.brandgating.service.BrandGatingService;
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 品牌Gating库(管理后台):独立小网页后台(gating_web_admin.py)并入数富管理后台,
* 共用主库同一张 biz_brand_gating 表。
*
* <p>页面权限键 {@code admin_brand_gating}(后台菜单 column_key,经
* {@link PermissionMenuService#requireConsolePageAccess} 把门);鉴权第一道门由
* AdminApiGuardFilter 的 /api/admin 前缀兜底(见 ApiGuardCoverageAuditor 口径)。</p>
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/admin/brand-gating")
@Tag(name = "品牌Gating库(后台)", description = "品牌Gating库的列表/筛选/统计、单条新增、启停、删除与批量导入。")
public class BrandGatingController {
/** 后台菜单权限键(columns.column_key,与 V164 菜单行同键)。 */
private static final String PERMISSION_KEY = "admin_brand_gating";
private static final String DENIAL_MESSAGE = "无权访问品牌Gating库";
private final BrandGatingService brandGatingService;
private final AdminAuthSupport adminAuthSupport;
private final PermissionMenuService permissionMenuService;
@GetMapping
@Operation(summary = "分页查询品牌Gating库", description = "keyword 模糊匹配品牌/ASIN,country/gatingType/status/source 精确筛选,ORDER BY id DESC。")
public ApiResponse<BrandGatingPageVo> page(
HttpServletRequest request,
@Parameter(description = "品牌/ASIN 模糊搜索") @RequestParam(name = "keyword", required = false) String keyword,
@Parameter(description = "国家(「全部」= 不限)") @RequestParam(name = "country", required = false) String country,
@Parameter(description = "Gating 类型(「全部」= 不限)") @RequestParam(name = "gatingType", required = false) String gatingType,
@Parameter(description = "状态:1=生效 0=停用,不传=全部") @RequestParam(name = "status", required = false) String status,
@Parameter(description = "来源(「全部」= 不限)") @RequestParam(name = "source", required = false) String source,
@Parameter(description = "页码(1 起)") @RequestParam(name = "page", required = false) Long page,
@Parameter(description = "每页数量(默认 10,上限 200)") @RequestParam(name = "size", required = false) Long size) {
requireBrandGatingAccess(request);
return ApiResponse.success(brandGatingService.page(page, size, keyword, country, gatingType, status, source));
}
@GetMapping("/stats")
@Operation(summary = "品牌Gating库统计", description = "total/active/disabled/brands 与按类型/国家/来源的分布(分组只数生效行,国家按 count DESC)。")
public ApiResponse<BrandGatingStatsVo> stats(HttpServletRequest request) {
requireBrandGatingAccess(request);
return ApiResponse.success(brandGatingService.stats());
}
@PostMapping
@Operation(summary = "单条新增", description = "brand 必填;缺省字段落桌面版同款默认(country=全部、gatingType=品牌gating、source=MANUAL);撞唯一键回 added=0。")
public ApiResponse<BrandGatingAddResultVo> add(
HttpServletRequest request,
@Valid @RequestBody BrandGatingAddRequest body) {
requireBrandGatingAccess(request);
return ApiResponse.success(brandGatingService.add(body));
}
@DeleteMapping("/{id}")
@Operation(summary = "删除单条", description = "物理删除该行,返回实际删除数。")
public ApiResponse<BrandGatingDeleteResultVo> delete(
HttpServletRequest request,
@PathVariable("id") Long id) {
requireBrandGatingAccess(request);
return ApiResponse.success(brandGatingService.delete(id));
}
@PostMapping("/{id}/status")
@Operation(summary = "启用/停用", description = "status=1 恢复生效并清 disabled_at;status=0 停用并记 disabled_at=NOW();都刷 updated_at。")
public ApiResponse<BrandGatingStatusResultVo> updateStatus(
HttpServletRequest request,
@PathVariable("id") Long id,
@Valid @RequestBody BrandGatingStatusRequest body) {
requireBrandGatingAccess(request);
return ApiResponse.success(brandGatingService.updateStatus(id, body.getStatus()));
}
@PostMapping("/import")
@Operation(summary = "批量导入", description = "body 优先 { text }:按桌面版批量导入行格式解析(逗号/分号/制表符切列,# 注释行跳过);也兼容 { items } 直传。重复行跳过,返回 { added, skipped }。")
public ApiResponse<BrandGatingImportResultVo> importRows(
HttpServletRequest request,
@RequestBody BrandGatingImportRequest body) {
requireBrandGatingAccess(request);
return ApiResponse.success(brandGatingService.importRows(body));
}
/** 逐端点把门:登录态 + 后台菜单权限(admin_brand_gating)。 */
private void requireBrandGatingAccess(HttpServletRequest request) {
AdminUserEntity operator = adminAuthSupport.requireUser(request);
permissionMenuService.requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY);
}
}
@@ -0,0 +1,52 @@
package com.nanri.aiimage.modules.brandgating.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingFilterQuery;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingNameCountRow;
import com.nanri.aiimage.modules.brandgating.model.entity.BrandGatingEntity;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingRowVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 品牌Gating库(biz_brand_gating)数据访问。
*
* <p>列表筛选/统计分组/启停这类动态 SQL 全部收口在本 Mapper 的 XML 里
* ({@code mapper/brandgating/BrandGatingMapper.xml}),筛选值一律 {@code #{}} 占位符;
* 单条新增/删除走 MyBatis-Plus BaseMapper 既有能力。</p>
*/
@Mapper
public interface BrandGatingMapper extends BaseMapper<BrandGatingEntity> {
/** 筛选命中总数。 */
long countByFilter(@Param("f") BrandGatingFilterQuery filter);
/** 当前页行:ORDER BY id DESC(对齐桌面版 list_gating)。 */
List<BrandGatingRowVo> selectRows(@Param("f") BrandGatingFilterQuery filter);
/** 全表行数。 */
long countAll();
/** 按状态计数:1=生效、0=停用。 */
long countByStatus(@Param("status") int status);
/** 生效行里的去重品牌数。 */
long countDistinctBrands();
/** 统计:按 Gating 类型分布(仅生效行)。 */
List<BrandGatingNameCountRow> countGroupByType();
/** 统计:按国家分布(仅生效行,count DESC,对齐桌面版 by_country)。 */
List<BrandGatingNameCountRow> countGroupByCountry();
/** 统计:按来源分布(仅生效行)。 */
List<BrandGatingNameCountRow> countGroupBySource();
/**
* 启停(对齐桌面版 set_status_gating):status=1 清 disabled_at、=0 记 disabled_at=NOW(),
* 两种都刷 updated_at=NOW()。
*/
int updateStatus(@Param("id") Long id, @Param("status") int status);
}
@@ -0,0 +1,22 @@
package com.nanri.aiimage.modules.brandgating.model.dto;
import lombok.Data;
/**
* 品牌Gating库单条新增请求(POST /api/admin/brand-gating)。
*
* <p>brand 必填(服务端校验,对齐桌面版 add_gating 的「品牌不能为空」);
* 其余字段可缺省,由服务端落桌面版同款默认:country→「全部」、gatingType→「品牌gating」、
* asin/shopName→NULL、remark→空串、source→「MANUAL」。</p>
*/
@Data
public class BrandGatingAddRequest {
private String brand;
private String asin;
private String country;
private String gatingType;
private String source;
private String shopName;
private String remark;
}
@@ -0,0 +1,29 @@
package com.nanri.aiimage.modules.brandgating.model.dto;
import lombok.Data;
/**
* 品牌Gating库列表筛选载体(Mapper XML 的 #{f.xxx} 取值源)。
*
* <p>筛选口径对齐桌面版 gating_web_admin.py list_gating(L138-156):
* keyword 模糊匹配 brand/asin,country/gatingType/source 精确等值(「全部」= 不限),
* status 精确等值(null = 不限)。分页偏移与容量由 Service 归一化后带入。</p>
*/
@Data
public class BrandGatingFilterQuery {
/** 品牌/ASIN 模糊搜索词(已 trim;空串 = 不限)。 */
private String keyword;
/** 国家精确等值(空串或「全部」= 不限)。 */
private String country;
/** Gating 类型精确等值(空串或「全部」= 不限)。 */
private String gatingType;
/** 状态精确等值:1=生效 0=停用(null = 不限)。 */
private Integer status;
/** 来源精确等值(空串或「全部」= 不限)。 */
private String source;
/** 分页偏移((page-1)*size,已归一化)。 */
private Long offset;
/** 每页条数(已归一化)。 */
private Long limit;
}
@@ -0,0 +1,31 @@
package com.nanri.aiimage.modules.brandgating.model.dto;
import lombok.Data;
import java.util.List;
/**
* 品牌Gating库批量导入请求(POST /api/admin/brand-gating/import)。
*
* <p>优先用 {@link #text}:原始粘贴文本整段交后端,按桌面版 parse_batch 的行格式解析
* (中英文逗号/分号/制表符切列,字段序 品牌国家/类型/ASIN/备注,空行与 # 注释行跳过)。
* 也兼容 {@link #items} 直传(text 为空时生效),每条按与单条新增同语义落库。</p>
*/
@Data
public class BrandGatingImportRequest {
/** 原始粘贴文本(桌面版批量导入文本框的整段内容)。 */
private String text;
/** 直传行(与 text 二选一,text 优先)。 */
private List<Item> items;
@Data
public static class Item {
private String brand;
private String country;
private String gatingType;
private String asin;
private String remark;
}
}
@@ -0,0 +1,14 @@
package com.nanri.aiimage.modules.brandgating.model.dto;
import lombok.Data;
/**
* 品牌Gating库统计分组单行(stats 的 byType/byCountry/bySource 共用)。
* SQL 侧 {@code SELECT xxx AS name, COUNT(*) AS count} 直接映射。
*/
@Data
public class BrandGatingNameCountRow {
private String name;
private long count;
}
@@ -0,0 +1,15 @@
package com.nanri.aiimage.modules.brandgating.model.dto;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
/**
* 品牌Gating库启停请求体(POST /api/admin/brand-gating/{id}/status)。
* status:1=生效(清 disabled_at)、0=停用(记 disabled_at=NOW())。
*/
@Data
public class BrandGatingStatusRequest {
@NotNull(message = "status 不能为空")
private Integer status;
}
@@ -0,0 +1,35 @@
package com.nanri.aiimage.modules.brandgating.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 品牌Gating库(biz_brand_gating)。
*
* <p>表在主库(aiimage)早已存在——原先是独立小网页后台(gating_web_admin.py,直连同一库)在维护,
* 本次把它并入数富管理后台,桌面/网页与后台共用同一份数据。<b>不建表、不走 Flyway</b>:
* 本模块只做实体/Mapper 映射既有表。status:1=生效、0=停用(停用记 disabled_at,恢复清空)。</p>
*/
@Data
@TableName("biz_brand_gating")
public class BrandGatingEntity {
@TableId(type = IdType.AUTO)
private Long id;
private String brand;
private String asin;
private String country;
private String gatingType;
/** 1=生效 0=停用。 */
private Integer status;
private String source;
private String shopName;
private String remark;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private LocalDateTime disabledAt;
}
@@ -0,0 +1,10 @@
package com.nanri.aiimage.modules.brandgating.model.vo;
import lombok.Data;
/** 单条新增结果:1=写入成功,0=命中唯一键重复被跳过(桌面版 add_gating 返回 False 的同语义)。 */
@Data
public class BrandGatingAddResultVo {
private int added;
}
@@ -0,0 +1,10 @@
package com.nanri.aiimage.modules.brandgating.model.vo;
import lombok.Data;
/** 删除结果:实际删除的行数(0=该 id 不存在)。 */
@Data
public class BrandGatingDeleteResultVo {
private int deleted;
}
@@ -0,0 +1,11 @@
package com.nanri.aiimage.modules.brandgating.model.vo;
import lombok.Data;
/** 批量导入结果:added=实际写入行数,skipped=跳过行数(批内重复/命中唯一键重复/无效行)。 */
@Data
public class BrandGatingImportResultVo {
private int added;
private int skipped;
}
@@ -0,0 +1,13 @@
package com.nanri.aiimage.modules.brandgating.model.vo;
import lombok.Data;
import java.util.List;
/** 品牌Gating库分页负载(GET /api/admin/brand-gating → { total, rows })。 */
@Data
public class BrandGatingPageVo {
private long total;
private List<BrandGatingRowVo> rows;
}
@@ -0,0 +1,27 @@
package com.nanri.aiimage.modules.brandgating.model.vo;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 品牌Gating库列表单行(GET /api/admin/brand-gating 的 rows 元素)。
* 字段与契约一一对应;时间字段原样透传(桌面版列表同列展示)。
*/
@Data
public class BrandGatingRowVo {
private Long id;
private String brand;
private String asin;
private String country;
private String gatingType;
/** 1=生效 0=停用。 */
private Integer status;
private String source;
private String shopName;
private String remark;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private LocalDateTime disabledAt;
}
@@ -0,0 +1,30 @@
package com.nanri.aiimage.modules.brandgating.model.vo;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingNameCountRow;
import lombok.Data;
import java.util.List;
/**
* 品牌Gating库统计负载(GET /api/admin/brand-gating/stats)。
* 口径对齐桌面版 stats()(L165-178):active/disabled 按全表数,byType/byCountry/bySource
* 只数 status=1 的行,byCountry 按 count DESC。
*/
@Data
public class BrandGatingStatsVo {
/** 全表行数(含停用)。 */
private long total;
/** 生效行数(status=1)。 */
private long active;
/** 停用行数(status=0)。 */
private long disabled;
/** 生效行里的去重品牌数。 */
private long brands;
/** 按 Gating 类型分布(仅生效行)。 */
private List<BrandGatingNameCountRow> byType;
/** 按国家分布(仅生效行,count DESC)。 */
private List<BrandGatingNameCountRow> byCountry;
/** 按来源分布(仅生效行)。 */
private List<BrandGatingNameCountRow> bySource;
}
@@ -0,0 +1,10 @@
package com.nanri.aiimage.modules.brandgating.model.vo;
import lombok.Data;
/** 启停结果:实际更新的行数(0=该 id 不存在)。 */
@Data
public class BrandGatingStatusResultVo {
private int updated;
}
@@ -0,0 +1,276 @@
package com.nanri.aiimage.modules.brandgating.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.brandgating.mapper.BrandGatingMapper;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingAddRequest;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingFilterQuery;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingImportRequest;
import com.nanri.aiimage.modules.brandgating.model.entity.BrandGatingEntity;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingAddResultVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingDeleteResultVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingImportResultVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingPageVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingRowVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingStatsVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingStatusResultVo;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* 品牌Gating库(biz_brand_gating)业务:独立小网页后台(gating_web_admin.py)并入数富管理后台。
*
* <p>行为口径逐条对齐桌面版(直连同一 aiimage 库,两端共用同一份数据):
* 列表筛选/统计见 {@code list_gating}/`stats`(L138-178),新增默认值见 {@code add_gating}
* (L181-193:asin/shop_name 空→NULL、country 空→「全部」、gating_type 空→「品牌gating」、
* source 固定「MANUAL」、remark 空→空串,created_at/updated_at 由库默认维护、INSERT 不显式写),
* 启停见 {@code set_status_gating}(L196-201),批量导入见 {@code parse_batch}/{@code batch_add}
* (L208-237)。重复判定沿用桌面版的唯一键兜底:INSERT 撞唯一键即跳过。</p>
*/
@Service
@RequiredArgsConstructor
public class BrandGatingService {
/** 手动新增行的固定来源(桌面版 add_gating/batch_add 同口径)。 */
public static final String SOURCE_MANUAL = "MANUAL";
/** 缺省国家(桌面版默认值,也是表内真实取值)。 */
public static final String DEFAULT_COUNTRY = "全部";
/** 缺省 Gating 类型。 */
public static final String DEFAULT_GATING_TYPE = "品牌gating";
/** 列表默认页容量(对齐桌面版 page_size=10)。 */
public static final long DEFAULT_PAGE_SIZE = 10;
/** 列表页容量上限(防御性截断,前端档位 10/20/50)。 */
public static final long MAX_PAGE_SIZE = 200;
/** 批量导入的去重维度:同 品牌+国家+类型+ASIN 的行只写第一条(批内重复跳过)。 */
private static final String IMPORT_DEDUP_KEY_FORMAT = "%s\n%s\n%s\n%s";
private final BrandGatingMapper brandGatingMapper;
// ---------------------------------------------------------------- 列表与统计
public BrandGatingPageVo page(Long page, Long size, String keyword, String country,
String gatingType, String status, String source) {
BrandGatingFilterQuery filter = new BrandGatingFilterQuery();
filter.setKeyword(trimToEmpty(keyword));
filter.setCountry(trimToEmpty(country));
filter.setGatingType(trimToEmpty(gatingType));
filter.setStatus(parseStatusFilter(status));
filter.setSource(trimToEmpty(source));
long pageSize = normalizePageSize(size);
filter.setLimit(pageSize);
filter.setOffset((normalizePage(page) - 1) * pageSize);
BrandGatingPageVo vo = new BrandGatingPageVo();
vo.setTotal(brandGatingMapper.countByFilter(filter));
List<BrandGatingRowVo> rows = brandGatingMapper.selectRows(filter);
vo.setRows(rows);
return vo;
}
public BrandGatingStatsVo stats() {
BrandGatingStatsVo vo = new BrandGatingStatsVo();
vo.setTotal(brandGatingMapper.countAll());
vo.setActive(brandGatingMapper.countByStatus(1));
vo.setDisabled(brandGatingMapper.countByStatus(0));
vo.setBrands(brandGatingMapper.countDistinctBrands());
vo.setByType(brandGatingMapper.countGroupByType());
vo.setByCountry(brandGatingMapper.countGroupByCountry());
vo.setBySource(brandGatingMapper.countGroupBySource());
return vo;
}
// ---------------------------------------------------------------- 单条操作
public BrandGatingAddResultVo add(BrandGatingAddRequest request) {
BrandGatingAddResultVo vo = new BrandGatingAddResultVo();
String brand = trimToEmpty(request.getBrand());
if (brand.isEmpty()) {
throw new BusinessException(400, "品牌不能为空");
}
BrandGatingEntity entity = new BrandGatingEntity();
entity.setBrand(brand);
applyOptionalDefaults(entity, request.getAsin(), request.getCountry(),
request.getGatingType(), request.getRemark());
entity.setSource(firstNonBlank(request.getSource(), SOURCE_MANUAL));
entity.setShopName(trimToNull(request.getShopName()));
// 撞唯一键 = 已存在同口径行:桌面版 add_gating 捕 IntegrityError 返回 False,这里同语义回 0
try {
brandGatingMapper.insert(entity);
vo.setAdded(1);
} catch (DuplicateKeyException ex) {
vo.setAdded(0);
}
return vo;
}
public BrandGatingDeleteResultVo delete(Long id) {
BrandGatingDeleteResultVo vo = new BrandGatingDeleteResultVo();
vo.setDeleted(brandGatingMapper.deleteById(id));
return vo;
}
public BrandGatingStatusResultVo updateStatus(Long id, Integer status) {
if (status == null || (status != 0 && status != 1)) {
throw new BusinessException(400, "status 只能为 0(停用)或 1(生效)");
}
BrandGatingStatusResultVo vo = new BrandGatingStatusResultVo();
vo.setUpdated(brandGatingMapper.updateStatus(id, status));
return vo;
}
// ---------------------------------------------------------------- 批量导入
public BrandGatingImportResultVo importRows(BrandGatingImportRequest request) {
List<BrandGatingImportRequest.Item> items = resolveImportItems(request);
BrandGatingImportResultVo vo = new BrandGatingImportResultVo();
Set<String> seen = new HashSet<>();
for (BrandGatingImportRequest.Item item : items) {
String brand = trimToEmpty(item.getBrand());
String country = firstNonBlank(item.getCountry(), DEFAULT_COUNTRY);
String gatingType = firstNonBlank(item.getGatingType(), DEFAULT_GATING_TYPE);
String asin = trimToEmpty(item.getAsin());
if (brand.isEmpty()) {
// 文本解析路径不会产出空品牌(空段被丢弃),items 直传路径在这里拦下
vo.setSkipped(vo.getSkipped() + 1);
continue;
}
String dedupKey = String.format(IMPORT_DEDUP_KEY_FORMAT, brand, country, gatingType, asin);
if (!seen.add(dedupKey)) {
vo.setSkipped(vo.getSkipped() + 1);
continue;
}
BrandGatingEntity entity = new BrandGatingEntity();
entity.setBrand(brand);
entity.setAsin(asin.isEmpty() ? null : asin);
entity.setCountry(country);
entity.setGatingType(gatingType);
entity.setSource(SOURCE_MANUAL);
entity.setShopName(null);
entity.setRemark(item.getRemark() == null ? "" : item.getRemark().strip());
// 撞唯一键 = 库里已有同口径行(桌面版 batch_add 捕 IntegrityError 计 skipped 的同语义)
try {
brandGatingMapper.insert(entity);
vo.setAdded(vo.getAdded() + 1);
} catch (DuplicateKeyException ex) {
vo.setSkipped(vo.getSkipped() + 1);
}
}
return vo;
}
/** text 优先(照桌面版行格式解析),text 为空回退 items 直传;两者都缺 → 400。 */
private List<BrandGatingImportRequest.Item> resolveImportItems(BrandGatingImportRequest request) {
if (request == null) {
throw new BusinessException(400, "导入内容为空");
}
if (request.getText() != null && !request.getText().isBlank()) {
return parseBatchText(request.getText());
}
if (request.getItems() != null && !request.getItems().isEmpty()) {
return request.getItems();
}
throw new BusinessException(400, "导入内容为空");
}
/**
* 桌面版 parse_batch(gating_web_admin.py L208-223)的逐行移植:
* <ul>
* <li>按行拆分;每行去首尾空白与 BOM(\uFEFF);</li>
* <li>空行与 # 开头的注释行跳过;</li>
* <li>行内按中英文逗号/分号/制表符([,\t;,;])切分,各段去空白后丢掉空段;</li>
* <li>字段序:品牌、国家(缺省「全部」)、Gating类型(缺省「品牌gating」)、ASIN(缺省空)、
* 备注(缺省空);第 5 列之后的段照桌面版丢弃。</li>
* </ul>
*/
public static List<BrandGatingImportRequest.Item> parseBatchText(String text) {
List<BrandGatingImportRequest.Item> out = new ArrayList<>();
if (text == null || text.isBlank()) {
return out;
}
for (String rawLine : text.split("\\R")) {
String line = rawLine.strip();
if (line.startsWith("\uFEFF")) {
line = line.substring(1).strip();
}
if (line.isEmpty() || line.startsWith("#")) {
continue;
}
List<String> parts = Arrays.stream(line.split("[,\\t;,;]"))
.map(String::strip)
.filter(part -> !part.isEmpty())
.toList();
if (parts.isEmpty()) {
continue;
}
BrandGatingImportRequest.Item item = new BrandGatingImportRequest.Item();
item.setBrand(parts.get(0));
item.setCountry(parts.size() > 1 ? parts.get(1) : DEFAULT_COUNTRY);
item.setGatingType(parts.size() > 2 ? parts.get(2) : DEFAULT_GATING_TYPE);
item.setAsin(parts.size() > 3 ? parts.get(3) : "");
item.setRemark(parts.size() > 4 ? parts.get(4) : "");
out.add(item);
}
return out;
}
// ---------------------------------------------------------------- 私有工具
/** 桌面版 add/batch_add 的可选字段默认值:asin/shop_name 空→NULL、country/gating_type 空→默认、remark 空→空串。 */
private void applyOptionalDefaults(BrandGatingEntity entity, String asin, String country,
String gatingType, String remark) {
entity.setAsin(trimToNull(asin));
entity.setCountry(firstNonBlank(country, DEFAULT_COUNTRY));
entity.setGatingType(firstNonBlank(gatingType, DEFAULT_GATING_TYPE));
entity.setRemark(remark == null ? "" : remark.strip());
}
private static String trimToEmpty(String value) {
return value == null ? "" : value.strip();
}
private static String trimToNull(String value) {
if (value == null) {
return null;
}
String trimmed = value.strip();
return trimmed.isEmpty() ? null : trimmed;
}
private static String firstNonBlank(String value, String fallback) {
String trimmed = trimToNull(value);
return trimmed == null ? fallback : trimmed;
}
/** "1"→1、"0"→0,其余(含空串与「全部」)→ null = 不限。 */
private static Integer parseStatusFilter(String status) {
if ("1".equals(status)) {
return 1;
}
if ("0".equals(status)) {
return 0;
}
return null;
}
private static long normalizePage(Long page) {
if (page == null || page < 1) {
return 1;
}
return page;
}
private static long normalizePageSize(Long size) {
if (size == null || size < 1) {
return DEFAULT_PAGE_SIZE;
}
return Math.min(size, MAX_PAGE_SIZE);
}
}
@@ -118,7 +118,10 @@ public class CollectDataBrandBatchFilter {
}
continue;
}
if (deadlineNanos != Long.MAX_VALUE && System.nanoTime() >= deadlineNanos) {
// 首批不掐(与 BrandCheckClient 的 attempt>1 同契约):预算检查只拦后续批次。
// 对首批也查的话,deadline 计算与检查之间一次 GC/调度停顿(毫秒级)就会把首批
// 整组误切成 queryFailed——1ms 预算的测试已实测抖出过这个形状。
if (start > 0 && deadlineNanos != Long.MAX_VALUE && System.nanoTime() >= deadlineNanos) {
// 预算用尽:不再为后面的批次捶上游。已缓存的品牌仍按缓存结论归类(不花代价),
// 没结论的行归 queryFailed 入补测队列——既不按 accepted 放行(可能正是侵权品牌),
// 也不按 rejected 拉黑(没查过就不该写进无效品牌表,否则永久误杀)。
@@ -29,6 +29,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
@@ -50,6 +51,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.zip.ZipEntry;
@@ -66,6 +68,12 @@ public class DedupeRunService {
private static final String BRAND_COLUMN_HEADER = "品牌";
/** 同时处理的文件数上限,避免多文件并行时打满内存与 OSS 上传带宽 */
private static final int MAX_PARALLEL_FILES = 4;
/**
* 单文件等待处理槽位的默认上限:槽位只有 {@code MAX_PARALLEL_FILES} 个,
* 原先 {@code acquire()} 没有超时——一个文件卡在 OSS 上传/解析上,同批与后续
* 任务的每个文件都会无限排在这里(任务不落终态,进度一直停在已提交的那格)。
*/
private static final long DEFAULT_FILE_SLOT_WAIT_SECONDS = 600;
/** 单用户同时运行中的去重任务数上限 */
private static final int MAX_RUNNING_TASKS_PER_USER = 2;
/** 单次请求最多允许的源文件数,避免每个文件形成大量排队对象。 */
@@ -97,6 +105,9 @@ public class DedupeRunService {
/** userId -> 运行中任务数,限制单用户并发任务 */
private final Map<Long, AtomicInteger> runningTaskCountMap = new ConcurrentHashMap<>();
private final Semaphore fileParallelSemaphore = new Semaphore(MAX_PARALLEL_FILES);
/** 等待处理槽位的上限秒数(默认见 {@code DEFAULT_FILE_SLOT_WAIT_SECONDS})。 */
@Value("${aiimage.dedupe.run.file-slot-wait-seconds:600}")
private long fileSlotWaitSeconds = DEFAULT_FILE_SLOT_WAIT_SECONDS;
/** 共享有界文件执行器;不再为请求中的每个文件创建虚拟线程。 */
// 队列改为有界:newFixedThreadPool 的无界队列在文件堆积时不会拒绝、会把内存吃满
private final ExecutorService fileExecutor = com.nanri.aiimage.common.support.ThreadPools
@@ -478,13 +489,31 @@ public class DedupeRunService {
fileTaskMapper.updateById(task);
}
/**
* 等待一个文件处理槽位,最多等 {@code fileSlotWaitSeconds} 秒。
* 返回 false 表示没排到——调用方必须把该文件判失败并写明原因,不许静默续等。
*/
boolean tryAcquireFileSlot() throws InterruptedException {
return fileParallelSemaphore.tryAcquire(fileSlotWaitSeconds, TimeUnit.SECONDS);
}
private DedupeResultItemVo processFile(DedupeSourceFileDto sourceFile, DedupeRunRequest request,
boolean folderMode, FileTaskEntity task,
Map<String, DedupeArchiveEntry> archiveEntries, Long companyId) {
DedupeResultItemVo item = new DedupeResultItemVo();
item.setSourceFilename(sourceFile.getOriginalFilename());
try {
fileParallelSemaphore.acquire();
if (!tryAcquireFileSlot()) {
// 槽位被别的文件长期占着(卡在上游/上传未完成):本文件判失败并写明
// 是没排到槽位,而不是装作处理过——静默排队会让整个任务不落终态。
item.setSuccess(false);
item.setError("等待处理槽位超时(>" + fileSlotWaitSeconds
+ "s,同批其它文件仍未处理完),本文件未处理,请稍后重试");
log.warn("[dedupe] 等待文件处理槽位超时 file={} waitSeconds={} occupied={}",
sourceFile.getOriginalFilename(), fileSlotWaitSeconds,
MAX_PARALLEL_FILES - fileParallelSemaphore.availablePermits());
return item;
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
item.setSuccess(false);
@@ -0,0 +1,168 @@
package com.nanri.aiimage.modules.keywordshare.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareAddRequest;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareDeleteRequest;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordUsageBatchRequest;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareAddResultVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareDeleteResultVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareFiltersVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareLogPageVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareMarkResultVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordSharePageVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareRowVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareStatsGroupedVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareUnmarkResultVo;
import com.nanri.aiimage.modules.keywordshare.service.KeywordShareService;
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 五国关键词共享(桌面 Tkinter 工具的 web 化)。
*
* <p>company_id 一律服务端从当前登录人取,前端不传、也不接受传参;
* 页面权限键 {@code keyword-share}(后台菜单 column_key,经 requireConsolePageAccess 把门)。</p>
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/keyword-share")
@Tag(name = "五国关键词共享", description = "五国关键词共享词库的查询、增删、使用标记与团队共享记录。数据按公司隔离。")
public class KeywordShareController {
/** 后台菜单权限键(column_key)。 */
private static final String PERMISSION_KEY = "keyword-share";
private static final String DENIAL_MESSAGE = "无权访问五国关键词共享";
private final KeywordShareService keywordShareService;
private final AdminAuthSupport adminAuthSupport;
private final PermissionMenuService permissionMenuService;
@GetMapping("/keywords")
@Operation(summary = "分页查询关键词", description = "按国家/类目/搜索/使用状态筛选,未使用在前、已使用按使用时间升序。")
public ApiResponse<KeywordSharePageVo> page(
HttpServletRequest request,
@Parameter(description = "国家") @RequestParam(name = "country", required = false) String country,
@Parameter(description = "一级类目") @RequestParam(name = "category", required = false) String category,
@Parameter(description = "二级类目") @RequestParam(name = "subCategory", required = false) String subCategory,
@Parameter(description = "三级类目") @RequestParam(name = "categoryL3", required = false) String categoryL3,
@Parameter(description = "四级类目") @RequestParam(name = "categoryL4", required = false) String categoryL4,
@Parameter(description = "关键词/中文模糊搜索") @RequestParam(name = "search", required = false) String search,
@Parameter(description = "使用状态:all|used|unused") @RequestParam(name = "status", required = false) String status,
@Parameter(description = "已使用时间范围(天):1=今天,7/30/90/180/360;0或不传=全部")
@RequestParam(name = "usedDays", required = false) Integer usedDays,
@Parameter(description = "仅看我使用") @RequestParam(name = "onlyMine", required = false) Boolean onlyMine,
@Parameter(description = "页码(1 起)") @RequestParam(name = "page", required = false) Long page,
@Parameter(description = "每页数量(默认 200,上限 500)") @RequestParam(name = "size", required = false) Long size) {
Operator operator = requireKeywordShareAccess(request);
return ApiResponse.success(keywordShareService.page(operator.companyId(), operator.userName(),
country, category, subCategory, categoryL3, categoryL4, search,
status, usedDays, Boolean.TRUE.equals(onlyMine), page, size));
}
@GetMapping("/filters")
@Operation(summary = "查询筛选名册", description = "国家列表与一级→二级→三级→四级类目树,按当前公司 DISTINCT。")
public ApiResponse<KeywordShareFiltersVo> filters(HttpServletRequest request) {
Operator operator = requireKeywordShareAccess(request);
return ApiResponse.success(keywordShareService.filters(operator.companyId()));
}
@GetMapping("/stats-grouped")
@Operation(summary = "词库统计", description = "国家与一级/二级/三级类目的分布计数(统计弹窗)。")
public ApiResponse<KeywordShareStatsGroupedVo> statsGrouped(HttpServletRequest request) {
Operator operator = requireKeywordShareAccess(request);
return ApiResponse.success(keywordShareService.statsGrouped(operator.companyId()));
}
@PostMapping("/keywords")
@Operation(summary = "批量添加关键词", description = "trim 规范化;已存在/为空跳过;成功行写词本体、添加留痕并自动标记已使用。")
public ApiResponse<KeywordShareAddResultVo> add(
HttpServletRequest request,
@Valid @RequestBody KeywordShareAddRequest body) {
Operator operator = requireKeywordShareAccess(request);
return ApiResponse.success("添加成功", keywordShareService.add(body, operator.companyId(), operator.userName()));
}
@DeleteMapping("/keywords")
@Operation(summary = "批量删除关键词", description = "删除本公司词行并写删除留痕。")
public ApiResponse<KeywordShareDeleteResultVo> delete(
HttpServletRequest request,
@Valid @RequestBody KeywordShareDeleteRequest body) {
Operator operator = requireKeywordShareAccess(request);
return ApiResponse.success("删除成功", keywordShareService.delete(body, operator.companyId(), operator.userName()));
}
@PostMapping("/usage/mark")
@Operation(summary = "批量标记已使用", description = "写 keyword_usage,唯一键冲突按 UPSERT 刷新,幂等。")
public ApiResponse<KeywordShareMarkResultVo> mark(
HttpServletRequest request,
@Valid @RequestBody KeywordUsageBatchRequest body) {
Operator operator = requireKeywordShareAccess(request);
return ApiResponse.success(keywordShareService.mark(body, operator.companyId(), operator.userName()));
}
@PostMapping("/usage/unmark")
@Operation(summary = "批量取消使用", description = "删除 keyword_usage 行。")
public ApiResponse<KeywordShareUnmarkResultVo> unmark(
HttpServletRequest request,
@Valid @RequestBody KeywordUsageBatchRequest body) {
Operator operator = requireKeywordShareAccess(request);
return ApiResponse.success(keywordShareService.unmark(body, operator.companyId()));
}
@GetMapping("/logs")
@Operation(summary = "团队共享记录", description = "使用/添加/删除三类留痕的分页只读展示。")
public ApiResponse<KeywordShareLogPageVo> logs(
HttpServletRequest request,
@Parameter(description = "记录类型:usage|add|delete", required = true)
@RequestParam(name = "type") String type,
@Parameter(description = "国家") @RequestParam(name = "country", required = false) String country,
@Parameter(description = "页码(1 起)") @RequestParam(name = "page", required = false) Long page,
@Parameter(description = "每页数量") @RequestParam(name = "size", required = false) Long size) {
Operator operator = requireKeywordShareAccess(request);
return ApiResponse.success(keywordShareService.logs(operator.companyId(), type, country, page, size));
}
@GetMapping("/random")
@Operation(summary = "随机抽词", description = "从当前筛选的未使用池随机抽取(默认 10 条)。")
public ApiResponse<List<KeywordShareRowVo>> random(
HttpServletRequest request,
@Parameter(description = "抽取条数(默认 10)") @RequestParam(name = "count", required = false) Long count,
@Parameter(description = "国家") @RequestParam(name = "country", required = false) String country,
@Parameter(description = "一级类目") @RequestParam(name = "category", required = false) String category,
@Parameter(description = "二级类目") @RequestParam(name = "subCategory", required = false) String subCategory,
@Parameter(description = "三级类目") @RequestParam(name = "categoryL3", required = false) String categoryL3,
@Parameter(description = "四级类目") @RequestParam(name = "categoryL4", required = false) String categoryL4,
@Parameter(description = "关键词/中文模糊搜索") @RequestParam(name = "search", required = false) String search) {
Operator operator = requireKeywordShareAccess(request);
return ApiResponse.success(keywordShareService.random(operator.companyId(), operator.userName(),
country, category, subCategory, categoryL3, categoryL4, search, count));
}
/**
* 逐端点把门:登录态 + 后台菜单权限;公司 id 与用户名都从登录人实体取,前端传参不参与归属判定。
*/
private Operator requireKeywordShareAccess(HttpServletRequest request) {
AdminUserEntity operator = adminAuthSupport.requireUser(request);
permissionMenuService.requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY);
return new Operator(operator.getCompanyId(), operator.getUsername());
}
private record Operator(Long companyId, String userName) {
}
}
@@ -0,0 +1,18 @@
package com.nanri.aiimage.modules.keywordshare.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordAddLogEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 五国关键词共享——添加留痕(keyword_add_log)数据访问。
*/
@Mapper
public interface KeywordAddLogMapper extends BaseMapper<KeywordAddLogEntity> {
/** 批量写添加留痕(对齐桌面版 ShareSync.log_add_batch 的单次往返)。 */
int insertBatch(@Param("rows") List<KeywordAddLogEntity> rows);
}
@@ -0,0 +1,18 @@
package com.nanri.aiimage.modules.keywordshare.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordDeleteLogEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 五国关键词共享——删除留痕(keyword_delete_log)数据访问。
*/
@Mapper
public interface KeywordDeleteLogMapper extends BaseMapper<KeywordDeleteLogEntity> {
/** 批量写删除留痕。 */
int insertBatch(@Param("rows") List<KeywordDeleteLogEntity> rows);
}
@@ -0,0 +1,62 @@
package com.nanri.aiimage.modules.keywordshare.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordCategoryCountRow;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordCategoryL3CountRow;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordCategoryTreeRow;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordNameCountRow;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareFilterQuery;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareKey;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordShareEntity;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareRowVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 五国关键词共享——词本体(keyword_share)数据访问。
*
* <p>列表/统计/随机这类带 JOIN 与动态筛选的 SQL 全部收口在本 Mapper 的 XML 里
* ({@code mapper/keywordshare/KeywordShareMapper.xml}),筛选值一律 {@code #{}} 占位符。</p>
*/
@Mapper
public interface KeywordShareMapper extends BaseMapper<KeywordShareEntity> {
/** 筛选命中总数(含状态/时间/仅看我条件)。 */
long countHit(@Param("f") KeywordShareFilterQuery filter);
/** 当前页行:未使用在前,已使用按 used_time 升序(对齐桌面版 _filtered_rows_conn 的口径)。 */
List<KeywordShareRowVo> selectRows(@Param("f") KeywordShareFilterQuery filter);
/** 未使用池随机抽词。 */
List<KeywordShareRowVo> selectRandomUnused(@Param("f") KeywordShareFilterQuery filter);
/** 本公司跨国重复词数:同词出现在 2 个及以上国家。 */
long countDupKeywords(@Param("companyId") Long companyId);
/** 类目筛选名册:国家平铺。 */
List<String> selectFilterCountries(@Param("companyId") Long companyId);
/** 类目筛选名册:类目树的去重行(category/sub_category/category_l3/category_l4)。 */
List<KeywordCategoryTreeRow> selectFilterTreeRows(@Param("companyId") Long companyId);
/** 词库统计:按国家分布。 */
List<KeywordNameCountRow> selectCountryCounts(@Param("companyId") Long companyId);
/** 词库统计:按一级类目分布。 */
List<KeywordNameCountRow> selectCategoryCounts(@Param("companyId") Long companyId);
/** 词库统计:按二级类目分布(挂一级类目下)。 */
List<KeywordCategoryCountRow> selectSubCategoryCounts(@Param("companyId") Long companyId);
/** 词库统计:按三级类目分布(挂一级+二级下)。 */
List<KeywordCategoryL3CountRow> selectCategoryL3Counts(@Param("companyId") Long companyId);
/** 批量查 (country, keyword) 已存在的键(添加防重预检)。 */
List<KeywordShareKey> selectExistingKeywords(@Param("companyId") Long companyId,
@Param("keys") List<KeywordShareKey> keys);
/** 批量 INSERT IGNORE:命中唯一键 uk_comp_cw 的重复行静默跳过,兜底与预检双保险。 */
int insertBatchIgnore(@Param("rows") List<KeywordShareEntity> rows);
}
@@ -0,0 +1,24 @@
package com.nanri.aiimage.modules.keywordshare.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareKey;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordUsageEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 五国关键词共享——使用状态(keyword_usage)数据访问。
* 批量标记走 UPSERT(对齐桌面版 ShareSync.mark 的 ON DUPLICATE KEY UPDATE 语义,天然幂等),
* SQL 收口在 {@code mapper/keywordshare/KeywordUsageMapper.xml}。
*/
@Mapper
public interface KeywordUsageMapper extends BaseMapper<KeywordUsageEntity> {
/** 批量 UPSERT:唯一键冲突时刷新 user_name/used_time,幂等。 */
int upsertBatch(@Param("rows") List<KeywordUsageEntity> rows);
/** 批量取消使用:按 (company_id, country, keyword) 删行,返回实际删除条数。 */
int deleteBatch(@Param("companyId") Long companyId, @Param("keys") List<KeywordShareKey> keys);
}
@@ -0,0 +1,14 @@
package com.nanri.aiimage.modules.keywordshare.model.dto;
import lombok.Data;
/**
* 词库统计的「一级类目 + 二级类目 + 计数」行。
*/
@Data
public class KeywordCategoryCountRow {
private String category;
private String name;
private Long count;
}
@@ -0,0 +1,15 @@
package com.nanri.aiimage.modules.keywordshare.model.dto;
import lombok.Data;
/**
* 词库统计的「一级 + 二级 + 三级类目 + 计数」行。
*/
@Data
public class KeywordCategoryL3CountRow {
private String category;
private String subCategory;
private String name;
private Long count;
}
@@ -0,0 +1,15 @@
package com.nanri.aiimage.modules.keywordshare.model.dto;
import lombok.Data;
/**
* 类目筛选名册的树行(DISTINCT category/sub_category/category_l3/category_l4),Service 内组装成树。
*/
@Data
public class KeywordCategoryTreeRow {
private String category;
private String subCategory;
private String categoryL3;
private String categoryL4;
}
@@ -0,0 +1,13 @@
package com.nanri.aiimage.modules.keywordshare.model.dto;
import lombok.Data;
/**
* 词库统计的「名称 + 计数」行(按国家 / 按一级类目)。
*/
@Data
public class KeywordNameCountRow {
private String name;
private Long count;
}
@@ -0,0 +1,36 @@
package com.nanri.aiimage.modules.keywordshare.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
import java.util.List;
/**
* 批量添加关键词请求。词的一级~四级类目由词库既有口径维护,web 端添加不携带类目字段
* (落库取列默认值 ''),与桌面版从纯文本行解析出 country/keyword/chinese 的输入面一致。
*/
@Data
@Schema(description = "五国关键词批量添加请求")
public class KeywordShareAddRequest {
@Valid
@NotEmpty(message = "items 不能为空")
@Schema(description = "待添加的词列表", requiredMode = Schema.RequiredMode.REQUIRED)
private List<Item> items;
@Data
@Schema(description = "单个待添加词")
public static class Item {
@Schema(description = "国家", requiredMode = Schema.RequiredMode.REQUIRED)
private String country;
@Schema(description = "关键词", requiredMode = Schema.RequiredMode.REQUIRED)
private String keyword;
@Schema(description = "中文翻译")
private String chinese;
}
}
@@ -0,0 +1,19 @@
package com.nanri.aiimage.modules.keywordshare.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
import java.util.List;
/**
* 批量删除关键词请求(按列表行的主键 id)。
*/
@Data
@Schema(description = "五国关键词批量删除请求")
public class KeywordShareDeleteRequest {
@NotEmpty(message = "ids 不能为空")
@Schema(description = "待删除的行主键列表", requiredMode = Schema.RequiredMode.REQUIRED)
private List<Long> ids;
}
@@ -0,0 +1,53 @@
package com.nanri.aiimage.modules.keywordshare.model.dto;
import lombok.Data;
import java.time.LocalDateTime;
/**
* keyword_share 列表/随机抽词的筛选条件与分页参数——Mapper XML 的唯一入参载体({@code @Param("f")})。
*
* <p>筛选值全部走 {@code #{}} 占位符,SQL 侧不做任何字符串拼接。</p>
*/
@Data
public class KeywordShareFilterQuery {
/** 归属公司,服务端从当前登录人取,永远不为空。 */
private Long companyId;
private String country;
private String category;
private String subCategory;
private String categoryL3;
private String categoryL4;
/** keyword/chinese 双列 LIKE 的搜索词,服务端已 trim;空串表示不筛。 */
private String search;
/** all | used | unused;空/未知按 all 处理。 */
private String status;
/** 已使用时间下限;null 表示不筛时间。 */
private LocalDateTime usedSince;
/** true = 仅看当前用户名标记的使用记录。 */
private Boolean onlyMine;
/** 仅看我的判定基准用户名。 */
private String userName;
private long offset;
private long limit;
/** 复制一份基础筛选,只换状态口径(命中总数与「命中内已使用数」要按不同 status 各数一遍)。 */
public KeywordShareFilterQuery copyWithStatus(String newStatus) {
KeywordShareFilterQuery copy = new KeywordShareFilterQuery();
copy.setCompanyId(companyId);
copy.setCountry(country);
copy.setCategory(category);
copy.setSubCategory(subCategory);
copy.setCategoryL3(categoryL3);
copy.setCategoryL4(categoryL4);
copy.setSearch(search);
copy.setStatus(newStatus);
copy.setUsedSince(usedSince);
copy.setOnlyMine(onlyMine);
copy.setUserName(userName);
copy.setOffset(offset);
copy.setLimit(limit);
return copy;
}
}
@@ -0,0 +1,20 @@
package com.nanri.aiimage.modules.keywordshare.model.dto;
import lombok.Data;
/**
* (country, keyword) 业务键——查重、标记/取消使用的批量键载体。
*/
@Data
public class KeywordShareKey {
private String country;
private String keyword;
public static KeywordShareKey of(String country, String keyword) {
KeywordShareKey key = new KeywordShareKey();
key.setCountry(country);
key.setKeyword(keyword);
return key;
}
}
@@ -0,0 +1,32 @@
package com.nanri.aiimage.modules.keywordshare.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
import java.util.List;
/**
* 批量标记/取消使用请求。接口语义由路径区分(/usage/mark 与 /usage/unmark),body 同形。
*/
@Data
@Schema(description = "五国关键词批量标记/取消使用请求")
public class KeywordUsageBatchRequest {
@Valid
@NotEmpty(message = "items 不能为空")
@Schema(description = "待操作的词键列表", requiredMode = Schema.RequiredMode.REQUIRED)
private List<Item> items;
@Data
@Schema(description = "单个词键")
public static class Item {
@Schema(description = "国家", requiredMode = Schema.RequiredMode.REQUIRED)
private String country;
@Schema(description = "关键词", requiredMode = Schema.RequiredMode.REQUIRED)
private String keyword;
}
}
@@ -0,0 +1,28 @@
package com.nanri.aiimage.modules.keywordshare.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 五国关键词共享——添加留痕(谁添加了哪个词,可追溯源头)。
*
* <p>列结构按桌面版 {@code ShareSync.log_add} 的 {@code CREATE TABLE IF NOT EXISTS keyword_add_log}
* 逐列核对(keyword_share_app.py L2058-2066):id 自增主键、country NOT NULL、keyword NOT NULL、
* user_name DEFAULT ''、add_time 可空、company_id NOT NULL DEFAULT 1。</p>
*/
@Data
@TableName("keyword_add_log")
public class KeywordAddLogEntity {
@TableId(type = IdType.AUTO)
private Long id;
private String country;
private String keyword;
private String userName;
private LocalDateTime addTime;
private Long companyId;
}
@@ -0,0 +1,28 @@
package com.nanri.aiimage.modules.keywordshare.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 五国关键词共享——删除留痕(谁删了哪个词,可追溯源头)。
*
* <p>列结构按桌面版 {@code ShareSync.log_delete} 的 {@code CREATE TABLE IF NOT EXISTS keyword_delete_log}
* 逐列核对(keyword_share_app.py L2019-2026):id 自增主键、country NOT NULL、keyword NOT NULL、
* user_name DEFAULT ''、delete_time 可空、company_id NOT NULL DEFAULT 1。</p>
*/
@Data
@TableName("keyword_delete_log")
public class KeywordDeleteLogEntity {
@TableId(type = IdType.AUTO)
private Long id;
private String country;
private String keyword;
private String userName;
private LocalDateTime deleteTime;
private Long companyId;
}
@@ -0,0 +1,35 @@
package com.nanri.aiimage.modules.keywordshare.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 五国关键词共享——词本体(含四级类目)。
*
* <p>表在主库(aiimage)早已存在且已是全量词库,桌面 Tkinter 工具与 web 版共用同一份,
* 每公司一份、按 {@code company_id} 隔离;唯一键 {@code uk_comp_cw(company_id, country, keyword)}。
* <b>不建表、不走 Flyway</b>:本模块只做实体/Mapper 映射既有表。</p>
*/
@Data
@TableName("keyword_share")
public class KeywordShareEntity {
@TableId(type = IdType.AUTO)
private Long id;
private String country;
private String keyword;
private String chinese;
private String ktype;
private String source;
private String category;
private String subCategory;
private String categoryL3;
private String categoryL4;
private String userName;
private LocalDateTime addTime;
private Long companyId;
}
@@ -0,0 +1,27 @@
package com.nanri.aiimage.modules.keywordshare.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 五国关键词共享——使用状态(谁在什么时候标记了哪个词)。
*
* <p>每公司一份,唯一键 {@code uk_comp_cw(company_id, country, keyword)};
* 「仅看我使用」按 {@code user_name = 当前用户名} 过滤。词的删除不走这里(删词写 keyword_delete_log)。</p>
*/
@Data
@TableName("keyword_usage")
public class KeywordUsageEntity {
@TableId(type = IdType.AUTO)
private Long id;
private String country;
private String keyword;
private String userName;
private LocalDateTime usedTime;
private Long companyId;
}
@@ -0,0 +1,18 @@
package com.nanri.aiimage.modules.keywordshare.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 批量添加结果:成功条数 + 跳过条数(已存在/为空/批内重复)。
*/
@Data
@Schema(description = "五国关键词批量添加结果")
public class KeywordShareAddResultVo {
@Schema(description = "成功添加条数")
private int added;
@Schema(description = "跳过条数(已存在、关键词为空或批内重复)")
private int skipped;
}
@@ -0,0 +1,15 @@
package com.nanri.aiimage.modules.keywordshare.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 批量删除结果。
*/
@Data
@Schema(description = "五国关键词批量删除结果")
public class KeywordShareDeleteResultVo {
@Schema(description = "实际删除条数(仅计本公司存在的行)")
private int deleted;
}
@@ -0,0 +1,53 @@
package com.nanri.aiimage.modules.keywordshare.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.List;
/**
* 类目筛选名册:国家平铺 + 一级→二级→三级→四级类目树(按当前公司 DISTINCT)。
*/
@Data
@Schema(description = "五国关键词筛选名册")
public class KeywordShareFiltersVo {
@Schema(description = "国家列表")
private List<String> countries;
@Schema(description = "类目树")
private List<CategoryNode> categories;
@Data
@Schema(description = "一级类目节点")
public static class CategoryNode {
@Schema(description = "一级类目名")
private String name;
@Schema(description = "二级类目列表")
private List<SubCategoryNode> subs;
}
@Data
@Schema(description = "二级类目节点")
public static class SubCategoryNode {
@Schema(description = "二级类目名")
private String name;
@Schema(description = "三级类目列表")
private List<L3Node> l3;
}
@Data
@Schema(description = "三级类目节点")
public static class L3Node {
@Schema(description = "三级类目名")
private String name;
@Schema(description = "四级类目列表")
private List<String> l4;
}
}
@@ -0,0 +1,20 @@
package com.nanri.aiimage.modules.keywordshare.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.List;
/**
* 团队共享记录分页。
*/
@Data
@Schema(description = "五国关键词共享记录分页")
public class KeywordShareLogPageVo {
@Schema(description = "总条数")
private long total;
@Schema(description = "当前页行")
private List<KeywordShareLogVo> rows;
}
@@ -0,0 +1,26 @@
package com.nanri.aiimage.modules.keywordshare.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 团队共享记录单行(使用 / 添加 / 删除三类留痕同形展示,只读)。
*/
@Data
@Schema(description = "五国关键词共享记录行")
public class KeywordShareLogVo {
@Schema(description = "操作人")
private String userName;
@Schema(description = "国家")
private String country;
@Schema(description = "关键词")
private String keyword;
@Schema(description = "操作时间(使用时间/添加时间/删除时间)")
private LocalDateTime time;
}
@@ -0,0 +1,15 @@
package com.nanri.aiimage.modules.keywordshare.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 批量标记使用结果。
*/
@Data
@Schema(description = "五国关键词批量标记使用结果")
public class KeywordShareMarkResultVo {
@Schema(description = "成功标记条数(唯一键冲突按幂等处理,仍计入)")
private int marked;
}
@@ -0,0 +1,23 @@
package com.nanri.aiimage.modules.keywordshare.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.List;
/**
* 关键词分页列表:总命中数 + 当前行 + 统计条。
*/
@Data
@Schema(description = "五国关键词分页列表")
public class KeywordSharePageVo {
@Schema(description = "筛选命中总数")
private long total;
@Schema(description = "当前页行")
private List<KeywordShareRowVo> rows;
@Schema(description = "统计条")
private KeywordShareStatsVo stats;
}
@@ -0,0 +1,47 @@
package com.nanri.aiimage.modules.keywordshare.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 关键词列表/随机抽词的单行。
*/
@Data
@Schema(description = "五国关键词行")
public class KeywordShareRowVo {
@Schema(description = "行主键")
private Long id;
@Schema(description = "国家")
private String country;
@Schema(description = "关键词")
private String keyword;
@Schema(description = "中文翻译")
private String chinese;
@Schema(description = "一级类目")
private String category;
@Schema(description = "二级类目")
private String subCategory;
@Schema(description = "三级类目")
private String categoryL3;
@Schema(description = "四级类目")
private String categoryL4;
@Schema(description = "是否已使用")
private boolean used;
@Schema(description = "使用时间(未使用为 null)")
private LocalDateTime usedTime;
@Schema(description = "使用者(未使用为空串)")
private String usedUser;
}
@@ -0,0 +1,29 @@
package com.nanri.aiimage.modules.keywordshare.model.vo;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordCategoryCountRow;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordCategoryL3CountRow;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordNameCountRow;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.List;
/**
* 词库统计弹窗:国家、一级/二级/三级类目的分布计数(对齐桌面版统计窗的四段展示)。
*/
@Data
@Schema(description = "五国关键词词库统计")
public class KeywordShareStatsGroupedVo {
@Schema(description = "按国家分布")
private List<KeywordNameCountRow> byCountry;
@Schema(description = "按一级类目分布")
private List<KeywordNameCountRow> byCategory;
@Schema(description = "按二级类目分布(挂一级类目下)")
private List<KeywordCategoryCountRow> bySubCategory;
@Schema(description = "按三级类目分布(挂一级+二级下)")
private List<KeywordCategoryL3CountRow> byCategoryL3;
}
@@ -0,0 +1,24 @@
package com.nanri.aiimage.modules.keywordshare.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 列表页统计条:命中数、命中内已/未使用数、本公司跨国重复词数。
*/
@Data
@Schema(description = "五国关键词列表统计")
public class KeywordShareStatsVo {
@Schema(description = "筛选命中总数")
private long hit;
@Schema(description = "命中内已使用数")
private long used;
@Schema(description = "命中内未使用数")
private long unused;
@Schema(description = "本公司跨国重复词数")
private long dup;
}
@@ -0,0 +1,15 @@
package com.nanri.aiimage.modules.keywordshare.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 批量取消使用结果。
*/
@Data
@Schema(description = "五国关键词批量取消使用结果")
public class KeywordShareUnmarkResultVo {
@Schema(description = "实际删除的使用记录条数")
private int unmarked;
}
@@ -0,0 +1,480 @@
package com.nanri.aiimage.modules.keywordshare.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.keywordshare.mapper.KeywordAddLogMapper;
import com.nanri.aiimage.modules.keywordshare.mapper.KeywordDeleteLogMapper;
import com.nanri.aiimage.modules.keywordshare.mapper.KeywordShareMapper;
import com.nanri.aiimage.modules.keywordshare.mapper.KeywordUsageMapper;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordCategoryCountRow;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordCategoryL3CountRow;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordCategoryTreeRow;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordNameCountRow;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareAddRequest;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareDeleteRequest;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareFilterQuery;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareKey;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordUsageBatchRequest;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordAddLogEntity;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordDeleteLogEntity;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordShareEntity;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordUsageEntity;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareAddResultVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareDeleteResultVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareFiltersVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareLogPageVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareLogVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareMarkResultVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordSharePageVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareRowVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareStatsGroupedVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareStatsVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareUnmarkResultVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 五国关键词共享(桌面 Tkinter 工具的 web 化)。
*
* <p>词本体 = 主库 keyword_share(每公司一份、company_id 隔离),web 版没有 SQLite;
* 所有查询/写入都以服务端取到的 companyId 收口,杜绝跨公司读写。
* 行为口径对齐桌面版 keyword_share_app.py:列表「未使用在前、已使用按 used_time 升序」,
* 添加即自动标记已使用(使用者=当前用户、时间=添加时刻),删词写 keyword_delete_log 留痕,
* 标记走 UPSERT 天然幂等。</p>
*/
@Service
@RequiredArgsConstructor
public class KeywordShareService {
/** 列表默认页容量(API 契约定值;不走 PageDefaults 以免挤进它的逐点台账)。 */
public static final long DEFAULT_PAGE_SIZE = 200;
/** 列表页容量上限。 */
public static final long MAX_PAGE_SIZE = 500;
/** 随机抽词默认条数。 */
public static final long DEFAULT_RANDOM_COUNT = 10;
/** 随机抽词单次上限。 */
public static final long MAX_RANDOM_COUNT = 100;
/** 共享记录单页上限(留痕查询非无限拉取)。 */
public static final long MAX_LOG_PAGE_SIZE = 500;
public static final String FILTER_ALL = "all";
public static final String FILTER_USED = "used";
public static final String FILTER_UNUSED = "unused";
/** 添加词的固定口径,与桌面版 insert_keyword 一致。 */
private static final String KTYPE_MANUAL = "主关键词";
private static final String SOURCE_MANUAL = "手动添加";
private final KeywordShareMapper keywordShareMapper;
private final KeywordUsageMapper keywordUsageMapper;
private final KeywordAddLogMapper keywordAddLogMapper;
private final KeywordDeleteLogMapper keywordDeleteLogMapper;
// ---------------------------------------------------------------- 列表与统计
public KeywordSharePageVo page(Long companyId, String viewerUserName,
String country, String category, String subCategory,
String categoryL3, String categoryL4, String search,
String status, Integer usedDays, boolean onlyMine,
Long page, Long size) {
KeywordShareFilterQuery filter = buildFilter(companyId, viewerUserName, country, category, subCategory,
categoryL3, categoryL4, search, status, usedDays, onlyMine);
filter.setOffset((safePage(page) - 1) * safePageSize(size));
filter.setLimit(safePageSize(size));
long hit = keywordShareMapper.countHit(filter);
// 命中内已使用数:状态过滤生效时命中即答案;否则按同条件单独数一遍 used
long used = switch (filter.getStatus()) {
case FILTER_USED -> hit;
case FILTER_UNUSED -> 0;
default -> keywordShareMapper.countHit(filter.copyWithStatus(FILTER_USED));
};
List<KeywordShareRowVo> rows = keywordShareMapper.selectRows(filter).stream()
.map(this::normalizeRow)
.toList();
long dup = keywordShareMapper.countDupKeywords(companyId);
KeywordShareStatsVo stats = new KeywordShareStatsVo();
stats.setHit(hit);
stats.setUsed(used);
stats.setUnused(hit - used);
stats.setDup(dup);
KeywordSharePageVo vo = new KeywordSharePageVo();
vo.setTotal(hit);
vo.setRows(rows);
vo.setStats(stats);
return vo;
}
public KeywordShareFiltersVo filters(Long companyId) {
KeywordShareFiltersVo vo = new KeywordShareFiltersVo();
vo.setCountries(keywordShareMapper.selectFilterCountries(companyId));
Map<String, Map<String, Map<String, Set<String>>>> tree = new LinkedHashMap<>();
for (KeywordCategoryTreeRow row : keywordShareMapper.selectFilterTreeRows(companyId)) {
tree.computeIfAbsent(orEmpty(row.getCategory()), c -> new LinkedHashMap<>())
.computeIfAbsent(orEmpty(row.getSubCategory()), s -> new LinkedHashMap<>())
.computeIfAbsent(orEmpty(row.getCategoryL3()), l -> new LinkedHashSet<>())
.add(orEmpty(row.getCategoryL4()));
}
List<KeywordShareFiltersVo.CategoryNode> categories = new ArrayList<>();
tree.forEach((category, subs) -> {
KeywordShareFiltersVo.CategoryNode categoryNode = new KeywordShareFiltersVo.CategoryNode();
categoryNode.setName(category);
List<KeywordShareFiltersVo.SubCategoryNode> subNodes = new ArrayList<>();
subs.forEach((sub, l3s) -> {
KeywordShareFiltersVo.SubCategoryNode subNode = new KeywordShareFiltersVo.SubCategoryNode();
subNode.setName(sub);
List<KeywordShareFiltersVo.L3Node> l3Nodes = new ArrayList<>();
l3s.forEach((l3, l4s) -> {
// 三级/四级类目与桌面版同口径:空值不进名册
if (l3.isBlank()) {
return;
}
KeywordShareFiltersVo.L3Node l3Node = new KeywordShareFiltersVo.L3Node();
l3Node.setName(l3);
l3Node.setL4(l4s.stream().filter(l4 -> !l4.isBlank()).toList());
l3Nodes.add(l3Node);
});
subNode.setL3(l3Nodes);
subNodes.add(subNode);
});
categoryNode.setSubs(subNodes);
categories.add(categoryNode);
});
vo.setCategories(categories);
return vo;
}
public KeywordShareStatsGroupedVo statsGrouped(Long companyId) {
KeywordShareStatsGroupedVo vo = new KeywordShareStatsGroupedVo();
vo.setByCountry(keywordShareMapper.selectCountryCounts(companyId));
vo.setByCategory(keywordShareMapper.selectCategoryCounts(companyId));
vo.setBySubCategory(keywordShareMapper.selectSubCategoryCounts(companyId));
vo.setByCategoryL3(keywordShareMapper.selectCategoryL3Counts(companyId));
return vo;
}
/** 未使用池随机抽词;国家/类目筛选可选,数量默认 10、上限 100。 */
public List<KeywordShareRowVo> random(Long companyId, String viewerUserName,
String country, String category, String subCategory,
String categoryL3, String categoryL4, String search,
Long count) {
KeywordShareFilterQuery filter = buildFilter(companyId, viewerUserName, country, category, subCategory,
categoryL3, categoryL4, search, FILTER_UNUSED, null, false);
filter.setOffset(0);
long safeCount = count == null ? DEFAULT_RANDOM_COUNT : Math.min(Math.max(count, 1), MAX_RANDOM_COUNT);
filter.setLimit(safeCount);
return keywordShareMapper.selectRandomUnused(filter).stream()
.map(this::normalizeRow)
.toList();
}
// ---------------------------------------------------------------- 写操作
/** 批量添加:trim 规范化、防重跳过;成功行写词本体 + 添加留痕 + 自动标记已使用。 */
@Transactional
public KeywordShareAddResultVo add(KeywordShareAddRequest request, Long companyId, String operatorName) {
int skipped = 0;
List<KeywordShareAddRequest.Item> candidates = new ArrayList<>();
Set<String> seen = new LinkedHashSet<>();
for (KeywordShareAddRequest.Item item : request.getItems()) {
String country = normalize(item == null ? null : item.getCountry());
String keyword = normalize(item == null ? null : item.getKeyword());
if (country.isEmpty() || keyword.isEmpty()) {
skipped++;
continue;
}
if (!seen.add(country + "\n" + keyword)) {
skipped++;
continue;
}
KeywordShareAddRequest.Item normalized = new KeywordShareAddRequest.Item();
normalized.setCountry(country);
normalized.setKeyword(keyword);
normalized.setChinese(normalize(item.getChinese()));
candidates.add(normalized);
}
List<KeywordShareEntity> toInsert = new ArrayList<>();
if (!candidates.isEmpty()) {
Set<String> existing = new LinkedHashSet<>();
List<KeywordShareKey> keys = candidates.stream()
.map(item -> KeywordShareKey.of(item.getCountry(), item.getKeyword()))
.toList();
for (KeywordShareKey key : keywordShareMapper.selectExistingKeywords(companyId, keys)) {
existing.add(key.getCountry() + "\n" + key.getKeyword());
}
for (KeywordShareAddRequest.Item item : candidates) {
if (existing.contains(item.getCountry() + "\n" + item.getKeyword())) {
skipped++;
continue;
}
KeywordShareEntity entity = new KeywordShareEntity();
entity.setCountry(item.getCountry());
entity.setKeyword(item.getKeyword());
entity.setChinese(item.getChinese());
entity.setKtype(KTYPE_MANUAL);
entity.setSource(SOURCE_MANUAL);
entity.setUserName(operatorName);
entity.setAddTime(LocalDateTime.now());
entity.setCompanyId(companyId);
toInsert.add(entity);
}
}
if (!toInsert.isEmpty()) {
keywordShareMapper.insertBatchIgnore(toInsert);
keywordAddLogMapper.insertBatch(toInsert.stream().map(row -> {
KeywordAddLogEntity log = new KeywordAddLogEntity();
log.setCompanyId(companyId);
log.setCountry(row.getCountry());
log.setKeyword(row.getKeyword());
log.setUserName(operatorName);
log.setAddTime(row.getAddTime());
return log;
}).toList());
markInternal(companyId, operatorName, toInsert.stream()
.map(row -> KeywordShareKey.of(row.getCountry(), row.getKeyword()))
.toList());
}
KeywordShareAddResultVo vo = new KeywordShareAddResultVo();
vo.setAdded(toInsert.size());
vo.setSkipped(skipped);
return vo;
}
/** 批量删除:只删本公司行并写删除留痕(谁删了哪个词可追溯)。 */
@Transactional
public KeywordShareDeleteResultVo delete(KeywordShareDeleteRequest request, Long companyId, String operatorName) {
List<Long> ids = request.getIds().stream().filter(java.util.Objects::nonNull).distinct().toList();
// 公司隔离在应用侧过滤:selectBatchIds 拉回后只认本公司行,别家的 id 传进来也不动
List<KeywordShareEntity> rows = ids.isEmpty() ? List.of()
: keywordShareMapper.selectBatchIds(ids).stream()
.filter(row -> companyId != null && companyId.equals(row.getCompanyId()))
.toList();
if (!rows.isEmpty()) {
keywordShareMapper.deleteBatchIds(rows.stream().map(KeywordShareEntity::getId).toList());
keywordDeleteLogMapper.insertBatch(rows.stream().map(row -> {
KeywordDeleteLogEntity log = new KeywordDeleteLogEntity();
log.setCompanyId(companyId);
log.setCountry(row.getCountry());
log.setKeyword(row.getKeyword());
log.setUserName(operatorName);
log.setDeleteTime(LocalDateTime.now());
return log;
}).toList());
}
KeywordShareDeleteResultVo vo = new KeywordShareDeleteResultVo();
vo.setDeleted(rows.size());
return vo;
}
/** 批量标记已使用;唯一键冲突按 UPSERT 刷新,幂等。used_time 取服务器当前时间。 */
@Transactional
public KeywordShareMarkResultVo mark(KeywordUsageBatchRequest request, Long companyId, String operatorName) {
int marked = markInternal(companyId, operatorName, normalizeKeys(request));
KeywordShareMarkResultVo vo = new KeywordShareMarkResultVo();
vo.setMarked(marked);
return vo;
}
/** 批量取消使用:删 usage 行,返回实际删除条数。 */
@Transactional
public KeywordShareUnmarkResultVo unmark(KeywordUsageBatchRequest request, Long companyId) {
List<KeywordShareKey> keys = normalizeKeys(request);
int unmarked = keys.isEmpty() ? 0 : keywordUsageMapper.deleteBatch(companyId, keys);
KeywordShareUnmarkResultVo vo = new KeywordShareUnmarkResultVo();
vo.setUnmarked(unmarked);
return vo;
}
// ---------------------------------------------------------------- 共享记录(只读)
public KeywordShareLogPageVo logs(Long companyId, String type, String country, Long page, Long size) {
long safePage = safePage(page);
long safeSize = size == null ? DEFAULT_PAGE_SIZE : Math.min(Math.max(size, 1), MAX_LOG_PAGE_SIZE);
return switch (type == null ? "" : type) {
case "usage" -> usageLogs(companyId, country, safePage, safeSize);
case "add" -> addLogs(companyId, country, safePage, safeSize);
case "delete" -> deleteLogs(companyId, country, safePage, safeSize);
default -> throw new BusinessException("type 只支持 usage / add / delete");
};
}
private KeywordShareLogPageVo usageLogs(Long companyId, String country, long page, long size) {
LambdaQueryWrapper<KeywordUsageEntity> query = new LambdaQueryWrapper<KeywordUsageEntity>()
.eq(KeywordUsageEntity::getCompanyId, companyId)
.eq(country != null && !country.isBlank(), KeywordUsageEntity::getCountry, normalize(country))
.orderByDesc(KeywordUsageEntity::getUsedTime)
.orderByDesc(KeywordUsageEntity::getId);
long total = keywordUsageMapper.selectCount(query);
List<KeywordUsageEntity> rows = keywordUsageMapper.selectList(
query.last("LIMIT " + ((page - 1) * size) + ", " + size));
return toLogPage(total, rows.stream().map(row -> {
KeywordShareLogVo vo = new KeywordShareLogVo();
vo.setUserName(orEmpty(row.getUserName()));
vo.setCountry(orEmpty(row.getCountry()));
vo.setKeyword(orEmpty(row.getKeyword()));
vo.setTime(row.getUsedTime());
return vo;
}).toList());
}
private KeywordShareLogPageVo addLogs(Long companyId, String country, long page, long size) {
LambdaQueryWrapper<KeywordAddLogEntity> query = new LambdaQueryWrapper<KeywordAddLogEntity>()
.eq(KeywordAddLogEntity::getCompanyId, companyId)
.eq(country != null && !country.isBlank(), KeywordAddLogEntity::getCountry, normalize(country))
.orderByDesc(KeywordAddLogEntity::getAddTime)
.orderByDesc(KeywordAddLogEntity::getId);
long total = keywordAddLogMapper.selectCount(query);
List<KeywordAddLogEntity> rows = keywordAddLogMapper.selectList(
query.last("LIMIT " + ((page - 1) * size) + ", " + size));
return toLogPage(total, rows.stream().map(row -> {
KeywordShareLogVo vo = new KeywordShareLogVo();
vo.setUserName(orEmpty(row.getUserName()));
vo.setCountry(orEmpty(row.getCountry()));
vo.setKeyword(orEmpty(row.getKeyword()));
vo.setTime(row.getAddTime());
return vo;
}).toList());
}
private KeywordShareLogPageVo deleteLogs(Long companyId, String country, long page, long size) {
LambdaQueryWrapper<KeywordDeleteLogEntity> query = new LambdaQueryWrapper<KeywordDeleteLogEntity>()
.eq(KeywordDeleteLogEntity::getCompanyId, companyId)
.eq(country != null && !country.isBlank(), KeywordDeleteLogEntity::getCountry, normalize(country))
.orderByDesc(KeywordDeleteLogEntity::getDeleteTime)
.orderByDesc(KeywordDeleteLogEntity::getId);
long total = keywordDeleteLogMapper.selectCount(query);
List<KeywordDeleteLogEntity> rows = keywordDeleteLogMapper.selectList(
query.last("LIMIT " + ((page - 1) * size) + ", " + size));
return toLogPage(total, rows.stream().map(row -> {
KeywordShareLogVo vo = new KeywordShareLogVo();
vo.setUserName(orEmpty(row.getUserName()));
vo.setCountry(orEmpty(row.getCountry()));
vo.setKeyword(orEmpty(row.getKeyword()));
vo.setTime(row.getDeleteTime());
return vo;
}).toList());
}
private KeywordShareLogPageVo toLogPage(long total, List<KeywordShareLogVo> rows) {
KeywordShareLogPageVo vo = new KeywordShareLogPageVo();
vo.setTotal(total);
vo.setRows(rows);
return vo;
}
// ---------------------------------------------------------------- 内部工具
private int markInternal(Long companyId, String operatorName, List<KeywordShareKey> keys) {
if (keys.isEmpty()) {
return 0;
}
LocalDateTime now = LocalDateTime.now();
List<KeywordUsageEntity> rows = keys.stream().map(key -> {
KeywordUsageEntity entity = new KeywordUsageEntity();
entity.setCompanyId(companyId);
entity.setCountry(key.getCountry());
entity.setKeyword(key.getKeyword());
entity.setUserName(operatorName);
entity.setUsedTime(now);
return entity;
}).toList();
keywordUsageMapper.upsertBatch(rows);
return rows.size();
}
private List<KeywordShareKey> normalizeKeys(KeywordUsageBatchRequest request) {
Set<String> seen = new LinkedHashSet<>();
List<KeywordShareKey> keys = new ArrayList<>();
for (KeywordUsageBatchRequest.Item item : request.getItems()) {
String country = normalize(item == null ? null : item.getCountry());
String keyword = normalize(item == null ? null : item.getKeyword());
if (country.isEmpty() || keyword.isEmpty() || !seen.add(country + "\n" + keyword)) {
continue;
}
keys.add(KeywordShareKey.of(country, keyword));
}
return keys;
}
private KeywordShareFilterQuery buildFilter(Long companyId, String viewerUserName,
String country, String category, String subCategory,
String categoryL3, String categoryL4, String search,
String status, Integer usedDays, boolean onlyMine) {
KeywordShareFilterQuery filter = new KeywordShareFilterQuery();
filter.setCompanyId(companyId);
filter.setCountry(normalizeOrNull(country));
filter.setCategory(normalizeOrNull(category));
filter.setSubCategory(normalizeOrNull(subCategory));
filter.setCategoryL3(normalizeOrNull(categoryL3));
filter.setCategoryL4(normalizeOrNull(categoryL4));
filter.setSearch(normalizeOrNull(search));
filter.setStatus(normalizeStatus(status));
filter.setUsedSince(resolveUsedSince(usedDays));
filter.setOnlyMine(onlyMine);
filter.setUserName(viewerUserName);
return filter;
}
/** usedDays:1=今天(当日零点起),7/30/90/180/360=往前推 N 天;0/null=不限。 */
private LocalDateTime resolveUsedSince(Integer usedDays) {
if (usedDays == null || usedDays <= 0) {
return null;
}
if (usedDays == 1) {
return LocalDate.now().atStartOfDay();
}
return LocalDateTime.now().minusDays(usedDays);
}
private String normalizeStatus(String status) {
String normalized = normalize(status);
return switch (normalized) {
case FILTER_USED -> FILTER_USED;
case FILTER_UNUSED -> FILTER_UNUSED;
default -> FILTER_ALL;
};
}
private KeywordShareRowVo normalizeRow(KeywordShareRowVo row) {
if (!row.isUsed()) {
row.setUsedUser("");
}
return row;
}
private long safePage(Long page) {
return page == null ? 1 : Math.max(page, 1);
}
private long safePageSize(Long size) {
return size == null ? DEFAULT_PAGE_SIZE : Math.min(Math.max(size, 1), MAX_PAGE_SIZE);
}
private String normalize(String value) {
return value == null ? "" : value.trim();
}
private String normalizeOrNull(String value) {
String normalized = normalize(value);
return normalized.isEmpty() ? null : normalized;
}
private String orEmpty(String value) {
return value == null ? "" : value;
}
}
@@ -33,6 +33,7 @@ public class PermissionMenuSchemaInitializer {
private final JdbcTemplate jdbcTemplate;
private static final List<DefaultAppMenu> DEFAULT_APP_MENUS = List.of(
new DefaultAppMenu("常用工具", "brand_common_tools", "brand-common-tools", 105),
new DefaultAppMenu("前端工具", "brand_front_tools", "brand-front-tools", 110),
new DefaultAppMenu("运营工具", "brand_operation_tools", "brand-operation-tools", 120),
new DefaultAppMenu("后勤工具", "brand_logistics_tools", "brand-logistics-tools", 130)
@@ -82,6 +83,7 @@ public class PermissionMenuSchemaInitializer {
new DefaultAdminMenu("品牌数据库", "admin_invalid_asin_data", "invalid-asin-data", 35, "admin_group_data"),
new DefaultAdminMenu("查询ASIN", "admin_query_asin", "query-asin", 65, "admin_group_data"),
new DefaultAdminMenu("商品类目", "admin_product_categories", "product-categories", 66, "admin_group_data"),
new DefaultAdminMenu("品牌Gating库", "admin_brand_gating", "brand-gating", 36, "admin_group_data"),
new DefaultAdminMenu("店铺密钥管理", "admin_shop_keys", "shop-keys", 40, "admin_group_shop"),
new DefaultAdminMenu("店铺管理", "admin_shop_manage", "shop-manage", 50, "admin_group_shop"),
new DefaultAdminMenu("最低价ASIN设置", "admin_skip_price_asin", "skip-price-asin", 60, "admin_group_shop"),
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.similarasin.client;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.retry.LlmRetryBackoff;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.HttpClientPool;
import com.nanri.aiimage.config.SimilarAsinProperties;
import com.nanri.aiimage.metrics.ExternalCallMetricsRecorder;
@@ -35,11 +36,21 @@ public class SimilarAsinLlmClient {
private static final MediaType APPLICATION_JSON_UTF8 = new MediaType("application", "json", StandardCharsets.UTF_8);
private static final int MAX_LLM_RESPONSE_BYTES = 16 * 1024 * 1024;
/**
* 连接层短路的证据数:同一次调用里连续这么多次「连接根本没建立」且中间没有一次成功,
* 就判定 LLM 网关不可达,立刻停掉重试阶梯。与 {@code AppearancePatentLlmClient}/
* {@code BrandCheckClient} 同口径(B19/B23)——连接层故障重试救不回来,只会把同一笔
* 失败摊满整段退避预算;本侧批量失败后还逐行回退,白等会再乘一个行数。
*/
private static final int LLM_CONNECT_FAILURE_EVIDENCE = 2;
/** 连接层判定不可达时交付的归因文案:说清是运维故障,不是这行数据的问题。 */
static final String REASON_LLM_UNREACHABLE = "LLM 服务不可达(连接未建立,非本行数据问题)";
private final SimilarAsinProperties properties;
private final ObjectMapper objectMapper;
private final ExternalCallMetricsRecorder externalCallMetrics;
private final UserSecretUsageService userSecretUsageService;
private final UnreachableCooldown unreachableCooldown;
private volatile RestClient sharedRestClient;
/** 首次尝试专用客户端(更短读超时,快速失败);配置未生效时与 sharedRestClient 相同。 */
@@ -48,11 +59,13 @@ public class SimilarAsinLlmClient {
public SimilarAsinLlmClient(SimilarAsinProperties properties,
ObjectMapper objectMapper,
ExternalCallMetricsRecorder externalCallMetrics,
UserSecretUsageService userSecretUsageService) {
UserSecretUsageService userSecretUsageService,
UnreachableCooldown unreachableCooldown) {
this.properties = properties;
this.objectMapper = objectMapper;
this.externalCallMetrics = externalCallMetrics;
this.userSecretUsageService = userSecretUsageService;
this.unreachableCooldown = unreachableCooldown;
}
/** 文本对话,json_object 输出。 */
@@ -73,12 +86,45 @@ public class SimilarAsinLlmClient {
String responseFormat) {
String resolvedKey = resolveApiKey(apiKey);
int attempts = Math.max(1, properties.getLlmRetryTimes());
long cooldownMillis = Math.max(0, properties.getLlmUnreachableCooldownMillis());
String host = properties.getLlmHost();
if (unreachableCooldown.isCoolingDown(host, cooldownMillis)) {
// 冷却期快失败不发请求、不记密钥次数:结论与短路版一致(调用方拿不到 LLM 结论),
// 差别只是不再为"确认还是断的"各烧一轮连接探针(B24)
log.debug("[similar-asin][llm] 客户端级冷却中,快速失败 model={} window={}ms", model, cooldownMillis);
throw new IllegalStateException(REASON_LLM_UNREACHABLE + ";lastErr=冷却中(连续连接未建立,暂不打上游)");
}
Exception lastFailure = null;
int connectFailures = 0;
for (int attempt = 1; attempt <= attempts; attempt++) {
try {
return invokeChatOnce(model, system, userText, images, resolvedKey, responseFormat, attempt);
String content = invokeChatOnce(model, system, userText, images, resolvedKey, responseFormat, attempt);
unreachableCooldown.recordConnected(host);
return content;
} catch (Exception ex) {
lastFailure = ex;
if (UnreachableCooldown.isConnectFailure(ex)) {
connectFailures++;
boolean entered = unreachableCooldown.recordConnectFailure(host, cooldownMillis);
if (entered) {
log.warn("[similar-asin][llm] 连接层证据累计 {} 次,进入客户端级冷却 {}ms host={}",
2, cooldownMillis, host);
}
if (connectFailures >= LLM_CONNECT_FAILURE_EVIDENCE
|| unreachableCooldown.isCoolingDown(host, cooldownMillis)) {
log.warn("[similar-asin][llm] LLM_UNREACHABLE 连续 {} 次连接未建立,判定 LLM 服务不可达,"
+ "停止重试 attempt={} model={} err={}",
LLM_CONNECT_FAILURE_EVIDENCE, attempt, model, failureMessage(ex));
// 结论不变(这次调用拿不到 LLM 结论),差别只在耗时与归因:
// 调用方拿到的是"网关没连上",而不是一句裸的连接异常
throw new IllegalStateException(
REASON_LLM_UNREACHABLE + ";lastErr=" + failureMessage(ex), ex);
}
} else {
// 连上了只是这次没答好(读超时/解析失败/5xx):连接层证据不作数
connectFailures = 0;
unreachableCooldown.recordConnected(host);
}
if (attempt >= attempts) {
break;
}
@@ -285,6 +285,12 @@ aiimage:
scan-retention-days: ${AIIMAGE_SHOP_DUPLICATE_SCAN_RETENTION_DAYS:7}
scan-retention-batch-size: ${AIIMAGE_SHOP_DUPLICATE_SCAN_RETENTION_BATCH_SIZE:200}
scan-retention-cron: ${AIIMAGE_SHOP_DUPLICATE_SCAN_RETENTION_CRON:0 50 3 * * *}
# 去重运行参数:2026-10-23 随 70b3be63 给文件槽位等待加上限时只写在 @Value 默认值里,
# 被「开关只写在代码默认值里」棘轮点名(scripts/tests/test_no_implicit_config_switches.py)。
# 出厂值与 @Value 默认(DEFAULT_FILE_SLOT_WAIT_SECONDS=600)逐字相同。
dedupe:
run:
file-slot-wait-seconds: ${AIIMAGE_DEDUPE_RUN_FILE_SLOT_WAIT_SECONDS:600}
# 客户端升级包(MinIO client 桶 nanri-image/versions/):此前只增不删,2026-09-21 已堆到
# 61 个对象 / 6.2GiB。按「保留最近 N 个」而不是按天过期——2026-09-08 那次给 client 桶根
# 加全桶 30 天 ILM,误删了 3.0.24~3.0.37 共 9 个仍在下载的安装包。默认 dry-run 只记日志。
@@ -378,6 +384,9 @@ aiimage:
total-timeout-millis: ${AIIMAGE_BRAND_CHECK_TOTAL_TIMEOUT_MILLIS:90000}
connect-timeout-millis: ${AIIMAGE_BRAND_CHECK_CONNECT_TIMEOUT_MILLIS:10000}
read-timeout-millis: ${AIIMAGE_BRAND_CHECK_READ_TIMEOUT_MILLIS:60000}
# 连接层不可达的客户端级冷却窗长(B24,0=关闭):连续 2 次连接未建立即进窗,
# 窗内整批品牌不发请求直接按服务不可达判失败;到期后下一批的真实请求兼作探针。
unreachable-cooldown-millis: ${AIIMAGE_BRAND_CHECK_UNREACHABLE_COOLDOWN_MILLIS:300000}
appearance-patent:
no-result-upload-timeout-minutes: ${AIIMAGE_APPEARANCE_PATENT_NO_RESULT_UPLOAD_TIMEOUT_MINUTES:180}
llm-host: ${AIIMAGE_APPEARANCE_PATENT_LLM_HOST:https://ai.t8star.org}
@@ -402,6 +411,9 @@ aiimage:
# 实测空行 20.7%~51.3%,全部放行且零告警,因为旧分母按解析行标题判、用户表不带标题列时分母为 0)。
llm-coverage-min-ratio: ${AIIMAGE_APPEARANCE_PATENT_LLM_COVERAGE_MIN_RATIO:0.95}
llm-retry-times: ${AIIMAGE_APPEARANCE_PATENT_LLM_RETRY_TIMES:3}
# 连接层不可达的客户端级冷却窗长(B24,0=关闭):连续 2 次连接未建立即进窗,
# 窗内调用不发请求直接按服务不可达快速失败;到期后下一条真实调用兼作探针。
llm-unreachable-cooldown-millis: ${AIIMAGE_APPEARANCE_PATENT_LLM_UNREACHABLE_COOLDOWN_MILLIS:300000}
# 单行最多被重发几轮(<=0 关闭)。失败行会被释放、下一轮重新申领,没有这道闸时
# 上游劣化窗口内会无限重发并每轮扣用户 LLM 次数——deferRunning 不累加 retryCount,任务层那道永不触发。
llm-row-max-rounds: ${AIIMAGE_APPEARANCE_PATENT_LLM_ROW_MAX_ROUNDS:3}
@@ -440,6 +452,9 @@ aiimage:
llm-read-timeout-millis: ${AIIMAGE_SIMILAR_ASIN_LLM_READ_TIMEOUT_MILLIS:600000}
llm-first-attempt-read-timeout-millis: ${AIIMAGE_SIMILAR_ASIN_LLM_FIRST_ATTEMPT_READ_TIMEOUT_MILLIS:300000}
llm-retry-times: ${AIIMAGE_SIMILAR_ASIN_LLM_RETRY_TIMES:3}
# 连接层不可达的客户端级冷却窗长(B24,0=关闭):连续 2 次连接未建立即进窗,
# 窗内调用不发请求直接按服务不可达快速失败;到期后下一条真实调用兼作探针。
llm-unreachable-cooldown-millis: ${AIIMAGE_SIMILAR_ASIN_LLM_UNREACHABLE_COOLDOWN_MILLIS:300000}
llm-row-concurrency: ${AIIMAGE_SIMILAR_ASIN_LLM_ROW_CONCURRENCY:5}
llm-category-batch-size: ${AIIMAGE_SIMILAR_ASIN_LLM_CATEGORY_BATCH_SIZE:10}
# 合规判定一次请求合并的行数:省往返不省判定,每件商品仍各自出 is_conform/reason/category。
@@ -0,0 +1,110 @@
-- =============================================================
-- 迁移 V163__brand_common_tools_app_menu
--
-- 1. 变更目的:工具台新增「常用工具」APP 一级分组(brand_common_tools)+ 三个工具子菜单
-- (pricing-calc 定价利润换算 / five-asin 五国ASIN快速查询 / keyword-share 五国关键词共享)。
-- 前端卡片经客户端壳拉起本地 exe,不在本后端开新页面。组与工具各自可授:默认给全量账号
-- 授组行(读侧由 PermissionMenuService 展开子孙,见 V80 头注),管理员可收组、也可只授单个工具。
--
-- 2. 口径:
-- - 排序 105~108:组行 105,三个子菜单 106/107/108,整组排在 前端工具(110)/运营工具(120)/
-- 后勤工具(130) 之前,作为首个分类。
-- - company_id = 1 的行是模板树(PermissionMenuService.copyTreeFromTemplate 的源头),
-- 其余公司(含 status=0 的停用公司)逐家复制一份——与 V149「复制对全公司做」同一理由:
-- 一行菜单不构成成本,别让启用公司变成要先补树的步骤。子菜单 parent_id 必须认本公司组行,
-- 严禁跨公司认父(V149 闸门 2 同一口径)。
-- - 【默认授权】给 users 全表每只账号按其 company_id 授本公司的组行(user_column_permission
-- 直授),保证现网用户升级后直接可见;读侧组行展开子孙 ⇒ 三个工具随组可见。子菜单行不预授,
-- 管理员收组后可按工具单独授。停用账号照授,不构成可见入口(登录即拦),省一条 WHERE 的同时
-- 避免解禁后菜单凭空消失。
--
-- 3. 影响表与数据量:columns +4N 行(N = 公司数;每家 1 组行 + 3 子菜单行),
-- user_column_permission +N×账号数行(只授组行);均为字典/授权小表,毫秒级,不碰锁热点表。
--
-- 4. 【可重跑性】组行 INSERT 带 NOT EXISTS 守卫(uk_company_column_key 口径),
-- 授权 INSERT IGNORE(依赖 uk_user_id_column_id 唯一键);重跑逐条 no-op。
-- =============================================================
-- ---------- 1. 公司 1 模板树插组行 ----------
INSERT INTO `columns` (`name`, `column_key`, `menu_type`, `route_path`, `sort_order`, `parent_id`, `company_id`)
SELECT '常用工具', 'brand_common_tools', 'app', 'brand-common-tools', 105, NULL, 1
WHERE NOT EXISTS (
SELECT 1 FROM `columns`
WHERE `company_id` = 1 AND `column_key` = 'brand_common_tools'
);
-- ---------- 2. 复制到其余公司(含停用公司) ----------
INSERT INTO `columns` (`name`, `column_key`, `menu_type`, `route_path`, `sort_order`, `parent_id`, `company_id`)
SELECT '常用工具', 'brand_common_tools', 'app', 'brand-common-tools', 105, NULL, c.`id`
FROM `company` c
WHERE c.`id` <> 1
AND NOT EXISTS (
SELECT 1 FROM `columns` col
WHERE col.`company_id` = c.`id` AND col.`column_key` = 'brand_common_tools'
);
-- ---------- 3. 三个工具子菜单(每家公司认本公司组行) ----------
INSERT INTO `columns` (`name`, `column_key`, `menu_type`, `route_path`, `sort_order`, `parent_id`, `company_id`)
SELECT '定价利润换算', 'pricing-calc', 'app', 'pricing-calc', 106, grp.`id`, grp.`company_id`
FROM `columns` grp
WHERE grp.`column_key` = 'brand_common_tools'
AND NOT EXISTS (
SELECT 1 FROM `columns` col
WHERE col.`company_id` = grp.`company_id` AND col.`column_key` = 'pricing-calc'
);
INSERT INTO `columns` (`name`, `column_key`, `menu_type`, `route_path`, `sort_order`, `parent_id`, `company_id`)
SELECT '五国ASIN快速查询', 'five-asin', 'app', 'five-asin', 107, grp.`id`, grp.`company_id`
FROM `columns` grp
WHERE grp.`column_key` = 'brand_common_tools'
AND NOT EXISTS (
SELECT 1 FROM `columns` col
WHERE col.`company_id` = grp.`company_id` AND col.`column_key` = 'five-asin'
);
INSERT INTO `columns` (`name`, `column_key`, `menu_type`, `route_path`, `sort_order`, `parent_id`, `company_id`)
SELECT '五国关键词共享', 'keyword-share', 'app', 'keyword-share', 108, grp.`id`, grp.`company_id`
FROM `columns` grp
WHERE grp.`column_key` = 'brand_common_tools'
AND NOT EXISTS (
SELECT 1 FROM `columns` col
WHERE col.`company_id` = grp.`company_id` AND col.`column_key` = 'keyword-share'
);
-- ---------- 4. 全量账号默认授权组行(按账号所属公司授本公司的组行) ----------
INSERT IGNORE INTO `user_column_permission` (`user_id`, `column_id`)
SELECT u.`id`, col.`id`
FROM `users` u
JOIN `columns` col
ON col.`company_id` = u.`company_id`
AND col.`column_key` = 'brand_common_tools';
-- ---------- 4. 自检闸门:不为 0 就抛 1048(列 must_be_zero NOT NULL,V143/V149 家族) ----------
CREATE TEMPORARY TABLE IF NOT EXISTS `_v163_gate` (`must_be_zero` INT NOT NULL);
-- 闸门 1:每家公司都必须恰好一行 brand_common_tools 组行——专防第 1/2 段被
-- NOT EXISTS 守卫"一声不响少一家树"(那家公司的账号静默看不到分类,而不是报错)。
INSERT INTO `_v163_gate`
SELECT IF(COUNT(*) = 0, 0, NULL)
FROM (
SELECT c.`id`
FROM `company` c
JOIN `columns` col ON col.`company_id` = c.`id` AND col.`column_key` = 'brand_common_tools'
GROUP BY c.`id`
HAVING COUNT(*) <> 1
) bad;
-- 闸门 2:brand_common_tools 的授权绝不指向别公司的组行(第 3 段的 JOIN 条件挡了,
-- 这一道给"手工插错授权"留复验位)。
INSERT INTO `_v163_gate`
SELECT IF(COUNT(*) = 0, 0, NULL)
FROM (
SELECT g.`user_id`
FROM `user_column_permission` g
JOIN `users` u ON u.`id` = g.`user_id`
JOIN `columns` col ON col.`id` = g.`column_id`
WHERE col.`column_key` = 'brand_common_tools'
AND col.`company_id` <> u.`company_id`
) bad;
DROP TEMPORARY TABLE `_v163_gate`;
@@ -0,0 +1,89 @@
-- =============================================================
-- 迁移 V164__admin_brand_gating_menu
--
-- 1. 变更目的:管理后台「数据管理」组新增「品牌Gating库」菜单行
-- (column_key='admin_brand_gating',route_path='brand-gating'),承接原独立小网页后台
-- (gating_web_admin.py)并入数富管理后台。业务表 biz_brand_gating 主库早已存在,
-- 本迁移只动菜单字典,不建业务表。
--
-- 2. 口径:
-- - 排序 36:admin_group_data 组现有行 30(去重数据汇总)/35(品牌数据库)/65(查询ASIN)/66(商品类目),
-- 取 35 与 65 之间的空档,紧跟品牌数据库。
-- - company_id = 1 的行是模板树(PermissionMenuSchemaInitializer 与
-- PermissionMenuService.copyTreeFromTemplate 的源头),其余公司(含 status=0 的停用公司)
-- 逐家复制一份(与 V149「复制对全公司做」、V163 同一理由)。子行 parent_id 必须认本公司
-- admin_group_data 组行,严禁跨公司认父(V149 闸门 2 同一口径)。
-- - 幂等守卫按 (company_id, column_key) 判存;同时防 (menu_type, route_path) 唯一键
-- (uk_company_menu_type_route_path)撞行——若该公司已有别的 column_key 占了
-- admin/brand-gating 路由,跳过插入而不是让迁移撞 1062 报错。
-- - 【默认授权】本迁移不预授 user_column_permission:管理员后续在「菜单权限配置」里
-- 自行按公司分配(requireConsolePageAccess 门:管理员角色直通,普通账号凭授权进)。
--
-- 3. 影响表与数据量:columns +N 行(N = 公司数),字典小表,毫秒级,不碰锁热点表。
--
-- 4. 【可重跑性】两段 INSERT 都带 NOT EXISTS 守卫;重跑逐条 no-op。
-- =============================================================
-- ---------- 1. 公司 1 模板行(认公司 1 的 admin_group_data 组行) ----------
INSERT INTO `columns` (`name`, `column_key`, `menu_type`, `route_path`, `sort_order`, `parent_id`, `company_id`)
SELECT '品牌Gating库', 'admin_brand_gating', 'admin', 'brand-gating', 36, grp.`id`, 1
FROM `columns` grp
WHERE grp.`company_id` = 1 AND grp.`column_key` = 'admin_group_data'
AND NOT EXISTS (
SELECT 1 FROM `columns`
WHERE `company_id` = 1
AND (`column_key` = 'admin_brand_gating'
OR (`menu_type` = 'admin' AND `route_path` = 'brand-gating'))
);
-- ---------- 2. 复制到其余公司(含停用公司),各自认本公司组行 ----------
INSERT INTO `columns` (`name`, `column_key`, `menu_type`, `route_path`, `sort_order`, `parent_id`, `company_id`)
SELECT '品牌Gating库', 'admin_brand_gating', 'admin', 'brand-gating', 36, grp.`id`, c.`id`
FROM `company` c
JOIN `columns` grp ON grp.`company_id` = c.`id` AND grp.`column_key` = 'admin_group_data'
WHERE c.`id` <> 1
AND NOT EXISTS (
SELECT 1 FROM `columns` col
WHERE col.`company_id` = c.`id`
AND (col.`column_key` = 'admin_brand_gating'
OR (col.`menu_type` = 'admin' AND col.`route_path` = 'brand-gating'))
);
-- ---------- 3. 自检闸门:不为 0 就抛 1048(列 must_be_zero NOT NULL,V143/V149/V163 家族) ----------
CREATE TEMPORARY TABLE IF NOT EXISTS `_v164_gate` (`must_be_zero` INT NOT NULL);
-- 闸门 1:每家公司都必须恰好一行 admin_brand_gating——LEFT JOIN 口径把"少一家"也算进来
-- (V163 闸门 1 是 INNER JOIN,只防重复不防缺失;这里两家闸门一起补全)。
-- 少一家 ⇒ 那家公司的管理员在菜单配置里静默看不到这一页,而不是报错,必须靠这道闸拦住。
INSERT INTO `_v164_gate`
SELECT IF(COUNT(*) = 0, 0, NULL)
FROM (
SELECT c.`id`
FROM `company` c
LEFT JOIN `columns` col ON col.`company_id` = c.`id` AND col.`column_key` = 'admin_brand_gating'
GROUP BY c.`id`
HAVING COUNT(col.`id`) <> 1
) bad;
-- 闸门 2:admin/brand-gating 路由不允许被别的 column_key 占行(NOT EXISTS 守卫挡了增量,
-- 这一道给"手工插错行/历史遗留撞行"留复验位,撞行即 uk_company_menu_type_route_path 隐患)。
INSERT INTO `_v164_gate`
SELECT IF(COUNT(*) = 0, 0, NULL)
FROM `columns` col
WHERE col.`menu_type` = 'admin'
AND col.`route_path` = 'brand-gating'
AND col.`column_key` <> 'admin_brand_gating';
-- 闸门 3:admin_brand_gating 行的父必须是本公司的 admin_group_data 组行(第 2 段的 JOIN
-- 条件挡了,这一道给"手工认错父/跨公司认父"留复验位,V149 闸门 2 同一口径)。
INSERT INTO `_v164_gate`
SELECT IF(COUNT(*) = 0, 0, NULL)
FROM (
SELECT col.`id`
FROM `columns` col
LEFT JOIN `columns` grp ON grp.`id` = col.`parent_id` AND grp.`column_key` = 'admin_group_data'
WHERE col.`column_key` = 'admin_brand_gating'
AND (grp.`id` IS NULL OR grp.`company_id` <> col.`company_id`)
) bad;
DROP TEMPORARY TABLE `_v164_gate`;
@@ -0,0 +1,83 @@
<?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.brandgating.mapper.BrandGatingMapper">
<!-- 品牌Gating库(biz_brand_gating)。行为口径对齐桌面版 gating_web_admin.py:
列表筛选(list_gating L138-162)、统计(stats L165-178)、启停(set_status_gating L196-201)。
动态筛选全部走 #{} 占位符,不做任何字符串拼接。 -->
<!-- 「全部」在桌面版里就是“不限”的哨兵值(也是表内真实取值),与空串一并放行不过滤 -->
<sql id="filterConds">
<if test="f.keyword != null and f.keyword != ''">
AND (brand LIKE CONCAT('%', #{f.keyword}, '%') OR asin LIKE CONCAT('%', #{f.keyword}, '%'))
</if>
<if test="f.country != null and f.country != '' and f.country != '全部'">AND country = #{f.country}</if>
<if test="f.gatingType != null and f.gatingType != '' and f.gatingType != '全部'">AND gating_type = #{f.gatingType}</if>
<if test="f.status != null">AND status = #{f.status}</if>
<if test="f.source != null and f.source != '' and f.source != '全部'">AND source = #{f.source}</if>
</sql>
<select id="countByFilter" resultType="long">
SELECT COUNT(*)
FROM biz_brand_gating
<where>
<include refid="filterConds"/>
</where>
</select>
<select id="selectRows" resultType="com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingRowVo">
SELECT id, brand, asin, country, gating_type, status, source, shop_name, remark,
created_at, updated_at, disabled_at
FROM biz_brand_gating
<where>
<include refid="filterConds"/>
</where>
ORDER BY id DESC
LIMIT #{f.offset}, #{f.limit}
</select>
<select id="countAll" resultType="long">
SELECT COUNT(*) FROM biz_brand_gating
</select>
<select id="countByStatus" resultType="long">
SELECT COUNT(*) FROM biz_brand_gating WHERE status = #{status}
</select>
<select id="countDistinctBrands" resultType="long">
SELECT COUNT(DISTINCT brand) FROM biz_brand_gating WHERE status = 1
</select>
<select id="countGroupByType" resultType="com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingNameCountRow">
SELECT gating_type AS name, COUNT(*) AS count
FROM biz_brand_gating
WHERE status = 1
GROUP BY gating_type
ORDER BY count DESC
</select>
<select id="countGroupByCountry" resultType="com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingNameCountRow">
SELECT country AS name, COUNT(*) AS count
FROM biz_brand_gating
WHERE status = 1
GROUP BY country
ORDER BY count DESC
</select>
<select id="countGroupBySource" resultType="com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingNameCountRow">
SELECT source AS name, COUNT(*) AS count
FROM biz_brand_gating
WHERE status = 1
GROUP BY source
ORDER BY count DESC
</select>
<!-- 启停:status=1 清 disabled_at、=0 记 disabled_at=NOW(),两种都刷 updated_at=NOW()
(桌面版 set_status_gating 的两条 UPDATE 合一;IF 走 #{} 参数,不拼接) -->
<update id="updateStatus">
UPDATE biz_brand_gating
SET status = #{status},
disabled_at = IF(#{status} = 1, NULL, NOW()),
updated_at = NOW()
WHERE id = #{id}
</update>
</mapper>
@@ -0,0 +1,11 @@
<?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.keywordshare.mapper.KeywordAddLogMapper">
<insert id="insertBatch">
INSERT INTO keyword_add_log (company_id, country, keyword, user_name, add_time)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.companyId}, #{row.country}, #{row.keyword}, #{row.userName}, #{row.addTime})
</foreach>
</insert>
</mapper>
@@ -0,0 +1,11 @@
<?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.keywordshare.mapper.KeywordDeleteLogMapper">
<insert id="insertBatch">
INSERT INTO keyword_delete_log (company_id, country, keyword, user_name, delete_time)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.companyId}, #{row.country}, #{row.keyword}, #{row.userName}, #{row.deleteTime})
</foreach>
</insert>
</mapper>
@@ -0,0 +1,138 @@
<?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.keywordshare.mapper.KeywordShareMapper">
<!-- 五国关键词共享(词本体 keyword_share)。动态筛选全部走 #{} 占位符,不做任何字符串拼接;
排序口径对齐桌面版 keyword_share_app.py _filtered_rows_conn(未使用在前、已使用按 used_time 升序)。 -->
<!-- 词表 × 使用状态:usage 行按 (company_id, country, keyword) 三键对上,与桌面版同一口径 -->
<sql id="usageJoin">
keyword_share k
LEFT JOIN keyword_usage u
ON u.company_id = k.company_id
AND u.country = k.country
AND u.keyword = k.keyword
</sql>
<sql id="filterConds">
k.company_id = #{f.companyId}
<if test="f.country != null and f.country != ''">AND k.country = #{f.country}</if>
<if test="f.category != null and f.category != ''">AND k.category = #{f.category}</if>
<if test="f.subCategory != null and f.subCategory != ''">AND k.sub_category = #{f.subCategory}</if>
<if test="f.categoryL3 != null and f.categoryL3 != ''">AND k.category_l3 = #{f.categoryL3}</if>
<if test="f.categoryL4 != null and f.categoryL4 != ''">AND k.category_l4 = #{f.categoryL4}</if>
<if test="f.search != null and f.search != ''">
AND (k.keyword LIKE CONCAT('%', #{f.search}, '%') OR k.chinese LIKE CONCAT('%', #{f.search}, '%'))
</if>
<if test="f.status == 'used'">AND u.country IS NOT NULL</if>
<if test="f.status == 'unused'">AND u.country IS NULL</if>
<if test="f.usedSince != null">AND u.country IS NOT NULL AND u.used_time &gt;= #{f.usedSince}</if>
<if test="f.onlyMine != null and f.onlyMine">AND u.user_name = #{f.userName}</if>
</sql>
<select id="countHit" resultType="long">
SELECT COUNT(*)
FROM <include refid="usageJoin"/>
WHERE <include refid="filterConds"/>
</select>
<select id="selectRows" resultType="com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareRowVo">
SELECT k.id, k.country, k.keyword, k.chinese, k.category, k.sub_category, k.category_l3, k.category_l4,
(u.country IS NOT NULL) AS used, u.used_time, u.user_name
FROM <include refid="usageJoin"/>
WHERE <include refid="filterConds"/>
ORDER BY (u.country IS NULL) ASC, u.used_time ASC
LIMIT #{f.offset}, #{f.limit}
</select>
<select id="selectRandomUnused" resultType="com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareRowVo">
SELECT k.id, k.country, k.keyword, k.chinese, k.category, k.sub_category, k.category_l3, k.category_l4,
(u.country IS NOT NULL) AS used, u.used_time, u.user_name
FROM <include refid="usageJoin"/>
WHERE <include refid="filterConds"/>
ORDER BY RAND()
LIMIT #{f.limit}
</select>
<select id="countDupKeywords" resultType="long">
SELECT COUNT(*)
FROM (
SELECT keyword FROM keyword_share
WHERE company_id = #{companyId}
GROUP BY keyword
HAVING COUNT(DISTINCT country) &gt; 1
) t
</select>
<select id="selectFilterCountries" resultType="string">
SELECT DISTINCT country
FROM keyword_share
WHERE company_id = #{companyId}
AND country IS NOT NULL AND country != ''
ORDER BY country
</select>
<select id="selectFilterTreeRows" resultType="com.nanri.aiimage.modules.keywordshare.model.dto.KeywordCategoryTreeRow">
SELECT DISTINCT category, sub_category, category_l3, category_l4
FROM keyword_share
WHERE company_id = #{companyId}
AND category IS NOT NULL AND category != ''
ORDER BY category, sub_category, category_l3, category_l4
</select>
<select id="selectCountryCounts" resultType="com.nanri.aiimage.modules.keywordshare.model.dto.KeywordNameCountRow">
SELECT country AS name, COUNT(*) AS count
FROM keyword_share
WHERE company_id = #{companyId}
AND country IS NOT NULL AND country != ''
GROUP BY country
ORDER BY count DESC
</select>
<select id="selectCategoryCounts" resultType="com.nanri.aiimage.modules.keywordshare.model.dto.KeywordNameCountRow">
SELECT category AS name, COUNT(*) AS count
FROM keyword_share
WHERE company_id = #{companyId}
AND category IS NOT NULL AND category != ''
GROUP BY category
ORDER BY count DESC
</select>
<select id="selectSubCategoryCounts" resultType="com.nanri.aiimage.modules.keywordshare.model.dto.KeywordCategoryCountRow">
SELECT category, sub_category AS name, COUNT(*) AS count
FROM keyword_share
WHERE company_id = #{companyId}
AND category IS NOT NULL AND category != ''
GROUP BY category, sub_category
ORDER BY category, count DESC
</select>
<select id="selectCategoryL3Counts" resultType="com.nanri.aiimage.modules.keywordshare.model.dto.KeywordCategoryL3CountRow">
SELECT category, sub_category, category_l3 AS name, COUNT(*) AS count
FROM keyword_share
WHERE company_id = #{companyId}
AND category IS NOT NULL AND category != ''
GROUP BY category, sub_category, category_l3
ORDER BY category, count DESC
</select>
<select id="selectExistingKeywords" resultType="com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareKey">
SELECT country, keyword
FROM keyword_share
WHERE company_id = #{companyId}
AND (country, keyword) IN
<foreach collection="keys" item="key" open="(" separator="," close=")">
(#{key.country}, #{key.keyword})
</foreach>
</select>
<insert id="insertBatchIgnore">
INSERT IGNORE INTO keyword_share
(country, keyword, chinese, ktype, source, category, sub_category, category_l3, category_l4,
user_name, add_time, company_id)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.country}, #{row.keyword}, #{row.chinese}, #{row.ktype}, #{row.source}, #{row.category},
#{row.subCategory}, #{row.categoryL3}, #{row.categoryL4}, #{row.userName}, #{row.addTime}, #{row.companyId})
</foreach>
</insert>
</mapper>
@@ -0,0 +1,23 @@
<?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.keywordshare.mapper.KeywordUsageMapper">
<!-- 使用状态批量写:UPSERT 语义对齐桌面版 ShareSync.mark(ON DUPLICATE KEY UPDATE),
唯一键 uk_comp_cw(company_id, country, keyword) 冲突即刷新使用者与时间,天然幂等。 -->
<insert id="upsertBatch">
INSERT INTO keyword_usage (company_id, country, keyword, user_name, used_time)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.companyId}, #{row.country}, #{row.keyword}, #{row.userName}, #{row.usedTime})
</foreach>
ON DUPLICATE KEY UPDATE user_name = VALUES(user_name), used_time = VALUES(used_time)
</insert>
<delete id="deleteBatch">
DELETE FROM keyword_usage
WHERE company_id = #{companyId}
AND (country, keyword) IN
<foreach collection="keys" item="key" open="(" separator="," close=")">
(#{key.country}, #{key.keyword})
</foreach>
</delete>
</mapper>
@@ -56,7 +56,10 @@ class MigratedSqlEquivalenceTest {
private static final int LEDGER_SELECTS = 60;
/** 全部 mapper XML 里的 select 总数:账本 60 + TaskImageCache 5 + AsinResultCache 1。 */
/** 66 → 67:会话容忍新增的 {@code AdminUserMapper#countLiveWorkFor} 也是一条 select。 */
private static final int XML_SELECT_TOTAL = 67;
// 67 → 78:keywordshare 模块新写 11 条 select,逐条登记进下方 NOT_MIGRATED。
// 78 → 86:brandgating 模块新写 8 条 select,同样逐条点入
// (与 SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 99 → 115 → 124)。
private static final int XML_SELECT_TOTAL = 86;
/**
* 不是「从注解搬过来」的语句:注解时代没有原文可对撞,等价性这支只能跳过它们。
@@ -68,7 +71,28 @@ class MigratedSqlEquivalenceTest {
// 与 increment 分两条是口径要求(计次先于响应、token 只在响应后有读数),不是随手加的第二条 SQL。
"UserSecretUsageMapper#addTokens",
// 失败任务重试:INSERT ... SELECT 复制采集明细行,非注解迁移件。
"CollectDataItemMapper#copyItemsForRetry");
"CollectDataItemMapper#copyItemsForRetry",
// keywordshare 模块(五国关键词共享网页化,2026-10-23)新写 11 条 select。
"KeywordShareMapper#countHit",
"KeywordShareMapper#selectRows",
"KeywordShareMapper#selectRandomUnused",
"KeywordShareMapper#countDupKeywords",
"KeywordShareMapper#selectFilterCountries",
"KeywordShareMapper#selectFilterTreeRows",
"KeywordShareMapper#selectCountryCounts",
"KeywordShareMapper#selectCategoryCounts",
"KeywordShareMapper#selectSubCategoryCounts",
"KeywordShareMapper#selectCategoryL3Counts",
"KeywordShareMapper#selectExistingKeywords",
// brandgating 模块(品牌Gating库并入管理后台,2026-10-23)新写 8 条 select。
"BrandGatingMapper#countByFilter",
"BrandGatingMapper#selectRows",
"BrandGatingMapper#countAll",
"BrandGatingMapper#countByStatus",
"BrandGatingMapper#countDistinctBrands",
"BrandGatingMapper#countGroupByType",
"BrandGatingMapper#countGroupByCountry",
"BrandGatingMapper#countGroupBySource");
/** 账本 5 列:接口 / 方法 / @Select 种类 / 是否含常量插值 / 注解原文(Java 反转义后那条串)。 */
private static final int LEDGER_COLUMNS = 5;
@@ -72,7 +72,10 @@ class SqlLayeringConventionTest {
// 计次发生在发请求之前(含重试),token 只有响应回来才有读数,合成一条 upsert 就会漏计超时的那次。
// 98 → 99:失败任务重试新增 {@code CollectDataItemMapper#copyItemsForRetry}
// (INSERT ... SELECT 复制明细行),同样登记进 NOT_MIGRATED。
private static final int XML_STATEMENT_TOTAL = 99;
// 99 → 115:keywordshare 模块(五国关键词共享网页化,5 份 XML 16 条)新写语句。
// 115 → 124:brandgating 模块(品牌Gating库并入管理后台,1 份 XML 9 条)新写语句;
// 两批 19 条 select 逐条登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED(67 → 78 → 86)。
private static final int XML_STATEMENT_TOTAL = 124;
/**
* wrapper 里夹带函数级 SQL 的现状("这不是简单单表"的证据),文件名 → 条数:
@@ -0,0 +1,77 @@
package com.nanri.aiimage.common.retry;
import org.junit.jupiter.api.Test;
import java.net.ConnectException;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 连接层不可达冷却表的口径(B24):阈值 2、证据可清零、窗长到点自然到期、
* 到期后一次失败即重进、窗长 0 整体关闭、同 host 不同写法必须同格。
*/
class UnreachableCooldownTest {
private final UnreachableCooldown cooldown = new UnreachableCooldown();
private static final long WINDOW = 60_000L;
@Test
void twoConsecutiveFailuresEnterCooldownOneDoesNot() {
assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isFalse();
assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isFalse();
assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isFalse();
// 第二次失败:冷却窗从无到有,返回 true 的那方打进入 warn;之后的失败不重复报
assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isTrue();
assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isTrue();
assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isFalse();
}
@Test
void connectedResetsEvidenceSoTwoFreshFailuresAreRequiredAgain() {
cooldown.recordConnectFailure("https://gw.example.com", WINDOW);
cooldown.recordConnected("https://gw.example.com");
assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isFalse();
assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isFalse();
}
@Test
void windowExpiresNaturallyAndSingleFailureReenters() throws InterruptedException {
String host = "https://short.example.com";
cooldown.recordConnectFailure(host, 50);
cooldown.recordConnectFailure(host, 50);
assertThat(cooldown.isCoolingDown(host, 50)).isTrue();
Thread.sleep(120);
assertThat(cooldown.isCoolingDown(host, 50)).isFalse();
// 历史证据不清零:到期后探针再失败,一次即重进(B24 拍板口径)
assertThat(cooldown.recordConnectFailure(host, 50)).isTrue();
assertThat(cooldown.isCoolingDown(host, 50)).isTrue();
}
@Test
void zeroWindowDisablesCooldownEntirely() {
for (int i = 0; i < 5; i++) {
assertThat(cooldown.recordConnectFailure("https://gw.example.com", 0)).isFalse();
}
assertThat(cooldown.isCoolingDown("https://gw.example.com", 0)).isFalse();
}
@Test
void sameHostDifferentSpellingSharesOneCell() {
cooldown.recordConnectFailure("https://GW.example.com", WINDOW);
// 尾斜杠/大小写不同写法落进同一格:这条是第 2 次失败,必须进冷却
assertThat(cooldown.recordConnectFailure("https://gw.example.com/", WINDOW)).isTrue();
assertThat(cooldown.isCoolingDown("https://Gw.Example.Com", WINDOW)).isTrue();
}
@Test
void isConnectFailureWalksCauseChainAndIgnoresOtherFailures() {
assertThat(UnreachableCooldown.isConnectFailure(new ConnectException("refused"))).isTrue();
RuntimeException wrapped = new RuntimeException(
new IllegalStateException(new ConnectException("refused")));
assertThat(UnreachableCooldown.isConnectFailure(wrapped)).isTrue();
assertThat(UnreachableCooldown.isConnectFailure(new RuntimeException("read timeout"))).isFalse();
assertThat(UnreachableCooldown.isConnectFailure(null)).isFalse();
}
}
@@ -1,6 +1,7 @@
package com.nanri.aiimage.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient.BrandCheckBatchResult;
import com.nanri.aiimage.modules.similarasin.client.SimilarAsinLlmClient;
@@ -96,7 +97,7 @@ class HttpClientConnectionReuseTest {
void test_task_077_brand_normal_default_path() throws Exception {
// 默认路径:品牌检查客户端通过共享池创建单例 RestClient,
// 工厂为带 keep-alive 连接池的 JdkClientHttpRequestFactory。
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null);
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
BrandCheckBatchResult result = client.checkTitleText(" ", null, null);
assertTrue(result.brands().isEmpty(), "空标题安全跳过,不创建无效资源");
@@ -107,8 +108,8 @@ class HttpClientConnectionReuseTest {
void test_task_077_brand_normal_multiple_items() throws Exception {
// 批量场景:LLM/品牌/紫鸟三个客户端各自持有独立 RestClient,
// 但底层连接池共用同一 HttpClient 实例,不重复创建。
SimilarAsinLlmClient llm = new SimilarAsinLlmClient(new SimilarAsinProperties(), new ObjectMapper(), null, null);
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
SimilarAsinLlmClient llm = new SimilarAsinLlmClient(new SimilarAsinProperties(), new ObjectMapper(), null, null, new UnreachableCooldown());
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
HttpClient llmClient = clientOf(factoryOf(restClientOf(llm)));
@@ -124,7 +125,7 @@ class HttpClientConnectionReuseTest {
void test_task_077_brand_normal_repeated_operation_is_idempotent() throws Exception {
// 幂等:同一客户端重复触发请求创建逻辑只产生一个 RestClient,
// 重复调用返回同一实例,不重复创建客户端对象。
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
assertSame(restClientOf(brand), restClientOf(brand), "品牌客户端复用同一 RestClient");
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
@@ -134,7 +135,7 @@ class HttpClientConnectionReuseTest {
@Test
void test_task_077_brand_boundary_empty_input() throws Exception {
// 空输入:空品牌列表不发起任何 HTTP 请求、不创建客户端资源。
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
BrandCheckBatchResult result = brand.checkAll(List.of(), "Terms", null);
assertTrue(result.brands().isEmpty());
@@ -145,7 +146,7 @@ class HttpClientConnectionReuseTest {
@Test
void test_task_077_brand_boundary_single_item() throws Exception {
// 单元素:单客户端单请求走共享池,工厂带连接池,行为与批量一致。
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
assertPooled(factoryOf(restClientOf(brand)));
}
@@ -155,7 +156,7 @@ class HttpClientConnectionReuseTest {
// 不随实例数量线性增长连接资源。
int instances = 8;
for (int i = 0; i < instances; i++) {
restClientOf(new BrandCheckClient(new BrandCheckProperties(), null, null));
restClientOf(new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown()));
restClientOf(new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper()));
}
assertSame(HttpClientPool.sharedHttpClient(), HttpClientPool.sharedHttpClient(),
@@ -1,6 +1,7 @@
package com.nanri.aiimage.metrics;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.config.SimilarAsinProperties;
import com.nanri.aiimage.config.ZiniaoProperties;
@@ -162,7 +163,7 @@ class ExternalCallMetricsRecorderTest {
SimilarAsinProperties props = llmProps();
SimilarAsinLlmClient client =
new SimilarAsinLlmClient(props, objectMapper, new ExternalCallMetricsRecorder(registry),
mock(UserSecretUsageService.class));
mock(UserSecretUsageService.class), new UnreachableCooldown());
String content = client.invokeChat("test-model", "system", "hello", "test-key");
@@ -181,7 +182,7 @@ class ExternalCallMetricsRecorderTest {
SimilarAsinProperties props = llmProps();
SimilarAsinLlmClient client =
new SimilarAsinLlmClient(props, objectMapper, new ExternalCallMetricsRecorder(registry),
mock(UserSecretUsageService.class));
mock(UserSecretUsageService.class), new UnreachableCooldown());
for (int i = 0; i < 3; i++) {
client.invokeChat("test-model", "system", "prompt-" + i, "test-key");
@@ -192,7 +193,7 @@ class ExternalCallMetricsRecorderTest {
"client", "llm", "result", "success"), "三次成功全部记录");
// 全部外部客户端(LLM / 品牌检查 / 紫鸟)在同一次批量中各自记录指标
BrandCheckClient brand = new BrandCheckClient(brandProps(), new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
BrandCheckClient brand = new BrandCheckClient(brandProps(), new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
brand.checkAll(List.of("Nintendo", "LEGO", "Sony"), "Terms", 7L);
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(ziniaoProps(), objectMapper, new ExternalCallMetricsRecorder(registry));
assertEquals(1001L, ziniao.getCompanyIdByApiKey("test-api-key"));
@@ -210,7 +211,7 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_normal_repeated_operation_is_idempotent() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
client.check("Nintendo", "Terms", 7L);
client.check("Nintendo", "Terms", 7L);
@@ -227,7 +228,7 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_boundary_empty_input() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of(), "Terms", 7L);
@@ -242,7 +243,7 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_boundary_single_item() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
BrandCheckClient.BrandCheckResponse response = client.check("LEGO", "Terms", 7L);
@@ -264,7 +265,7 @@ class ExternalCallMetricsRecorderTest {
int index = i;
pool.submit(() -> {
SimilarAsinLlmClient client = new SimilarAsinLlmClient(props, objectMapper, recorder,
mock(UserSecretUsageService.class));
mock(UserSecretUsageService.class), new UnreachableCooldown());
try {
client.invokeChat("test-model", "system", "prompt-" + index, "test-key");
} catch (Exception ignored) {
@@ -297,7 +298,7 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_invalid_input_rejected() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(null, "Terms", 7L);
@@ -315,7 +316,7 @@ class ExternalCallMetricsRecorderTest {
SimilarAsinProperties props = llmProps();
SimilarAsinLlmClient client =
new SimilarAsinLlmClient(props, objectMapper, recorder,
mock(UserSecretUsageService.class));
mock(UserSecretUsageService.class), new UnreachableCooldown());
// 第一次调用走 500 失败路径,第二次调用恢复成功:错误可恢复
llmFailNext.set(true);
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.appearancepatent.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.AppearancePatentProperties;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
@@ -46,7 +47,7 @@ class AppearancePatentBrandBatchTest {
null,
brandCheckClient,
mock(UserSecretUsageService.class)
);
, new UnreachableCooldown());
@Test
void mergedBatchAsksBrandCheckOnceAndAttributesPerRow() throws Exception {
@@ -115,7 +116,7 @@ class AppearancePatentBrandBatchTest {
properties.setLlmRetryTimes(1);
properties.setLlmReadTimeoutMillis(5_000);
AppearancePatentLlmClient inspectClient = new AppearancePatentLlmClient(
properties, new ObjectMapper(), null, brandCheckClient, mock(UserSecretUsageService.class));
properties, new ObjectMapper(), null, brandCheckClient, mock(UserSecretUsageService.class), new UnreachableCooldown());
List<AppearancePatentResultRowDto> rows = inspectClient.inspectRows(
List.of(
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.appearancepatent.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.AppearancePatentProperties;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
@@ -72,7 +73,7 @@ class AppearancePatentLlmClientHttpTest {
null,
localBrandCheckClient(),
mock(UserSecretUsageService.class)
);
, new UnreachableCooldown());
}
/**
@@ -85,7 +86,7 @@ class AppearancePatentLlmClientHttpTest {
brand.setRetryTimes(1);
brand.setRetryIntervalMillis(0);
brand.setTotalTimeoutMillis(5000);
return new BrandCheckClient(brand, null, null);
return new BrandCheckClient(brand, null, null, new UnreachableCooldown());
}
private void handleBrandCheck(HttpExchange exchange) throws IOException {
@@ -290,7 +291,40 @@ class AppearancePatentLlmClientHttpTest {
properties.setLlmHost("http://127.0.0.1:" + server.getAddress().getPort());
customizer.accept(properties);
return new AppearancePatentLlmClient(properties, objectMapper, null,
localBrandCheckClient(), mock(UserSecretUsageService.class));
localBrandCheckClient(), mock(UserSecretUsageService.class), new UnreachableCooldown());
}
/** 连接层故障短路:网关根本连不上时,重试阶梯必须在证据数处停下,行级拿到可读的真因。 */
@Test
void unreachableGatewayShortCircuitsLadderAndSurfacesReason() {
AppearancePatentLlmClient deadHostClient = buildClient(p -> {
p.setLlmHost("http://127.0.0.1:1");
p.setLlmRetryTimes(3);
});
List<AppearancePatentResultRowDto> result = deadHostClient.inspectRows(
List.of(row("9", "B009", "任意标题", "", "https://img.example.com/9.jpg")), null, "test-key");
assertThat(result).hasSize(1);
AppearancePatentResultRowDto failed = result.get(0);
// 行级原因走标题回退链路(invokeTitleBatchSafely 把异常折进 titleErrors)
assertThat(failed.getTitleReason()).contains("LLM 服务不可达(连接未建立,非本行数据问题)");
assertThat(failed.getTitleReason()).doesNotContain("LLM http ");
// 短路发生在第 2 次连接失败,第 3 次尝试不再发出:连接层故障重试救不回来
assertThat(textRequestCount.get()).isZero();
assertThat(imageRequestCount.get()).isZero();
}
/** 反向对照:连得上只是 5xx(非连接层故障)不得触发短路,重试阶梯要跑满。 */
@Test
void httpFailureKeepsFullRetryLadder() {
failTextRequests = true;
AppearancePatentLlmClient threeAttemptClient = buildClient(p -> p.setLlmRetryTimes(3));
threeAttemptClient.inspectRows(
List.of(row("10", "B010", "任意标题", "", "https://img.example.com/10.jpg")), null, "test-key");
assertThat(textRequestCount.get()).isEqualTo(3);
}
private boolean messagesBodyContains(HttpExchange exchange, String text) {
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.appearancepatent.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.AppearancePatentProperties;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
@@ -68,7 +69,7 @@ class AppearancePatentLlmClientImageDedupeTest {
properties.setLlmHost("http://127.0.0.1:" + server.getAddress().getPort());
properties.setLlmRetryTimes(1);
client = new AppearancePatentLlmClient(properties, objectMapper, null, localBrandCheckClient(),
mock(UserSecretUsageService.class));
mock(UserSecretUsageService.class), new UnreachableCooldown());
}
private BrandCheckClient localBrandCheckClient() {
@@ -77,7 +78,7 @@ class AppearancePatentLlmClientImageDedupeTest {
brand.setRetryTimes(1);
brand.setRetryIntervalMillis(0);
brand.setTotalTimeoutMillis(5000);
return new BrandCheckClient(brand, null, null);
return new BrandCheckClient(brand, null, null, new UnreachableCooldown());
}
@AfterEach
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.appearancepatent.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.AppearancePatentProperties;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
@@ -26,9 +27,9 @@ class AppearancePatentLlmClientTest {
new AppearancePatentProperties(),
new ObjectMapper(),
null,
new BrandCheckClient(new BrandCheckProperties(), null, null),
new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown()),
mock(UserSecretUsageService.class)
);
, new UnreachableCooldown());
@Test
void markRowsFailedLeavesUserFacingResultFilledWithReviewMessage() {
@@ -123,7 +124,7 @@ class AppearancePatentLlmClientTest {
null,
brandCheckClient,
mock(UserSecretUsageService.class)
);
, new UnreachableCooldown());
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
row.setId("1");
String dataText = """
@@ -140,7 +141,7 @@ class AppearancePatentLlmClientTest {
private static AppearancePatentLlmClient clientWith(BrandCheckClient brandCheckClient) {
return new AppearancePatentLlmClient(new AppearancePatentProperties(), new ObjectMapper(),
null, brandCheckClient, mock(UserSecretUsageService.class));
null, brandCheckClient, mock(UserSecretUsageService.class), new UnreachableCooldown());
}
private static AppearancePatentResultRowDto newRow(String id) {
@@ -204,7 +205,7 @@ class AppearancePatentLlmClientTest {
null,
brandCheckClient,
mock(UserSecretUsageService.class)
);
, new UnreachableCooldown());
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
row.setId("1");
String dataText = """
@@ -228,7 +229,7 @@ class AppearancePatentLlmClientTest {
null,
brandCheckClient,
mock(UserSecretUsageService.class)
);
, new UnreachableCooldown());
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
row.setId("1");
String dataText = """
@@ -251,7 +252,7 @@ class AppearancePatentLlmClientTest {
null,
brandCheckClient,
mock(UserSecretUsageService.class)
);
, new UnreachableCooldown());
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
row.setId("1");
String dataText = """
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.appearancepatent.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.AppearancePatentProperties;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
@@ -59,7 +60,7 @@ class AppearancePatentLlmClientTokenUsageTest {
AppearancePatentProperties properties = new AppearancePatentProperties();
properties.setLlmHost("http://127.0.0.1:" + server.getAddress().getPort());
properties.setLlmRetryTimes(1);
client = new AppearancePatentLlmClient(properties, objectMapper, null, localBrandCheckClient(), usageService);
client = new AppearancePatentLlmClient(properties, objectMapper, null, localBrandCheckClient(), usageService, new UnreachableCooldown());
}
private BrandCheckClient localBrandCheckClient() {
@@ -68,7 +69,7 @@ class AppearancePatentLlmClientTokenUsageTest {
brand.setRetryTimes(1);
brand.setRetryIntervalMillis(0);
brand.setTotalTimeoutMillis(5000);
return new BrandCheckClient(brand, null, null);
return new BrandCheckClient(brand, null, null, new UnreachableCooldown());
}
@AfterEach
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.brand.client;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import com.sun.net.httpserver.HttpServer;
import org.junit.jupiter.api.Test;
@@ -34,7 +35,7 @@ class BrandCheckClientTest {
@Test
void splitTitleTextSupportsCommonSeparatorsAndQuotes() {
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null);
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
List<String> brands = client.splitTitleText("'阿凡达,任天堂'; Disney、LEGO\nSony");
@@ -43,13 +44,43 @@ class BrandCheckClientTest {
@Test
void splitTitleTextDeduplicatesBlankValues() {
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null);
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
List<String> brands = client.splitTitleText(" 任天堂, ,任天堂,Sony ");
assertThat(brands).containsExactly("任天堂", "Sony");
}
@Test
void clientLevelCooldownFailsWholeBatchWithoutHittingUpstream() throws Exception {
AtomicInteger attempts = new AtomicInteger();
HttpServer server = startBrandCheckServer(exchange -> {
attempts.incrementAndGet();
respond(exchange, "{\"faild_data\":[],\"query_faild_data\":[]}");
});
try {
UnreachableCooldown cooldown = new UnreachableCooldown();
BrandCheckProperties props = new BrandCheckProperties();
props.setBaseUrl("http://127.0.0.1:" + server.getAddress().getPort());
props.setPath("/brand_check");
props.setRetryTimes(3);
props.setRetryIntervalMillis(1);
BrandCheckClient client = new BrandCheckClient(props, null, stubbedUserSecrets(), cooldown);
cooldown.recordConnectFailure(props.getBaseUrl(), 300_000L);
cooldown.recordConnectFailure(props.getBaseUrl(), 300_000L);
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple", "Nike"), "Terms", 7L);
assertThat(attempts.get()).as("冷却期内一个请求都不该发").isZero();
assertThat(result.hasQueryFailedData()).isTrue();
assertThat(result.queryFaildData()).containsExactly("Apple", "Nike");
assertThat(result.reason()).contains("品牌检测服务不可达(连接未建立,非本行数据问题)");
} finally {
server.stop(0);
}
}
@Test
void queryFailureRetriesUntilSuccess() throws Exception {
AtomicInteger attempts = new AtomicInteger();
@@ -242,7 +273,7 @@ class BrandCheckClientTest {
props.setRetryTimes(retryTimes);
props.setRetryIntervalMillis(retryIntervalMillis);
props.setTotalTimeoutMillis(totalTimeoutMillis);
return new BrandCheckClient(props, null, stubbedUserSecrets());
return new BrandCheckClient(props, null, stubbedUserSecrets(), new UnreachableCooldown());
}
private BrandCheckClient newClient(HttpServer server, int retryTimes) {
@@ -251,7 +282,7 @@ class BrandCheckClientTest {
props.setPath("/brand_check");
props.setRetryTimes(retryTimes);
props.setRetryIntervalMillis(1);
return new BrandCheckClient(props, null, stubbedUserSecrets());
return new BrandCheckClient(props, null, stubbedUserSecrets(), new UnreachableCooldown());
}
/** 重试阶梯可调的构造:用于验证「耗时预算掐的是重试,不是首轮」。 */
@@ -262,7 +293,7 @@ class BrandCheckClientTest {
props.setRetryTimes(retryTimes);
props.setRetryIntervalMillis(retryIntervalMillis);
props.setTotalTimeoutMillis(totalTimeoutMillis);
return new BrandCheckClient(props, null, stubbedUserSecrets());
return new BrandCheckClient(props, null, stubbedUserSecrets(), new UnreachableCooldown());
}
/** 有真实 HTTP 断言的用例必须桩出非空代理,否则客户端零请求短路、阶梯与指标全测不到。 */
@@ -361,7 +392,7 @@ class BrandCheckClientTest {
props.setRetryIntervalMillis(1);
props.setReadTimeoutMillis(1_000);
props.setTotalTimeoutMillis(0);
BrandCheckClient client = new BrandCheckClient(props, null, stubbedUserSecrets());
BrandCheckClient client = new BrandCheckClient(props, null, stubbedUserSecrets(), new UnreachableCooldown());
ExecutorService pool = (ExecutorService) ReflectionTestUtils.getField(client, "checkExecutor");
CountDownLatch occupied = new CountDownLatch(3);
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.brand.client;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import com.sun.net.httpserver.HttpServer;
import org.junit.jupiter.api.Test;
@@ -137,7 +138,7 @@ class BrandCheckClientTitleBatchTest {
props.setRetryIntervalMillis(1);
UserApiSecretService secrets = mock(UserApiSecretService.class);
when(secrets.findPlainValue(anyLong(), anyString())).thenReturn("http://user:pass@127.0.0.1:9");
return new BrandCheckClient(props, null, secrets);
return new BrandCheckClient(props, null, secrets, new UnreachableCooldown());
}
private HttpServer startBrandCheckServer(com.sun.net.httpserver.HttpHandler handler) throws IOException {
@@ -0,0 +1,479 @@
package com.nanri.aiimage.modules.brandgating.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.brandgating.mapper.BrandGatingMapper;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingAddRequest;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingFilterQuery;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingImportRequest;
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingNameCountRow;
import com.nanri.aiimage.modules.brandgating.model.entity.BrandGatingEntity;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingImportResultVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingPageVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingRowVo;
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingStatsVo;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.dao.DuplicateKeyException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 品牌Gating库 Service 单测:列表筛选/分页归一化、stats 三组分组、新增默认值与唯一键跳过、
* 启停(disabled_at 语义由 XML 契约钉住)、删除、批量导入(照桌面版行格式:正常行/缺字段行/重复行)。
* 另以源码契约测试把 Mapper XML 与控制器的权限键把门钉住。
*/
@ExtendWith(MockitoExtension.class)
class BrandGatingServiceTest {
@Mock
private BrandGatingMapper brandGatingMapper;
private BrandGatingService newService() {
return new BrandGatingService(brandGatingMapper);
}
private static BrandGatingRowVo row(long id, String brand) {
BrandGatingRowVo vo = new BrandGatingRowVo();
vo.setId(id);
vo.setBrand(brand);
vo.setStatus(1);
return vo;
}
private static BrandGatingImportRequest.Item item(String brand, String country, String gatingType,
String asin, String remark) {
BrandGatingImportRequest.Item item = new BrandGatingImportRequest.Item();
item.setBrand(brand);
item.setCountry(country);
item.setGatingType(gatingType);
item.setAsin(asin);
item.setRemark(remark);
return item;
}
// ---------------------------------------------------------------- 列表
@Test
@DisplayName("列表:筛选条件与分页进 Mapper 载体,page/size 归一化(page<1→1、size 缺省 10、上限 200)")
void pageCarriesFilterAndNormalizedPaging() {
when(brandGatingMapper.countByFilter(any(BrandGatingFilterQuery.class))).thenReturn(0L);
when(brandGatingMapper.selectRows(any(BrandGatingFilterQuery.class))).thenReturn(List.of());
newService().page(0L, 1000L, " Anker ", " 英国 ", "品牌gating", "1", "AUTO");
ArgumentCaptor<BrandGatingFilterQuery> captor = ArgumentCaptor.forClass(BrandGatingFilterQuery.class);
verify(brandGatingMapper).selectRows(captor.capture());
BrandGatingFilterQuery filter = captor.getValue();
assertThat(filter.getKeyword()).isEqualTo("Anker");
assertThat(filter.getCountry()).isEqualTo("英国");
assertThat(filter.getGatingType()).isEqualTo("品牌gating");
assertThat(filter.getStatus()).isEqualTo(1);
assertThat(filter.getSource()).isEqualTo("AUTO");
// page=0→1、size=1000→200(上限截断)
assertThat(filter.getOffset()).isZero();
assertThat(filter.getLimit()).isEqualTo(200L);
}
@Test
@DisplayName("列表:缺省分页 = 桌面版口径(page 1、size 10);status 非 0/1(含「全部」)→ 不限")
void pageDefaultsFollowDesktop() {
when(brandGatingMapper.countByFilter(any(BrandGatingFilterQuery.class))).thenReturn(0L);
when(brandGatingMapper.selectRows(any(BrandGatingFilterQuery.class))).thenReturn(List.of());
newService().page(null, null, null, "全部", null, "全部", null);
ArgumentCaptor<BrandGatingFilterQuery> captor = ArgumentCaptor.forClass(BrandGatingFilterQuery.class);
verify(brandGatingMapper).selectRows(captor.capture());
assertThat(captor.getValue().getOffset()).isZero();
assertThat(captor.getValue().getLimit()).isEqualTo(10L);
assertThat(captor.getValue().getStatus()).isNull();
}
@Test
@DisplayName("列表:total 取同条件命中数,行按 Mapper 返回顺序透传(SQL 侧已 ORDER BY id DESC)")
void pageReturnsTotalAndRows() {
when(brandGatingMapper.countByFilter(any(BrandGatingFilterQuery.class))).thenReturn(42L);
when(brandGatingMapper.selectRows(any(BrandGatingFilterQuery.class))).thenReturn(List.of(row(9, "B"), row(3, "A")));
BrandGatingPageVo vo = newService().page(2L, 10L, null, null, null, null, null);
assertThat(vo.getTotal()).isEqualTo(42L);
assertThat(vo.getRows()).extracting(BrandGatingRowVo::getId).containsExactly(9L, 3L);
}
@ParameterizedTest
@ValueSource(strings = {"2", "生效", "abc"})
@DisplayName("列表:status 非法值一律按不限处理(桌面版下拉只有 生效/停用/全部)")
void pageTreatsIllegalStatusAsNoFilter(String status) {
when(brandGatingMapper.countByFilter(any(BrandGatingFilterQuery.class))).thenReturn(0L);
when(brandGatingMapper.selectRows(any(BrandGatingFilterQuery.class))).thenReturn(List.of());
newService().page(1L, 10L, null, null, null, status, null);
verify(brandGatingMapper).selectRows(any(BrandGatingFilterQuery.class));
ArgumentCaptor<BrandGatingFilterQuery> captor = ArgumentCaptor.forClass(BrandGatingFilterQuery.class);
verify(brandGatingMapper).selectRows(captor.capture());
assertThat(captor.getValue().getStatus()).isNull();
}
// ---------------------------------------------------------------- 统计
@Test
@DisplayName("stats:total/active/disabled/brands 与 byType/byCountry/bySource 三组分组透传")
void statsAggregatesAllGroups() {
when(brandGatingMapper.countAll()).thenReturn(100L);
when(brandGatingMapper.countByStatus(1)).thenReturn(80L);
when(brandGatingMapper.countByStatus(0)).thenReturn(20L);
when(brandGatingMapper.countDistinctBrands()).thenReturn(31L);
BrandGatingNameCountRow typeRow = new BrandGatingNameCountRow();
typeRow.setName("品牌gating");
typeRow.setCount(60);
BrandGatingNameCountRow countryRow = new BrandGatingNameCountRow();
countryRow.setName("英国");
countryRow.setCount(50);
BrandGatingNameCountRow sourceRow = new BrandGatingNameCountRow();
sourceRow.setName("MANUAL");
sourceRow.setCount(30);
when(brandGatingMapper.countGroupByType()).thenReturn(List.of(typeRow));
when(brandGatingMapper.countGroupByCountry()).thenReturn(List.of(countryRow));
when(brandGatingMapper.countGroupBySource()).thenReturn(List.of(sourceRow));
BrandGatingStatsVo vo = newService().stats();
assertThat(vo.getTotal()).isEqualTo(100L);
assertThat(vo.getActive()).isEqualTo(80L);
assertThat(vo.getDisabled()).isEqualTo(20L);
assertThat(vo.getBrands()).isEqualTo(31L);
assertThat(vo.getByType()).extracting(BrandGatingNameCountRow::getName).containsExactly("品牌gating");
assertThat(vo.getByCountry()).extracting(BrandGatingNameCountRow::getName).containsExactly("英国");
assertThat(vo.getBySource()).extracting(BrandGatingNameCountRow::getName).containsExactly("MANUAL");
// 桌面版口径:分组计数各查一遍,且 active/disabled 分别按 1/0 数
verify(brandGatingMapper).countByStatus(1);
verify(brandGatingMapper).countByStatus(0);
}
// ---------------------------------------------------------------- 新增
@Test
@DisplayName("新增:缺省字段落桌面版同款默认(asin/shopName→NULL、country=全部、type=品牌gating、source=MANUAL、remark=空串)")
void addAppliesDesktopDefaults() {
BrandGatingAddRequest request = new BrandGatingAddRequest();
request.setBrand(" Anker ");
var result = newService().add(request);
assertThat(result.getAdded()).isEqualTo(1);
ArgumentCaptor<BrandGatingEntity> captor = ArgumentCaptor.forClass(BrandGatingEntity.class);
verify(brandGatingMapper).insert(captor.capture());
BrandGatingEntity entity = captor.getValue();
assertThat(entity.getBrand()).isEqualTo("Anker");
assertThat(entity.getAsin()).isNull();
assertThat(entity.getShopName()).isNull();
assertThat(entity.getCountry()).isEqualTo("全部");
assertThat(entity.getGatingType()).isEqualTo("品牌gating");
assertThat(entity.getSource()).isEqualTo("MANUAL");
assertThat(entity.getRemark()).isEmpty();
// created_at/updated_at 由库默认维护:INSERT 不显式写时间
assertThat(entity.getCreatedAt()).isNull();
assertThat(entity.getUpdatedAt()).isNull();
}
@Test
@DisplayName("新增:请求显式给 source/shopName/asin 时按请求值落库")
void addKeepsExplicitFields() {
BrandGatingAddRequest request = new BrandGatingAddRequest();
request.setBrand("Anker");
request.setAsin("B0ABCDEFG");
request.setCountry("德国");
request.setGatingType("类目gating");
request.setSource("AUTO");
request.setShopName("店铺A");
request.setRemark("备注");
newService().add(request);
ArgumentCaptor<BrandGatingEntity> captor = ArgumentCaptor.forClass(BrandGatingEntity.class);
verify(brandGatingMapper).insert(captor.capture());
assertThat(captor.getValue().getAsin()).isEqualTo("B0ABCDEFG");
assertThat(captor.getValue().getCountry()).isEqualTo("德国");
assertThat(captor.getValue().getGatingType()).isEqualTo("类目gating");
assertThat(captor.getValue().getSource()).isEqualTo("AUTO");
assertThat(captor.getValue().getShopName()).isEqualTo("店铺A");
assertThat(captor.getValue().getRemark()).isEqualTo("备注");
}
@Test
@DisplayName("新增:品牌为空(含纯空白)→ 400「品牌不能为空」,不动库")
void addRejectsBlankBrand() {
BrandGatingAddRequest request = new BrandGatingAddRequest();
request.setBrand(" ");
assertThatThrownBy(() -> newService().add(request))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("品牌不能为空")
.extracting(e -> ((BusinessException) e).getCode())
.isEqualTo(400);
verify(brandGatingMapper, never()).insert(any(BrandGatingEntity.class));
}
@Test
@DisplayName("新增:撞唯一键(已存在同口径行)→ added=0,不抛错(桌面版 add_gating 返回 False 的同语义)")
void addReturnsZeroOnDuplicate() {
BrandGatingAddRequest request = new BrandGatingAddRequest();
request.setBrand("Anker");
when(brandGatingMapper.insert(any(BrandGatingEntity.class)))
.thenThrow(new DuplicateKeyException("dup"));
var result = newService().add(request);
assertThat(result.getAdded()).isZero();
}
// ---------------------------------------------------------------- 启停 / 删除
@Test
@DisplayName("启停:status 0/1 透传 Mapper(disabled_at 语义由 XML 契约钉住),返回实际更新数")
void updateStatusDelegatesToMapper() {
when(brandGatingMapper.updateStatus(7L, 0)).thenReturn(1);
when(brandGatingMapper.updateStatus(8L, 1)).thenReturn(0);
assertThat(newService().updateStatus(7L, 0).getUpdated()).isEqualTo(1);
assertThat(newService().updateStatus(8L, 1).getUpdated()).isZero();
verify(brandGatingMapper).updateStatus(7L, 0);
verify(brandGatingMapper).updateStatus(8L, 1);
}
@Test
@DisplayName("启停:status 非 0/1 → 400,不动库")
void updateStatusRejectsIllegalValue() {
assertThatThrownBy(() -> newService().updateStatus(7L, 2))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("status");
assertThatThrownBy(() -> newService().updateStatus(7L, null))
.isInstanceOf(BusinessException.class);
verify(brandGatingMapper, never()).updateStatus(any(), eq(2));
}
@Test
@DisplayName("删除:透传 Mapper,返回实际删除数(0=该 id 不存在)")
void deleteReturnsAffectedRows() {
when(brandGatingMapper.deleteById(7L)).thenReturn(1);
when(brandGatingMapper.deleteById(99L)).thenReturn(0);
assertThat(newService().delete(7L).getDeleted()).isEqualTo(1);
assertThat(newService().delete(99L).getDeleted()).isZero();
}
// ---------------------------------------------------------------- 批量导入:文本解析(照桌面版 parse_batch)
@Test
@DisplayName("导入解析:正常行全字段按 品牌/国家/类型/ASIN/备注 切列")
void parseBatchTextKeepsAllFiveFields() {
List<BrandGatingImportRequest.Item> items =
BrandGatingService.parseBatchText("Anker,英国,品牌gating,B0ABCDEFG,跟卖重灾区");
assertThat(items).hasSize(1);
assertThat(items.get(0).getBrand()).isEqualTo("Anker");
assertThat(items.get(0).getCountry()).isEqualTo("英国");
assertThat(items.get(0).getGatingType()).isEqualTo("品牌gating");
assertThat(items.get(0).getAsin()).isEqualTo("B0ABCDEFG");
assertThat(items.get(0).getRemark()).isEqualTo("跟卖重灾区");
}
@Test
@DisplayName("导入解析:缺字段行落桌面版默认(国家=全部、类型=品牌gating、ASIN/备注空),空段丢弃、第 5 列之后丢弃")
void parseBatchTextAppliesDefaultsAndDropsExtraColumns() {
List<BrandGatingImportRequest.Item> items = BrandGatingService.parseBatchText(
"Anker\nAnker2,德国\nAnker3,法国,类目gating\n"
+ "Anker4,西班牙,类目gating,B09ZZZZZZZ,多余列1,多余列2\n"
+ "Anker5,,英国");
assertThat(items).hasSize(5);
assertThat(items.get(0).getCountry()).isEqualTo("全部");
assertThat(items.get(0).getGatingType()).isEqualTo("品牌gating");
assertThat(items.get(0).getAsin()).isEmpty();
assertThat(items.get(1).getCountry()).isEqualTo("德国");
assertThat(items.get(1).getGatingType()).isEqualTo("品牌gating");
assertThat(items.get(2).getGatingType()).isEqualTo("类目gating");
assertThat(items.get(2).getAsin()).isEmpty();
assertThat(items.get(3).getCountry()).isEqualTo("西班牙");
assertThat(items.get(3).getGatingType()).isEqualTo("类目gating");
assertThat(items.get(3).getAsin()).isEqualTo("B09ZZZZZZZ");
assertThat(items.get(3).getRemark()).isEqualTo("多余列1");
// 空段被丢弃(桌面版 filter p.strip() 同口径):Anker5 行的 ASIN 位空掉后,英国顶上来当国家
assertThat(items.get(4).getCountry()).isEqualTo("英国");
assertThat(items.get(4).getGatingType()).isEqualTo("品牌gating");
}
@Test
@DisplayName("导入解析:中英文逗号/分号/制表符都切列,各段去空白、空段丢弃;BOM 与 # 注释行/空行跳过")
void parseBatchTextSplitsOnAllSeparatorsAndSkipsNoiseLines() {
String text = "\uFEFFAnker\t英国;品牌gating\n"
+ "# 注释行\n"
+ " \n"
+ " Anker2 , 德国 ,类目gating, B0X ,备注带空格 \n"
+ ",,,\n";
List<BrandGatingImportRequest.Item> items = BrandGatingService.parseBatchText(text);
assertThat(items).hasSize(2);
assertThat(items.get(0).getBrand()).isEqualTo("Anker");
assertThat(items.get(0).getCountry()).isEqualTo("英国");
assertThat(items.get(0).getGatingType()).isEqualTo("品牌gating");
assertThat(items.get(1).getBrand()).isEqualTo("Anker2");
assertThat(items.get(1).getCountry()).isEqualTo("德国");
assertThat(items.get(1).getGatingType()).isEqualTo("类目gating");
assertThat(items.get(1).getAsin()).isEqualTo("B0X");
assertThat(items.get(1).getRemark()).isEqualTo("备注带空格");
}
@Test
@DisplayName("导入解析:null/空文本 → 空清单")
void parseBatchTextHandlesBlankInput() {
assertThat(BrandGatingService.parseBatchText(null)).isEmpty();
assertThat(BrandGatingService.parseBatchText(" \n # x")).isEmpty();
}
// ---------------------------------------------------------------- 批量导入:写库与去重
@Test
@DisplayName("导入写库:逐条按单条新增同语义落库(source=MANUAL、shopName=NULL、缺省字段落默认)")
void importWritesRowsWithSameSemanticsAsAdd() {
BrandGatingImportRequest request = new BrandGatingImportRequest();
request.setText("Anker,英国\nAnker2,德国,类目gating,B09ZZZZZZZ,备注");
BrandGatingImportResultVo result = newService().importRows(request);
assertThat(result.getAdded()).isEqualTo(2);
assertThat(result.getSkipped()).isZero();
ArgumentCaptor<BrandGatingEntity> captor = ArgumentCaptor.forClass(BrandGatingEntity.class);
verify(brandGatingMapper, times(2)).insert(captor.capture());
List<BrandGatingEntity> rows = captor.getAllValues();
assertThat(rows.get(0).getBrand()).isEqualTo("Anker");
assertThat(rows.get(0).getCountry()).isEqualTo("英国");
assertThat(rows.get(0).getGatingType()).isEqualTo("品牌gating");
assertThat(rows.get(0).getAsin()).isNull();
assertThat(rows.get(0).getSource()).isEqualTo("MANUAL");
assertThat(rows.get(0).getShopName()).isNull();
assertThat(rows.get(1).getRemark()).isEqualTo("备注");
}
@Test
@DisplayName("导入去重:批内重复行(同品牌+国家+类型+ASIN)只写第一条,其余计入 skipped")
void importSkipsInBatchDuplicates() {
BrandGatingImportRequest request = new BrandGatingImportRequest();
request.setText("Anker,英国\nAnker,英国\nAnker,英国,品牌gating,B0X\nAnker,英国,品牌gating,B0X\nAnker,德国");
BrandGatingImportResultVo result = newService().importRows(request);
assertThat(result.getAdded()).isEqualTo(3);
assertThat(result.getSkipped()).isEqualTo(2);
verify(brandGatingMapper, times(3)).insert(any(BrandGatingEntity.class));
}
@Test
@DisplayName("导入去重:撞库内唯一键(已存在同口径行)计入 skipped,不中断后续行")
void importSkipsDbDuplicatesAndContinues() {
BrandGatingImportRequest request = new BrandGatingImportRequest();
request.setText("Anker,英国\nAnker2,德国");
when(brandGatingMapper.insert(any(BrandGatingEntity.class)))
.thenThrow(new DuplicateKeyException("dup"))
.thenReturn(1);
BrandGatingImportResultVo result = newService().importRows(request);
assertThat(result.getAdded()).isEqualTo(1);
assertThat(result.getSkipped()).isEqualTo(1);
}
@Test
@DisplayName("导入 items 直传:缺省字段同语义落默认,空品牌行跳过")
void importAcceptsDirectItems() {
BrandGatingImportRequest request = new BrandGatingImportRequest();
request.setItems(List.of(
item("Anker", "英国", null, "B0X", null),
item(" ", "德国", null, null, null)));
BrandGatingImportResultVo result = newService().importRows(request);
assertThat(result.getAdded()).isEqualTo(1);
assertThat(result.getSkipped()).isEqualTo(1);
ArgumentCaptor<BrandGatingEntity> captor = ArgumentCaptor.forClass(BrandGatingEntity.class);
verify(brandGatingMapper).insert(captor.capture());
assertThat(captor.getValue().getGatingType()).isEqualTo("品牌gating");
assertThat(captor.getValue().getRemark()).isEmpty();
}
@Test
@DisplayName("导入:text 优先于 items;两者都缺 → 400「导入内容为空」,不动库")
void importPrefersTextAndRejectsEmptyPayload() {
BrandGatingImportRequest both = new BrandGatingImportRequest();
both.setText("Anker,英国");
both.setItems(List.of(item("Anker2", "德国", null, null, null)));
assertThat(newService().importRows(both).getAdded()).isEqualTo(1);
verify(brandGatingMapper, times(1)).insert(any(BrandGatingEntity.class));
assertThatThrownBy(() -> newService().importRows(new BrandGatingImportRequest()))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("导入内容为空");
assertThatThrownBy(() -> newService().importRows(null))
.isInstanceOf(BusinessException.class);
verify(brandGatingMapper, times(1)).insert(any(BrandGatingEntity.class));
}
// ---------------------------------------------------------------- 契约钉子(SQL / 权限键)
@Test
@DisplayName("Mapper XML 契约:ORDER BY id DESC、#{} 占位符禁 ${}、启停的 disabled_at/updated_at 语义、统计分组只数生效行且国家按 count DESC")
void mapperXmlKeepsSqlContract() throws Exception {
String xml = Files.readString(
Path.of("src/main/resources/mapper/brandgating/BrandGatingMapper.xml"), StandardCharsets.UTF_8);
// 列表:桌面版 list_gating 的排序与分页
assertThat(xml).contains("ORDER BY id DESC").contains("LIMIT #{f.offset}, #{f.limit}");
// 严禁拼接筛选值
assertThat(xml).doesNotContain("${");
// 关键词:brand/asin 双列 LIKE(桌面版同口径)
assertThat(xml).contains("brand LIKE CONCAT('%', #{f.keyword}, '%')")
.contains("asin LIKE CONCAT('%', #{f.keyword}, '%')");
// 启停:status=1 清 disabled_at、=0 记 NOW(),都刷 updated_at=NOW()(桌面版 set_status_gating)
assertThat(xml).contains("disabled_at = IF(#{status} = 1, NULL, NOW())").contains("updated_at = NOW()");
// 统计:分组只数生效行(status=1),国家按 count DESC(桌面版 by_country)
assertThat(xml).contains("WHERE status = 1").doesNotContain("WHERE status = 1 AND");
assertThat(xml).contains("ORDER BY count DESC");
// 「全部」哨兵不过滤(桌面版 country != "全部" 同口径)
assertThat(xml).contains("f.country != '全部'").contains("f.gatingType != '全部'").contains("f.source != '全部'");
}
@Test
@DisplayName("控制器契约:权限键 admin_brand_gating、逐端点 requireUser + requireConsolePageAccess 把门")
void controllerGatesEveryEndpointWithPermissionKey() throws Exception {
String source = Files.readString(Path.of(
"src/main/java/com/nanri/aiimage/modules/brandgating/controller/BrandGatingController.java"),
StandardCharsets.UTF_8);
assertThat(source).contains("PERMISSION_KEY = \"admin_brand_gating\"");
assertThat(source).contains("adminAuthSupport.requireUser(request)");
assertThat(source).contains("requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY)");
// 6 个端点方法都必须先过把门助手
long gated = source.split("requireBrandGatingAccess\\(request\\);", -1).length - 1;
assertThat(gated).isEqualTo(6);
}
}
@@ -0,0 +1,87 @@
package com.nanri.aiimage.modules.dedupe.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.config.InstanceMetadata;
import com.nanri.aiimage.config.StorageProperties;
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.concurrent.Semaphore;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* 文件处理槽位的等待上限(回归:原先 {@code Semaphore.acquire()} 没有超时)。
*
* <p>去重的文件并行度只有 4 个槽位。一个文件卡在上游读取或 OSS 上传时,同批与后续任务
* 的每个文件都原先**无限**排在 {@code acquire()} 上:任务不落终态、进度冻在已提交的那一格,
* 用户只能干等。改造后等不到槽位就把该文件判失败并写明"是没排到槽位",等待上限可配置。
*/
@ExtendWith(MockitoExtension.class)
class DedupeRunServiceFileSlotTest {
@Mock
private FileTaskMapper fileTaskMapper;
@Mock
private FileResultMapper fileResultMapper;
@Mock
private StorageProperties storageProperties;
@Mock
private OssStorageService ossStorageService;
@Mock
private DedupeTotalDataService dedupeTotalDataService;
@Mock
private com.nanri.aiimage.common.service.CompanyScopeSupport companyScopeSupport;
@Mock
private StringRedisTemplate redisTemplate;
private final ObjectMapper objectMapper = new ObjectMapper();
private DedupeRunService newService(long waitSeconds) {
DedupeRunService service = new DedupeRunService(fileTaskMapper, fileResultMapper,
storageProperties, ossStorageService, dedupeTotalDataService, companyScopeSupport,
redisTemplate, objectMapper, new InstanceMetadata("server-110"));
ReflectionTestUtils.setField(service, "fileSlotWaitSeconds", waitSeconds);
return service;
}
private Semaphore slots(DedupeRunService service) {
Semaphore semaphore = (Semaphore) ReflectionTestUtils.getField(service, "fileParallelSemaphore");
assertNotNull(semaphore, "文件并行槽位必须存在");
return semaphore;
}
@Test
@DisplayName("槽位被占满时等待必须到点返回 false,而不是无限排队")
void timesOutWhenNoSlotIsFree() throws Exception {
DedupeRunService service = newService(1L);
Semaphore slots = slots(service);
assertTrue(slots.drainPermits() > 0, "用例前提:全部槽位被其它文件占着");
assertFalse(service.tryAcquireFileSlot(), "槽位占满时必须超时返回,不能继续无界排队");
assertEquals(0, slots.availablePermits(), "超时返回不得顺手占住一个槽位");
}
@Test
@DisplayName("有空槽位时照常拿到(超时不是无条件拒绝)")
void grantsSlotWhenOneIsFree() throws Exception {
DedupeRunService service = newService(1L);
Semaphore slots = slots(service);
slots.release();
int before = slots.availablePermits();
assertTrue(service.tryAcquireFileSlot(), "有空槽位就必须放行");
assertEquals(before - 1, slots.availablePermits(), "拿到槽位要真的占住一个");
}
}
@@ -0,0 +1,479 @@
package com.nanri.aiimage.modules.keywordshare.service;
import com.nanri.aiimage.modules.keywordshare.mapper.KeywordAddLogMapper;
import com.nanri.aiimage.modules.keywordshare.mapper.KeywordDeleteLogMapper;
import com.nanri.aiimage.modules.keywordshare.mapper.KeywordShareMapper;
import com.nanri.aiimage.modules.keywordshare.mapper.KeywordUsageMapper;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareFilterQuery;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareKey;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordAddLogEntity;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordDeleteLogEntity;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordShareEntity;
import com.nanri.aiimage.modules.keywordshare.model.entity.KeywordUsageEntity;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareAddRequest;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordShareDeleteRequest;
import com.nanri.aiimage.modules.keywordshare.model.dto.KeywordUsageBatchRequest;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordSharePageVo;
import com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareRowVo;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 五国关键词共享 Service 单测:列表筛选/状态过滤/排序契约、分页与 usedDays 归一化、
* mark 幂等、add 查重跳过 + 留痕 + 自动标记、delete 留痕、random、dup 计数。
*/
@ExtendWith(MockitoExtension.class)
class KeywordShareServiceTest {
private static final Long COMPANY_ID = 7L;
private static final String VIEWER = "阿华";
@Mock
private KeywordShareMapper keywordShareMapper;
@Mock
private KeywordUsageMapper keywordUsageMapper;
@Mock
private KeywordAddLogMapper keywordAddLogMapper;
@Mock
private KeywordDeleteLogMapper keywordDeleteLogMapper;
private KeywordShareService newService() {
return new KeywordShareService(keywordShareMapper, keywordUsageMapper,
keywordAddLogMapper, keywordDeleteLogMapper);
}
private static KeywordShareRowVo row(long id, String keyword, boolean used, String usedUser) {
KeywordShareRowVo vo = new KeywordShareRowVo();
vo.setId(id);
vo.setCountry("英国");
vo.setKeyword(keyword);
vo.setChinese("翻译" + id);
vo.setUsed(used);
if (used) {
vo.setUsedTime(LocalDateTime.now().minusHours(id));
vo.setUsedUser(usedUser);
}
return vo;
}
// ---------------------------------------------------------------- 列表
@Test
@DisplayName("列表:命中数按用户筛选口径、命中内已使用数单独数一遍、dup 计数进统计条")
void pageComputesHitUsedUnusedAndDup() {
KeywordShareFilterQuery all = new KeywordShareFilterQuery();
all.setCompanyId(COMPANY_ID);
all.setStatus("all");
when(keywordShareMapper.countHit(any(KeywordShareFilterQuery.class))).thenReturn(10L, 4L);
when(keywordShareMapper.selectRows(any(KeywordShareFilterQuery.class))).thenReturn(List.of());
when(keywordShareMapper.countDupKeywords(COMPANY_ID)).thenReturn(3L);
KeywordSharePageVo vo = newService().page(COMPANY_ID, VIEWER, null, null, null, null, null,
null, "all", 0, false, 1L, 200L);
assertThat(vo.getTotal()).isEqualTo(10L);
assertThat(vo.getStats().getHit()).isEqualTo(10L);
assertThat(vo.getStats().getUsed()).isEqualTo(4L);
assertThat(vo.getStats().getUnused()).isEqualTo(6L);
assertThat(vo.getStats().getDup()).isEqualTo(3L);
// 第二次 countHit 是「命中内已使用数」,必须把 status 换成 used、其余条件原样
ArgumentCaptor<KeywordShareFilterQuery> captor = ArgumentCaptor.forClass(KeywordShareFilterQuery.class);
verify(keywordShareMapper, org.mockito.Mockito.times(2)).countHit(captor.capture());
assertThat(captor.getAllValues().get(0).getStatus()).isEqualTo("all");
assertThat(captor.getAllValues().get(1).getStatus()).isEqualTo("used");
assertThat(captor.getAllValues().get(1).getCompanyId()).isEqualTo(COMPANY_ID);
}
@Test
@DisplayName("列表:状态过滤 used/unused 时命中内已/未使用数直接由命中推导,不再多查一遍")
void pageWithActiveStatusFilterDerivesStats() {
when(keywordShareMapper.countHit(any(KeywordShareFilterQuery.class))).thenReturn(6L);
when(keywordShareMapper.selectRows(any(KeywordShareFilterQuery.class))).thenReturn(List.of());
when(keywordShareMapper.countDupKeywords(COMPANY_ID)).thenReturn(0L);
KeywordSharePageVo used = newService().page(COMPANY_ID, VIEWER, null, null, null, null, null,
null, "used", 0, false, 1L, 200L);
assertThat(used.getStats().getHit()).isEqualTo(6L);
assertThat(used.getStats().getUsed()).isEqualTo(6L);
assertThat(used.getStats().getUnused()).isZero();
KeywordSharePageVo unused = newService().page(COMPANY_ID, VIEWER, null, null, null, null, null,
null, "unused", 0, false, 1L, 200L);
assertThat(unused.getStats().getUsed()).isZero();
assertThat(unused.getStats().getUnused()).isEqualTo(6L);
// 状态过滤生效时不再为「命中内已使用数」多查一遍:两次 page 各查 1 次
verify(keywordShareMapper, org.mockito.Mockito.times(2)).countHit(any(KeywordShareFilterQuery.class));
}
@Test
@DisplayName("列表:筛选条件(类目/搜索/仅看我/usedDays)与分页进 Mapper 载体,size 上限 500")
void pageCarriesFilterAndNormalizedPaging() {
when(keywordShareMapper.countHit(any(KeywordShareFilterQuery.class))).thenReturn(0L);
when(keywordShareMapper.selectRows(any(KeywordShareFilterQuery.class))).thenReturn(List.of());
when(keywordShareMapper.countDupKeywords(COMPANY_ID)).thenReturn(0L);
newService().page(COMPANY_ID, VIEWER, " 英国 ", "厨房用品", "锅具", "煎锅", "平底锅",
" storage ", "all", 30, true, 0L, 1000L);
ArgumentCaptor<KeywordShareFilterQuery> captor = ArgumentCaptor.forClass(KeywordShareFilterQuery.class);
verify(keywordShareMapper).selectRows(captor.capture());
KeywordShareFilterQuery filter = captor.getValue();
assertThat(filter.getCountry()).isEqualTo("英国");
assertThat(filter.getCategory()).isEqualTo("厨房用品");
assertThat(filter.getSubCategory()).isEqualTo("锅具");
assertThat(filter.getCategoryL3()).isEqualTo("煎锅");
assertThat(filter.getCategoryL4()).isEqualTo("平底锅");
assertThat(filter.getSearch()).isEqualTo("storage");
assertThat(filter.getStatus()).isEqualTo("all");
assertThat(filter.getOnlyMine()).isTrue();
assertThat(filter.getUserName()).isEqualTo(VIEWER);
// usedDays=30:往前推 30 天
assertThat(filter.getUsedSince()).isNotNull();
assertThat(filter.getUsedSince()).isBefore(LocalDateTime.now());
assertThat(filter.getUsedSince()).isAfter(LocalDateTime.now().minusDays(31));
// 分页归一化:page=0→1、size=1000→500
assertThat(filter.getOffset()).isZero();
assertThat(filter.getLimit()).isEqualTo(500L);
}
@Test
@DisplayName("usedDays=1 表示「今天」:时间下限取当日零点;不传/0 表示不限")
void usedDaysOneMeansStartOfToday() {
when(keywordShareMapper.countHit(any(KeywordShareFilterQuery.class))).thenReturn(0L);
when(keywordShareMapper.selectRows(any(KeywordShareFilterQuery.class))).thenReturn(List.of());
when(keywordShareMapper.countDupKeywords(COMPANY_ID)).thenReturn(0L);
newService().page(COMPANY_ID, VIEWER, null, null, null, null, null, null, "all", 1, false, 1L, 200L);
ArgumentCaptor<KeywordShareFilterQuery> captor = ArgumentCaptor.forClass(KeywordShareFilterQuery.class);
verify(keywordShareMapper).selectRows(captor.capture());
assertThat(captor.getValue().getUsedSince()).isEqualTo(LocalDate.now().atStartOfDay());
newService().page(COMPANY_ID, VIEWER, null, null, null, null, null, null, "all", null, false, 1L, 200L);
verify(keywordShareMapper, org.mockito.Mockito.times(2)).selectRows(captor.capture());
assertThat(captor.getValue().getUsedSince()).isNull();
}
@Test
@DisplayName("列表:行按 Mapper 返回顺序透传(SQL 侧已按未使用在前、已使用按 used_time 升序排好),未使用行不带使用者")
void pageRowsKeepMapperOrderAndNormalizeUnusedRows() {
when(keywordShareMapper.countHit(any(KeywordShareFilterQuery.class))).thenReturn(3L);
when(keywordShareMapper.selectRows(any(KeywordShareFilterQuery.class))).thenReturn(List.of(
row(2, "kw-old-used", true, "张三"),
row(3, "kw-new-used", true, VIEWER),
row(1, "kw-unused", false, "不该有名字")));
when(keywordShareMapper.countDupKeywords(COMPANY_ID)).thenReturn(0L);
KeywordSharePageVo vo = newService().page(COMPANY_ID, VIEWER, null, null, null, null, null,
null, "all", 0, false, 1L, 200L);
assertThat(vo.getRows()).extracting(KeywordShareRowVo::getId).containsExactly(2L, 3L, 1L);
assertThat(vo.getRows().get(0).isUsed()).isTrue();
assertThat(vo.getRows().get(0).getUsedUser()).isEqualTo("张三");
assertThat(vo.getRows().get(2).isUsed()).isFalse();
assertThat(vo.getRows().get(2).getUsedTime()).isNull();
assertThat(vo.getRows().get(2).getUsedUser()).isEmpty();
}
@Test
@DisplayName("排序契约:Mapper XML 的 ORDER BY 必须是「已使用在前(对齐桌面版)、按 used_time 升序」,且禁止 ${} 拼接")
void mapperXmlKeepsSortContractAndPlaceholderOnly() throws Exception {
String xml = Files.readString(Path.of(
"src/main/resources/mapper/keywordshare/KeywordShareMapper.xml"), StandardCharsets.UTF_8);
assertThat(xml).contains("ORDER BY (u.country IS NULL) ASC, u.used_time ASC");
assertThat(xml).doesNotContain("${");
String usageXml = Files.readString(Path.of(
"src/main/resources/mapper/keywordshare/KeywordUsageMapper.xml"), StandardCharsets.UTF_8);
assertThat(usageXml).contains("ON DUPLICATE KEY UPDATE").doesNotContain("${");
}
// ---------------------------------------------------------------- 标记/取消使用
@Test
@DisplayName("mark:UPSERT 幂等——重复标记同一词两次结果一致,使用者与时间都取服务端当前值")
void markIsIdempotentWithServerTime() {
KeywordUsageBatchRequest request = new KeywordUsageBatchRequest();
KeywordUsageBatchRequest.Item item1 = new KeywordUsageBatchRequest.Item();
item1.setCountry("英国");
item1.setKeyword(" storage box ");
KeywordUsageBatchRequest.Item item2 = new KeywordUsageBatchRequest.Item();
item2.setCountry("德国");
item2.setKeyword("aufbewahrungsbox");
request.setItems(List.of(item1, item2));
KeywordShareService service = newService();
LocalDateTime before = LocalDateTime.now();
var first = service.mark(request, COMPANY_ID, VIEWER);
var second = service.mark(request, COMPANY_ID, VIEWER);
LocalDateTime after = LocalDateTime.now();
assertThat(first.getMarked()).isEqualTo(2);
assertThat(second.getMarked()).isEqualTo(2);
ArgumentCaptor<List<KeywordUsageEntity>> captor = ArgumentCaptor.forClass(List.class);
verify(keywordUsageMapper, org.mockito.Mockito.times(2)).upsertBatch(captor.capture());
List<KeywordUsageEntity> rows = captor.getAllValues().get(0);
assertThat(rows).hasSize(2);
assertThat(rows).allSatisfy(row -> {
assertThat(row.getCompanyId()).isEqualTo(COMPANY_ID);
assertThat(row.getUserName()).isEqualTo(VIEWER);
assertThat(row.getUsedTime()).isAfterOrEqualTo(before).isBeforeOrEqualTo(after);
});
assertThat(rows.get(0).getKeyword()).isEqualTo("storage box");
assertThat(rows.get(1).getCountry()).isEqualTo("德国");
}
@Test
@DisplayName("mark:批内重复键去重、空键跳过;全空时不动库")
void markSkipsBlankAndDuplicateKeys() {
KeywordUsageBatchRequest request = new KeywordUsageBatchRequest();
KeywordUsageBatchRequest.Item blank = new KeywordUsageBatchRequest.Item();
blank.setCountry("英国");
blank.setKeyword(" ");
KeywordUsageBatchRequest.Item dup = new KeywordUsageBatchRequest.Item();
dup.setCountry("英国");
dup.setKeyword("box");
KeywordUsageBatchRequest.Item dupAgain = new KeywordUsageBatchRequest.Item();
dupAgain.setCountry(" 英国 ");
dupAgain.setKeyword("box");
request.setItems(List.of(blank, dup, dupAgain));
var result = newService().mark(request, COMPANY_ID, VIEWER);
assertThat(result.getMarked()).isEqualTo(1);
@SuppressWarnings("unchecked")
ArgumentCaptor<List<KeywordUsageEntity>> captor = ArgumentCaptor.forClass(List.class);
verify(keywordUsageMapper).upsertBatch(captor.capture());
assertThat(captor.getValue()).hasSize(1);
assertThat(captor.getValue().get(0).getKeyword()).isEqualTo("box");
KeywordUsageBatchRequest allBlank = new KeywordUsageBatchRequest();
KeywordUsageBatchRequest.Item item = new KeywordUsageBatchRequest.Item();
item.setKeyword(" ");
allBlank.setItems(List.of(item));
assertThat(newService().mark(allBlank, COMPANY_ID, VIEWER).getMarked()).isZero();
}
@Test
@DisplayName("unmark:按 (company, country, keyword) 删行,返回实际删除条数")
void unmarkDeletesRowsAndCountsAffected() {
KeywordUsageBatchRequest request = new KeywordUsageBatchRequest();
KeywordUsageBatchRequest.Item item = new KeywordUsageBatchRequest.Item();
item.setCountry("英国");
item.setKeyword("box");
request.setItems(List.of(item));
when(keywordUsageMapper.deleteBatch(eq(COMPANY_ID), anyList())).thenReturn(1);
var result = newService().unmark(request, COMPANY_ID);
assertThat(result.getUnmarked()).isEqualTo(1);
@SuppressWarnings("unchecked")
ArgumentCaptor<List<KeywordShareKey>> captor = ArgumentCaptor.forClass(List.class);
verify(keywordUsageMapper).deleteBatch(eq(COMPANY_ID), captor.capture());
assertThat(captor.getValue().get(0).getKeyword()).isEqualTo("box");
}
// ---------------------------------------------------------------- 添加
@Test
@DisplayName("add:查重跳过已存在词,空词/批内重复跳过;成功行写词本体 + 添加留痕 + 自动标记已使用")
void addSkipsExistingAndBlankAndWritesLogsAndAutoMarks() {
KeywordShareAddRequest request = new KeywordShareAddRequest();
KeywordShareAddRequest.Item good = new KeywordShareAddRequest.Item();
good.setCountry("英国");
good.setKeyword(" storage box ");
good.setChinese("收纳盒");
KeywordShareAddRequest.Item existing = new KeywordShareAddRequest.Item();
existing.setCountry("德国");
existing.setKeyword("aufbewahrungsbox");
existing.setChinese("收纳箱");
KeywordShareAddRequest.Item blank = new KeywordShareAddRequest.Item();
blank.setCountry("法国");
blank.setKeyword(" ");
KeywordShareAddRequest.Item dup = new KeywordShareAddRequest.Item();
dup.setCountry("英国");
dup.setKeyword("storage box");
request.setItems(List.of(good, existing, blank, dup));
KeywordShareKey existingKey = KeywordShareKey.of("德国", "aufbewahrungsbox");
when(keywordShareMapper.selectExistingKeywords(eq(COMPANY_ID), anyList())).thenReturn(List.of(existingKey));
LocalDateTime before = LocalDateTime.now();
var result = newService().add(request, COMPANY_ID, VIEWER);
assertThat(result.getAdded()).isEqualTo(1);
assertThat(result.getSkipped()).isEqualTo(3);
@SuppressWarnings("unchecked")
ArgumentCaptor<List<KeywordShareEntity>> insertCaptor = ArgumentCaptor.forClass(List.class);
verify(keywordShareMapper).insertBatchIgnore(insertCaptor.capture());
KeywordShareEntity inserted = insertCaptor.getValue().get(0);
assertThat(inserted.getCountry()).isEqualTo("英国");
assertThat(inserted.getKeyword()).isEqualTo("storage box");
assertThat(inserted.getChinese()).isEqualTo("收纳盒");
assertThat(inserted.getKtype()).isEqualTo("主关键词");
assertThat(inserted.getSource()).isEqualTo("手动添加");
assertThat(inserted.getCompanyId()).isEqualTo(COMPANY_ID);
assertThat(inserted.getUserName()).isEqualTo(VIEWER);
assertThat(inserted.getAddTime()).isAfterOrEqualTo(before);
@SuppressWarnings("unchecked")
ArgumentCaptor<List<KeywordAddLogEntity>> logCaptor = ArgumentCaptor.forClass(List.class);
verify(keywordAddLogMapper).insertBatch(logCaptor.capture());
assertThat(logCaptor.getValue()).hasSize(1);
assertThat(logCaptor.getValue().get(0).getKeyword()).isEqualTo("storage box");
assertThat(logCaptor.getValue().get(0).getUserName()).isEqualTo(VIEWER);
@SuppressWarnings("unchecked")
ArgumentCaptor<List<KeywordUsageEntity>> usageCaptor = ArgumentCaptor.forClass(List.class);
verify(keywordUsageMapper).upsertBatch(usageCaptor.capture());
assertThat(usageCaptor.getValue()).hasSize(1);
assertThat(usageCaptor.getValue().get(0).getKeyword()).isEqualTo("storage box");
assertThat(usageCaptor.getValue().get(0).getUserName()).isEqualTo(VIEWER);
}
@Test
@DisplayName("add:全部跳过时不动任何表(词本体/留痕/使用状态都不写)")
void addWithNothingToAddTouchesNothing() {
KeywordShareAddRequest request = new KeywordShareAddRequest();
KeywordShareAddRequest.Item blank = new KeywordShareAddRequest.Item();
blank.setCountry("英国");
blank.setKeyword("");
request.setItems(List.of(blank));
var result = newService().add(request, COMPANY_ID, VIEWER);
assertThat(result.getAdded()).isZero();
assertThat(result.getSkipped()).isEqualTo(1);
verify(keywordShareMapper, never()).insertBatchIgnore(anyList());
verify(keywordAddLogMapper, never()).insertBatch(anyList());
verify(keywordUsageMapper, never()).upsertBatch(anyList());
verify(keywordShareMapper, never()).selectExistingKeywords(any(), anyList());
}
// ---------------------------------------------------------------- 删除
@Test
@DisplayName("delete:只删本公司存在的行并逐行写删除留痕(谁删了哪个词可追溯)")
void deleteWritesDeleteLogForCompanyRows() {
KeywordShareDeleteRequest request = new KeywordShareDeleteRequest();
request.setIds(List.of(1L, 2L, 999L));
KeywordShareEntity row1 = new KeywordShareEntity();
row1.setId(1L);
row1.setCountry("英国");
row1.setKeyword("box");
row1.setCompanyId(COMPANY_ID);
KeywordShareEntity row2 = new KeywordShareEntity();
row2.setId(2L);
row2.setCountry("德国");
row2.setKeyword("kiste");
row2.setCompanyId(COMPANY_ID);
KeywordShareEntity foreign = new KeywordShareEntity();
foreign.setId(999L);
foreign.setCountry("法国");
foreign.setKeyword("mot-etranger");
foreign.setCompanyId(8L);
when(keywordShareMapper.selectBatchIds(List.of(1L, 2L, 999L))).thenReturn(List.of(row1, row2, foreign));
LocalDateTime before = LocalDateTime.now();
var result = newService().delete(request, COMPANY_ID, VIEWER);
// 公司隔离:999 号行是别家公司的,即使 id 在请求里也绝不能删
assertThat(result.getDeleted()).isEqualTo(2);
verify(keywordShareMapper).deleteBatchIds(List.of(1L, 2L));
@SuppressWarnings("unchecked")
ArgumentCaptor<List<KeywordDeleteLogEntity>> logCaptor = ArgumentCaptor.forClass(List.class);
verify(keywordDeleteLogMapper).insertBatch(logCaptor.capture());
List<KeywordDeleteLogEntity> logs = logCaptor.getValue();
assertThat(logs).hasSize(2);
assertThat(logs).extracting(KeywordDeleteLogEntity::getKeyword).containsExactly("box", "kiste");
assertThat(logs).allSatisfy(log -> {
assertThat(log.getCompanyId()).isEqualTo(COMPANY_ID);
assertThat(log.getUserName()).isEqualTo(VIEWER);
assertThat(log.getDeleteTime()).isAfterOrEqualTo(before);
});
}
@Test
@DisplayName("delete:请求里的行都不属于本公司(或不存在)时,不动库也不留痕")
void deleteWithNoCompanyRowsTouchesNothing() {
KeywordShareDeleteRequest request = new KeywordShareDeleteRequest();
request.setIds(List.of(1L));
when(keywordShareMapper.selectBatchIds(anyList())).thenReturn(List.of());
var result = newService().delete(request, COMPANY_ID, VIEWER);
assertThat(result.getDeleted()).isZero();
verify(keywordShareMapper, never()).deleteBatchIds(anyList());
verify(keywordDeleteLogMapper, never()).insertBatch(anyList());
}
// ---------------------------------------------------------------- 随机抽词
@Test
@DisplayName("random:只从「未使用」池抽,条数默认 10、上限 100、下限 1")
void randomUsesUnusedPoolWithClampedCount() {
when(keywordShareMapper.selectRandomUnused(any(KeywordShareFilterQuery.class))).thenReturn(List.of(
row(5, "kw-random", false, "")));
var result = newService().random(COMPANY_ID, VIEWER, "英国", null, null, null, null, null, 5L);
assertThat(result).hasSize(1);
assertThat(result.get(0).getKeyword()).isEqualTo("kw-random");
ArgumentCaptor<KeywordShareFilterQuery> captor = ArgumentCaptor.forClass(KeywordShareFilterQuery.class);
verify(keywordShareMapper).selectRandomUnused(captor.capture());
assertThat(captor.getValue().getStatus()).isEqualTo("unused");
assertThat(captor.getValue().getLimit()).isEqualTo(5L);
assertThat(captor.getValue().getCountry()).isEqualTo("英国");
newService().random(COMPANY_ID, VIEWER, null, null, null, null, null, null, null);
verify(keywordShareMapper, org.mockito.Mockito.times(2)).selectRandomUnused(captor.capture());
assertThat(captor.getValue().getLimit()).isEqualTo(KeywordShareService.DEFAULT_RANDOM_COUNT);
newService().random(COMPANY_ID, VIEWER, null, null, null, null, null, null, 1000L);
verify(keywordShareMapper, org.mockito.Mockito.times(3)).selectRandomUnused(captor.capture());
assertThat(captor.getValue().getLimit()).isEqualTo(KeywordShareService.MAX_RANDOM_COUNT);
}
// ---------------------------------------------------------------- 防回流的供应商隔离自检
@Test
@DisplayName("公司隔离:全部 Mapper 写路径的第一个参数都是服务端给的 companyId(抽查写 SQL 收口)")
void writeSqlStaysInMapperLayerWithCompanyScope() throws Exception {
String shareXml = Files.readString(Path.of(
"src/main/resources/mapper/keywordshare/KeywordShareMapper.xml"), StandardCharsets.UTF_8);
// 词本体的读路径一律带 company_id 条件(含 dup 子查询与类目名册)
long scopedSelects = shareXml.lines().filter(line -> line.contains("company_id = #{")).count();
assertThat(scopedSelects).isGreaterThanOrEqualTo(5);
String usageXml = Files.readString(Path.of(
"src/main/resources/mapper/keywordshare/KeywordUsageMapper.xml"), StandardCharsets.UTF_8);
assertThat(usageXml).contains("company_id = #{companyId}");
String addXml = Files.readString(Path.of(
"src/main/resources/mapper/keywordshare/KeywordAddLogMapper.xml"), StandardCharsets.UTF_8);
String deleteXml = Files.readString(Path.of(
"src/main/resources/mapper/keywordshare/KeywordDeleteLogMapper.xml"), StandardCharsets.UTF_8);
assertThat(addXml + deleteXml)
.contains("company_id", "user_name")
.doesNotContain("${");
}
}
@@ -0,0 +1,178 @@
package com.nanri.aiimage.modules.similarasin.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.SimilarAsinProperties;
import com.nanri.aiimage.modules.usersecret.service.UserSecretUsageService;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
/**
* 连接层短路口径(B23):网关根本连不上时,重试阶梯必须在证据数处停下并给出可读归因;
* 连得上只是没答好(5xx/读超时)不得短路,重试阶梯照跑满。
*/
class SimilarAsinLlmClientHttpTest {
private HttpServer server;
private final ObjectMapper objectMapper = new ObjectMapper();
private final AtomicInteger requestCount = new AtomicInteger();
/** 请求前挂起时长:模拟"连上了但不应答"的读超时形状。 */
private volatile long responseDelayMillis = 0L;
private volatile boolean failWith502 = false;
@BeforeEach
void setUp() throws IOException {
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/v1/chat/completions", this::handleChat);
// 默认单线程派发:handler 睡眠模拟读超时时会吞掉超时后补发的下一次请求
server.setExecutor(Executors.newCachedThreadPool());
server.start();
}
@AfterEach
void tearDown() {
server.stop(0);
}
private void handleChat(HttpExchange exchange) throws IOException {
exchange.getRequestBody().readAllBytes();
requestCount.incrementAndGet();
long delay = responseDelayMillis;
if (delay > 0L) {
try {
Thread.sleep(delay);
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
}
}
String body = failWith502
? "{\"error\":{\"message\":\"bad gateway\"}}"
: "{\"choices\":[{\"message\":{\"content\":\"{\\\"ok\\\":true}\"}}]}";
respond(exchange, failWith502 ? 502 : 200, body);
}
private void respond(HttpExchange exchange, int status, String body) throws IOException {
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
exchange.sendResponseHeaders(status, bytes.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(bytes);
}
}
/** 连接层故障短路:网关根本连不上时,第 2 次连接失败即停,归因文案说清是运维故障。 */
@Test
void unreachableGatewayShortCircuitsLadderAndSurfacesReason() {
SimilarAsinLlmClient deadHostClient = buildClient(p -> {
p.setLlmHost("http://127.0.0.1:1");
p.setLlmRetryTimes(3);
});
long startedAt = System.nanoTime();
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("LLM 服务不可达(连接未建立,非本行数据问题)")
.hasMessageNotContaining("LLM http ");
long elapsedMs = (System.nanoTime() - startedAt) / 1_000_000;
// 短路发生在第 2 次连接失败:只付 1 段退避(2s±30%);本机回环拒绝连接实测 ~2.3s/次
// ⇒ 短路 ~7.7s。跑满 3 次阶梯要多付一段 10s±30%(≥7s)⇒ 实测 ~20s,12s 档两侧都稳
assertThat(elapsedMs).isLessThan(12_000L);
}
/** 反向对照:连得上只是 5xx(非连接层故障)不得触发短路,重试阶梯要跑满。 */
@Test
void httpFailureKeepsFullRetryLadder() {
failWith502 = true;
SimilarAsinLlmClient threeAttemptClient = buildClient(p -> p.setLlmRetryTimes(3));
assertThatThrownBy(() -> threeAttemptClient.invokeChat("test-model", "s", "u", "test-key"))
.hasMessageContaining("LLM http 502");
assertThat(requestCount.get()).isEqualTo(3);
}
/** 读超时是"连上了没答好",故意不算连接层证据:阶梯照跑满,归因文案不得冒充不可达。 */
@Test
void readTimeoutIsNotCountedAsConnectFailure() {
responseDelayMillis = 1_000L;
SimilarAsinLlmClient shortTimeoutClient = buildClient(p -> {
p.setLlmRetryTimes(2);
p.setLlmFirstAttemptReadTimeoutMillis(300);
p.setLlmReadTimeoutMillis(400);
});
assertThatThrownBy(() -> shortTimeoutClient.invokeChat("test-model", "s", "u", "test-key"))
.hasMessageNotContaining("LLM 服务不可达");
assertThat(requestCount.get()).isEqualTo(2);
}
/** B24:首次调用确认不可达即进客户端级冷却,冷却期内的后续调用零探针快速失败。 */
@Test
void cooldownMakesSubsequentCallsFailFastWithoutProbes() {
SimilarAsinLlmClient deadHostClient = buildClient(p -> p.setLlmHost("http://127.0.0.1:1"));
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
.hasMessageContaining("LLM 服务不可达(连接未建立,非本行数据问题)");
assertThat(lastCooldown.isCoolingDown(lastHost, lastWindow)).isTrue();
// 快速失败走的是入口冷却分支(";lastErr=冷却中"后缀只有该路径会拼),一次探针都不发
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
.hasMessageContaining("LLM 服务不可达(连接未建立,非本行数据问题)")
.hasMessageContaining(";lastErr=冷却中");
}
/** B24:冷却到期后下一条真实调用兼作探针;探针仍连不上 ⇒ 立即重进冷却。 */
@Test
void expiredCooldownProbesAgainAndReentersOnFailure() throws InterruptedException {
SimilarAsinLlmClient deadHostClient = buildClient(p -> {
p.setLlmHost("http://127.0.0.1:1");
p.setLlmUnreachableCooldownMillis(200);
});
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
.hasMessageContaining("LLM 服务不可达(连接未建立,非本行数据问题)");
Thread.sleep(400);
assertThat(lastCooldown.isCoolingDown(lastHost, 200)).isFalse();
// 探针真发:lastErr 带的是连接异常而不是"冷却中";失败即重进
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
.hasMessageContaining("LLM 服务不可达(连接未建立,非本行数据问题)")
.hasMessageNotContaining("冷却中");
assertThat(lastCooldown.isCoolingDown(lastHost, 200)).isTrue();
// 重进之后的调用回到零探针快速失败
assertThatThrownBy(() -> deadHostClient.invokeChat("test-model", "s", "u", "test-key"))
.hasMessageContaining(";lastErr=冷却中");
}
private SimilarAsinLlmClient buildClient(java.util.function.Consumer<SimilarAsinProperties> customizer) {
SimilarAsinProperties properties = new SimilarAsinProperties();
properties.setLlmHost("http://127.0.0.1:" + server.getAddress().getPort());
customizer.accept(properties);
lastCooldown = new UnreachableCooldown();
lastHost = properties.getLlmHost();
lastWindow = Math.max(0, properties.getLlmUnreachableCooldownMillis());
return new SimilarAsinLlmClient(properties, objectMapper, null, mock(UserSecretUsageService.class), lastCooldown);
}
/** 最近一次 buildClient 的冷却表与 host/窗长:冷却态断言直接读同一实例,不靠时间猜。 */
private UnreachableCooldown lastCooldown;
private String lastHost;
private long lastWindow;
}
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.similarasin.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.config.OssProperties;
import com.nanri.aiimage.config.SimilarAsinProperties;
@@ -61,7 +62,7 @@ public class SimilarAsinLlmLocalVerify {
props.setLlmRowConcurrency(2);
props.setLlmImageDownloadTimeoutSeconds(10);
SimilarAsinLlmClient client = new SimilarAsinLlmClient(props, objectMapper, null, null);
SimilarAsinLlmClient client = new SimilarAsinLlmClient(props, objectMapper, null, null, new UnreachableCooldown());
OssProperties ossProps = new OssProperties();
ossProps.setEndpoint("https://oss.aishufu.top");
ossProps.setPublicEndpoint("https://oss.aishufu.top");
+2 -1
View File
@@ -7,7 +7,8 @@
"dev": "vite --host --port 5173",
"build": "vue-tsc --noEmit && vite build",
"preview": "vite preview",
"test": "node --test --test-force-exit --test-timeout=180000 tests/*.test.ts"
"test": "node --test --test-force-exit --test-timeout=180000 tests/*.test.ts",
"test:mount": "vitest run"
},
"dependencies": {
"axios": "^1.13.6",
@@ -0,0 +1,38 @@
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
echo 正在执行 PowerShell 命令解除文件锁定...
powershell -Command "Get-ChildItem -Path \"%cd%\" -Recurse | Unblock-File"
if %errorlevel% equ 0 (
echo 文件解锁完成
) else (
echo 文件解锁失败,请检查 PowerShell 权限
)
echo.
echo 检查 ShuFuAi.exe.config 文件...
if exist "ShuFuAi.exe.config" (
echo ShuFuAi.exe.config 已存在,跳过创建
) else (
echo 正在创建 ShuFuAi.exe.config...
(
echo ^<?xml version="1.0" encoding="utf-8" ?^>
echo ^<configuration^>
echo ^<runtime^>
echo ^<loadFromRemoteSources enabled="true"/^>
echo ^</runtime^>
echo ^</configuration^>
) > "ShuFuAi.exe.config"
if exist "ShuFuAi.exe.config" (
echo ShuFuAi.exe.config 创建成功
) else (
echo ShuFuAi.exe.config 创建失败,请检查权限
)
)
echo.
echo 操作完成!
pause
@@ -12,15 +12,15 @@
<span class="hero-pill">运营辅助 · 数据安全 · 高效跟价</span>
<h1 class="hero-title">让亚马逊运营,更清晰高效</h1>
<p class="hero-sub">
从采集、去重、查品到上架跟价,20 个工具按「前端 · 运营 · 后勤」三线组织,每条工具一句话讲清用途,标准流程一步不缺。
从换算、查询、采集、去重到上架跟价,23 个工具按「常用 · 前端 · 运营 · 后勤」四线组织,每条工具一句话讲清用途,标准流程一步不缺。
</p>
<div class="hero-stats">
<div class="stat">
<strong style="color: #2dd4bf">20</strong>
<strong style="color: #2dd4bf">23</strong>
<span>个工具</span>
</div>
<div class="stat">
<strong style="color: #60a5fa">3</strong>
<strong style="color: #60a5fa">4</strong>
<span>大分类</span>
</div>
<div class="stat">
@@ -128,7 +128,7 @@ import AmazonFooterBar from '@/pages/amazon/components/AmazonFooterBar.vue'
import ToolIcon from '@/pages/amazon/components/ToolIcon.vue'
import { TUTORIAL_DOWNLOAD_URL, resolveTutorialDownloadUrl } from '@/pages/amazon/download-btn'
import { filterGroupsByPermission, filterWorkflowByPermission, TOOL_GROUPS, TOOL_MAP } from '@/pages/amazon/tool-catalog'
import type { ToolGroup, ToolInfo, VisibleGroup, WorkflowStep } from '@/pages/amazon/tool-catalog'
import type { ToolGroup, ToolGroupKey, ToolInfo, VisibleGroup, WorkflowStep } from '@/pages/amazon/tool-catalog'
import { resolvePageHref } from '@/shared/page-prefix'
import { isHttpUrl, safeHttpUrl } from '@/shared/utils/safe-url'
import { prefetchRouteChunk } from '@/shared/route-prefetch'
@@ -143,7 +143,7 @@ const visibleGroups = ref<VisibleGroup[]>(TOOL_GROUPS.map((group) => ({ ...group
const permissionUnavailable = ref(false)
const statusText = ref('')
const statusType = ref<'normal' | 'error'>('normal')
const activeCat = ref<'front' | 'ops' | 'logi'>('front')
const activeCat = ref<ToolGroupKey>('front')
let statusTimer: number | undefined
const currentGroup = computed(() => visibleGroups.value.find((g) => g.key === activeCat.value) ?? visibleGroups.value[0])
@@ -249,15 +249,15 @@ async function onDownloadClick() {
window.open(safeHttpUrl(url), '_blank', 'noopener')
}
function onCatChange(key: 'front' | 'ops' | 'logi') {
function onCatChange(key: ToolGroupKey) {
activeCat.value = key
history.replaceState(null, '', `#group-${key}`)
}
function parseHashGroup() {
const hash = window.location.hash.replace('#', '')
const m = /^group-(front|ops|logi)$/.exec(hash)
if (m) activeCat.value = m[1] as 'front' | 'ops' | 'logi'
const m = /^group-(common|front|ops|logi)$/.exec(hash)
if (m) activeCat.value = m[1] as ToolGroupKey
}
onMounted(async () => {
@@ -35,6 +35,7 @@ import AmazonTopBar from '@/pages/amazon/components/AmazonTopBar.vue'
import AmazonFooterBar from '@/pages/amazon/components/AmazonFooterBar.vue'
import DownloadProgressPanel from '@/shared/components/DownloadProgressPanel.vue'
import { TOOL_DETAILS, TOOL_GROUPS, TOOL_MAP } from '@/pages/amazon/tool-catalog'
import type { ToolGroupKey } from '@/pages/amazon/tool-catalog'
import { resolvePageHref } from '@/shared/page-prefix'
/** Aliprice 注册推广链接(对齐 gemini-code 工具页头 Aliprice 按钮) */
@@ -48,7 +49,7 @@ const props = defineProps<{
const tool = computed(() => TOOL_MAP.get(props.toolId))
const group = computed(() => TOOL_GROUPS.find((g) => g.tools.some((t) => t.id === props.toolId)))
const groupKey = computed(() => group.value?.key ?? 'front')
const groupKey = computed<ToolGroupKey>(() => group.value?.key ?? 'front')
const detailText = computed(() => {
if (!tool.value) return ''
return TOOL_DETAILS[tool.value.id] ?? tool.value.desc

Some files were not shown because too many files have changed in this diff Show More