duheng
2024-09-20 7d17fabc46e87ea0f0896f760034f4d16a4dfed0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
namespace HStation.WinFrmUI
{
    public class AsstesAutoMatchingHelper
    {
        public static T AutoMatching<T>(T input, List<Vmo.AdaptingManageVmo> adaptingManageVmos) where T : AdaptingViewModel
        {
            Vmo.AdaptingManageVmo vmo = null;
            int firstCount = 0;
            //口径最小差值
            const double caliberTolerance = 10.0;
            // 绝对匹配
            var absoluteMatch = adaptingManageVmos.Where(i =>
              ((input.Caliber == null && i.Caliber == "默认") || i.Caliber == input.Caliber) &&
               ((input.Material == null && i.Material == "默认") || i.Material == input.Material)).ToList();
            if (absoluteMatch.Any())
            {
                foreach (var range in absoluteMatch)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, range.Name);
                    if (commonCount > firstCount)
                    {
                        vmo = range;
                        firstCount = commonCount;
                    }
                }
            }
            else
            {
                double inputCaliber;
                if (!double.TryParse(input.Caliber, out inputCaliber))
                {
                    return null;
                }
 
                //区间匹配
                var rangeMatch = adaptingManageVmos.Where(item =>
                {
                    double itemCaliber;
                    if (double.TryParse(item.Caliber, out itemCaliber))
                    {
                        return Math.Abs(itemCaliber - inputCaliber) <= caliberTolerance;
                    }
                    return false;
                })
             .ToList();
                if (rangeMatch != null)
                {
                    foreach (var range in rangeMatch)
                    {
                        //以材料为条件开始匹配
                        int commonCount = CountCommonCharacters(input.Material, range.Material);
                        if (commonCount > firstCount)
                        {
                            vmo = range;
                            firstCount = commonCount;
                        }
                    }
                }
            }
            //口径和材料都没有匹配上,就用型号名匹配
            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.MinorLoss = vmo.Coefficient;
                return input;
            }
            return null;
        }
 
        //泵匹配
        public static PumpMatchingViewModel PumpMatching(PumpMatchingViewModel viewModel, List<Vmo.PumpMainVmo> pumpMainVmos)
        {
            const double speedTolerance = 100;
            const double flowTolerance = 10;
            const double headTolerance = 5;
            const double powerTolerance = 0.05;
            Vmo.PumpMainVmo vmo = null;
            int firstCount = 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 > firstCount)
                    {
                        vmo = item;
                        firstCount = 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 > firstCount)
                        {
                            vmo = item;
                            firstCount = commonCount;
                        }
                    }
                }
            }
            return new PumpMatchingViewModel();
        }
 
        //管道匹配
        public static PipeLineMatchingViewModel AutoMatching(PipeLineMatchingViewModel input, List<Vmo.PipeLineManageVmo> adaptingManageVmos)
        {
            Vmo.PipeLineManageVmo vmo = null;
            int StartCount = 0;
            //口径最小差值
            const double caliberTolerance = 10.0;
            // 绝对匹配
            var absoluteMatch = adaptingManageVmos.Where(i =>
              ((input.Caliber == null && i.Caliber == "默认") || i.Caliber == input.Caliber) &&
               ((input.Material == null && i.Material == "默认") || i.Material == input.Material)).ToList();
            if (absoluteMatch.Any())
            {
                foreach (var range in absoluteMatch)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, range.Name);
                    if (commonCount > StartCount)
                    {
                        vmo = range;
                        StartCount = commonCount;
                    }
                }
            }
            else
            {
                double inputCaliber;
                if (!double.TryParse(input.Caliber, out inputCaliber))
                {
                    return null;
                }
 
                //区间匹配
                var rangeMatch = adaptingManageVmos.Where(item =>
                {
                    double itemCaliber;
                    if (double.TryParse(item.Caliber, out itemCaliber))
                    {
                        return Math.Abs(itemCaliber - inputCaliber) <= caliberTolerance;
                    }
                    return false;
                })
             .ToList();
                if (rangeMatch != null)
                {
                    foreach (var range in rangeMatch)
                    {
                        //以材料为条件开始匹配
                        int commonCount = CountCommonCharacters(input.Material, range.Material);
                        if (commonCount > StartCount)
                        {
                            vmo = range;
                            StartCount = commonCount;
                        }
                    }
                }
            }
            //口径和材料都没有匹配上,就用型号名匹配
            if (vmo == null)
            {
                foreach (var item in adaptingManageVmos)
                {
                    int commonCount = CountCommonCharacters(input.ModelType, item.Name);
                    if (commonCount > StartCount)
                    {
                        vmo = item;
                        StartCount = commonCount;
                    }
                }
            }
            if (vmo != null)
            {
                switch (input.eAlgorithmType)
                {
                    case HStation.Assets.eAlgorithmType.Hazen:
                        input.MinorLoss = vmo.Hazen;
                        return input;
 
                    case HStation.Assets.eAlgorithmType.Manning:
                        input.MinorLoss = vmo.Manning;
                        return input;
 
                    case HStation.Assets.eAlgorithmType.Darcy:
                        input.MinorLoss = vmo.Darcy;
                        return input;
 
                    default:
                        input.MinorLoss = vmo.Hazen;
                        return input;
                }
            }
            return null;
        }
 
        //返回两个字符串之间相同的字符
        private static int CountCommonCharacters(string baseString, string comparisonString)
        {
            // 将字符串转换为字符集合
            HashSet<char> baseChars = new HashSet<char>(baseString);
            HashSet<char> comparisonChars = new HashSet<char>(comparisonString);
            // 计算两个集合的交集
            int commonCount = baseChars.Intersect(comparisonChars).Count();
            return commonCount;
        }
    }
}