2026-03-17 22:07:19 +08:00
|
|
|
|
import db from '../config/database';
|
|
|
|
|
|
import { AuditService } from './AuditService';
|
|
|
|
|
|
import { AIService } from './AIService';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* [BIZ_FIN_16] 租户级税务优惠政策自动发现
|
|
|
|
|
|
* 负责扫描全球各地区的最新税务优惠(如 IOSS 减免、特定类目退税等),并自动提醒租户
|
|
|
|
|
|
*/
|
|
|
|
|
|
export class TaxIncentivesService {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 自动发现税务优惠
|
|
|
|
|
|
*/
|
|
|
|
|
|
static async discoverIncentives(tenantId: string, countryCode: string, traceId: string): Promise<void> {
|
|
|
|
|
|
// 1. 获取该地区的最新税务政策 (模拟调用 AI 服务分析全球税法库)
|
2026-03-21 15:04:06 +08:00
|
|
|
|
const result = await AIService.searchTaxPolicies({
|
|
|
|
|
|
countryCode: countryCode,
|
|
|
|
|
|
keywords: ['tax incentive', 'e-commerce'],
|
|
|
|
|
|
category: 'general'
|
|
|
|
|
|
});
|
2026-03-17 22:07:19 +08:00
|
|
|
|
|
2026-03-21 15:04:06 +08:00
|
|
|
|
for (const policy of result.policies) {
|
2026-03-17 22:07:19 +08:00
|
|
|
|
// 检查是否已记录
|
|
|
|
|
|
const existing = await db('cf_tax_incentives')
|
2026-03-21 15:04:06 +08:00
|
|
|
|
.where({ tenant_id: tenantId, country_code: countryCode, policy_name: policy.title })
|
2026-03-17 22:07:19 +08:00
|
|
|
|
.first();
|
|
|
|
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
|
await db.transaction(async (trx) => {
|
|
|
|
|
|
const [id] = await trx('cf_tax_incentives').insert({
|
|
|
|
|
|
tenant_id: tenantId,
|
|
|
|
|
|
country_code: countryCode,
|
2026-03-21 15:04:06 +08:00
|
|
|
|
policy_name: policy.title,
|
|
|
|
|
|
description: policy.summary,
|
|
|
|
|
|
estimated_savings: policy.relevance * 1000,
|
2026-03-17 22:07:19 +08:00
|
|
|
|
status: 'DISCOVERED'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 审计记录
|
|
|
|
|
|
await AuditService.log({
|
2026-03-21 15:04:06 +08:00
|
|
|
|
tenantId: tenantId,
|
2026-03-17 22:07:19 +08:00
|
|
|
|
action: 'TAX_INCENTIVE_DISCOVERED',
|
2026-03-21 15:04:06 +08:00
|
|
|
|
resourceType: 'TAX_INCENTIVE',
|
|
|
|
|
|
resourceId: id.toString(),
|
|
|
|
|
|
traceId: traceId,
|
|
|
|
|
|
userId: 'SYSTEM',
|
|
|
|
|
|
module: 'TAX_INCENTIVE',
|
|
|
|
|
|
source: 'node',
|
|
|
|
|
|
result: 'success',
|
|
|
|
|
|
afterSnapshot: { policyName: policy.title, relevance: policy.relevance },
|
|
|
|
|
|
metadata: { countryCode }
|
2026-03-17 22:07:19 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取租户所有已发现的优惠
|
|
|
|
|
|
*/
|
|
|
|
|
|
static async getIncentives(tenantId: string) {
|
|
|
|
|
|
return await db('cf_tax_incentives')
|
|
|
|
|
|
.where({ tenant_id: tenantId })
|
|
|
|
|
|
.orderBy('estimated_savings', 'desc');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|