namespace HStation.WinFrmUI { public class AsstesAutoMatchingHelper { public static T AutoMatching(T input, List adaptingManageVmos) where T : AdaptingViewModel { Vmo.AdaptingManageVmo vmo = null; int firstCount = 0; //口径最小差值 const double caliberTolerance = 10.0; // 绝对匹配 var absoluteMatch = adaptingManageVmos.Where(i => ((input.Caliber == null && i.Caliber == "默认") || i.Caliber == input.Caliber) && ((input.Material == null && i.Material == "默认") || i.Material == input.Material)).ToList(); if (absoluteMatch.Any()) { foreach (var range in absoluteMatch) { int commonCount = CountCommonCharacters(input.ModelType, range.Name); if (commonCount > firstCount) { vmo = range; firstCount = commonCount; } } } else { double inputCaliber; if (!double.TryParse(input.Caliber, out inputCaliber)) { return null; } //区间匹配 var rangeMatch = adaptingManageVmos.Where(item => { double itemCaliber; if (double.TryParse(item.Caliber, out itemCaliber)) { return Math.Abs(itemCaliber - inputCaliber) <= caliberTolerance; } return false; }) .ToList(); if (rangeMatch != null) { foreach (var range in rangeMatch) { //以材料为条件开始匹配 int commonCount = CountCommonCharacters(input.Material, range.Material); if (commonCount > firstCount) { vmo = range; firstCount = commonCount; } } } } //口径和材料都没有匹配上,就用型号名匹配 if (vmo == null) { foreach (var item in adaptingManageVmos) { int commonCount = CountCommonCharacters(input.ModelType, item.Name); if (commonCount > firstCount) { vmo = item; firstCount = commonCount; } } } if (vmo != null) { input.MinorLoss = vmo.Coefficient; return input; } return null; } //泵匹配 public static PumpMatchingViewModel PumpMatching(PumpMatchingViewModel viewModel, List pumpMainVmos) { const double speedTolerance = 100; const double flowTolerance = 10; const double headTolerance = 5; const double powerTolerance = 0.05; Vmo.PumpMainVmo vmo = null; int firstCount = 0; // 尝试绝对匹配 var absoluteMatch = pumpMainVmos.Where(item => (viewModel.RatedN == null || viewModel.RatedN == item.RatedSpeed) && (viewModel.RatedQ == null || viewModel.RatedQ == item.RatedFlow) && (viewModel.RatedH == null || viewModel.RatedH == item.RatedHead) && (viewModel.RatedP == item.RatedPower)).ToList(); if (absoluteMatch != null && absoluteMatch.Count != 0) { foreach (var item in absoluteMatch) { int commonCount = CountCommonCharacters(viewModel.ModelType, item.Name); if (commonCount > firstCount) { vmo = item; firstCount = commonCount; } } } else { // 尝试区间匹配 var rangeMatch = pumpMainVmos.Where(item => (viewModel.RatedN.HasValue ? Math.Abs(viewModel.RatedN.Value - item.RatedSpeed) <= speedTolerance : true) && (viewModel.RatedQ.HasValue ? Math.Abs(viewModel.RatedQ.Value - item.RatedFlow) <= flowTolerance : true) && (viewModel.RatedH.HasValue ? Math.Abs(viewModel.RatedH.Value - item.RatedHead) <= headTolerance : true) && (Math.Abs(viewModel.RatedP - item.RatedPower) <= powerTolerance)).ToList(); if (rangeMatch != null && rangeMatch.Count != 0) { foreach (var item in rangeMatch) { int commonCount = CountCommonCharacters(viewModel.ModelType, item.Name); if (commonCount > firstCount) { vmo = item; firstCount = commonCount; } } } } return new PumpMatchingViewModel(); } //管道匹配 public static PipeLineMatchingViewModel AutoMatching(PipeLineMatchingViewModel input, List adaptingManageVmos) { Vmo.PipeLineManageVmo vmo = null; int StartCount = 0; //口径最小差值 const double caliberTolerance = 10.0; // 绝对匹配 var absoluteMatch = adaptingManageVmos.Where(i => ((input.Caliber == null && i.Caliber == "默认") || i.Caliber == input.Caliber) && ((input.Material == null && i.Material == "默认") || i.Material == input.Material)).ToList(); if (absoluteMatch.Any()) { foreach (var range in absoluteMatch) { int commonCount = CountCommonCharacters(input.ModelType, range.Name); if (commonCount > StartCount) { vmo = range; StartCount = commonCount; } } } else { double inputCaliber; if (!double.TryParse(input.Caliber, out inputCaliber)) { return null; } //区间匹配 var rangeMatch = adaptingManageVmos.Where(item => { double itemCaliber; if (double.TryParse(item.Caliber, out itemCaliber)) { return Math.Abs(itemCaliber - inputCaliber) <= caliberTolerance; } return false; }) .ToList(); if (rangeMatch != null) { foreach (var range in rangeMatch) { //以材料为条件开始匹配 int commonCount = CountCommonCharacters(input.Material, range.Material); if (commonCount > StartCount) { vmo = range; StartCount = commonCount; } } } } //口径和材料都没有匹配上,就用型号名匹配 if (vmo == null) { foreach (var item in adaptingManageVmos) { int commonCount = CountCommonCharacters(input.ModelType, item.Name); if (commonCount > StartCount) { vmo = item; StartCount = commonCount; } } } if (vmo != null) { switch (input.eAlgorithmType) { case HStation.Assets.eAlgorithmType.Hazen: input.MinorLoss = vmo.Hazen; return input; case HStation.Assets.eAlgorithmType.Manning: input.MinorLoss = vmo.Manning; return input; case HStation.Assets.eAlgorithmType.Darcy: input.MinorLoss = vmo.Darcy; return input; default: input.MinorLoss = vmo.Hazen; return input; } } return null; } //返回两个字符串之间相同的字符 private static int CountCommonCharacters(string baseString, string comparisonString) { // 将字符串转换为字符集合 HashSet baseChars = new HashSet(baseString); HashSet comparisonChars = new HashSet(comparisonString); // 计算两个集合的交集 int commonCount = baseChars.Intersect(comparisonChars).Count(); return commonCount; } } }