feat: 初始化项目结构并添加核心功能模块
- 新增文档模板和导航结构 - 实现服务器基础API路由和控制器 - 添加扩展插件配置和前端框架 - 引入多租户和权限管理模块 - 集成日志和数据库配置 - 添加核心业务模型和类型定义
This commit is contained in:
103
server/src/services/SovereignCreditPoolService.ts
Normal file
103
server/src/services/SovereignCreditPoolService.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
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'
|
||||
});
|
||||
|
||||
await AuditService.log({
|
||||
tenant_id: tenantId,
|
||||
action: 'SOV_CREDIT_POOL_INIT',
|
||||
target_type: 'FINANCE_CREDIT',
|
||||
target_id: tenantId,
|
||||
trace_id: traceId,
|
||||
new_data: JSON.stringify({ initialLimit, multiplier }),
|
||||
metadata: JSON.stringify({ 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: JSON.stringify({ 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user