using HStation.Vmo; using System.Windows.Media.Media3D; using Yw.WinFrmUI.Q3d; namespace HStation.WinFrmUI { public class AsstesAutoMatchingHelper { private readonly Lazy _bll_ex = new(); //连接件匹配 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; } else { if (item.Caliber == "默认") { return true; } } return false; }) .ToList(); if (rangeMatch != null && rangeMatch.Count > 0) { var materialList = new List(); foreach (var range in rangeMatch) { //以材料为条件开始匹配 if (range.Material == "默认") { materialList.Add(range); } else { int commonCount = CountCommonCharacters(input.Material, range.Material); if (commonCount > firstCount) { materialList.Add(range); firstCount = commonCount; } } } //用已经筛选完成的列表中以名称筛选 firstCount = 0; foreach (var material in materialList) { int commonCount = CountCommonCharacters(input.ModelType, material.Name); if (commonCount > firstCount) { vmo = material; firstCount = commonCount; } } } } //口径和材料都没有匹配上,就用型号名匹配 firstCount = 0; 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.AlterMinorLoss = vmo.Coefficient; input.MatchingType = Xhs.eMatchingType.Success; return input; } return null; } //阀门匹配 public static ValveMatchingViewModel ValveMatching(ValveMatchingViewModel input, List adaptingManageVmos) { HStation.Vmo.ValveMainVmo 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; } else { if (item.Caliber == "默认") { return true; } } return false; }) .ToList(); if (rangeMatch != null && rangeMatch.Count > 0) { var materialList = new List(); foreach (var range in rangeMatch) { //以材料为条件开始匹配 if (range.Material == "默认") { materialList.Add(range); } else { int commonCount = CountCommonCharacters(input.Material, range.Material); if (commonCount > firstCount) { materialList.Add(range); firstCount = commonCount; } } } //用已经筛选完成的列表中以名称筛选 firstCount = 0; foreach (var material in materialList) { int commonCount = CountCommonCharacters(input.ModelType, material.Name); if (commonCount > firstCount) { vmo = material; firstCount = commonCount; } } } } //口径和材料都没有匹配上,就用型号名匹配 firstCount = 0; 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.AlterMinorLoss = vmo.Coefficient; input.MatchingType = Xhs.eMatchingType.Success; return input; } return null; } //泵匹配 public static async Task 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 startCount = 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 > startCount) { vmo = item; startCount = 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 > startCount) { vmo = item; startCount = commonCount; } } } } // if (vmo == null) { foreach (var item in pumpMainVmos) { int commonCount = CountCommonCharacters(viewModel.ModelType, item.Name); if (commonCount > startCount) { vmo = item; startCount = commonCount; } } } if (vmo != null) { viewModel.AlterRatedH = vmo.RatedHead; viewModel.AlterRatedN = vmo.RatedSpeed; viewModel.AlterRatedQ = vmo.RatedFlow; viewModel.AlterRatedP = vmo.RatedPower; viewModel.AlterDbID = vmo.ID.ToString(); var list = await new BLL.XhsPumpMainPhartMappingExtensions().GetByPumpMainID(vmo.ID); if (list != null && list.Count > 0) { viewModel.AlterChartDbID = list.First().ID; } } return null; } //管道匹配 public static PipeLineMatchingViewModel PipeAutoMatching(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.AlterMinorLoss = vmo.Hazen; return input; case HStation.Assets.eAlgorithmType.Manning: input.AlterMinorLoss = vmo.Manning; return input; case HStation.Assets.eAlgorithmType.Darcy: input.AlterMinorLoss = vmo.Darcy; return input; default: input.AlterMinorLoss = vmo.Hazen; return input; } } return null; } //返回两个字符串之间相同的字符个数 private static int CountCommonCharacters(string baseString, string compareString) { // 将字符串转换为字符集合 HashSet baseChars = new HashSet(baseString); HashSet comparisonChars = new HashSet(compareString); // 计算两个集合的交集 int commonCount = baseChars.Intersect(comparisonChars).Count(); return commonCount; } } }