//! AI估值适配器 //! //! 调用nac-ai-valuation模块进行多元AI协同估值 use crate::error::{OnboardingError, Result}; use crate::types::{AssetSubmission, ValuationResult}; use nac_ai_valuation::{ValuationEngine, ValuationEngineConfig, Asset, AssetType, Jurisdiction, InternationalAgreement}; use rust_decimal::Decimal; use chrono::Utc; use tracing::{info, error}; /// AI估值适配器 pub struct ValuationAdapter { engine: ValuationEngine, } impl ValuationAdapter { /// 创建新的适配器 pub fn new( chatgpt_key: String, deepseek_key: String, doubao_key: String, ) -> Result { let engine = ValuationEngine::new( chatgpt_key, deepseek_key, doubao_key, ValuationEngineConfig::default(), ) .map_err(|e| OnboardingError::ValuationError(format!("初始化失败: {}", e)))?; Ok(Self { engine }) } /// 执行估值 pub async fn appraise(&self, submission: &AssetSubmission) -> Result { info!("开始AI估值: {}", submission.asset_name); // 构建资产对象 let asset = self.build_asset(submission)?; // 解析辖区 let jurisdiction = self.parse_jurisdiction(&submission.jurisdiction)?; // 默认使用WTO协定 let agreement = InternationalAgreement::WTO; // 执行估值 let result = self.engine.appraise(&asset, jurisdiction, agreement) .await .map_err(|e| OnboardingError::ValuationError(format!("估值失败: {}", e)))?; info!("AI估值完成: {} XTZH (置信度: {:.1}%)", result.valuation_xtzh, result.confidence * 100.0); Ok(ValuationResult { valuation_xtzh: result.valuation_xtzh, confidence: result.confidence, model_results: result.model_results.iter() .map(|r| format!("{:?}", r)) .collect(), timestamp: Utc::now(), }) } /// 构建资产对象 fn build_asset(&self, submission: &AssetSubmission) -> Result { let asset_type = self.parse_asset_type(&submission.asset_type)?; Ok(Asset::new( uuid::Uuid::new_v4().to_string(), asset_type, "GNACS-TEMP".to_string(), // 临时GNACS,后续会被DNA模块替换 submission.asset_name.clone(), Decimal::from_f64_retain(submission.initial_valuation_usd) .ok_or_else(|| OnboardingError::ValuationError("无效的估值".to_string()))?, "USD".to_string(), )) } /// 解析资产类型 fn parse_asset_type(&self, type_str: &str) -> Result { match type_str { "RealEstate" => Ok(AssetType::RealEstate), "Equity" => Ok(AssetType::FinancialAsset), "Bond" => Ok(AssetType::FinancialAsset), "Commodity" => Ok(AssetType::Commodity), "IntellectualProperty" => Ok(AssetType::IntellectualProperty), "Art" => Ok(AssetType::ArtCollectible), "Collectible" => Ok(AssetType::ArtCollectible), "Infrastructure" => Ok(AssetType::Infrastructure), "Vehicle" => Ok(AssetType::Movable), "Equipment" => Ok(AssetType::Movable), "Inventory" => Ok(AssetType::Movable), "Other" => Ok(AssetType::Other), _ => Err(OnboardingError::InvalidParameter(format!("未知的资产类型: {}", type_str))), } } /// 解析辖区 fn parse_jurisdiction(&self, jurisdiction_str: &str) -> Result { match jurisdiction_str { "US" => Ok(Jurisdiction::US), "EU" => Ok(Jurisdiction::EU), "China" => Ok(Jurisdiction::China), "HK" => Ok(Jurisdiction::HongKong), "Singapore" => Ok(Jurisdiction::SG), "UK" => Ok(Jurisdiction::UK), "Japan" => Ok(Jurisdiction::JP), "MiddleEast" => Ok(Jurisdiction::ME), _ => Err(OnboardingError::InvalidParameter(format!("未知的辖区: {}", jurisdiction_str))), } } } #[cfg(test)] mod tests { use super::*; #[test] fn test_parse_asset_type() { let adapter = ValuationAdapter::new( "test".to_string(), "test".to_string(), "test".to_string(), ).expect("mainnet: handle error"); assert!(matches!( adapter.parse_asset_type("RealEstate"), Ok(AssetType::RealEstate) )); } }