package service import "math" // RiskInput 风险评分输入(各系数取值 0~1) type RiskInput struct { AI float64 // AI 识别置信度(权重 0.5) Env float64 // 环境风险系数(权重 0.2) Stage float64 // 饲养阶段风险系数(权重 0.15) Uniformity float64 // 群体整齐度偏离度(权重 0.15) } // ComputeRiskScore 按规格书 3.1.4 公式计算 0~100 风险分: // 风险分 = 0.5×AI置信度 + 0.2×环境系数 + 0.15×阶段系数 + 0.15×整齐度偏离度 func ComputeRiskScore(in RiskInput) float64 { score := 0.5*in.AI + 0.2*in.Env + 0.15*in.Stage + 0.15*in.Uniformity score = math.Max(0, math.Min(1, score)) return score * 100 } // RiskLevel 按规格书 3.1.4 分级:绿 0-30 / 黄 31-60 / 橙 61-80 / 红 81-100 func RiskLevel(score float64) string { switch { case score <= 30: return "green" case score <= 60: return "yellow" case score <= 80: return "orange" default: return "red" } } // StageCoefficient 蚕房阶段 → 阶段风险系数(Room.Stage 粗粒度映射; // 待 #7 蚕匾/批次管理的龄期字段落地后细化) func StageCoefficient(stage string) float64 { switch stage { case "pupa": return 0.5 // 核型多角体病 5龄后期至蛹期高发 case "larva": return 0.4 // 软化病 5 龄集中暴发等 case "moth": return 0.2 case "egg": return 0.1 default: return 0 } } // EnvCoefficient 由最新温湿度计算环境风险系数(规则取自规格书 3.2.3) func EnvCoefficient(temp, humidity *float64) float64 { coef := 0.0 if humidity != nil { switch { case *humidity >= 80: coef = math.Max(coef, 0.8) // 白僵病等真菌病高湿条件 case *humidity >= 75: coef = math.Max(coef, 0.5) } } if temp != nil { if *temp > 30 || *temp < 20 { coef = math.Max(coef, 0.5) // 温度突变诱发核型多角体病 } } return coef }