duheng
2025-01-17 9c3d55bf7ad8c9d373dd542e0321e94c8e457ee1
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using DevExpress.Utils.DragDrop;
using DevExpress.XtraBars.Docking2010;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Yw;
using Yw.WinFrmUI;
 
namespace HStation.WinFrmUI
{
    /// <summary>
    /// 菜单管理界面
    /// </summary>
    public partial class MenuMgrPage : DocumentPage
    {
        public MenuMgrPage()
        {
            InitializeComponent();
            this.treeList1.InitialMultiColSettings(30);
            this.PageTitle.Caption = "菜单管理";
            this.PageTitle.SvgImageSize = new Size(24, 24);
        }
 
        private List<MenuViewModel> _allBindingList = null;//绑定列表
 
        private Yw.BLL.ProjectMenu _bll = new Yw.BLL.ProjectMenu();
 
        /// <summary>
        /// 初始化数据
        /// </summary>
        public override async void InitialDataSource()
        {
            var allList = await new Yw.BLL.ProjectMenu().GetByProjectID(GlobalParas._GlobalParas.ProjectID);
            _allBindingList = new List<MenuViewModel>();
            if (allList != null)
            {
                foreach (var item in allList)
                {
                    var model = new MenuViewModel(item);
                    _allBindingList.Add(model);
                }
            }
            this.menuViewModelBindingSource.DataSource = _allBindingList;
            this.menuViewModelBindingSource.ResetBindings(false);
            //  this.treeList1.ForceInitialize();
            this.treeList1.ExpandAll();
        }
 
        public override void VerifyAuth()
        {
            this.AuthTree.VerifyAuth(this.ribbonControl1);
        }
 
        //添加
        private void barBtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_allBindingList == null)
                return;
            var row = this.treeList1.GetCurrentViewModel(_allBindingList);
            var dlg = new AddMenuDlg();
            dlg.SetBindingData(row != null ? row.ID : 0);
            dlg.ReloadDataEvent += async (rhs) =>
            {
                var id = await _bll.InsertEx(rhs);
                if (id > 0)
                {
                    rhs.ID = id;
                    this._allBindingList.Add(new MenuViewModel(rhs));
                    this.menuViewModelBindingSource.ResetBindings(false);
                    //this.treeList1.Refresh();
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        //编辑
        private async void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_allBindingList == null)
                return;
            var row = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (row == null)
                return;
            var dlg = new EditMenuDlg();
            var model = await _bll.GetByID(row.ID);
            if (model == null)
                return;
            dlg.SetBindingData(model);
            dlg.ReloadDataEvent += async (rhs) =>
            {
                if (await _bll.Update(rhs))
                {
                    row.Reset(rhs);
                    this.treeList1.RefreshNode(this.treeList1.FocusedNode);
                    return true;
                }
                return false;
            };
 
            dlg.ShowDialog();
        }
 
