tangxu
2024-02-27 707a73304e0406b865548645c7cd1880a3651cc4
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using DevExpress.Utils.Colors;
using DevExpress.Utils.Extensions;
using DevExpress.XtraEditors;
using IStation.Model;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms; 
 
namespace IStation.WinFrmUI.Basic
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ElecPriceMgrCtrl : XtraUserControl
    {
        public ElecPriceMgrCtrl()
        {
            InitializeComponent();
            this.gridView1.SetNormalView();
            this.gridView1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.gridView1.OptionsView.AllowCellMerge = true;
            this.gridView1.ActiveFilterEnabled = false;
            this.gridView1.ShowViewCaption();
            this.layoutControl1.SetupLayoutControl();
        }
 
        public class CurrentViewModel : Model.ElecPriceHourSetting
        {
            public CurrentViewModel() { }
 
            public CurrentViewModel(Model.ElecPriceHourSetting rhs, string belongName) : base(rhs)
            {
                this.BelongName = belongName;
            }
 
            public string BelongName { get; set; }
 
            public int StartMonth { get; set; }
 
            public int EndMonth { get; set; }
        }
 
 
        private BindingList<CurrentViewModel> _allBindingList = null;//所有绑定列表
                                                                    
 
 
        private Model.ElecPrice _elecPrice = null;
        //提问:这个这个和实例化有什么区别
        
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void Clear()
        {
            _allBindingList = new BindingList<CurrentViewModel>();
            this.currentViewModelBindingSource.DataSource = _allBindingList;
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData()
        {
            WaitFrmHelper.ShowWaitForm("正在加载数据...");
            _allBindingList = new BindingList<CurrentViewModel>();
            var bll = new BLL.ElecPrice();
            _elecPrice = bll.GetAll().First();
            if (_elecPrice != null)
            {
               this.gridView1.ViewCaption = _elecPrice.Name;
            
                if (_elecPrice.Settings != null && _elecPrice.Settings.MonthList != null)
                {
                    var months = _elecPrice.Settings.MonthList.OrderBy(x => x.StartMonth);
                    foreach (var month in months)
                    {
                        if (month.HourList == null)
                            continue;
                        var belongName = $"{month.StartMonth}~{month.EndMonth}(月)";
                        var hours = month.HourList.OrderBy(x => x.StartHour);
                        foreach (var hour in hours)
                        {
                            var vm = new CurrentViewModel(hour, belongName);
                            vm.StartMonth = month.StartMonth;
                            vm.EndMonth = month.EndMonth;
                            _allBindingList.Add(vm);
                        }
                    }
 
                }
            }
            this.currentViewModelBindingSource.DataSource = _allBindingList;
            this.gridView1.BestFitColumns();
            WaitFrmHelper.HideWaitForm();
        }
 
        //添加电费
        public void AddElePrice()
        {
            var dlg = new AddElectricDlg();
            dlg.SetBindingData();
            dlg.ReloadDataEvent += (price) =>
            {
                _elecPrice.Settings.MonthList.Add(price);
                var bll = new BLL.ElecPrice();
                var isok = bll.Update(_elecPrice);
                if (isok)
                {
                    this.SetBindingData();
                    this.currentViewModelBindingSource.ResetBindings(false);
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
        //  var oldbelongName= _allBindingList.Where(e=>e.BelongName==belongName).FirstOrDefault();
        //if (oldbelongName == null)
 
        //编辑电费
        public void EditElePrice()
        {
            var row = gridView1.GetCurrentViewModel(_allBindingList);
            if (row == null)
                return;
            var editTime = _elecPrice.Settings;  //提问:这句话点不到hourlist   new一个monthsetting就能.到hourlist
            var FindMonth = editTime.MonthList.Find(x => x.StartMonth == row.StartMonth && x.EndMonth == row.EndMonth);
            var addHour = _elecPrice.Settings.MonthList.IndexOf(FindMonth);
            var editHour = editTime.MonthList[addHour];
            var dlg = new EditElectricDlg();
            dlg.SetBindingData(editHour.HourList);
            dlg.ReLoadDataevent += (price) =>
            {
                editHour.HourList.Clear();
                editHour.HourList=price;
                var bll = new BLL.ElecPrice();
                var isok = bll.Update(_elecPrice);
                if (isok)
                {
                    this.currentViewModelBindingSource.ResetBindings(false);
                    this.SetBindingData();
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
 
        }
        //检查按钮
 
        public bool PriceCheck()
        {
            //判断月份
            var allMonths = _elecPrice.Settings.MonthList;
            allMonths = allMonths.OrderBy(x => x.EndMonth).ToList();
            var monthCount = allMonths.Count;
            if (allMonths.First().StartMonth != 1 || allMonths.Last().EndMonth != 12)
            {
                XtraMessageBox.Show("开始月份和结束月份有误");
                return false;
            }
            for (int i = 0; i < monthCount - 1; i++)
            {
                if (allMonths[i].EndMonth + 1 != allMonths[i + 1].StartMonth)
                {
                    XtraMessageBox.Show("月份输入有误");
                    return false;
                }
 
            }
 
            //判断时间
            for (int k = 0; k < monthCount; k++)
            {
                var eleprice = allMonths[k];
                var hourList = eleprice.HourList.OrderBy(x => x.StartHour).ToList();
                if (hourList.First().StartHour != 1 || hourList.Last().EndHour != 24)
                {
                    XtraMessageBox.Show("结束时间或开始时间输入有误");
                    return false;
                }
                for (int x = 0; x < eleprice.HourList.Count - 1; x++)
                {
                    var frist = hourList[x];
                    var second = hourList[x + 1];
                    var last = hourList[eleprice.HourList.Count - 1];
                    if (second.StartHour == second.EndHour)
                    {
                        XtraMessageBox.Show("开始时间和结束时间相同");
                        return false;
                    }
                    if (frist.EndHour != second.StartHour)
                    {
                        XtraMessageBox.Show("时间输入有误");
                        return false;
                    }
                }
            }
            XtraMessageBox.Show("输入正确");
            return false;
 
        }
        //var eleprice = allMonths[i];
        //var hourList = eleprice.HourList.OrderBy(x => x.StartHour).ToList();
        //for (int j = 0; i < eleprice.HourList.Count; j++)
        //{
        //    if (hourList[j].EndHour != hourList[j + 1].StartHour)
        //    {
        //        XtraMessageBox.Show("时间输入有误");
        //        return;
        //    }
        //}
        // var monthCount = _elecPrice.Settings.MonthList;
        //for (int i = 0; i < monthCount; i++)
        /// <summary>
        /// 删除电费
        /// </summary>
        public void DeletePrice()
        {
            var row = this.gridView1.GetCurrentViewModel(_allBindingList);
            if (row == null)
                return;
            if (XtraMessageBox.Show("确定要删除所选中?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) != DialogResult.OK)
                return;
 
            _elecPrice.Settings.MonthList.RemoveAll(x => x.StartMonth == row.StartMonth && x.EndMonth == row.EndMonth);
 
            var isok = new BLL.ElecPrice().Update(_elecPrice);
            if (isok)
            {
                this.currentViewModelBindingSource.ResetBindings(false);
            }
             this.SetBindingData();
            this.currentViewModelBindingSource.ResetBindings(false);
            MessageBox.Show("删除成功", "提示");
        }
    }
}