duheng
2024-11-17 a2a57963e160a319276c5c8499f16c9809056e4c
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
using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using HStation.Vmo;
 
namespace HStation.WinFrmUI.Assets
{
    public partial class EditExchangerMainDlg : DevExpress.XtraEditors.XtraForm
    {
        public EditExchangerMainDlg()
        {
            InitializeComponent();
            this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon;
        }
 
        private Vmo.AssetsExchangerMainVmo _ExchangerVmo = null;
 
        private List<Vmo.AssetsExchangerCoefficientVmo> _AssetsExchangerCoefficient;
 
        public async void SetBindingData(Vmo.AssetsExchangerMainVmo ExchangerVmo)
        {
            var bll = new BLL.AssetsExchangerCoefficient();
            _AssetsExchangerCoefficient = await bll.GetAll();
            this.exchangerCoefficientViewModelBindingSource.DataSource = _AssetsExchangerCoefficient;
 
            _ExchangerVmo = ExchangerVmo;
            var allMaterial = await new Yw.BLL.SysDictData().GetByTypeCode("3");
            if (allMaterial != null)
            {
                foreach (var item in allMaterial)
                {
                    var imageItem = new ImageComboBoxItem(item.Name, item.Name);
                    TextEditMaterial.Properties.Items.Add(imageItem);
                }
            }
            if (_ExchangerVmo.Material == null)
            {
                TextEditMaterial.EditValue = "默认";
            }
            else
            {
                TextEditMaterial.EditValue = _ExchangerVmo.Material.ToString();
            }
            this.TextEditName.Text = _ExchangerVmo.Name;
            this.TextEditMinorLoss.Text = _ExchangerVmo.MinorLoss.ToString();
            this.DescriptionTextEdit.Text = _ExchangerVmo.Description;
            this.TextEditKeyWord.Text = string.Join(",", _ExchangerVmo.KeyWord);
        }
 
        public event Func<Vmo.AssetsExchangerMainVmo, Task<bool>> ReloadDataEvent = null;
 
        //数据验证
        private bool Valid()
        {
            this.dxErrorProvider1.ClearErrors();
            if (string.IsNullOrEmpty(TextEditName.Text.Trim()))
            {
                this.dxErrorProvider1.SetError(this.TextEditName, "必填项");
                return false;
            }
            return true;
        }
 
        //材料选择变化事件
        private void TextEditMaterial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_AssetsExchangerCoefficient == null)
                return;
 
            var select = GetCoefficientByMaterial(TextEditMaterial.Text);
            if (select == null)
            {
                this.TextEditMinorLoss.Text = string.Empty;
                return;
            }
            this.TextEditMinorLoss.Text = select.MinorLoss.ToString();
        }
 
        //找到最相近的材料
        private AssetsExchangerCoefficientVmo GetCoefficientByMaterial(string name)
        {
            AssetsExchangerCoefficientVmo select = null;
            int maxMatchedChars = 0;
            foreach (var item in _AssetsExchangerCoefficient)
            {
                int matchedChars = GetIntersect(item.Material, name);
                if (matchedChars > maxMatchedChars)
                {
                    maxMatchedChars = matchedChars;
                    select = item;
                    return select;
                }
            }
            return select;
        }
 
        private int GetIntersect(string str1, string str2)
        {
            if (str1 == null || str2 == null) return 0;
            return string.Join("", str1.Intersect(str2)).Count();
        }
 
        //完成
        private async void BtnOk_ClickAsync(object sender, EventArgs e)
        {
            if (!(Valid()))
                return;
            _ExchangerVmo.Material = TextEditMaterial.Text.Trim();
            _ExchangerVmo.Description = DescriptionTextEdit.Text.Trim();
            _ExchangerVmo.Name = TextEditName.Text.Trim();
            _ExchangerVmo.KeyWord = this.TextEditKeyWord.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (double.TryParse(TextEditMinorLoss.Text, out double MinorLoss))
            {
                _ExchangerVmo.MinorLoss = MinorLoss;
            }
            if (await this.ReloadDataEvent.Invoke(_ExchangerVmo))
            {
                TipFormHelper.ShowSucceed("修改成功!");
            }
            else
            {
                TipFormHelper.ShowSucceed("修改失败!");
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}