        //显示详细信息
        private void barBtnInfo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_allBindingList == null)
                return;
            var row = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (row == null)
                return;
            var dlg = new ViewMenuDlg();
            dlg.SetBindingData(row);
            dlg.ShowDialog();
        }
 
        //删除
        private async void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_allBindingList == null)
                return;
            var row = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (row == null)
                return;
            if (XtraMessageBox.Show($"确认删除数据行?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) != DialogResult.OK)
                return;
 
            if (!await new Yw.BLL.ProjectMenu().DeleteByID(row.ID))
            {
                XtraMessageBox.Show($"删除失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _allBindingList.Remove(row);
            this.menuViewModelBindingSource.ResetBindings(false);
            //this.treeList1.RefreshDataSource();
            XtraMessageBox.Show($"删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
 
        //设置菜单功能点
        private void SetFuncList()
        {
            /*  if (_allBindingList == null)
                  return;
              var row = this.treeList1.GetCurrentViewModel(_allBindingList);
              if (row == null)
                  return;
              var page = new FuncMgrPage();
              page.SetBindingData(row);
              page.AuthTree = this.AuthTree?.Children?.Find(x => x.Code == "func");
              this.ToNextPage(page);*/
        }
 
        private async void dragDropEvents1_DragDrop(object sender, DevExpress.Utils.DragDrop.DragDropEventArgs e)
        {
            var nodes = e.Data as List<TreeListNode>;
            if (nodes == null || nodes.Count < 1)
            {
                e.Handled = true;
                return;
            }
            var sourceNode = nodes[0];
            var sourceNodeObj = this.treeList1.GetDataRecordByNode(sourceNode) as MenuViewModel;
            if (sourceNodeObj == null)
            {
                e.Handled = true;
                return;
            }
            var destNode = this.treeList1.GetNodeByCP(e.Location);
            if (destNode == null)
            {
                e.Handled = true;
                return;
            }
            var destNodeObj = this.treeList1.GetDataRecordByNode(destNode) as MenuViewModel;
            if (destNode == null)
            {
                e.Handled = true;
                return;
            }
 
            long id = sourceNodeObj.ID;
            long parentId = 0;
            int sortCode = 0;
 
            var sorters = new List<Yw.Vmo.Sorter>();
 
            if (e.InsertType == InsertType.Before)
            {
                parentId = destNodeObj.ParentID;
                sortCode = destNodeObj.SortCode;
                _allBindingList.ForEach(x =>
                {
                    if (x.ParentID == destNodeObj.ParentID)
                    {
                        if (x.SortCode >= destNodeObj.SortCode)
                        {
                            if (x != sourceNodeObj)
                            {
                                sorters.Add(new Yw.Vmo.Sorter() { ID = x.ID, SortCode = x.SortCode + 1 });
                            }
                        }
                    }
                });
            }
            else if (e.InsertType == InsertType.After)
            {
                parentId = destNodeObj.ParentID;
                sortCode = destNodeObj.SortCode + 1;
                _allBindingList.ForEach(x =>
                {
                    if (x.ParentID == destNodeObj.ParentID)
                    {
                        if (x.SortCode > destNodeObj.SortCode)
                        {
                            if (x != sourceNodeObj)
                            {
                                sorters.Add(new Yw.Vmo.Sorter() { ID = x.ID, SortCode = x.SortCode + 1 });
                            }
                        }
                    }
                });
            }
            else if (e.InsertType == InsertType.AsChild)
            {
                parentId = destNodeObj.ID;
                var allChildList = _allBindingList.Where(x => x.ParentID == destNodeObj.ParentID).ToList();
                sortCode = allChildList.Count < 1 ? 1 : allChildList.Max(x => x.SortCode) + 1;
            }
            else
            {
                e.Handled = true;
                return;
            }
 
            var bll = BLLFactory<Yw.BLL.ProjectMenu>.Instance;
            var bol = await bll.UpdateTreeSortCode(id, parentId, sortCode);
            if (!bol)
            {
                e.Handled = true;
                return;
            }
            if (sorters != null && sorters.Count > 0)
            {
                bol = await bll.UpdateSorter(sorters);
                if (!bol)
                {
                    e.Handled = true;
                    return;
                }
            }
 
            _allBindingList.ForEach(x =>
            {
                if (x.ID == id)
                {
                    x.ParentID = parentId;
                    x.SortCode = sortCode;
                }
                if (sorters.Count > 0)
                {
                    var sorter = sorters.Find(t => t.ID == x.ID);
                    if (sorter != null)
                    {
                        x.SortCode = sorter.SortCode;
                    }
                }
            });
        }
 
        //设置拖拽可用性
        private void SetDragEnable(bool allowArag)
        {
            var be = this.behaviorManager1.GetBehavior<DevExpress.Utils.DragDrop.DragDropBehavior>(this.treeList1);
            be.Properties.AllowDrag = allowArag;
        }
 
        private void barBtnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.treeList1.OptionsFind.AlwaysVisible = !this.treeList1.OptionsFind.AlwaysVisible;
        }
 
        private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.InitialDataSource();
        }
 
        private void barCkDrag_CheckedChanged_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetDragEnable(this.barCkDrag.Checked);
        }
    }
}