Shuxia Ning
2024-10-08 cf4967a0aebab18c5a37137f3e4c61b2d73a54bb
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
using DevExpress.CodeParser;
using DevExpress.XtraEditors;
using System;
using System.Windows.Forms;
 
namespace IStation.WinFrmUI
{
    /// <summary>
    /// 设置一天小时段
    /// </summary>
    public partial class ArrayFormatParasCtrl : UserControl, IFormatParas
    {
        public ArrayFormatParasCtrl()
        {
            InitializeComponent();
        }
 
        private Model.SignalType.ArrayFormatParas _model = null;
 
        private List<Model.UnitType> _unitTypes = null;
        private List<Model.UnitValue> _unitValues = null;
 
        private bool _isNON = false;//0
        private bool _isCustomer = false;//999
 
 
        //初始化数据
        public async void Set(string paras)
        {
            _unitTypes = await new BLL.UnitType().GetAll();
            _unitValues = await new BLL.UnitValue().GetAll();
 
 
            if (_unitTypes != null && _unitTypes.Count > 0)
            {
                foreach (var unitType in _unitTypes)
                {
                    this.UnitTypeImageComboBoxEdit.Properties.Items.Add(unitType.Name, unitType.Code, -1);
                }
            }
 
            _model = Model.SignalType.ArrayFormatParas.ToModel(paras);
            if (_model == null)
            {
                _model = new Model.SignalType.ArrayFormatParas();
                this.UnitTypeImageComboBoxEdit.SelectedIndex = 0;
            }
            else
            {
 
                this.UnitTypeImageComboBoxEdit.EditValue = _model.UnitType;
            }
 
            this.DecimalPlacesTextEdit.EditValue = _model.DecimalPlaces;
        }
 
 
        //单位类型变换
        private void UnitTypeImageComboBoxEdit_EditValueChanged(object sender, EventArgs e)
        {
            if (_unitTypes == null || _unitTypes.Count < 1)
                return;
            var code = (int)this.UnitTypeImageComboBoxEdit.EditValue;
            SetUnitValueComboBoxEdit(code);
        }
 
        private void SetUnitValueComboBoxEdit(int unitTypeCode)
        {
            _isNON = false;
            this.UnitValueComboBoxEdit.Text = string.Empty;
            this.UnitValueComboBoxEdit.Properties.Items.Clear();
            this.UnitValueComboBoxEdit.Enabled = true;
            this.UnitValueComboBoxEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            if (unitTypeCode == 0)
            {
                _isNON = true;
                this.UnitValueComboBoxEdit.Enabled = false;
            }
            else if (unitTypeCode == 999)
            {
                _isCustomer = true;
                this.UnitValueComboBoxEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
                if (unitTypeCode == _model.UnitType)
                {
                    this.UnitValueComboBoxEdit.Text = _model.UnitValue;
                }
            }
            else
            {
                var unitType = _unitTypes.Find(x => x.Code == unitTypeCode);
                if (unitType == null)
                    return;
                var unitValues = _unitValues.FindAll(x => x.TypeID == unitType.ID);
                if (unitValues == null || unitValues.Count < 1)
                    return;
                foreach (var item in unitValues)
                {
                    var keyIntValue = new KeyIntValue(item.Name, item.Code);
                    this.UnitValueComboBoxEdit.Properties.Items.Add(keyIntValue);
                }
 
                this.UnitValueComboBoxEdit.SelectedIndex = 0;
                if (unitTypeCode == _model.UnitType)
                {
                    if (int.TryParse(_model.UnitValue, out int value))
                    {
                        var index = unitValues.FindIndex(x => x.Code == value);
                        this.UnitValueComboBoxEdit.SelectedIndex = index;
                    }
                }
            }
        }
 
 
        //验证
        public bool Valid()
        {
            if (this.UnitTypeImageComboBoxEdit.EditValue == null)
            {
                XtraMessageBox.Show("请选择单位类型!");
                return false;
            }
            if (!_isNON)
            {
                if (string.IsNullOrEmpty(this.UnitValueComboBoxEdit.Text))
                {
                    XtraMessageBox.Show("请选择/录入单位值!");
                    return false;
                }
            }
 
            return true;
        }
 
 
        //获取时间
        public string Get()
        {
            if (!Valid())
                return default;
            _model.UnitType = (int)this.UnitTypeImageComboBoxEdit.EditValue;
            if (_isNON)
            {
                _model.UnitValue = string.Empty;
            }
            else if (_isCustomer)
            {
                _model.UnitValue = this.UnitValueComboBoxEdit.Text.TrimEnd();
            }
            else
            {
                var keyIntValue = (this.UnitValueComboBoxEdit.SelectedItem as KeyIntValue);
                _model.UnitValue = keyIntValue?.Value.ToString() ?? string.Empty;
            }
 
            var decimalPlaces = this.DecimalPlacesTextEdit.Text.Trim();
            if (!string.IsNullOrEmpty(decimalPlaces))
                _model.DecimalPlaces = int.Parse(decimalPlaces);
            else
                _model.DecimalPlaces = null;
 
            return _model.ToJson();
        }
 
 
    }
}