来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
357 lines
9.7 KiB
TypeScript
357 lines
9.7 KiB
TypeScript
import {
|
|
CATEGORY_TAX_NAME,
|
|
NAME_TAX_RULES,
|
|
coreSkuName,
|
|
normalizeTaxText,
|
|
} from './taxCodeRules';
|
|
import {
|
|
getTaxCatalogEntryByName,
|
|
getTaxCatalogEntryByCode,
|
|
getTaxCatalogEntryByNorm,
|
|
getTaxCatalogVersion,
|
|
searchTaxCatalog,
|
|
type TaxCatalogEntry,
|
|
} from './taxCatalogService';
|
|
import { lookupOfficialTaxCode } from './providers/taxCodeOfficialAdapter';
|
|
import { suggestTaxCodeWithAi } from './taxCodeAiService';
|
|
|
|
export type TaxCodeSource =
|
|
| 'rule_exact'
|
|
| 'rule_keyword'
|
|
| 'rule_category'
|
|
| 'official_search'
|
|
| 'official_api'
|
|
| 'ai';
|
|
|
|
export type TaxCodeCandidate = {
|
|
tax_code: string;
|
|
tax_name: string;
|
|
short_name: string;
|
|
score: number;
|
|
source: TaxCodeSource;
|
|
reason: string;
|
|
};
|
|
|
|
export type TaxCodeSuggestion = {
|
|
matched: boolean;
|
|
tax_code: string | null;
|
|
tax_name: string | null;
|
|
short_name: string | null;
|
|
source: TaxCodeSource | null;
|
|
confidence: number;
|
|
reason: string | null;
|
|
catalog_version: string | null;
|
|
needs_confirmation: boolean;
|
|
candidates: TaxCodeCandidate[];
|
|
};
|
|
|
|
function toCandidate(
|
|
entry: TaxCatalogEntry,
|
|
score: number,
|
|
source: TaxCodeSource,
|
|
reason: string
|
|
): TaxCodeCandidate {
|
|
return {
|
|
tax_code: entry.tax_code,
|
|
tax_name: entry.name,
|
|
short_name: entry.short_name,
|
|
score,
|
|
source,
|
|
reason,
|
|
};
|
|
}
|
|
|
|
function matchByExactCatalog(skuName: string) {
|
|
for (const candidate of [skuName, coreSkuName(skuName)]) {
|
|
const entry = getTaxCatalogEntryByNorm(normalizeTaxText(candidate));
|
|
if (entry) {
|
|
return { entry, reason: 'exact_tax_name', confidence: 98 };
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function matchByNameRules(skuName: string) {
|
|
const name = coreSkuName(skuName);
|
|
for (const rule of NAME_TAX_RULES) {
|
|
for (const kw of rule.keywords) {
|
|
if (name.includes(kw)) {
|
|
const entry = getTaxCatalogEntryByName(rule.taxName);
|
|
if (entry) {
|
|
return { entry, reason: `keyword:${kw}`, confidence: 88 };
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function matchByCategory(categoryName: string) {
|
|
const taxName = CATEGORY_TAX_NAME[categoryName];
|
|
if (!taxName) return null;
|
|
const entry = getTaxCatalogEntryByName(taxName);
|
|
if (!entry) return null;
|
|
return { entry, reason: `category:${categoryName}`, confidence: 62 };
|
|
}
|
|
|
|
function needsConfirmation(source: TaxCodeSource, confidence: number) {
|
|
if (source === 'rule_exact' && confidence >= 95) return false;
|
|
if (source === 'official_api' && confidence >= 92) return false;
|
|
if (source === 'ai' || source === 'rule_category') return true;
|
|
return confidence < 85;
|
|
}
|
|
|
|
function buildSuggestion(
|
|
entry: TaxCatalogEntry,
|
|
source: TaxCodeSource,
|
|
confidence: number,
|
|
reason: string,
|
|
candidates: TaxCodeCandidate[]
|
|
): TaxCodeSuggestion {
|
|
const primary = toCandidate(entry, confidence, source, reason);
|
|
const merged = [primary, ...candidates.filter((item) => item.tax_code !== entry.tax_code)].slice(0, 8);
|
|
return {
|
|
matched: true,
|
|
tax_code: entry.tax_code,
|
|
tax_name: entry.name,
|
|
short_name: entry.short_name,
|
|
source,
|
|
confidence,
|
|
reason,
|
|
catalog_version: getTaxCatalogVersion(),
|
|
needs_confirmation: needsConfirmation(source, confidence),
|
|
candidates: merged,
|
|
};
|
|
}
|
|
|
|
function emptySuggestion(candidates: TaxCodeCandidate[] = []): TaxCodeSuggestion {
|
|
return {
|
|
matched: false,
|
|
tax_code: null,
|
|
tax_name: null,
|
|
short_name: null,
|
|
source: null,
|
|
confidence: 0,
|
|
reason: null,
|
|
catalog_version: getTaxCatalogVersion(),
|
|
needs_confirmation: true,
|
|
candidates: candidates.slice(0, 8),
|
|
};
|
|
}
|
|
|
|
/** 规则引擎:同步快速匹配 */
|
|
export function suggestTaxCodeByRules(input: {
|
|
skuName?: string | null;
|
|
categoryName?: string | null;
|
|
}): TaxCodeSuggestion {
|
|
const skuName = String(input.skuName || '').trim();
|
|
const categoryName = String(input.categoryName || '').trim() || '(无分类)';
|
|
if (!skuName && categoryName === '(无分类)') return emptySuggestion();
|
|
|
|
if (skuName) {
|
|
const exact = matchByExactCatalog(skuName);
|
|
if (exact) {
|
|
return buildSuggestion(exact.entry, 'rule_exact', exact.confidence, exact.reason, []);
|
|
}
|
|
const keyword = matchByNameRules(skuName);
|
|
if (keyword) {
|
|
return buildSuggestion(keyword.entry, 'rule_keyword', keyword.confidence, keyword.reason, []);
|
|
}
|
|
}
|
|
|
|
const category = matchByCategory(categoryName);
|
|
if (category) {
|
|
return buildSuggestion(category.entry, 'rule_category', category.confidence, category.reason, []);
|
|
}
|
|
|
|
return emptySuggestion();
|
|
}
|
|
|
|
/** AI + 规则引擎 + 实时官方库:分层确认 */
|
|
export async function resolveTaxCodeConfirmation(input: {
|
|
skuName?: string | null;
|
|
categoryName?: string | null;
|
|
spec?: string | null;
|
|
branchCode?: string | null;
|
|
useAi?: boolean;
|
|
}): Promise<TaxCodeSuggestion> {
|
|
const skuName = String(input.skuName || '').trim();
|
|
const categoryName = String(input.categoryName || '').trim() || '(无分类)';
|
|
const spec = String(input.spec || '').trim();
|
|
const useAi = input.useAi !== false;
|
|
|
|
if (!skuName && categoryName === '(无分类)') {
|
|
return emptySuggestion();
|
|
}
|
|
|
|
const searchQuery = [skuName, coreSkuName(skuName), categoryName !== '(无分类)' ? categoryName : '']
|
|
.filter(Boolean)
|
|
.join(' ');
|
|
const catalogCandidates = searchTaxCatalog(searchQuery || skuName || categoryName, 15).map((item) =>
|
|
toCandidate(item, item.score, 'official_search', 'official_catalog_search')
|
|
);
|
|
|
|
const ruleResult = suggestTaxCodeByRules({ skuName, categoryName });
|
|
if (ruleResult.matched && ruleResult.source === 'rule_exact') {
|
|
return {
|
|
...ruleResult,
|
|
candidates: [
|
|
toCandidate(
|
|
{
|
|
tax_code: ruleResult.tax_code!,
|
|
name: ruleResult.tax_name!,
|
|
short_name: ruleResult.short_name || ruleResult.tax_name!,
|
|
norm: '',
|
|
short_norm: '',
|
|
},
|
|
ruleResult.confidence,
|
|
ruleResult.source!,
|
|
ruleResult.reason || ''
|
|
),
|
|
...catalogCandidates.filter((item) => item.tax_code !== ruleResult.tax_code),
|
|
].slice(0, 8),
|
|
};
|
|
}
|
|
|
|
if (skuName) {
|
|
try {
|
|
const official = await lookupOfficialTaxCode({
|
|
sku_name: skuName,
|
|
category_name: categoryName,
|
|
spec,
|
|
});
|
|
if (official?.matched && official.tax_code) {
|
|
const entry =
|
|
getTaxCatalogEntryByCode(official.tax_code) ||
|
|
({
|
|
tax_code: official.tax_code,
|
|
name: official.tax_name || official.tax_code,
|
|
short_name: official.short_name || official.tax_name || official.tax_code,
|
|
norm: '',
|
|
short_norm: '',
|
|
} as TaxCatalogEntry);
|
|
return buildSuggestion(
|
|
entry,
|
|
'official_api',
|
|
official.confidence,
|
|
official.message || 'official_api',
|
|
catalogCandidates
|
|
);
|
|
}
|
|
} catch {
|
|
// 官方接口不可用时继续走本地库与 AI
|
|
}
|
|
}
|
|
|
|
const topCatalog = catalogCandidates[0];
|
|
if (topCatalog && topCatalog.score >= 78) {
|
|
const entry = {
|
|
tax_code: topCatalog.tax_code,
|
|
name: topCatalog.tax_name,
|
|
short_name: topCatalog.short_name,
|
|
norm: '',
|
|
short_norm: '',
|
|
};
|
|
return buildSuggestion(
|
|
entry,
|
|
'official_search',
|
|
topCatalog.score,
|
|
'official_catalog_search',
|
|
catalogCandidates.slice(1)
|
|
);
|
|
}
|
|
|
|
if (ruleResult.matched && ruleResult.confidence >= 80) {
|
|
return {
|
|
...ruleResult,
|
|
candidates: [
|
|
toCandidate(
|
|
{
|
|
tax_code: ruleResult.tax_code!,
|
|
name: ruleResult.tax_name!,
|
|
short_name: ruleResult.short_name || ruleResult.tax_name!,
|
|
norm: '',
|
|
short_norm: '',
|
|
},
|
|
ruleResult.confidence,
|
|
ruleResult.source!,
|
|
ruleResult.reason || ''
|
|
),
|
|
...catalogCandidates.filter((item) => item.tax_code !== ruleResult.tax_code),
|
|
].slice(0, 8),
|
|
};
|
|
}
|
|
|
|
if (useAi && skuName && catalogCandidates.length) {
|
|
const aiResult = await suggestTaxCodeWithAi(
|
|
{ skuName, categoryName, spec },
|
|
catalogCandidates.map((item) => ({
|
|
tax_code: item.tax_code,
|
|
name: item.tax_name,
|
|
short_name: item.short_name,
|
|
norm: '',
|
|
short_norm: '',
|
|
score: item.score,
|
|
})),
|
|
{ branchCode: input.branchCode }
|
|
);
|
|
|
|
if (aiResult.matched && aiResult.tax_code) {
|
|
return buildSuggestion(
|
|
{
|
|
tax_code: aiResult.tax_code,
|
|
name: aiResult.tax_name || aiResult.tax_code,
|
|
short_name: aiResult.short_name || aiResult.tax_name || aiResult.tax_code,
|
|
norm: '',
|
|
short_norm: '',
|
|
},
|
|
'ai',
|
|
aiResult.confidence,
|
|
aiResult.rationale || 'ai_pick',
|
|
catalogCandidates
|
|
);
|
|
}
|
|
}
|
|
|
|
if (ruleResult.matched) {
|
|
return {
|
|
...ruleResult,
|
|
candidates: [
|
|
toCandidate(
|
|
{
|
|
tax_code: ruleResult.tax_code!,
|
|
name: ruleResult.tax_name!,
|
|
short_name: ruleResult.short_name || ruleResult.tax_name!,
|
|
norm: '',
|
|
short_norm: '',
|
|
},
|
|
ruleResult.confidence,
|
|
ruleResult.source!,
|
|
ruleResult.reason || ''
|
|
),
|
|
...catalogCandidates.filter((item) => item.tax_code !== ruleResult.tax_code),
|
|
].slice(0, 8),
|
|
};
|
|
}
|
|
|
|
if (catalogCandidates.length) {
|
|
const first = catalogCandidates[0];
|
|
return {
|
|
matched: true,
|
|
tax_code: first.tax_code,
|
|
tax_name: first.tax_name,
|
|
short_name: first.short_name,
|
|
source: 'official_search',
|
|
confidence: first.score,
|
|
reason: 'official_catalog_fallback',
|
|
catalog_version: getTaxCatalogVersion(),
|
|
needs_confirmation: true,
|
|
candidates: catalogCandidates.slice(0, 8),
|
|
};
|
|
}
|
|
|
|
return emptySuggestion();
|
|
}
|
|
|
|
export { searchTaxCatalog, getTaxCatalogVersion };
|