using Castle.Core.Internal; using DevExpress.Dialogs.Core.View; using DevExpress.XtraSpreadsheet.Commands; namespace HStation.WinFrmUI { public class AdaptingAutoMatchingHelper { public static T AutoMatching(T input, List adaptingManageVmos) where T : AdaptingViewModel { Vmo.AdaptingManageVmo vmo = null; int firstCount = 0; const double speedTolerance = 10.0; // 绝对匹配 var absoluteMatch = adaptingManageVmos.Where(i => i.Caliber == input.Caliber && 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) <= speedTolerance; } 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.LossCoefficient = vmo.Coefficient; 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; } } }