Files
makemd/server/src/services/SovereignCreditPoolService.ts
wurenzhi 5cfd0c4c89 feat: 实现服务层核心功能与文档更新
refactor(ProductService): 修复createProduct方法和其他方法错误
fix(InventoryAgingService): 修复AGING_THRESHOLD_DAYS引用问题
fix(InventoryService): 修复predictSKUDemand方法
refactor(ChatBotController): 从tsoa风格改为Express风格
fix(CommandCenterController): 修复类型问题
fix(AdAutoService): 修复stock可能为undefined的问题
docs: 更新SERVICE_MAP、DOMAIN_MODEL等架构文档
chore: 启动前端服务(运行在http://localhost:8000)
2026-03-18 12:35:52 +08:00

109 lines
3.5 KiB
TypeScript

import db from '../config/database';
import { AuditService } from './AuditService';
import { SovereigntyIdentityService } from './SovereigntyIdentityService';
import { FeatureGovernanceService } from '../core/governance/FeatureGovernanceService';
/**
* [BIZ_FIN_20] 基于主权身份的全球授信池 (Sovereign Credit Pool)
* 负责根据租户的主权身份 (DID) 与聚合声誉,自动分配并动态调整全球范围内的授信额度
*/
export class SovereignCreditPoolService {
/**
* 初始化租户授信池
*/
static async initializeCreditPool(tenantId: string, traceId: string): Promise<void> {
// Feature Flag Check
if (!(await FeatureGovernanceService.isEnabled('BIZ_FIN_CREDIT_POOL', tenantId))) {
return;
}
const identity = await SovereigntyIdentityService.getIdentity(tenantId);
if (!identity) throw new Error('Sovereignty Identity not found');
// 1. 基于声誉分计算初始乘数 (模拟)
const reputation = JSON.parse(identity.reputation_score);
const multiplier = reputation.fulfillment > 90 ? 1.5 : 1.0;
const initialLimit = 50000 * multiplier;
await db.transaction(async (trx) => {
await trx('cf_sov_credit_pool').insert({
tenant_id: tenantId,
max_credit_limit: initialLimit,
available_credit: initialLimit,
reputation_multiplier: multiplier,
status: 'ACTIVE'
});
// 4. 审计日志
await AuditService.log({
tenantId: tenantId,
userId: 'SYSTEM',
module: 'CREDIT_POOL',
action: 'ESTABLISH_CREDIT_PROFILE',
resourceType: 'FINANCE_CREDIT',
resourceId: tenantId,
traceId: traceId,
afterSnapshot: JSON.stringify({ initialLimit, multiplier }),
result: 'success',
source: 'node',
metadata: { did: identity.did }
});
});
}
/**
* 动态调整授信额度 (BIZ_FIN_20)
*/
static async refreshCreditLimit(tenantId: string, traceId: string): Promise<void> {
const pool = await db('cf_sov_credit_pool').where({ tenant_id: tenantId }).first();
if (!pool) return;
// 1. 获取最新跨平台声誉
await SovereigntyIdentityService.syncCrossPlatformReputation(tenantId, traceId);
const identity = await SovereigntyIdentityService.getIdentity(tenantId);
const reputation = JSON.parse(identity.reputation_score);
// 2. 重新计算乘数
const newMultiplier = reputation.aggregated_market_score > 4.8 ? 2.0 : 1.2;
const newLimit = 50000 * newMultiplier;
await db('cf_sov_credit_pool')
.where({ tenant_id: tenantId })
.update({
max_credit_limit: newLimit,
reputation_multiplier: newMultiplier,
updated_at: db.fn.now()
});
await AuditService.log({
tenant_id: tenantId,
action: 'SOV_CREDIT_LIMIT_REFRESHED',
target_type: 'FINANCE_CREDIT',
target_id: tenantId,
trace_id: traceId,
new_data: JSON.stringify({ newLimit, newMultiplier }),
metadata: { score: reputation.aggregated_market_score }
});
}
/**
* 使用授信 (预占)
*/
static async useCredit(tenantId: string, amount: number, traceId: string): Promise<boolean> {
return await db.transaction(async (trx) => {
const pool = await trx('cf_sov_credit_pool')
.where({ tenant_id: tenantId })
.forUpdate()
.first();
if (!pool || pool.available_credit < amount) return false;
await trx('cf_sov_credit_pool')
.where({ tenant_id: tenantId })
.decrement('available_credit', amount);
return true;
});
}
}