/** * 多因子综合评价组件 * 提供地块适宜性评价、权重配置、批量分析和作物推荐功能 */ 'use client'; import { useState, useEffect } from 'react'; import { Card } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Input } from '@/components/ui/input'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogTrigger } from '@/components/ui/dialog'; import { Progress } from '@/components/ui/progress'; import { Slider } from '@/components/ui/slider'; import { Textarea } from '@/components/ui/textarea'; import { Leaf, TrendingUp, Award, AlertTriangle, CheckCircle2, Play, Settings, Download, Eye, Calculator, Database, RefreshCw, Zap, Target, Droplet, Cloud, Sun, ThermometerSun, BookOpen, Beaker, Info, BarChart3, Filter } from 'lucide-react'; import { toast } from 'sonner'; import { EvaluationFactor, SuitabilityResult, FactorWeight, BatchProgress, getGradeColor, getScoreColor, getSuitabilityLevelColor, formatDate, MOCK_FIELDS } from './multiFactorTypes'; import { MultiFactorService } from './multiFactorService'; import { matchCropsForField, cropKnowledgeBase } from './cropKnowledgeBase'; export function MultiFactorEvaluation() { const [selectedField, setSelectedField] = useState('field-1'); const [showWeightConfig, setShowWeightConfig] = useState(false); const [showKnowledgeBase, setShowKnowledgeBase] = useState(false); const [batchProgress, setBatchProgress] = useState({ total: 0, processed: 0, highSuitability: 0, mediumSuitability: 0, lowSuitability: 0, currentField: '', }); const [isBatchRunning, setIsBatchRunning] = useState(false); const [batchAnalysisResults, setBatchAnalysisResults] = useState([]); // 评价因子权重配置 const [factorWeights, setFactorWeights] = useState([ { id: 'ph', name: 'pH值', weight: 20, unit: '' }, { id: 'organic', name: '有机质含量', weight: 25, unit: 'g/kg' }, { id: 'depth', name: '土层厚度', weight: 20, unit: 'cm' }, { id: 'nitrogen', name: '全氮', weight: 10, unit: 'g/kg' }, { id: 'phosphorus', name: '全磷', weight: 10, unit: 'g/kg' }, { id: 'potassium', name: '全钾', weight: 10, unit: 'g/kg' }, { id: 'drainage', name: '排水性', weight: 5, unit: '' }, ]); // 模拟适宜性评价结果 const [evaluationResults, setEvaluationResults] = useState( MultiFactorService.generateMockEvaluationData() ); // 获取当前选中的地块结果 const currentResult = evaluationResults.find(r => r.fieldId === selectedField) || batchAnalysisResults.find(r => r.fieldId === selectedField) || evaluationResults[0]; // 批量分析处理函数 const handleRunBatchAnalysis = async () => { const validation = MultiFactorService.validateWeights(factorWeights); if (!validation.isValid) { toast.error(`权重总和必须为100%才能进行批量分析(当前:${validation.totalWeight}%)`); return; } setIsBatchRunning(true); setBatchProgress({ total: 68, processed: 0, highSuitability: 0, mediumSuitability: 0, lowSuitability: 0, currentField: '', }); setBatchAnalysisResults([]); toast.success('开始批量分析,正在读取地块数据...'); try { const results = await MultiFactorService.runBatchAnalysis( factorWeights, (progress) => { setBatchProgress(progress); } ); setBatchAnalysisResults(results); setIsBatchRunning(false); toast.success(`批量分析完成!已为${results.length}个地块生成适宜性评价结果`); } catch (error) { setIsBatchRunning(false); toast.error('批量分析失败'); } }; const handleUpdateWeight = (id: string, newWeight: number) => { setFactorWeights(prev => MultiFactorService.updateWeight(prev, id, newWeight) ); }; const handleResetWeights = () => { setFactorWeights(MultiFactorService.resetWeights()); toast.success('权重已恢复默认值'); }; const handleApplyPreset = (preset: 'grain' | 'economic' | 'default') => { setFactorWeights(MultiFactorService.getWeightPreset(preset)); const presetName = preset === 'grain' ? '粮食作物' : preset === 'economic' ? '经济作物' : '默认'; toast.success(`已应用${presetName}权重方案`); }; const totalWeight = factorWeights.reduce((sum, f) => sum + f.weight, 0); // 执行地块适宜性评价 const handleEvaluateField = () => { const validation = MultiFactorService.validateWeights(factorWeights); if (!validation.isValid) { toast.error(`权重总和必须为100%才能进行评价(当前:${validation.totalWeight}%)`); return; } try { const result = MultiFactorService.generateEvaluationResult(selectedField, factorWeights); setEvaluationResults(prev => prev.map(r => r.fieldId === selectedField ? result : r) ); toast.success('评价完成!已应用当前权重配置计算综合得分'); } catch (error) { toast.error('评价失败'); } }; const exportResults = () => { const resultsToExport = batchAnalysisResults.length > 0 ? batchAnalysisResults : evaluationResults; toast.success('正在导出评价结果...'); // 模拟导出功能 setTimeout(() => { toast.success(`已导出${resultsToExport.length}个地块的评价结果`); }, 2000); }; return (

多因子综合评价

地块适宜性评价、自动化空间分析与作物适配推荐

{/* 多因子综合评价 */}
{/* 评价结果总览 */}

综合评分

{currentResult.totalScore}

{currentResult.grade}

优秀因子

{currentResult.factors.filter(f => f.score >= 80).length}

/ {currentResult.factors.length} 项

待改善因子

{currentResult.factors.filter(f => f.score < 70).length}

需要优化

评价时间

{formatDate(currentResult.timestamp)}

最近更新

{/* 因子详细评分 */}

评价因子详细分析

{currentResult.factors.map((factor) => (
{factor.name} 权重: {factor.weight}%
实际值: {factor.value.toFixed(1)}{factor.unit} 最佳范围: {factor.optimalRange[0]}-{factor.optimalRange[1]}{factor.unit} {factor.score}分
))}
{/* 加权计算说明 */}

层次分析法(AHP)加权计算

计算公式:

总分 = Σ(因子得分 × 因子权重)

= ({currentResult.factors.map((f, i) => `${f.score} × ${f.weight}%${i < currentResult.factors.length - 1 ? ' + ' : ''}` ).join('')})

= {currentResult.totalScore}分

高度适宜 (≥80分)

  • • 土壤条件优秀
  • • 适合多种作物
  • • 预期产量高

一般适宜 (60-79分)

  • • 部分因子需改善
  • • 可种植耐性作物
  • • 需适当改良

不适宜 (<60分)

  • • 土壤条件较差
  • • 需大幅改良
  • • 风险较高
{/* 多因子综合评价功能说明 */}

多因子综合评价功能说明:

  • 关键指标整合: 整合pH值、有机质含量、土层厚度、全氮、全磷、全钾、排水性等7项关键土壤指标
  • 层次分析法(AHP): 采用层次分析法构建加权评分体系,公式:总分 = Σ(因子得分 × 因子权重)
  • 单因子评分: 根据实际值与最佳范围的接近程度计算各因子得分(0-100分),范围内得分85-100分
  • 自定义权重: 支持手动调整各指标权重,提供粮食作物和经济作物两种预设方案
  • 综合得分: 输出0-100分的适宜性评分,自动计算加权总分
  • 三级分级: 高度适宜(≥80分)、一般适宜(60-79分)、不适宜(<60分)
  • 可视化展示: 提供进度条、颜色标识等多维度可视化,直观展示各因子评分与最佳范围对比
  • 改善建议: 自动识别待改善因子,为土壤改良提供决策依据