lixiaojun
2024-12-02 f54adb8f368def21b9aa01e466b09bed6cd8347c
WinFrmUI/HStation.WinFrmUI.Xhs.Core/00-core/AssetsMatchingHelper.cs
@@ -1,4 +1,6 @@
namespace HStation.WinFrmUI
using HStation.WinFrmUI.PhartRelation;
namespace HStation.WinFrmUI
{
    public class AssetsMatchingHelper
    {
@@ -9,85 +11,107 @@
        private const double _headTolerance = 5;
        private const double _powerTolerance = 0.05;
        private const double _valveLift = 10;//阀门开度范围
        private const int _angle = 1;//角度范围
        //资产自动匹配
        public static async Task<bool> Matching(AssetsMatchingViewModel assetsAutoMatchingView)
        public static bool Matching(AssetsMatchingViewModel assetsAutoMatchingView, out string Error)
        {
            Error = string.Empty;
            bool IsMaching = false;
            var pumpMain = new BLL.PumpMain();
            var AssetsPumpMain = new BLL.AssetsPumpMain();
            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)
            var pipeLineManage = new BLL.AssetsPipeMain();
            var AssetsValveMain = new BLL.AssetsValveMain();
            var AssetsElbowMain = new BLL.AssetsElbowMain();
            var AssetsThreelinkMain = new BLL.AssetsThreelinkMain();
            var AssetsFourlinkMain = new BLL.AssetsFourlinkMain();
            try
            {
                if (await MatchingPumps(item, allPump))
                var allPump = Task.Run(async () => await AssetsPumpMain.GetAll()).Result;
                var allAdapting = Task.Run(async () => await adaptingManage.GetAll()).Result;
                var allPipeLine = Task.Run(async () => await pipeLineManage.GetAll()).Result;
                var allValve = Task.Run(async () => await AssetsValveMain.GetAll()).Result;
                var allElbow = Task.Run(async () => await AssetsElbowMain.GetAll()).Result;
                var allThreeLink = Task.Run(async () => await AssetsThreelinkMain.GetAll()).Result;
                var allFourLink = Task.Run(async () => await AssetsFourlinkMain.GetAll()).Result;
                //泵匹配
                foreach (var item in assetsAutoMatchingView.PumpMatchingList)
                {
                    IsMaching = true;
                    if (MatchingPumps(item, allPump))
                    {
                        IsMaching = true;
                    }
                }
                //三通匹配
                foreach (var item in assetsAutoMatchingView.ThreelinkMatchingList)
                {
                    if (MatchingThreelink(item, allThreeLink))
                    {
                        IsMaching = true;
                    }
                }
                //四通匹配
                foreach (var item in assetsAutoMatchingView.FourlinkMatchingList)
                {
                    if (MatchingFourlink(item, allFourLink))
                    {
                        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.ElbowMatchingList)
                {
                    if (MatchingElbow(item, allElbow))
                    {
                        IsMaching = true;
                    }
                }
            }
            //三通匹配
            foreach (var item in assetsAutoMatchingView.ThreelinkMatchingList)
            catch (Exception ex)
            {
                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;
                }
                Error = ex.Message;
                return false;
            }
            return IsMaching;
        }
        //泵匹配
        public static async Task<bool> MatchingPumps(PumpMatchingViewModel viewModel, List<Vmo.PumpMainVmo> pumpMainVmos)
        public static bool MatchingPumps(PumpMatchingViewModel InputModel, List<Vmo.AssetsPumpMainVmo> AssetsPumpMainVmos)
        {
            Vmo.PumpMainVmo vmo = null;
            if (AssetsPumpMainVmos == null)
            {
                return true;
            }
            Vmo.AssetsPumpMainVmo 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();
            var absoluteMatch = AssetsPumpMainVmos.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 = CountCommonCharacters(viewModel.ModelType, item.Name);
                    int commonCount = GetIntersect(InputModel.ModelType, item.Name);
                    if (commonCount > startCount)
                    {
                        vmo = item;
@@ -98,16 +122,16 @@
            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();
                var rangeMatch = AssetsPumpMainVmos.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 = CountCommonCharacters(viewModel.ModelType, item.Name);
                        int commonCount = GetIntersect(InputModel.ModelType, item.Name);
                        if (commonCount > startCount)
                        {
                            vmo = item;
@@ -119,9 +143,9 @@
            //
            if (vmo == null)
            {
                foreach (var item in pumpMainVmos)
                foreach (var item in AssetsPumpMainVmos)
                {
                    int commonCount = CountCommonCharacters(viewModel.ModelType, item.Name);
                    int commonCount = GetIntersect(InputModel.ModelType, item.Name);
                    if (commonCount > startCount)
                    {
                        vmo = item;
@@ -131,15 +155,47 @@
            }
            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);
                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 = Task.Run(async () => await new BLL.XhsPumpMainPhartMappingExtensions().GetByPumpMainID(vmo.ID)).Result;
                if (list != null && list.Count > 0)
                {
                    viewModel.MatchingCurveDbId = list.First().ID.ToString();
                    var maxImportanceItem = list.OrderByDescending(x => x.Importance).First();
                    InputModel.MatchingCurveDbId = maxImportanceItem.ID.ToString();
                    var graph_qh = maxImportanceItem.Diagram.GraphList.Find(x => x.GraphType == HStation.PhartRelation.eGraphType.PumpQH);
                    var graph_qe = maxImportanceItem.Diagram.GraphList.Find(x => x.GraphType == HStation.PhartRelation.eGraphType.PumpQE);
                    var graph_qp = maxImportanceItem.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<CurvePointMatchingViewModel>();
                        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<CurvePointMatchingViewModel>();
                        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<CurvePointMatchingViewModel>();
                        foreach (var item in points_qp)
                        {
                            InputModel.MatchingCurveQP.Add(new CurvePointMatchingViewModel(item.X, item.Y));
                        }
                    }
                }
                return true;
            }
@@ -147,501 +203,408 @@
        }
        //阀门匹配
        public static bool MatchingValve(ValveMatchingViewModel input, List<Vmo.ValveMainVmo> adaptingManageVmos)
        public static bool MatchingValve(ValveMatchingViewModel input, List<Vmo.AssetsValveMainVmo> valveMainVmos)
        {
            HStation.Vmo.ValveMainVmo vmo = null;
            int firstCount = 0;
            if (valveMainVmos == null)
            {
                return true;
            }
            HStation.Vmo.AssetsValveMainVmo vmo = null;
            //口径最小差值
            // 绝对匹配
            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())
            valveMainVmos = valveMainVmos.Where(x => x.ValveType == input.ValveType).ToList();
            var nameMatching = valveMainVmos.Where(x =>
        (x.KeyWord != null && x.KeyWord.Any(keyword => GetIntersect(keyword, input.ModelType) >= 1)) ||
        (x.KeyWord == null || !x.KeyWord.Any()) && GetIntersect(x.Name, input.ModelType) >= 1
        ) //先进行筛选,后进行从大到小排序
       .OrderByDescending(x =>
        (x.KeyWord != null && x.KeyWord.Any(keyword => GetIntersect(keyword, input.ModelType) >= 1)) ||
        (x.KeyWord == null || !x.KeyWord.Any()) && GetIntersect(x.Name, input.ModelType) >= 1)
       .ToList();//使用型号名或者关键字找出相同字符在1以上并且进行排序
            if (nameMatching.Count > 0)
            {
                foreach (var range in absoluteMatch)
                var absoluteMatching = nameMatching.Where(i =>
              ((i.Caliber == null) || Math.Abs(Convert.ToInt64(i.Caliber) - input.Diameter) <= _caliberTolerance) &&  //直径约束
               i.Material == input.Material  //材料约束
                 ).ToList();
                if (absoluteMatching.Count > 1)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, range.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = range;
                        firstCount = commonCount;
                    }
                    return false;
                }
                else if (absoluteMatching.Count == 1)
                {
                    vmo = absoluteMatching.First();
                }
                else
                    return false;
            }
            else if (nameMatching.Count == 1)
            {
                vmo = nameMatching.First();
            }
            else
            {
                //区间匹配
                var rangeMatch = adaptingManageVmos.Where(item =>
                var absoluteMatching = valveMainVmos.Where(i =>
          ((i.Caliber == null) || Math.Abs(Convert.ToInt64(i.Caliber) - input.Diameter) <= _caliberTolerance) &&  //直径约束
           i.Material == input.Material   //材料约束
             ).ToList();
                if (absoluteMatching.Count > 1)
                {
                    if (item.Caliber != null && input.Diameter != null)
                    {
                        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<Vmo.ValveMainVmo>();
                    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)
                else if (absoluteMatching.Count == 1)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, item.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = item;
                        firstCount = commonCount;
                    }
                    vmo = absoluteMatching.First();
                }
                else
                    return false;
            }
            if (vmo != null)
            {
                input.MatchingMinorLoss = vmo.Coefficient;
                return true;
                input.MatchingMinorLoss = vmo.MinorLoss;
                input.MatchingDbId = vmo.ID.ToString();
                input.MatchingDiameter = vmo.Caliber;
                input.MatchingMaterial = vmo.Material;
                input.MatchingModelType = vmo.Name;
                input.MatchingValveSetting = vmo.ValveSetting;
                input.MatchingValveType = vmo.ValveType;
                if (vmo.ValveType == HStation.Assets.eValveType.GPV)
                {
                    var list = Task.Run(async () => await new BLL.XhsPumpMainPhartMappingExtensions().GetByPumpMainID(vmo.ID)).Result;
                    if (list != null && list.Count > 0)
                    {
                        var maxImportanceItem = list.OrderByDescending(x => x.Importance).First();
                        input.MatchingCurveDbId = maxImportanceItem.ID.ToString();
                        var graph_ql = maxImportanceItem.Diagram.GraphList.Find(x => x.GraphType == HStation.PhartRelation.eGraphType.ValveQL);
                        var graph_ol = maxImportanceItem.Diagram.GraphList.Find(x => x.GraphType == HStation.PhartRelation.eGraphType.ValveOL);
                        if (graph_ql != null)
                        {
                            var points_qh = PhartPerformCurveHelper.GetFeatPointList(graph_ql.GraphType, graph_ql.GeometryInfo, 100, null);
                            input.MatchingCurveQL = new List<CurvePointMatchingViewModel>();
                            foreach (var item in points_qh)
                            {
                                input.MatchingCurveQL.Add(new CurvePointMatchingViewModel(item.X, item.Y));
                            }
                        }
                    }
                    return true;
                }
                else if (vmo.ValveType == HStation.Assets.eValveType.TCV)
                {
                    var list = Task.Run(async () => await new BLL.XhsPumpMainPhartMappingExtensions().GetByPumpMainID(vmo.ID)).Result;
                    if (list != null && list.Count > 0)
                    {
                        var maxImportanceItem = list.OrderByDescending(x => x.Importance).First();
                        input.MatchingCurveDbId = maxImportanceItem.ID.ToString();
                        var graph_ol = maxImportanceItem.Diagram.GraphList.Find(x => x.GraphType == HStation.PhartRelation.eGraphType.ValveOL);
                        if (graph_ol != null)
                        {
                            var points_qh = PhartPerformCurveHelper.GetFeatPointList(graph_ol.GraphType, graph_ol.GeometryInfo, 100, null);
                            input.MatchingCurveOL = new List<CurvePointMatchingViewModel>();
                            foreach (var item in points_qh)
                            {
                                input.MatchingCurveOL.Add(new CurvePointMatchingViewModel(item.X, item.Y));
                            }
                        }
                    }
                    return true;
                }
            }
            return false;
        }
        //管道匹配
        public static bool MatchingPipe(PipeMatchingViewModel input, List<Vmo.PipeLineManageVmo> pipeLineManageVmos)
        public static bool MatchingPipe(PipeMatchingViewModel input, List<Vmo.AssetsPipeMainVmo> pipeLineMainVmos)
        {
            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())
            if (pipeLineMainVmos == null)
            {
                foreach (var range in absoluteMatch)
                return true;
            }
            Vmo.AssetsPipeMainVmo vmo = null;
            var nameMatching = pipeLineMainVmos.Where(x =>
           (x.KeyWord != null && x.KeyWord.Any(keyword => GetIntersect(keyword, input.ModelType) >= 1)) ||
           (x.KeyWord == null || !x.KeyWord.Any()) && GetIntersect(x.Name, input.ModelType) >= 1
           ) //先进行筛选,后进行从大到小排序
          .OrderByDescending(x =>
           (x.KeyWord != null && x.KeyWord.Any(keyword => GetIntersect(keyword, input.ModelType) >= 1)) ||
           (x.KeyWord == null || !x.KeyWord.Any()) && GetIntersect(x.Name, input.ModelType) >= 1).ToList();
            if (nameMatching.Count > 1)
            {
                var absoluteMatching = nameMatching.Where(i =>
              ((i.Caliber == null) || Math.Abs(Convert.ToInt64(i.Caliber) - input.Diameter) <= _caliberTolerance) &&  //直径约束
               i.Material == input.Material  //材料约束
                 ).ToList();
                if (absoluteMatching.Count > 1)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, range.Name);
                    if (commonCount > StartCount)
                    {
                        vmo = range;
                        StartCount = commonCount;
                    }
                    return false;
                }
                else if (absoluteMatching.Count == 1)
                {
                    vmo = absoluteMatching.First();
                }
                else
                    return false;
            }
            else if (nameMatching.Count == 1)
            {
                vmo = nameMatching.First();
            }
            else
            {
                //区间匹配
                var rangeMatch = pipeLineManageVmos.Where(item =>
                var absoluteMatching = pipeLineMainVmos.Where(i =>
          ((i.Caliber == null) || Math.Abs(Convert.ToInt64(i.Caliber) - input.Diameter) <= _caliberTolerance) &&  //直径约束
           i.Material == input.Material   //材料约束
             ).ToList();
                if (absoluteMatching.Count > 1)
                {
                    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 = CountCommonCharacters(input.Material, range.Material);
                        if (commonCount > StartCount)
                        {
                            vmo = range;
                            StartCount = commonCount;
                        }
                    }
                }
            }
            //口径和材料都没有匹配上,就用型号名匹配
            if (vmo == null)
            {
                foreach (var item in pipeLineManageVmos)
                else if (absoluteMatching.Count == 1)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, item.Name);
                    if (commonCount > StartCount)
                    {
                        vmo = item;
                        StartCount = commonCount;
                    }
                    vmo = absoluteMatching.First();
                }
                else
                    return false;
            }
            if (vmo != null)
            {
                switch (input.eAlgorithmType)
                {
                    case HStation.Assets.eAlgorithmType.Hazen:
                        input.MatchingMinorLoss = vmo.Hazen;
                        input.MatchingRoughness = vmo.Hazen;
                        break;
                    case HStation.Assets.eAlgorithmType.Manning:
                        input.MatchingMinorLoss = vmo.Manning;
                        input.MatchingRoughness = vmo.Manning;
                        break;
                    case HStation.Assets.eAlgorithmType.Darcy:
                        input.MatchingMinorLoss = vmo.Darcy;
                        input.MatchingRoughness = vmo.Darcy;
                        break;
                    default:
                        input.MatchingMinorLoss = vmo.Hazen;
                        input.MatchingRoughness = vmo.Hazen;
                        break;
                }
                input.MatchingDbId = vmo.ID.ToString();
                input.MatchingMaterial = vmo.Material;
                input.MatchingModelType = vmo.Name;
                input.MatchingMinorLoss = vmo.MinorLoss;
                return true;
            }
            return false;
        }
        //弯头匹配
        public static bool MatchingElbow(ElbowsMatchingViewModel input, List<Vmo.AdaptingManageVmo> adaptingManageVmos)
        public static bool MatchingElbow(ElbowMatchingViewModel input, List<Vmo.AssetsElbowMainVmo> elbowMainVmos)
        {
            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())
            if (elbowMainVmos == null)
            {
                foreach (var range in absoluteMatch)
                return true;
            }
            Vmo.AssetsElbowMainVmo vmo = null;
            var nameMatching = elbowMainVmos.Where(x =>
        (x.KeyWord != null && x.KeyWord.Any(keyword => GetIntersect(keyword, input.ModelType) >= 1)) ||
        (x.KeyWord == null || !x.KeyWord.Any()) && GetIntersect(x.Name, input.ModelType) >= 1
        ) //先进行筛选,后进行从大到小排序
       .OrderByDescending(x =>
        (x.KeyWord != null && x.KeyWord.Any(keyword => GetIntersect(keyword, input.ModelType) >= 1)) ||
        (x.KeyWord == null || !x.KeyWord.Any()) && GetIntersect(x.Name, input.ModelType) >= 1).ToList();
            if (nameMatching.Count > 1)
            {
                var absoluteMatching = nameMatching.Where(i =>
              ((i.Caliber == null || input.Caliber == null) || Math.Abs(Convert.ToInt64(i.Caliber) - Convert.ToInt64(input.Caliber)) <= _caliberTolerance) &&  //直径约束
               i.Material == input.Material  //材料约束
                 ).ToList();
                if (absoluteMatching.Count > 1)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, range.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = range;
                        firstCount = commonCount;
                    }
                    return false;
                }
                else if (absoluteMatching.Count == 1)
                {
                    vmo = absoluteMatching.First();
                }
                else
                    return false;
            }
            else if (nameMatching.Count == 1)
            {
                vmo = nameMatching.First();
            }
            else
            {
                //区间匹配
                var rangeMatch = adaptingManageVmos.Where(item =>
                var absoluteMatching = elbowMainVmos.Where(i =>
          ((i.Caliber == null || input.Caliber == null) || Math.Abs(Convert.ToInt64(i.Caliber) - Convert.ToInt64(input.Caliber)) <= _caliberTolerance) &&  //直径约束
           i.Material == input.Material   //材料约束
             ).ToList();
                if (absoluteMatching.Count > 1)
                {
                    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<Vmo.AdaptingManageVmo>();
                    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)
                else if (absoluteMatching.Count == 1)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, item.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = item;
                        firstCount = commonCount;
                    }
                    vmo = absoluteMatching.First();
                }
                else
                    return false;
            }
            if (vmo != null)
            {
                input.MatchingMinorLoss = vmo.Coefficient;
                input.MatchingMinorLoss = vmo.MinorLoss;
                input.MatchingDbId = vmo.ID.ToString();
                input.MatchingMaterial = vmo.Material;
                input.MatchingModelType = vmo.Name;
                input.MatchingConnectionLength = vmo.ElbowLengthType;
                input.MatchingAngle = vmo.Angle;
                return true;
            }
            return false;
        }
        //三通匹配
        public static bool MatchingThreelink(ThreelinkMatchingViewModel input, List<Vmo.AdaptingManageVmo> adaptingManageVmos)
        public static bool MatchingThreelink(ThreelinkMatchingViewModel input, List<Vmo.AssetsThreelinkMainVmo> threelinkVmos)
        {
            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())
            Vmo.AssetsThreelinkMainVmo vmo = null;
            if (threelinkVmos == null)
            {
                foreach (var range in absoluteMatch)
                return true;
            }
            var nameMatching = threelinkVmos.Where(x =>
        (x.KeyWord != null && x.KeyWord.Any(keyword => GetIntersect(keyword, input.ModelType) >= 1)) ||
        (x.KeyWord == null || !x.KeyWord.Any()) && GetIntersect(x.Name, input.ModelType) >= 1
        ) //先进行筛选,后进行从大到小排序
       .OrderByDescending(x =>
        (x.KeyWord != null && x.KeyWord.Any(keyword => GetIntersect(keyword, input.ModelType) >= 1)) ||
        (x.KeyWord == null || !x.KeyWord.Any()) && GetIntersect(x.Name, input.ModelType) >= 1).ToList();
            if (nameMatching.Count > 1)
            {
                var absoluteMatching = nameMatching.Where(i =>
              ((i.Caliber == null || input.Caliber == null) || Math.Abs(Convert.ToInt64(i.Caliber) - Convert.ToInt64(input.Caliber)) <= _caliberTolerance) &&  //直径约束
               i.Material == input.Material  //材料约束
                 ).ToList();
                if (absoluteMatching.Count > 1)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, range.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = range;
                        firstCount = commonCount;
                    }
                    return false;
                }
                else if (absoluteMatching.Count == 1)
                {
                    vmo = absoluteMatching.First();
                }
                else
                    return false;
            }
            else if (nameMatching.Count == 1)
            {
                vmo = nameMatching.First();
            }
            else
            {
                //区间匹配
                var rangeMatch = adaptingManageVmos.Where(item =>
                var absoluteMatching = threelinkVmos.Where(i =>
          ((i.Caliber == null || input.Caliber == null) || Math.Abs(Convert.ToInt64(i.Caliber) - Convert.ToInt64(input.Caliber)) <= _caliberTolerance) &&  //直径约束
           i.Material == input.Material   //材料约束
             ).ToList();
                if (absoluteMatching.Count > 1)
                {
                    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<Vmo.AdaptingManageVmo>();
                    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)
                else if (absoluteMatching.Count == 1)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, item.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = item;
                        firstCount = commonCount;
                    }
                    vmo = absoluteMatching.First();
                }
                else
                    return false;
            }
            if (vmo != null)
            {
                input.MatchingMinorLoss = vmo.Coefficient;
                input.MatchingMinorLoss = vmo.MinorLoss;
                input.MatchingDbId = vmo.ID.ToString();
                input.MatchingMaterial = vmo.Material;
                input.MatchingModelType = vmo.Name;
                input.MatchingRunThroughCoefficient = vmo.RunThroughMinorLoss;
                input.MatchingBranchThroughCoefficient = vmo.BranchThroughMinorLoss;
                return true;
            }
            return false;
        }
        //四通匹配
        public static bool MatchingFourlink(FourlinkMatchingViewModel input, List<Vmo.AdaptingManageVmo> adaptingManageVmos)
        public static bool MatchingFourlink(FourlinkMatchingViewModel input, List<Vmo.AssetsFourlinkMainVmo> fourLinkVmos)
        {
            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())
            if (fourLinkVmos == null)
            {
                foreach (var range in absoluteMatch)
                return true;
            }
            Vmo.AssetsFourlinkMainVmo vmo = null;
            var nameMatching = fourLinkVmos.Where(x =>
 (x.KeyWord != null && x.KeyWord.Any(keyword => GetIntersect(keyword, input.ModelType) >= 1)) ||
 (x.KeyWord == null || !x.KeyWord.Any()) && GetIntersect(x.Name, input.ModelType) >= 1
 ) //先进行筛选,后进行从大到小排序
