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
using DevExpress.XtraEditors;
using IStation.Unit;
using System;
using System.Windows.Forms;
 
namespace IStation.WinFrmUI
{
    /// <summary>
    /// 设置一天小时段
    /// </summary>
    public partial class NumericFormatParasCtrl : UserControl, IFormatParas
    {
        public NumericFormatParasCtrl()
        {
            InitializeComponent();
        }
 
 
        private Model.SignalType.NumericFormatParas _model = null;
 
 
        private bool _isNON = false;//0
        private bool _isCustomer = false;//999
 
 
        //初始化数据
        public void Set(string paras)
        {
 
            this.UnitTypeImageComboBoxEdit.Properties.AddEnum(typeof(Unit.eUnitType), false);
            _model = Model.SignalType.NumericFormatParas.ToModel(paras);
            if (_model == null)
            {
                _model = new Model.SignalType.NumericFormatParas();
                this.UnitTypeImageComboBoxEdit.SelectedIndex = 0;
            }
            else
            {
                this.UnitTypeImageComboBoxEdit.EditValue = _model.UnitType;
            }
 
            this.DecimalPlacesTextEdit.EditValue = _model.DecimalPlaces;
        }
 
 
        //单位类型变换
        private void UnitTypeImageComboBoxEdit_EditValueChanged(object sender, EventArgs e)
        {
            var code = (Unit.eUnitType)this.UnitTypeImageComboBoxEdit.EditValue;
            SetUnitValueComboBoxEdit(code);
        }
 
        private void SetUnitValueComboBoxEdit(Unit.eUnitType unitType)
        {
            _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;
 
            var unitValueDict = UnitHelper.GetEnUnitDict(unitType);
            if (unitValueDict == null || unitValueDict.Count < 1)
                return;
            foreach (var item in unitValueDict)
            {
                var keyIntValue = new KeyIntValue(item.Value, item.Key);
                this.UnitValueComboBoxEdit.Properties.Items.Add(keyIntValue);
            }
 
            this.UnitValueComboBoxEdit.SelectedIndex = 0;
            if (unitType == _model.UnitType)
            {
                if (int.TryParse(_model.UnitValue, out int value))
                {
                    this.UnitValueComboBoxEdit.SelectedIndex = value;
                }
            }
 
 
        }
 
 
        //验证
        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 = (Unit.eUnitType)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();
        }
 
 
    }
}