duheng
2024-09-19 a72a2fafc5cd95d9fae4957d855929fabf594415
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
using Castle.Core.Internal;
using DevExpress.Dialogs.Core.View;
using DevExpress.XtraSpreadsheet.Commands;
 
namespace HStation.WinFrmUI
{
    public class AdaptingAutoMatchingHelper
    {
        public static T AutoMatching<T>(T input, List<Vmo.AdaptingManageVmo> adaptingManageVmos) where T : AdaptingViewModel
        {
            Vmo.AdaptingManageVmo vmo = null;
            int firstCount = 0;
            const double speedTolerance = 10.0;
            // 绝对匹配
            var absoluteMatch = adaptingManageVmos.Where(i =>
                i.Caliber == input.Caliber &&
                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) <= speedTolerance;
                    }
                    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.LossCoefficient = vmo.Coefficient;
                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;
        }
    }
}