//! AI资产估值引擎 //! //! 整合所有模块,提供完整的估值服务 use rust_decimal::Decimal; use anyhow::{Result, Context}; use crate::{ Asset, Jurisdiction, InternationalAgreement, AIModelManager, Arbitrator, DynamicWeightCalculator, FinalValuationResult, ArbitrationConfig, }; /// 估值引擎配置 #[derive(Debug, Clone)] pub struct ValuationEngineConfig { /// XTZH当前价格(USD) pub xtzh_price_usd: Decimal, /// 仲裁配置 pub arbitration_config: ArbitrationConfig, } impl Default for ValuationEngineConfig { fn default() -> Self { Self { xtzh_price_usd: Decimal::new(100, 0), // 默认100 USD arbitration_config: ArbitrationConfig::default(), } } } /// AI资产估值引擎 pub struct ValuationEngine { ai_manager: AIModelManager, arbitrator: Arbitrator, config: ValuationEngineConfig, } impl ValuationEngine { /// 创建新的估值引擎 pub fn new( chatgpt_api_key: String, deepseek_api_key: String, doubao_api_key: String, config: ValuationEngineConfig, ) -> Result { let ai_manager = AIModelManager::new( chatgpt_api_key, deepseek_api_key, doubao_api_key, )?; let arbitrator = Arbitrator::new(config.arbitration_config.clone()); Ok(Self { ai_manager, arbitrator, config, }) } /// 执行完整的资产估值流程 pub async fn appraise( &self, asset: &Asset, jurisdiction: Jurisdiction, agreement: InternationalAgreement, ) -> Result { log::info!( "开始估值: 资产ID={}, 辖区={:?}, 协定={:?}", asset.id, jurisdiction, agreement ); // 1. 计算动态权重 let weights = DynamicWeightCalculator::calculate_weights( jurisdiction, asset.asset_type, ); log::debug!("动态权重: {:?}", weights); // 2. 并行调用所有AI模型 let ai_results = self.ai_manager.appraise_all( asset, jurisdiction, agreement, self.config.xtzh_price_usd, ).await.context("AI模型估值失败")?; log::info!("收到 {} 个AI模型估值结果", ai_results.len()); // 3. 执行协同仲裁 let final_result = self.arbitrator.arbitrate(ai_results, weights) .context("协同仲裁失败")?; log::info!( "估值完成: {} XTZH (置信度: {:.1}%, 需要人工审核: {})", final_result.valuation_xtzh, final_result.confidence * 100.0, final_result.requires_human_review ); Ok(final_result) } /// 批量估值 pub async fn appraise_batch( &self, assets: Vec<(Asset, Jurisdiction, InternationalAgreement)>, ) -> Result>> { let mut results = Vec::new(); for (asset, jurisdiction, agreement) in assets { let result = self.appraise(&asset, jurisdiction, agreement).await; results.push(result); } Ok(results) } /// 更新XTZH价格 pub fn update_xtzh_price(&mut self, xtzh_price_usd: Decimal) { self.config.xtzh_price_usd = xtzh_price_usd; log::info!("XTZH价格已更新: {} USD", xtzh_price_usd); } /// 获取当前XTZH价格 pub fn get_xtzh_price(&self) -> Decimal { self.config.xtzh_price_usd } } #[cfg(test)] mod tests { use super::*; use crate::AssetType; #[tokio::test] #[ignore] // 需要真实的API密钥 async fn test_valuation_engine() { let engine = ValuationEngine::new( "test_chatgpt_key".to_string(), "test_deepseek_key".to_string(), "test_doubao_key".to_string(), ValuationEngineConfig::default(), ).unwrap(); let asset = Asset::new( "test_asset".to_string(), AssetType::RealEstate, "GNACS-001".to_string(), "Test Property".to_string(), Decimal::new(1000000, 0), "USD".to_string(), ); let result = engine.appraise( &asset, Jurisdiction::US, InternationalAgreement::WTO, ).await; assert!(result.is_ok()); } }