duheng
2025-03-24 6e1dd6f75cd7265f549a8b9e4d77ff5d88da9651
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
using DevExpress.XtraEditors;
using DevExpress.XtraGantt.Scheduling;
using DevExpress.XtraGrid.Views.Grid;
using System.ComponentModel;
using Yw.WinFrmUI;
 
namespace PBS.WinFrmUI
{
    public partial class ElecPriceMgr : Yw.WinFrmUI.DocumentPage
    {
        public ElecPriceMgr()
        {
            InitializeComponent();
            this.gridView1.SetNormalView(30);
            this.gridView1.CellMerge += GridView1_CellMerge;
            this.gridView1.OptionsView.AllowCellMerge = true;
        }
 
        private void BtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddElePrice();
        }
 
        private BindingList<ElecPriceViewModel> _allBindingList = null;//所有绑定列表
 
        private PBS.Vmo.ElecPriceVmo _elecPrice = null;
 
        public override void InitialDataSource()
        {
            SetBindingData();
        }
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public async void SetBindingData()
        {
            _allBindingList = new BindingList<ElecPriceViewModel>();
            var bll = new BLL.ElecPrice();
            var allList = await bll.GetAll();
            if (allList != null)
                _elecPrice = allList.First();
            if (_elecPrice != null)
            {
                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 ElecPriceViewModel(belongName, hour);
                            vm.StartMonth = month.StartMonth;
                            vm.EndMonth = month.EndMonth;
                            _allBindingList.Add(vm);
                        }
                    }
                }
                else
                {
                    _elecPrice.Settings = new Vmo.ElecPriceSetting();
                    _elecPrice.Settings.MonthList = new List<Vmo.ElecPriceMonthSetting> { new Vmo.ElecPriceMonthSetting() };
                }
            }
            this.elecPriceViewModelBindingSource.DataSource = _allBindingList;
            this.elecPriceViewModelBindingSource.ResetBindings(false);
            //this.gridView1.BestFitColumns();
        }
 
        //添加电费
        public void AddElePrice()
        {
            var dlg = new AddElectricDlg();
            dlg.SetBindingData();
            dlg.ReloadDataEvent += async (price) =>
            {
                var bll = new BLL.ElecPrice();
                _elecPrice.Settings.MonthList.Add(price);
                var bol = await bll.Update(_elecPrice);
                if (bol)
                {
                    foreach (var item in price.HourList)
                    {
                        this._allBindingList.Add(new ElecPriceViewModel
                        {
                            BelongName = $"{price.StartMonth}~{price.EndMonth}(月)",
                            StartHour = item.StartHour,
                            EndHour = item.EndHour,
                            Price = item.Price
                        });
                    }
                    this.elecPriceViewModelBindingSource.ResetBindings(false);
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        //编辑电费
        public void EditElePrice()
        {
            var row = gridView1.GetCurrentViewModel(_allBindingList);
            if (row == null)
                return;
            var editTime = _elecPrice.Settings;
            var FindMonth = editTime.MonthList.Find(x => x.StartMonth == row.StartMonth && x.EndMonth == row.EndMonth);
            var dlg = new EditElectricDlg();
            dlg.SetBindingData(FindMonth.HourList);
            dlg.ReLoadDataevent += async (price) =>
            {
                FindMonth.HourList.Clear();
                FindMonth.HourList = price;
                var bll = new BLL.ElecPrice();
                var isok = await bll.Update(_elecPrice);
                if (isok)
                {
                   this.SetBindingData();
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        /// <summary>
        /// 删除电费
        /// </summary>
        public async 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 = await new BLL.ElecPrice().Update(_elecPrice);
            if (isok)
            {
                this.elecPriceViewModelBindingSource.ResetBindings(false);
            }
            this.SetBindingData();
            this.elecPriceViewModelBindingSource.ResetBindings(false);
            MessageBox.Show("删除成功", "提示");
        }
 
        //控制只允许分组合并
        private void GridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)
        {
            if (e.Column.FieldName == "BelongName")
            {
                // 获取当前单元格和下一个单元格的值
                var value1 = gridView1.GetRowCellValue(e.RowHandle1, e.Column);
                var value2 = gridView1.GetRowCellValue(e.RowHandle2, e.Column);
 
                // 如果两个单元格的值相等,则允许合并
                e.Merge = value1 != null && value1.Equals(value2);
                e.Handled = true;
            }
            else
            {
                // 其他列不允许合并
                e.Merge = false;
                e.Handled = true;
            }
        }
 
        private void BtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EditElePrice();
        }
 
        private void BtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeletePrice();
        }
    }
}