using HStation.WinFrmUI.PhartRelation; namespace HStation.WinFrmUI { public class AssetsMatchingHelper { private readonly Lazy _bll_ex = new(); private const double _caliberTolerance = 10.0; private const double _speedTolerance = 100; private const double _flowTolerance = 10; private const double _headTolerance = 5; private const double _powerTolerance = 0.05; //资产自动匹配 public static async Task Matching(AssetsMatchingViewModel assetsAutoMatchingView) { bool IsMaching = false; var pumpMain = new BLL.PumpMain(); var adaptingManage = new BLL.AdaptingManage(); var pipeLineManage = new BLL.PipeLineManage(); var valveMain = new BLL.ValveMain(); var allPump = await pumpMain.GetAll(); var allAdapting = await adaptingManage.GetAll(); var allPipeLine = await pipeLineManage.GetAll(); var allValve = await valveMain.GetAll(); //泵匹配 foreach (var item in assetsAutoMatchingView.PumpMatchingList) { if (await MatchingPumps(item, allPump)) { IsMaching = true; } } //三通匹配 foreach (var item in assetsAutoMatchingView.ThreelinkMatchingList) { if (MatchingThreelink(item, allAdapting)) { IsMaching = true; } } //四通匹配 foreach (var item in assetsAutoMatchingView.FourlinkMatchingList) { if (MatchingFourlink(item, allAdapting)) { IsMaching = true; } } //管道匹配 foreach (var item in assetsAutoMatchingView.PipeMatchingList) { if (MatchingPipe(item, allPipeLine)) { IsMaching = true; } } //阀门匹配 foreach (var item in assetsAutoMatchingView.ValveMatchingList) { if (MatchingValve(item, allValve)) { IsMaching = true; } } //弯头匹配 foreach (var item in assetsAutoMatchingView.ElbowsMatchingList) { if (MatchingElbow(item, allAdapting)) { IsMaching = true; } } return IsMaching; } //泵匹配 public static async Task MatchingPumps(PumpMatchingViewModel InputModel, List pumpMainVmos) { Vmo.PumpMainVmo vmo = null; int startCount = 0; // 尝试绝对匹配 var absoluteMatch = pumpMainVmos.Where(item => (InputModel.RatedN == null || InputModel.RatedN == item.RatedSpeed) && (InputModel.RatedQ == null || InputModel.RatedQ == item.RatedFlow) && (InputModel.RatedH == null || InputModel.RatedH == item.RatedHead) && (InputModel.RatedP == item.RatedPower)).ToList(); if (absoluteMatch != null && absoluteMatch.Count != 0) { foreach (var item in absoluteMatch) { int commonCount = GetIntersect(InputModel.ModelType, item.Name); if (commonCount > startCount) { vmo = item; startCount = commonCount; } } } else { // 尝试区间匹配 var rangeMatch = pumpMainVmos.Where(item => (InputModel.RatedN.HasValue ? Math.Abs(InputModel.RatedN.Value - item.RatedSpeed) <= _speedTolerance : true) && (InputModel.RatedQ.HasValue ? Math.Abs(InputModel.RatedQ.Value - item.RatedFlow) <= _flowTolerance : true) && (InputModel.RatedH.HasValue ? Math.Abs(InputModel.RatedH.Value - item.RatedHead) <= _headTolerance : true) && (Math.Abs(InputModel.RatedP - item.RatedPower) <= _powerTolerance)).ToList(); if (rangeMatch != null && rangeMatch.Count != 0) { foreach (var item in rangeMatch) { int commonCount = GetIntersect(InputModel.ModelType, item.Name); if (commonCount > startCount) { vmo = item; startCount = commonCount; } } } } // if (vmo == null) { foreach (var item in pumpMainVmos) { int commonCount = GetIntersect(InputModel.ModelType, item.Name); if (commonCount > startCount) { vmo = item; startCount = commonCount; } } } if (vmo != null) { InputModel.MatchingRatedH = vmo.RatedHead; InputModel.MatchingRatedN = vmo.RatedSpeed; InputModel.MatchingRatedQ = vmo.RatedFlow; InputModel.MatchingRatedP = vmo.RatedPower; InputModel.MatchingDbId = vmo.ID.ToString(); InputModel.MatchingModelType = vmo.Name; var list = await new BLL.XhsPumpMainPhartMappingExtensions().GetByPumpMainID(vmo.ID); if (list != null && list.Count > 0) { InputModel.MatchingCurveDbId = list.First().ID.ToString(); var graph_qh = list.First().Diagram.GraphList.Find(x => x.GraphType == HStation.PhartRelation.eGraphType.PumpQH); var graph_qe = list.First().Diagram.GraphList.Find(x => x.GraphType == HStation.PhartRelation.eGraphType.PumpQE); var graph_qp = list.First().Diagram.GraphList.Find(x => x.GraphType == HStation.PhartRelation.eGraphType.PumpQP); if (graph_qh != null) { var points_qh = PhartPerformCurveHelper.GetFeatPointList(graph_qh.GraphType, graph_qh.GeometryInfo, 100, null); InputModel.MatchingCurveQH = new List(); foreach (var item in points_qh) { InputModel.MatchingCurveQH.Add(new CurvePointMatchingViewModel(item.X, item.Y)); } } if (graph_qe != null) { var points_qe = PhartPerformCurveHelper.GetFeatPointList(graph_qe.GraphType, graph_qe.GeometryInfo, 100, null); InputModel.MatchingCurveQE = new List(); foreach (var item in points_qe) { InputModel.MatchingCurveQE.Add(new CurvePointMatchingViewModel(item.X, item.Y)); } } if (graph_qp != null) { var points_qp = PhartPerformCurveHelper.GetFeatPointList(graph_qp.GraphType, graph_qp.GeometryInfo, 100, null); InputModel.MatchingCurveQP = new List(); foreach (var item in points_qp) { InputModel.MatchingCurveQP.Add(new CurvePointMatchingViewModel(item.X, item.Y)); } } } return true; } return false; } //阀门匹配 public static bool MatchingValve(ValveMatchingViewModel input, List adaptingManageVmos) { HStation.Vmo.ValveMainVmo vmo = null; int firstCount = 0; //口径最小差值 // 绝对匹配 var absoluteMatch = adaptingManageVmos.Where(i => ((i.Caliber == null) || i.Caliber == input.Diameter) && ((input.Material == null && i.Material == "默认") || i.Material == input.Material)).ToList(); if (absoluteMatch.Any()) { foreach (var range in absoluteMatch) { int commonCount = GetIntersect(input.ModelType, range.Name); if (commonCount > firstCount) { vmo = range; firstCount = commonCount; } } } else { //区间匹配 var rangeMatch = adaptingManageVmos.Where(item => { if (item.Caliber.HasValue) { return Math.Abs(Convert.ToInt64(item.Caliber - input.Diameter)) <= _caliberTolerance; } else { if (item.Caliber == null) { 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 = GetIntersect(input.Material, range.Material); if (commonCount > firstCount) { materialList.Add(range); firstCount = commonCount; } } } //用已经筛选完成的列表中以名称筛选 firstCount = 0; foreach (var material in materialList) { int commonCount = GetIntersect(input.ModelType, material.Name); if (commonCount > firstCount) { vmo = material; firstCount = commonCount; } } } } //口径和材料都没有匹配上,就用型号名匹配 firstCount = 0; if (vmo == null) { foreach (var item in adaptingManageVmos) { int commonCount = GetIntersect(input.ModelType, item.Name); if (commonCount > firstCount) { vmo = item; firstCount = commonCount; } } } if (vmo != null) { input.MatchingMinorLoss = vmo.Coefficient; input.MatchingDbId = vmo.ID.ToString(); input.MatchingDiameter = vmo.Caliber; input.MatchingMaterial = vmo.Material; input.MatchingModelType = vmo.Name; // input.MatchingValveSetting = input.MatchingValveType = vmo.Type.ToString(); return true; } return false; } //管道匹配 public static bool MatchingPipe(PipeMatchingViewModel input, List pipeLineManageVmos) { Vmo.PipeLineManageVmo vmo = null; int StartCount = 0; //口径最小差值 // 绝对匹配 var absoluteMatch = pipeLineManageVmos.Where(i => ((i.Caliber == null) || i.Caliber == input.Diameter) && ((input.Material == null && i.Material == "默认") || i.Material == input.Material)).ToList(); if (absoluteMatch.Any()) { foreach (var range in absoluteMatch) { int commonCount = GetIntersect(input.ModelType, range.Name); if (commonCount > StartCount) { vmo = range; StartCount = commonCount; } } } else { //区间匹配 var rangeMatch = pipeLineManageVmos.Where(item => { if (item.Caliber != null) { return Math.Abs(Convert.ToInt64(item.Caliber - input.Diameter)) <= _caliberTolerance; } return false; }) .ToList(); if (rangeMatch != null) { foreach (var range in rangeMatch) { //以材料为条件开始匹配 int commonCount = GetIntersect(input.Material, range.Material); if (commonCount > StartCount) { vmo = range; StartCount = commonCount; } } } } //口径和材料都没有匹配上,就用型号名匹配 if (vmo == null) { foreach (var item in pipeLineManageVmos) { int commonCount = GetIntersect(input.ModelType, item.Name); if (commonCount > StartCount) { vmo = item; StartCount = commonCount; } } } if (vmo != null) { switch (input.eAlgorithmType) { case HStation.Assets.eAlgorithmType.Hazen: input.MatchingRoughness = vmo.Hazen; break; case HStation.Assets.eAlgorithmType.Manning: input.MatchingRoughness = vmo.Manning; break; case HStation.Assets.eAlgorithmType.Darcy: input.MatchingRoughness = vmo.Darcy; break; default: input.MatchingRoughness = vmo.Hazen; break; } input.MatchingDbId = vmo.ID.ToString(); input.MatchingMaterial = vmo.Material; input.MatchingModelType = vmo.Name; input.MatchingMinorLoss = vmo.Coefficient; return true; } return false; } //弯头匹配 public static bool MatchingElbow(ElbowsMatchingViewModel input, List adaptingManageVmos) { Vmo.AdaptingManageVmo vmo = null; int firstCount = 0; //口径最小差值 // 绝对匹配 var absoluteMatch = adaptingManageVmos.Where(i => ((input.Caliber == null && i.Caliber == null) || i.Caliber == input.Caliber) && ((input.Material == null && i.Material == "默认") || i.Material == input.Material)).ToList(); if (absoluteMatch.Any()) { foreach (var range in absoluteMatch) { int commonCount = GetIntersect(input.ModelType, range.Name); if (commonCount > firstCount) { vmo = range; firstCount = commonCount; } } } else { //区间匹配 var rangeMatch = adaptingManageVmos.Where(item => { if (item.Caliber != null && input.Caliber != null) { return Math.Abs(Convert.ToInt64(item.Caliber - input.Caliber)) <= _caliberTolerance; } else { if (item.Caliber == null) { 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 = GetIntersect(input.Material, range.Material); if (commonCount > firstCount) { materialList.Add(range); firstCount = commonCount; } } } //用已经筛选完成的列表中以名称筛选 firstCount = 0; foreach (var material in materialList) { int commonCount = GetIntersect(input.ModelType, material.Name); if (commonCount > firstCount) { vmo = material; firstCount = commonCount; } } } } //口径和材料都没有匹配上,就用型号名匹配 firstCount = 0; if (vmo == null) { foreach (var item in adaptingManageVmos) { int commonCount = GetIntersect(input.ModelType, item.Name); if (commonCount > firstCount) { vmo = item; firstCount = commonCount; } } } if (vmo != null) { input.MatchingMinorLoss = vmo.Coefficient; input.MatchingDbId = vmo.ID.ToString(); input.MatchingMaterial = vmo.Material; input.MatchingModelType = vmo.Name; return true; } return false; } //三通匹配 public static bool MatchingThreelink(ThreelinkMatchingViewModel input, List adaptingManageVmos) { Vmo.AdaptingManageVmo vmo = null; int firstCount = 0; //口径最小差值 // 绝对匹配 var absoluteMatch = adaptingManageVmos.Where(i => ((input.Caliber == null && i.Caliber == null) || i.Caliber == input.Caliber) && ((input.Material == null && i.Material == "默认") || i.Material == input.Material)).ToList(); if (absoluteMatch.Any()) { foreach (var range in absoluteMatch) { int commonCount = GetIntersect(input.ModelType, range.Name); if (commonCount > firstCount) { vmo = range; firstCount = commonCount; } } } else { //区间匹配 var rangeMatch = adaptingManageVmos.Where(item => { if (item.Caliber != null && input.Caliber != null) { return Math.Abs(Convert.ToInt64(item.Caliber - input.Caliber)) <= _caliberTolerance; } else { if (item.Caliber == null) { 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 = GetIntersect(input.Material, range.Material); if (commonCount > firstCount) { materialList.Add(range); firstCount = commonCount; } } } //用已经筛选完成的列表中以名称筛选 firstCount = 0; foreach (var material in materialList) { int commonCount = GetIntersect(input.ModelType, material.Name); if (commonCount > firstCount) { vmo = material; firstCount = commonCount; } } } } //口径和材料都没有匹配上,就用型号名匹配 firstCount = 0; if (vmo == null) { foreach (var item in adaptingManageVmos) { int commonCount = GetIntersect(input.ModelType, item.Name); if (commonCount > firstCount) { vmo = item; firstCount = commonCount; } } } if (vmo != null) { input.MatchingMinorLoss = vmo.Coefficient; input.MatchingDbId = vmo.ID.ToString(); input.MatchingMaterial = vmo.Material; input.MatchingModelType = vmo.Name; return true; } return false; } //四通匹配 public static bool MatchingFourlink(FourlinkMatchingViewModel input, List adaptingManageVmos) { Vmo.AdaptingManageVmo vmo = null; int firstCount = 0; //口径最小差值 // 绝对匹配 var absoluteMatch = adaptingManageVmos.Where(i => ((input.Caliber == null && i.Caliber == null) || i.Caliber == input.Caliber) && ((input.Material == null && i.Material == "默认") || i.Material == input.Material)).ToList(); if (absoluteMatch.Any()) { foreach (var range in absoluteMatch) { int commonCount = GetIntersect(input.ModelType, range.Name); if (commonCount > firstCount) { vmo = range; firstCount = commonCount; } } } else { //区间匹配 var rangeMatch = adaptingManageVmos.Where(item => { if (item.Caliber != null && input.Caliber != null) { return Math.Abs(Convert.ToInt64(item.Caliber - input.Caliber)) <= _caliberTolerance; } else { if (item.Caliber == null) { 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 = GetIntersect(input.Material, range.Material); if (commonCount > firstCount) { materialList.Add(range); firstCount = commonCount; } } } //用已经筛选完成的列表中以名称筛选 firstCount = 0; foreach (var material in materialList) { int commonCount = GetIntersect(input.ModelType, material.Name); if (commonCount > firstCount) { vmo = material; firstCount = commonCount; } } } } //口径和材料都没有匹配上,就用型号名匹配 firstCount = 0; if (vmo == null) { foreach (var item in adaptingManageVmos) { int commonCount = GetIntersect(input.ModelType, item.Name); if (commonCount > firstCount) { vmo = item; firstCount = commonCount; } } } if (vmo != null) { input.MatchingMinorLoss = vmo.Coefficient; input.MatchingDbId = vmo.ID.ToString(); input.MatchingMaterial = vmo.Material; input.MatchingModelType = vmo.Name; return true; } return false; } /* //返回两个字符串之间相同的字符个数 private static int GetIntersect(string baseString, string compareString) { // 将字符串转换为字符集合 if (baseString == string.Empty || baseString == null || compareString == string.Empty || compareString == null) { return 0; } HashSet baseChars = new HashSet(baseString); HashSet comparisonChars = new HashSet(compareString); // 计算两个集合的交集 int commonCount = baseChars.Intersect(comparisonChars).Count(); return commonCount; }*/ /// /// 获取两个字符串的所有交集 /// /// /// /// public static int GetIntersect(string str1, string str2) { if (str1 == null || str2 == null) return 0; return string.Join("", str1.Intersect(str2)).Count(); } } }