.OrderByDescending(x =>
 (x.KeyWord != null && x.KeyWord.Any(keyword => GetIntersect(keyword, input.ModelType) >= 1)) ||
 (x.KeyWord == null || !x.KeyWord.Any()) && GetIntersect(x.Name, input.ModelType) >= 1).ToList();
            if (nameMatching.Count > 1)
            {
                var absoluteMatching = nameMatching.Where(i =>
              ((i.Caliber == null || input.Caliber == null) || Math.Abs(Convert.ToInt64(i.Caliber) - Convert.ToInt64(input.Caliber)) <= _caliberTolerance) &&  //直径约束
               i.Material == input.Material  //材料约束
                 ).ToList();
                if (absoluteMatching.Count > 1)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, range.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = range;
                        firstCount = commonCount;
                    }
                    return false;
                }
                else if (absoluteMatching.Count == 1)
                {
                    vmo = absoluteMatching.First();
                }
                else
                    return false;
            }
            else if (nameMatching.Count == 1)
            {
                vmo = nameMatching.First();
            }
            else
            {
                //区间匹配
                var rangeMatch = adaptingManageVmos.Where(item =>
                var absoluteMatching = fourLinkVmos.Where(i =>
          ((i.Caliber == null || input.Caliber == null) || Math.Abs(Convert.ToInt64(i.Caliber) - Convert.ToInt64(input.Caliber)) <= _caliberTolerance) &&  //直径约束
           i.Material == input.Material   //材料约束
             ).ToList();
                if (absoluteMatching.Count > 1)
                {
                    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<Vmo.AdaptingManageVmo>();
                    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)
                else if (absoluteMatching.Count == 1)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, item.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = item;
                        firstCount = commonCount;
                    }
                    vmo = absoluteMatching.First();
                }
                else
                    return false;
            }
            if (vmo != null)
            {
                input.MatchingMinorLoss = vmo.Coefficient;
                input.MatchingMinorLoss = vmo.MinorLoss;
                input.MatchingDbId = vmo.ID.ToString();
                input.MatchingMaterial = vmo.Material;
                input.MatchingModelType = vmo.Name;
                return true;
            }
            return false;
        }
        //返回两个字符串之间相同的字符个数
        private static int CountCommonCharacters(string baseString, string compareString)
        {
            // 将字符串转换为字符集合
            if (baseString == string.Empty || baseString == null || compareString == string.Empty || compareString == null)
            {
                return 0;
            }
            HashSet<char> baseChars = new HashSet<char>(baseString);
            HashSet<char> comparisonChars = new HashSet<char>(compareString);
            // 计算两个集合的交集
            int commonCount = baseChars.Intersect(comparisonChars).Count();
            return commonCount;
        }
        /// <summary>
        /// 获取两个字符串的所有交集
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public static string GetIntersect(string str1, string str2)
        public static int GetIntersect(string str1, string str2)
        {
            if (str1 == null || str2 == null) return null;
            return string.Join("", str1.Intersect(str2));
            if (str1 == null || str2 == null) return 0;
            return string.Join("", str1.Intersect(str2)).Count();
        }
    }
}