refactor: 优化服务层代码并修复类型问题 docs: 更新开发进度文档 feat(merchant): 新增商户监控与数据统计服务 feat(dashboard): 添加商户管理前端页面与服务 fix: 修复类型转换与可选参数处理 feat: 实现商户订单、店铺与结算管理功能 refactor: 重构审计日志格式与服务调用 feat: 新增商户入驻与身份注册功能 fix(controller): 修复路由参数类型问题 feat: 添加商户排名与统计报告功能 chore: 更新模拟数据与服务配置
104 lines
3.5 KiB
TypeScript
104 lines
3.5 KiB
TypeScript
import db from '../config/database';
|
|
import { AuditService } from './AuditService';
|
|
import { AIService } from './AIService';
|
|
import { SupplierService } from './SupplierService';
|
|
|
|
/**
|
|
* [BIZ_ECO_01] 自治供应链生态系统 (Autonomous Eco)
|
|
* 负责代理自动寻找、评估并签约全球供应商,实现供应链的自我演化
|
|
*/
|
|
export class AutonomousEcoService {
|
|
/**
|
|
* 初始化表结构
|
|
*/
|
|
static async initTable() {
|
|
const hasTable = await db.schema.hasTable('cf_autonomous_eco_contracts');
|
|
if (!hasTable) {
|
|
console.log('📦 Creating cf_autonomous_eco_contracts table...');
|
|
await db.schema.createTable('cf_autonomous_eco_contracts', (table) => {
|
|
table.increments('id').primary();
|
|
table.string('tenant_id', 64).notNullable().index();
|
|
table.string('supplier_id', 64).notNullable();
|
|
table.string('contract_hash', 128).notNullable();
|
|
table.json('sla_terms');
|
|
table.string('status', 16).defaultTo('SIGNED');
|
|
table.timestamps(true, true);
|
|
});
|
|
console.log('✅ Table cf_autonomous_eco_contracts created');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 自动寻找并评估新供应商
|
|
*/
|
|
static async discoverAndEvaluateSuppliers(tenantId: string, category: string, traceId: string): Promise<void> {
|
|
// 1. 全网供应商扫描 (模拟调用 AI 供应商库扫描)
|
|
// 模拟数据:替代 AIService.scanGlobalSuppliers
|
|
const candidates = [
|
|
{ id: 'supplier-1', name: 'Global Supplier Inc.' },
|
|
{ id: 'supplier-2', name: 'Quality Manufacturers Ltd.' },
|
|
{ id: 'supplier-3', name: 'Eco-friendly Products Co.' }
|
|
];
|
|
|
|
for (const supplier of candidates) {
|
|
// 2. 深度风险评估 (BIZ_SC_14 Risk Radar 联动)
|
|
// 模拟数据:替代 AIService.evaluateSupplierRisk
|
|
const riskScore = Math.random() * 0.5; // 生成 0-0.5 之间的风险分数
|
|
|
|
if (riskScore < 0.3) { // 风险低于 0.3 视为优质
|
|
await db.transaction(async (trx) => {
|
|
// 3. 自动生成并签约 SLA (智能合约模拟)
|
|
const contractHash = 'HASH-' + Math.random().toString(36).substring(7).toUpperCase();
|
|
const slaTerms = {
|
|
deliveryLeadTime: 7,
|
|
qualityPassRate: 0.99,
|
|
paymentTerm: 'NET_30'
|
|
};
|
|
|
|
await trx('cf_autonomous_eco_contracts').insert({
|
|
tenant_id: tenantId,
|
|
supplier_id: supplier.id,
|
|
contract_hash: contractHash,
|
|
sla_terms: JSON.stringify(slaTerms),
|
|
status: 'SIGNED'
|
|
});
|
|
|
|
// 4. 将供应商加入可用列表
|
|
await trx('cf_suppliers').insert({
|
|
tenant_id: tenantId,
|
|
supplier_id: supplier.id,
|
|
name: supplier.name,
|
|
category,
|
|
rating: 5,
|
|
status: 'ACTIVE'
|
|
});
|
|
|
|
// 审计记录
|
|
await AuditService.log({
|
|
tenantId: tenantId,
|
|
userId: 'SYSTEM_BOT',
|
|
module: 'AUTONOMOUS_ECO',
|
|
action: 'AUTONOMOUS_SUPPLIER_SIGNED',
|
|
resourceType: 'SUPPLIER',
|
|
resourceId: supplier.id,
|
|
traceId: traceId,
|
|
afterSnapshot: JSON.stringify({ contractHash, slaTerms }),
|
|
result: 'success',
|
|
source: 'node',
|
|
metadata: { category }
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取租户所有自治签约的合同
|
|
*/
|
|
static async getContracts(tenantId: string) {
|
|
return await db('cf_autonomous_eco_contracts')
|
|
.where({ tenant_id: tenantId })
|
|
.orderBy('created_at', 'desc');
|
|
}
|
|
}
|