duheng
2024-09-19 d88ae862d5eda101f20385f3e34a2d7e6f9d3471
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
using DevExpress.Dialogs.Core.View;
 
namespace HStation.WinFrmUI
{
    public partial class PumpMatchingCtrl : DevExpress.XtraEditors.XtraUserControl
    {
        public PumpMatchingCtrl()
        {
            InitializeComponent();
            this.gridView1.SetNormalView(30);
            this.gridView1.OptionsView.ShowDetailButtons = true;
            this.gridView1.OptionsView.ShowGroupPanel = false;
        }
 
        private List<PumpMatchingViewModel> _allBindingList = null;
        private BLL.PumpMain _pumpMainBll = null;
 
        public List<PumpMatchingViewModel> SetBindingData(List<PumpMatchingViewModel> pumpMatchingViewModel, out List<PumpMatchingViewModel> errorList)
        {
            _pumpMainBll = new BLL.PumpMain();
            errorList = null;
            _allBindingList = new List<PumpMatchingViewModel>();
            if (pumpMatchingViewModel == null)
                return null;
            var alllist = _pumpMainBll.GetAll().Result;
            const double speedTolerance = 100;
            const double flowTolerance = 10;
            const double headTolerance = 5;
            const double efficiencyTolerance = 0.05;
            foreach (var viewModel in pumpMatchingViewModel)
            {
                foreach (var pumpMain in alllist)
                {
                    // 尝试绝对匹配
                    var absoluteMatch = alllist.FirstOrDefault(item =>
                        viewModel.RatedN == item.RatedSpeed &&
                        viewModel.RatedQ == item.RatedFlow &&
                        viewModel.RatedH == item.RatedHead &&
                        viewModel.RatedP == item.RatedPower);
                    if (absoluteMatch != null)
                    {
                    }
                    else
                    {
                        // 尝试区间匹配
                        var rangeMatch = alllist.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) <= efficiencyTolerance)).ToList();
                        if (rangeMatch != null)
                        {
                            var vmo = new Vmo.PumpMainVmo();
                            int firstCount = 0;
                            foreach (var item in rangeMatch)
                            {
                                int commonCount = CountCommonCharacters(viewModel.ModelType, item.Name);
                                if (commonCount > firstCount)
                                {
                                    vmo = item;
                                    firstCount = commonCount;
                                }
                            }
                        }
                    }
                }
            }
            this.pumpMatchingViewModelBindingSource.DataSource = _allBindingList;
            return new List<PumpMatchingViewModel>();
        }
 
        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;
        }
    }
}