lixiaojun
2025-02-18 1f7091dac2a5dddf4a0a40acb0940d3787cf35f5
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
using DevExpress.XtraEditors;
 
namespace Yw.WinFrmUI
{
    public partial class HydroWorkingMgrDlg : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public HydroWorkingMgrDlg()
        {
            InitializeComponent();
            this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon;
            this.layoutControl1.SetupLayoutControl();
            this.gridView1.SetNormalEditView();
            this.generalOkAndCancelCtrl1.OkEvent += GeneralOkAndCancelCtrl1_OkEvent;
            this.repositoryItemButtonEdit1.ButtonClick += RepositoryItemButtonEdit1_ButtonClick;
        }
 
        /// <summary>
        /// 重载数据事件
        /// </summary>
        public event Action<Dictionary<HydroWorkingVmo, bool>> ReloadDataEvent;
 
        /// <summary>
        /// 更新数据事件
        /// </summary>
        public event Action<HydroWorkingVmo> UpdateDataEvent;
 
        /// <summary>
        /// 应用数据事件
        /// </summary>
        public event Action<HydroWorkingVmo> ApplyDataEvent;
 
        /// <summary>
        /// 删除数据事件
        /// </summary>
        public event Action<HydroWorkingVmo> DeleteDataEvent;
 
        private BindingList<HydroWorkingMgrViewModel> _allBindingList = null;//所有绑定列表
 
        /// <summary>
        /// 绑定列表
        /// </summary>
        public void SetBindingData(Dictionary<HydroWorkingVmo, bool> dict)
        {
            _allBindingList = new BindingList<HydroWorkingMgrViewModel>();
            if (dict != null && dict.Count > 0)
            {
                foreach (var item in dict)
                {
                    var vm = new HydroWorkingMgrViewModel(item.Key, item.Value);
                    _allBindingList.Add(vm);
                }
            }
            this.hydroWorkingMgrViewModelBindingSource.DataSource = _allBindingList;
            this.hydroWorkingMgrViewModelBindingSource.ResetBindings(false);
        }
 
        //获取当前视图
        private HydroWorkingMgrViewModel GetCurrentViewModel()
        {
            if (_allBindingList == null)
            {
                TipFormHelper.ShowError("数据初始化失败!");
                return null;
            }
            if (_allBindingList.Count < 1)
            {
                TipFormHelper.ShowInfo("无数据!");
                return null;
            }
            var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                TipFormHelper.ShowWarn("请选择数据行!");
                return null;
            }
            return vm;
        }
 
        //编辑
        private void Edit()
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var dlg = new EditHydroWorkingDlg();
            dlg.ReloadDataEvent += (rhs) =>
            {
                vm.Reset(rhs);
                this.UpdateDataEvent?.Invoke(vm.Vmo);
                this.gridView1.RefreshRow(this.gridView1.FocusedRowHandle);
                TipFormHelper.ShowSucceed("更新成功");
            };
            dlg.SetBindingData(vm.Vmo);
            dlg.ShowDialog();
        }
 
        //应用
        private void Apply()
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            this.ApplyDataEvent?.Invoke(vm.Vmo);
            TipFormHelper.ShowSucceed("应用成功");
        }
 
        //删除
        private async void Delete()
        {
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var result = XtraMessageBox.Show("请问确认删除当前数据吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes;
            if (!result)
            {
                return;
            }
            var bol = await BLLFactory<Yw.BLL.HydroWorking>.Instance.DeleteByID(vm.ID);
            if (!bol)
            {
                TipFormHelper.ShowError("删除失败!");
                return;
            }
            _allBindingList.Remove(vm);
            this.DeleteDataEvent?.Invoke(vm.Vmo);
            this.hydroWorkingMgrViewModelBindingSource.ResetBindings(false);
            TipFormHelper.ShowSucceed("删除成功!");
        }
 
        //确定
        private void GeneralOkAndCancelCtrl1_OkEvent()
        {
            if (_allBindingList == null)
            {
                TipFormHelper.ShowError("数据初始化失败!");
                return;
            }
            var dict = new Dictionary<HydroWorkingVmo, bool>();
            foreach (var item in _allBindingList)
            {
                dict.Add(item.Vmo, item.HasChecked);
            }
            this.ReloadDataEvent?.Invoke(dict);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
 
        //操作点击
        private void RepositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var tag = e.Button.Tag?.ToString();
            switch (tag)
            {
                case "edit": Edit(); break;
                case "apply": Apply(); break;
                case "delete": Delete(); break;
                default: break;
            }
        }
 
 
    }
}