lixiaojun
2024-09-27 1e5868571fdeb64bead897396ed38b44eda1bcaa
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
    {
@@ -10,8 +12,9 @@
        private const double _powerTolerance = 0.05;
        //资产自动匹配
        public static async Task<AssetsMatchingViewModel> Matching(AssetsMatchingViewModel assetsAutoMatchingView)
        public static async Task<bool> Matching(AssetsMatchingViewModel assetsAutoMatchingView)
        {
            bool IsMaching = false;
            var pumpMain = new BLL.PumpMain();
            var adaptingManage = new BLL.AdaptingManage();
            var pipeLineManage = new BLL.PipeLineManage();
@@ -20,68 +23,73 @@
            var allAdapting = await adaptingManage.GetAll();
            var allPipeLine = await pipeLineManage.GetAll();
            var allValve = await valveMain.GetAll();
            var assetsAutoMatching = new AssetsMatchingViewModel();
            var pumpMatchingList = new List<PumpMatchingViewModel>();
            var threeLinkMatchingList = new List<ThreelinkMatchingViewModel>();
            var fourLinkMatchingList = new List<FourlinkMatchingViewModel>();
            var pipeLineMatchingList = new List<PipeMatchingViewModel>();
            var valveMatchingList = new List<ValveMatchingViewModel>();
            var elbowsMatchingList = new List<ElbowsMatchingViewModel>();
            //泵匹配
            foreach (var item in assetsAutoMatchingView.PumpMatchingList)
            {
                pumpMatchingList.Add(await MatchingPumps(item, allPump));
                if (await MatchingPumps(item, allPump))
                {
                    IsMaching = true;
                }
            }
            //三通匹配
            foreach (var item in assetsAutoMatchingView.ThreelinkMatchingList)
            {
                threeLinkMatchingList.Add(MatchingThreelink(item, allAdapting));
                if (MatchingThreelink(item, allAdapting))
                {
                    IsMaching = true;
                }
            }
            //四通匹配
            foreach (var item in assetsAutoMatchingView.FourlinkMatchingList)
            {
                fourLinkMatchingList.Add(MatchingFourlink(item, allAdapting));
                if (MatchingFourlink(item, allAdapting))
                {
                    IsMaching = true;
                }
            }
            //管道匹配
            foreach (var item in assetsAutoMatchingView.PipeMatchingList)
            {
                pipeLineMatchingList.Add(MatchingPipe(item, allPipeLine));
                if (MatchingPipe(item, allPipeLine))
                {
                    IsMaching = true;
                }
            }
            //阀门匹配
            foreach (var item in assetsAutoMatchingView.ValveMatchingList)
            {
                valveMatchingList.Add(MatchingValve(item, allValve));
                if (MatchingValve(item, allValve))
                {
                    IsMaching = true;
                }
            }
            //弯头匹配
            foreach (var item in assetsAutoMatchingView.ElbowsMatchingList)
            {
                elbowsMatchingList.Add(MatchingElbow(item, allAdapting));
                if (MatchingElbow(item, allAdapting))
                {
                    IsMaching = true;
                }
            }
            assetsAutoMatching.PumpMatchingList = pumpMatchingList;
            assetsAutoMatching.ThreelinkMatchingList = threeLinkMatchingList;
            assetsAutoMatching.FourlinkMatchingList = fourLinkMatchingList;
            assetsAutoMatching.ElbowsMatchingList = elbowsMatchingList;
            assetsAutoMatching.PipeMatchingList = pipeLineMatchingList;
            assetsAutoMatching.ValveMatchingList = valveMatchingList;
            return assetsAutoMatching;
            return IsMaching;
        }
        //泵匹配
        public static async Task<PumpMatchingViewModel> MatchingPumps(PumpMatchingViewModel viewModel, List<Vmo.PumpMainVmo> pumpMainVmos)
        public static async Task<bool> MatchingPumps(PumpMatchingViewModel InputModel, List<Vmo.PumpMainVmo> 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();
            (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;
@@ -93,15 +101,15 @@
            {
                // 尝试区间匹配
                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();
                   (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;
@@ -115,7 +123,7 @@
            {
                foreach (var item in pumpMainVmos)
                {
                    int commonCount = CountCommonCharacters(viewModel.ModelType, item.Name);
                    int commonCount = GetIntersect(InputModel.ModelType, item.Name);
                    if (commonCount > startCount)
                    {
                        vmo = item;
@@ -125,35 +133,67 @@
            }
            if (vmo != null)
            {
                viewModel.MatchingRatedH = vmo.RatedHead;
                viewModel.MatchingRatedN = vmo.RatedSpeed;
                viewModel.MatchingRatedQ = vmo.RatedFlow;
                viewModel.MatchingRatedP = vmo.RatedPower;
                viewModel.MatchingDbId = vmo.ID.ToString();
                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)
                {
                    viewModel.MatchingCurveDbId = list.First().ID;
                    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<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;
            }
            return null;
            return false;
        }
        //阀门匹配
        public static ValveMatchingViewModel MatchingValve(ValveMatchingViewModel input, List<Vmo.ValveMainVmo> adaptingManageVmos)
        public static bool MatchingValve(ValveMatchingViewModel input, List<Vmo.ValveMainVmo> adaptingManageVmos)
        {
            HStation.Vmo.ValveMainVmo vmo = null;
            int firstCount = 0;
            //口径最小差值
            // 绝对匹配
            var absoluteMatch = adaptingManageVmos.Where(i =>
              ((input.Diameter == null && i.Caliber == null) || i.Caliber == input.Diameter) &&
              ((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 = CountCommonCharacters(input.ModelType, range.Name);
                    int commonCount = GetIntersect(input.ModelType, range.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = range;
@@ -166,7 +206,7 @@
                //区间匹配
                var rangeMatch = adaptingManageVmos.Where(item =>
                {
                    if (item.Caliber != null && input.Diameter != null)
                    if (item.Caliber.HasValue)
                    {
                        return Math.Abs(Convert.ToInt64(item.Caliber - input.Diameter)) <= _caliberTolerance;
                    }
@@ -192,7 +232,7 @@
                        }
                        else
                        {
                            int commonCount = CountCommonCharacters(input.Material, range.Material);
                            int commonCount = GetIntersect(input.Material, range.Material);
                            if (commonCount > firstCount)
                            {
                                materialList.Add(range);
@@ -204,7 +244,7 @@
                    firstCount = 0;
                    foreach (var material in materialList)
                    {
                        int commonCount = CountCommonCharacters(input.ModelType, material.Name);
                        int commonCount = GetIntersect(input.ModelType, material.Name);
                        if (commonCount > firstCount)
                        {
                            vmo = material;
@@ -219,7 +259,7 @@
            {
                foreach (var item in adaptingManageVmos)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, item.Name);
                    int commonCount = GetIntersect(input.ModelType, item.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = item;
@@ -230,26 +270,32 @@
            if (vmo != null)
            {
                input.MatchingMinorLoss = vmo.Coefficient;
                return input;
                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 input;
            return false;
        }
        //管道匹配
        public static PipeMatchingViewModel MatchingPipe(PipeMatchingViewModel input, List<Vmo.PipeLineManageVmo> pipeLineManageVmos)
        public static bool MatchingPipe(PipeMatchingViewModel input, List<Vmo.PipeLineManageVmo> pipeLineManageVmos)
        {
            Vmo.PipeLineManageVmo vmo = null;
            int StartCount = 0;
            //口径最小差值
            // 绝对匹配
            var absoluteMatch = pipeLineManageVmos.Where(i =>
              ((input.Caliber == null && i.Caliber == null) || i.Caliber == input.Caliber) &&
            ((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 = CountCommonCharacters(input.ModelType, range.Name);
                    int commonCount = GetIntersect(input.ModelType, range.Name);
                    if (commonCount > StartCount)
                    {
                        vmo = range;
@@ -262,9 +308,9 @@
                //区间匹配
                var rangeMatch = pipeLineManageVmos.Where(item =>
                {
                    if (item.Caliber != null && input.Caliber != null)
                    if (item.Caliber != null)
                    {
                        return Math.Abs(Convert.ToInt64(item.Caliber - input.Caliber)) <= _caliberTolerance;
                        return Math.Abs(Convert.ToInt64(item.Caliber - input.Diameter)) <= _caliberTolerance;
                    }
                    return false;
                })
@@ -274,7 +320,7 @@
                    foreach (var range in rangeMatch)
                    {
                        //以材料为条件开始匹配
                        int commonCount = CountCommonCharacters(input.Material, range.Material);
                        int commonCount = GetIntersect(input.Material, range.Material);
                        if (commonCount > StartCount)
                        {
                            vmo = range;
@@ -288,7 +334,7 @@
            {
                foreach (var item in pipeLineManageVmos)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, item.Name);
                    int commonCount = GetIntersect(input.ModelType, item.Name);
                    if (commonCount > StartCount)
                    {
                        vmo = item;
@@ -301,27 +347,32 @@
                switch (input.eAlgorithmType)
                {
                    case HStation.Assets.eAlgorithmType.Hazen:
                        input.MatchingMinorLoss = vmo.Hazen;
                        return input;
                        input.MatchingRoughness = vmo.Hazen;
                        break;
                    case HStation.Assets.eAlgorithmType.Manning:
                        input.MatchingMinorLoss = vmo.Manning;
                        return input;
                        input.MatchingRoughness = vmo.Manning;
                        break;
                    case HStation.Assets.eAlgorithmType.Darcy:
                        input.MatchingMinorLoss = vmo.Darcy;
                        return input;
                        input.MatchingRoughness = vmo.Darcy;
                        break;
                    default:
                        input.MatchingMinorLoss = vmo.Hazen;
                        return input;
                        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 input;
            return false;
        }
        //弯头匹配
        public static ElbowsMatchingViewModel MatchingElbow(ElbowsMatchingViewModel input, List<Vmo.AdaptingManageVmo> adaptingManageVmos)
        public static bool MatchingElbow(ElbowMatchingViewModel input, List<Vmo.AdaptingManageVmo> adaptingManageVmos)
        {
            Vmo.AdaptingManageVmo vmo = null;
            int firstCount = 0;
@@ -334,7 +385,7 @@
            {
                foreach (var range in absoluteMatch)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, range.Name);
                    int commonCount = GetIntersect(input.ModelType, range.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = range;
@@ -373,7 +424,7 @@
                        }
                        else
                        {
                            int commonCount = CountCommonCharacters(input.Material, range.Material);
                            int commonCount = GetIntersect(input.Material, range.Material);
                            if (commonCount > firstCount)
                            {
                                materialList.Add(range);
@@ -385,7 +436,7 @@
                    firstCount = 0;
                    foreach (var material in materialList)
                    {
                        int commonCount = CountCommonCharacters(input.ModelType, material.Name);
                        int commonCount = GetIntersect(input.ModelType, material.Name);
                        if (commonCount > firstCount)
                        {
                            vmo = material;
@@ -400,7 +451,7 @@
            {
                foreach (var item in adaptingManageVmos)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, item.Name);
                    int commonCount = GetIntersect(input.ModelType, item.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = item;
@@ -411,13 +462,16 @@
            if (vmo != null)
            {
                input.MatchingMinorLoss = vmo.Coefficient;
                return input;
                input.MatchingDbId = vmo.ID.ToString();
                input.MatchingMaterial = vmo.Material;
                input.MatchingModelType = vmo.Name;
                return true;
            }
            return input;
            return false;
        }
        //三通匹配
        public static ThreelinkMatchingViewModel MatchingThreelink(ThreelinkMatchingViewModel input, List<Vmo.AdaptingManageVmo> adaptingManageVmos)
        public static bool MatchingThreelink(ThreelinkMatchingViewModel input, List<Vmo.AdaptingManageVmo> adaptingManageVmos)
        {
            Vmo.AdaptingManageVmo vmo = null;
            int firstCount = 0;
@@ -430,7 +484,7 @@
            {
                foreach (var range in absoluteMatch)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, range.Name);
                    int commonCount = GetIntersect(input.ModelType, range.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = range;
@@ -469,7 +523,7 @@
                        }
                        else
                        {
                            int commonCount = CountCommonCharacters(input.Material, range.Material);
                            int commonCount = GetIntersect(input.Material, range.Material);
                            if (commonCount > firstCount)
                            {
                                materialList.Add(range);
@@ -481,7 +535,7 @@
                    firstCount = 0;
                    foreach (var material in materialList)
                    {
                        int commonCount = CountCommonCharacters(input.ModelType, material.Name);
                        int commonCount = GetIntersect(input.ModelType, material.Name);
                        if (commonCount > firstCount)
                        {
                            vmo = material;
@@ -496,7 +550,7 @@
            {
                foreach (var item in adaptingManageVmos)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, item.Name);
                    int commonCount = GetIntersect(input.ModelType, item.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = item;
@@ -507,13 +561,16 @@
            if (vmo != null)
            {
                input.MatchingMinorLoss = vmo.Coefficient;
                return input;
                input.MatchingDbId = vmo.ID.ToString();
                input.MatchingMaterial = vmo.Material;
                input.MatchingModelType = vmo.Name;
                return true;
            }
            return input;
            return false;
        }
        //四通匹配
        public static FourlinkMatchingViewModel MatchingFourlink(FourlinkMatchingViewModel input, List<Vmo.AdaptingManageVmo> adaptingManageVmos)
        public static bool MatchingFourlink(FourlinkMatchingViewModel input, List<Vmo.AdaptingManageVmo> adaptingManageVmos)
        {
            Vmo.AdaptingManageVmo vmo = null;
            int firstCount = 0;
@@ -526,7 +583,7 @@
            {
                foreach (var range in absoluteMatch)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, range.Name);
                    int commonCount = GetIntersect(input.ModelType, range.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = range;
@@ -565,7 +622,7 @@
                        }
                        else
                        {
                            int commonCount = CountCommonCharacters(input.Material, range.Material);
                            int commonCount = GetIntersect(input.Material, range.Material);
                            if (commonCount > firstCount)
                            {
                                materialList.Add(range);
@@ -577,7 +634,7 @@
                    firstCount = 0;
                    foreach (var material in materialList)
                    {
                        int commonCount = CountCommonCharacters(input.ModelType, material.Name);
                        int commonCount = GetIntersect(input.ModelType, material.Name);
                        if (commonCount > firstCount)
                        {
                            vmo = material;
@@ -592,7 +649,7 @@
            {
                foreach (var item in adaptingManageVmos)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, item.Name);
                    int commonCount = GetIntersect(input.ModelType, item.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = item;
@@ -603,26 +660,29 @@
            if (vmo != null)
            {
                input.MatchingMinorLoss = vmo.Coefficient;
                return input;
                input.MatchingDbId = vmo.ID.ToString();
                input.MatchingMaterial = vmo.Material;
                input.MatchingModelType = vmo.Name;
                return true;
            }
            return input;
            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;
        }
        /*
                //返回两个字符串之间相同的字符个数
                private static int GetIntersect(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>
        /// 获取两个字符串的所有交集
@@ -630,12 +690,11 @@
        /// <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;
            if (str1 == null || str2 == null) return 0;
            return string.Join("", str1.Intersect(str2));
            return string.Join("", str1.Intersect(str2)).Count();
        }
    }
}