Files
makemd/server/src/services/GreenSupplyService.ts

59 lines
2.4 KiB
TypeScript
Raw Normal View History

import db from '../config/database';
import { logger } from '../utils/logger';
import { DecisionExplainabilityEngine } from '../core/ai/DecisionExplainabilityEngine';
export interface CarbonMetric {
routeId: string;
co2PerKg: number;
estimatedDays: number;
}
/**
* [BIZ_OPS_144] 线 (Carbon)
* @description 绿
*/
export class GreenSupplyService {
/**
* (BIZ_OPS_144)
*/
static async recommendLowCarbonRoutes(tenantId: string, orderId: string): Promise<any> {
logger.info(`[GreenSupply] Calculating carbon footprint for Order: ${orderId}, Tenant: ${tenantId}`);
try {
// 1. 获取当前物流选项 (模拟)
const options: CarbonMetric[] = [
{ routeId: 'AIR_EXPRESS', co2PerKg: 15.2, estimatedDays: 3 },
{ routeId: 'SEA_FREIGHT', co2PerKg: 0.8, estimatedDays: 25 },
{ routeId: 'RAIL_WAY', co2PerKg: 2.5, estimatedDays: 15 }
];
// 2. 识别低碳选项 (相比空运节省 > 80% 排放)
const standard = options.find(o => o.routeId === 'AIR_EXPRESS')!;
const greenOption = options.find(o => o.routeId === 'RAIL_WAY')!; // 中欧班列示例
const carbonSavings = standard.co2PerKg - greenOption.co2PerKg;
const advice = `Green Route Recommended: Switching to ${greenOption.routeId} reduces carbon footprint by ${carbonSavings.toFixed(1)}kg CO2 per kg of cargo. ` +
`Trade-off: +${greenOption.estimatedDays - standard.estimatedDays} days delivery time.`;
// 3. [UX_XAI_01] 记录决策证据链
await DecisionExplainabilityEngine.logDecision({
tenantId,
module: 'ESG_GOVERNANCE',
resourceId: orderId,
decisionType: 'LOW_CARBON_ROUTE_RECOMMENDATION',
causalChain: advice,
factors: [
{ name: 'CarbonSavings', value: carbonSavings, weight: 0.8, impact: 'POSITIVE' },
{ name: 'DeliveryDelay', value: greenOption.estimatedDays - standard.estimatedDays, weight: 0.2, impact: 'NEGATIVE' }
],
traceId: 'green-supply-' + Date.now()
});
return { success: true, greenOption, advice, status: 'PENDING_REVIEW' };
} catch (err: any) {
logger.error(`[GreenSupply][WARN] Calculation failed: ${err.message}`);
return { success: false, error: err.message };
}
}
}