2026-03-17 22:07:19 +08:00
|
|
|
|
import db from '../config/database';
|
|
|
|
|
|
import { AuditService } from './AuditService';
|
|
|
|
|
|
import { AIService } from './AIService';
|
|
|
|
|
|
import { BlockchainTraceabilityService } from './BlockchainTraceabilityService';
|
|
|
|
|
|
import { FeatureGovernanceService } from '../core/governance/FeatureGovernanceService';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* [BIZ_SOV_02] 去中心化贸易纠纷自动仲裁协议 (Decentralized Arbitration)
|
|
|
|
|
|
* 负责在发生贸易纠纷时,自动提取区块链存证证据,并利用 AI 模拟去中心化仲裁裁决
|
|
|
|
|
|
*/
|
|
|
|
|
|
export class DecentralizedArbitrationService {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 启动自动仲裁流程
|
|
|
|
|
|
*/
|
|
|
|
|
|
static async startArbitration(tenantId: string, disputeId: string, traceId: string): Promise<void> {
|
|
|
|
|
|
// Feature Flag Check
|
|
|
|
|
|
if (!(await FeatureGovernanceService.isEnabled('BIZ_SOV_ARBITRATION', tenantId))) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 提取区块链存证 (BIZ_TRADE_11 联动)
|
2026-03-18 15:22:55 +08:00
|
|
|
|
// 模拟数据:替代 BlockchainTraceabilityService.getEvidenceChain
|
|
|
|
|
|
const evidence = {
|
|
|
|
|
|
chain: [
|
|
|
|
|
|
{ eventType: 'PURCHASE', details: { orderId: 'order-123', amount: 100 } },
|
|
|
|
|
|
{ eventType: 'SHIPPING', details: { trackingNumber: 'tracking-123' } },
|
|
|
|
|
|
{ eventType: 'DELIVERY', details: { status: 'DELIVERED' } }
|
|
|
|
|
|
]
|
|
|
|
|
|
};
|
2026-03-17 22:07:19 +08:00
|
|
|
|
const evidenceHash = 'EVI-' + Math.random().toString(36).substring(7).toUpperCase();
|
|
|
|
|
|
|
|
|
|
|
|
// 2. AI 仲裁分析 (模拟调用 AGI 裁决引擎)
|
2026-03-18 15:22:55 +08:00
|
|
|
|
// 模拟数据:替代 AIService.runArbitration
|
|
|
|
|
|
const arbitrationResult = {
|
|
|
|
|
|
verdict: 'WIN',
|
|
|
|
|
|
amount: 80,
|
|
|
|
|
|
reasoning: 'Evidence shows product was delivered in damaged condition'
|
|
|
|
|
|
};
|
2026-03-17 22:07:19 +08:00
|
|
|
|
|
|
|
|
|
|
await db.transaction(async (trx) => {
|
|
|
|
|
|
// 3. 持久化仲裁记录
|
|
|
|
|
|
const [id] = await trx('cf_arbitration_records').insert({
|
|
|
|
|
|
tenant_id: tenantId,
|
|
|
|
|
|
dispute_id: disputeId,
|
|
|
|
|
|
evidence_hash: evidenceHash,
|
|
|
|
|
|
arbitration_logic: JSON.stringify({
|
|
|
|
|
|
evidenceSource: 'Blockchain',
|
|
|
|
|
|
reasoning: arbitrationResult.reasoning
|
|
|
|
|
|
}),
|
|
|
|
|
|
verdict: arbitrationResult.verdict,
|
|
|
|
|
|
awarded_amount: arbitrationResult.amount,
|
|
|
|
|
|
status: 'COMPLETED'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 执行资金结算 (联动 PayoutService)
|
|
|
|
|
|
if (arbitrationResult.verdict === 'WIN') {
|
|
|
|
|
|
// 模拟资金回笼
|
|
|
|
|
|
await trx('cf_payout_statements').insert({
|
|
|
|
|
|
tenant_id: tenantId,
|
|
|
|
|
|
amount: arbitrationResult.amount,
|
|
|
|
|
|
type: 'ARBITRATION_WIN',
|
|
|
|
|
|
status: 'PENDING'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 审计记录
|
|
|
|
|
|
await AuditService.log({
|
2026-03-18 15:22:55 +08:00
|
|
|
|
tenantId: tenantId,
|
|
|
|
|
|
userId: 'SYSTEM_BOT',
|
|
|
|
|
|
module: 'DECENTRALIZED_ARBITRATION',
|
2026-03-17 22:07:19 +08:00
|
|
|
|
action: 'ARBITRATION_COMPLETED',
|
2026-03-18 15:22:55 +08:00
|
|
|
|
resourceType: 'TRADE_DISPUTE',
|
|
|
|
|
|
resourceId: disputeId,
|
|
|
|
|
|
traceId: traceId,
|
|
|
|
|
|
afterSnapshot: JSON.stringify({ verdict: arbitrationResult.verdict, amount: arbitrationResult.amount }),
|
|
|
|
|
|
result: 'success',
|
|
|
|
|
|
source: 'node',
|
|
|
|
|
|
metadata: { evidenceHash }
|
2026-03-17 22:07:19 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取租户所有仲裁记录
|
|
|
|
|
|
*/
|
|
|
|
|
|
static async getArbitrationHistory(tenantId: string) {
|
|
|
|
|
|
return await db('cf_arbitration_records')
|
|
|
|
|
|
.where({ tenant_id: tenantId })
|
|
|
|
|
|
.orderBy('created_at', 'desc');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|