duheng
2024-12-27 1c3e5bc50d3045d51cb9a9f747d53442b82d9e2c
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
using DevExpress.XtraEditors;
using DevExpress.XtraScheduler.Outlook.Interop;
using HStation.Service.Assets;
using HStation.Vmo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Yw;
 
namespace HStation.WinFrmUI
{
    public partial class AddAssetsThreelinkMainDlg : DevExpress.XtraEditors.XtraForm
    {
        public AddAssetsThreelinkMainDlg()
        {
            InitializeComponent();
            this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon;
            this.layoutControl1.SetupLayoutControl();
            this.generalOkAndCancelCtrl1.OkEvent += GeneralOkAndCancelCtrl1_OkEvent;
        }
 
        private static AssetsThreelinkMainVmo _last = null;
 
        /// <summary>
        /// 返回数据事件
        /// </summary>
        public event Action<HStation.Vmo.AssetsThreelinkMainVmo> ReloadDataEvent;
 
        private HStation.Vmo.AssetsThreelinkSeriesVmo _series = null;
        private HStation.Vmo.AssetsThreelinkMainVmo _vmo = null;
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public async void SetBindingData(HStation.Vmo.AssetsThreelinkSeriesVmo series)
        {
            if (series == null)
            {
                return;
            }
            _series = series;
            _vmo = new Vmo.AssetsThreelinkMainVmo();
            _vmo.SeriesID = series.ID;
            var flags = await BLLFactory<Yw.BLL.SysFlag>.Instance.GetBySysType(HStation.Assets.DataType.ThreelinkMain);
            this.setFlagsEditCtrl1.SetBindingData(flags?.Select(x => x.Name).ToList(), null);
            if (_last != null)
            {
                this.txtName.EditValue = _last.Name;
                this.txtKeyWord.EditValue = KeyWordHelper.ToString(_last.KeyWords);
                this.setFlagsEditCtrl1.SetBindingData(flags?.Select(x => x.Name).ToList(), _last.Flags);
                this.txtTagName.EditValue = _last.TagName;
                this.txtDescription.EditValue = _last.Description;
            }
        }
 
        //验证
        private async Task<bool> Valid()
        {
            this.dxErrorProvider1.ClearErrors();
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                this.dxErrorProvider1.SetError(this.txtName, "必填项");
                return false;
            }
            if (string.IsNullOrEmpty(this.textEditRunThroughCoefficient.Text.Trim()))
            {
                this.dxErrorProvider1.SetError(this.textEditRunThroughCoefficient, "必填项");
                return false;
            }
            if (string.IsNullOrEmpty(this.textEditBranchThroughCoefficient.Text.Trim()))
            {
                this.dxErrorProvider1.SetError(this.textEditBranchThroughCoefficient, "必填项");
                return false;
            }
            var tagname = this.txtTagName.Text.Trim();
            if (!string.IsNullOrEmpty(tagname))
            {
                if (await BLLFactory<HStation.BLL.AssetsThreelinkMain>.Instance.IsExistTagName(tagname))
                {
                    this.dxErrorProvider1.SetError(this.txtTagName, "重复");
                    return false;
                }
            }
            return true;
        }
 
        //确定
        private async void GeneralOkAndCancelCtrl1_OkEvent()
        {
            if (_series == null)
            {
                return;
            }
            if (_vmo == null)
            {
                return;
            }
            if (!await Valid())
            {
                return;
            }
            _vmo.Name = this.txtName.Text.Trim();
            _vmo.KeyWords = HStation.Service.Assets.KeyWordHelper.ToList(this.txtKeyWord.Text.Trim());
            _vmo.Flags = this.setFlagsEditCtrl1.SelectedFlagList;
            _vmo.TagName = this.txtTagName.Text.Trim();
            _vmo.Description = this.txtDescription.Text.Trim();
            _vmo.Caliber = double.Parse(this.txtCaliber.EditValue?.ToString());
            _vmo.Material = this.txtMaterial.Text.Trim();
            _vmo.RunThroughMinorLoss = double.Parse(this.textEditRunThroughCoefficient.EditValue?.ToString());
            _vmo.BranchThroughMinorLoss = double.Parse(this.textEditBranchThroughCoefficient.EditValue?.ToString());
            var id = await BLLFactory<HStation.BLL.AssetsThreelinkMain>.Instance.Insert(_vmo);
            if (id < 1)
            {
                TipFormHelper.ShowError("添加失败!");
                return;
            }
            var vmo = await BLLFactory<HStation.BLL.AssetsThreelinkMain>.Instance.GetByID(id);
            _last = vmo;
            this.ReloadDataEvent?.Invoke(vmo);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
 
        //获取所有系数列表
        private async Task<List<AssetsThreelinkFactorVmo>> GetAllFactorList()
        {
            if (_allFactorList == null)
            {
                _allFactorList = await BLLFactory<HStation.BLL.AssetsThreelinkFactor>.Instance.GetAll();
                if (_allFactorList == null)
                {
                    _allFactorList = new List<AssetsThreelinkFactorVmo>();
                }
            }
            return _allFactorList;
        }
 
        private List<AssetsThreelinkFactorVmo> _allFactorList = null;
 
        //匹配系数
        private async void MatchingFactor()
        {
            if (this.textEditRunThroughCoefficient.EditValue == null && this.textEditBranchThroughCoefficient.EditValue == null)
            {
                var allFactorList = await GetAllFactorList();
                if (allFactorList != null && allFactorList.Count > 0)
                {
                    var name = this.txtName.Text.Trim();
                    var factorList = allFactorList.ToList();
                    if (!string.IsNullOrEmpty(name))
                    {
                        factorList = factorList.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.Contains(name)).ToList();
                    }
                    var factor = factorList.FirstOrDefault();
                    if (factor != null)
                    {
                        this.textEditRunThroughCoefficient.EditValue = factor.RunThroughMinorLoss;
                        this.textEditBranchThroughCoefficient.EditValue = factor.BranchThroughMinorLoss;
                    }
                }
            }
        }
 
        private void txtMaterial_EditValueChanged(object sender, EventArgs e)
        {
            MatchingFactor();
        }
 
        private void txtCaliber_EditValueChanged(object sender, EventArgs e)
        {
            MatchingFactor();
        }
    }
}