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 AssetsMatching(AssetsAutoMatchingViewModel assetsAutoMatchingView) { 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(); var assetsAutoMatching = new AssetsAutoMatchingViewModel(); var pumpMatchingList = new List(); var threeLinkMatchingList = new List(); var fourLinkMatchingList = new List(); var pipeLineMatchingList = new List(); var valveMatchingList = new List(); var elbowsMatchingList = new List(); //泵匹配 foreach (var item in assetsAutoMatchingView.PumpMatchingModels) { pumpMatchingList.Add(await MatchingPumps(item, allPump)); } //三通匹配 foreach (var item in assetsAutoMatchingView.ThreeLinkMatchingModels) { threeLinkMatchingList.Add(MatchingThreeLink(item, allAdapting)); } //四通匹配 foreach (var item in assetsAutoMatchingView.FourLinkMatchingModels) { fourLinkMatchingList.Add(MatchingFourLink(item, allAdapting)); } //管道匹配 foreach (var item in assetsAutoMatchingView.PipeLineMatchingModels) { pipeLineMatchingList.Add(MatchingPipes(item, allPipeLine)); } //阀门匹配 foreach (var item in assetsAutoMatchingView.ValveMatchingModels) { valveMatchingList.Add(MatchingValves(item, allValve)); } //弯头匹配 foreach (var item in assetsAutoMatchingView.ElbowsMatchingModels) { elbowsMatchingList.Add(MatchingElbows(item, allAdapting)); } assetsAutoMatching.PumpMatchingModels = pumpMatchingList; assetsAutoMatching.ThreeLinkMatchingModels = threeLinkMatchingList; assetsAutoMatching.FourLinkMatchingModels = fourLinkMatchingList; assetsAutoMatching.ElbowsMatchingModels = elbowsMatchingList; assetsAutoMatching.PipeLineMatchingModels = pipeLineMatchingList; assetsAutoMatching.ValveMatchingModels = valveMatchingList; return assetsAutoMatching; } //弯头匹配 public static ElbowsMatchingViewModel MatchingElbows(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 = CountCommonCharacters(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 = 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.MatchingMinorLoss = vmo.Coefficient; return input; } return input; } //四通匹配 public static FourLinkMatchingViewModel 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 = CountCommonCharacters(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 = 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.MatchingMinorLoss = vmo.Coefficient; return input; } return input; } //三通匹配 public static ThreeLinkMatchingViewModel 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 = CountCommonCharacters(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 = 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.MatchingMinorLoss = vmo.Coefficient; return input; } return input; } //阀门匹配 public static ValveMatchingViewModel MatchingValves(ValveMatchingViewModel input, List adaptingManageVmos) { HStation.Vmo.ValveMainVmo 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 = CountCommonCharacters(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 = 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.MatchingMinorLoss = vmo.Coefficient; return input; } return input; } //泵匹配 public static async Task MatchingPumps(PumpMatchingViewModel viewModel, List pumpMainVmos) { 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.MatchingRatedH = vmo.RatedHead; viewModel.MatchingRatedN = vmo.RatedSpeed; viewModel.MatchingRatedQ = vmo.RatedFlow; viewModel.MatchingRatedP = vmo.RatedPower; viewModel.MatchingDbID = vmo.ID.ToString(); var list = await new BLL.XhsPumpMainPhartMappingExtensions().GetByPumpMainID(vmo.ID); if (list != null && list.Count > 0) { viewModel.MatchingChartDbID = list.First().ID; } } return null; } //管道匹配 public static PipeLineMatchingViewModel MatchingPipes(PipeLineMatchingViewModel input, List pipeLineManageVmos) { Vmo.PipeLineManageVmo vmo = null; int StartCount = 0; //口径最小差值 // 绝对匹配 var absoluteMatch = pipeLineManageVmos.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 = CountCommonCharacters(input.ModelType, range.Name); if (commonCount > StartCount) { vmo = range; StartCount = commonCount; } } } else { //区间匹配 var rangeMatch = pipeLineManageVmos.Where(item => { if (item.Caliber != null && input.Caliber != null) { return Math.Abs(Convert.ToInt64(item.Caliber - input.Caliber)) <= _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 pipeLineManageVmos) { 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.MatchingMinorLoss = vmo.Hazen; return input; case HStation.Assets.eAlgorithmType.Manning: input.MatchingMinorLoss = vmo.Manning; return input; case HStation.Assets.eAlgorithmType.Darcy: input.MatchingMinorLoss = vmo.Darcy; return input; default: input.MatchingMinorLoss = vmo.Hazen; return input; } } return input; } //返回两个字符串之间相同的字符个数 private static int CountCommonCharacters(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 string GetIntersect(string str1, string str2) { if (str1 == null || str2 == null) return null; return string.Join("", str1.Intersect(str2)); } } }