duheng
2024-11-08 bb817e5d4ba0afa9848cc80375ee2e10105e106e
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using DevExpress.UIAutomation;
using DevExpress.Utils.DragDrop;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Handler;
using DevExpress.XtraTreeList.Nodes;
using SqlSugar;
using System.ComponentModel;
using System.Reflection;
 
namespace HStation.WinFrmUI.Basic
{
    public partial class SysCatalogManageMainPanel : DocumentPage
    {
        public SysCatalogManageMainPanel()
        {
            InitializeComponent();
            this.PageTitle.Caption = "分类管理";
            this.treeList1.InitialMultiColSettings();
            this.PageTitle.HeaderSvgImage = this.svgImage32[0];
            this.PageTitle.SvgImageSize = new Size(24, 24);
            this.typeTreeListCtrl2.FocusedChangedEvent += TypeTreeListCtrl2_FocusedChangedEvent;
        }
 
        private List<SysCatalogViewModel> _allBindingList;
 
        private Yw.BLL.SysCatalog _bll;
 
        private List<SysCatalogViewModel> _IndexList;
 
        public override void InitialDataSource()
        {
            this.typeTreeListCtrl2.SetBindingData();
            _bll = new Yw.BLL.SysCatalog();
        }
 
        //聚焦切换
        private async void TypeTreeListCtrl2_FocusedChangedEvent(long typeID)
        {
            _allBindingList = new List<SysCatalogViewModel>();
            var alllist = await _bll.GetByTypeID(typeID);
            _allBindingList.Clear();
            foreach (var item in alllist)
            {
                _allBindingList.Add(new SysCatalogViewModel(item));
            }
            this.catalogViewModelBindingSource.DataSource = _allBindingList;
            _IndexList = _allBindingList.Select(x => new SysCatalogViewModel(x)).ToList();
            //_IndexList.Sort((x, y) => x.ParentID.CompareTo(y.ParentID));
            this.catalogViewModelBindingSource.ResetBindings(false);
            this.treeList1.ExpandAll();
        }
 
        //添加
        private void BtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new AddSysCatalogDlg();
            var typeID = this.typeTreeListCtrl2.GetCurrentID();
            if (typeID == 0)
                return;
            dlg.SetBindingData(typeID);
            dlg.ReloadDataEvent += async (rhs) =>
           {
               var id = await _bll.Insert(rhs);
               if (id > 0)
               {
                   var model = await _bll.GetByID(id);
                   _allBindingList.Add(new SysCatalogViewModel(model));
                   this.catalogViewModelBindingSource.ResetBindings(false);
                   return true;
               }
               return false;
           };
            dlg.ShowDialog();
        }
 
        //编辑
        private void barBtnEditPumpCurve_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new EditSysCatalogDlg();
            var vm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return;
            }
            dlg.SetBindingData(vm.ID);
            dlg.ReloadDataEvent += async (rhs) =>
            {
                if (await _bll.Update(rhs))
                {
                    vm.Reset(rhs);
                    this.catalogViewModelBindingSource.ResetBindings(false);
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        //删除
        private async void BtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return;
            }
            if (MessageBoxHelper.IsClickOk($"确认删除数据行?", "提示"))
                return;
            try
            {
                var result = await _bll.DeleteByID(currentVm.ID);
                if (result)
                {
                    _allBindingList.Remove(currentVm);
                    this.catalogViewModelBindingSource.ResetBindings(false);
                    MessageBoxHelper.ShowSuccess($"删除成功!");
                }
                else
                {
                    MessageBoxHelper.ShowError($"删除失败!");
                    return;
                }
            }
            catch (Yw.Dto.InternalException ex)
            {
                MessageBoxHelper.ShowError(ex.ErrorMsg);
            }
        }
 
        //相应属性编辑
        private async void BtnPropEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm != null)
            {
                var dlg = new SetSysPropForCatalogDlg();
                dlg.SetBindingData(currentVm.ID);
                dlg.ShowDialog();
            }
            else
            {
                MessageBoxHelper.ShowWarning("选择数据行!");
                return;
            }
        }
 
        /// <summary>
        ///设置父级
        /// </summary>
        private void barBtnUpdateParent_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.treeList1.GetCurrentViewModel(_allBindingList);
            if (currentVm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return;
            }
            var dlg = new UpdateCatalogParentDlg();
            dlg.SetBindingData(currentVm.TypeID, currentVm.ID, currentVm.ParentID);
            dlg.ReloadEvent += async (parentId) =>
            {
                var sortCode = GetDataCountByNodeID(parentId);
                var bol = await _bll.UpdateTreeSortCode(currentVm.ID, parentId, sortCode + 1);
                if (bol)
                {
                    this.InitialDataSource();
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        private TreeListNode currentDragNode = null;
 
        private async void treeList1_DragDrop(object sender, DragEventArgs e)
        {
            currentDragNode = (TreeListNode)e.Data.GetData(typeof(TreeListNode));
            var currentvm = GetViewModel(currentDragNode);
            var dragInsertPosition = AjustDirection(sender, e);
            var targetNode = GetTargetNode(e);
            var targetVm = GetViewModel(targetNode);
            var currentIndex = GetIndexOf(currentvm, _IndexList); //当前位置索引
            var targetIndex = GetIndexOf(targetVm, _IndexList);  //目标位置索引
            if (dragInsertPosition == DragInsertPosition.AsChild || targetVm == null || currentvm == null)
            {
                e.Effect = DragDropEffects.None;
                return;
            }
            if (dragInsertPosition == DragInsertPosition.After)
            {
                _IndexList.RemoveAt(currentIndex);
                _IndexList.Insert(targetIndex, currentvm);
            }
            else if (dragInsertPosition == DragInsertPosition.Before)
            {
                _IndexList.RemoveAt(currentIndex);
                _IndexList.Insert(targetIndex, currentvm);
            }
            if (currentvm.ParentID == targetVm.ParentID)
            {
                e.Effect = await SetTreeListSorter(currentvm.ParentID) ? DragDropEffects.Move : DragDropEffects.None;
                return;
            }
            e.Effect = DragDropEffects.None;
        }
 
        /// <summary>
        /// 获取拖动过程中的方向
        /// </summary>
        private DragInsertPosition AjustDirection(object sender, DragEventArgs e)
        {
            PropertyInfo pi = typeof(TreeList).GetProperty("Handler", BindingFlags.Instance | BindingFlags.NonPublic);
            TreeListHandler handler = (TreeListHandler)pi.GetValue(this.treeList1, null);
            return handler.StateData.DragInfo.DragInsertPosition;
        }
 
        private async Task<bool> SetTreeListSorter(long parentId)
        {
             var sortList = new List<Yw.Vmo.Sorter>();
            var targetList = _IndexList.Where(x => x.ParentID == parentId).ToList();
            int i = 1;
            foreach (var item in targetList)
            {
                sortList.Add(new Yw.Vmo.Sorter { ID = item.ID, SortCode = i });
                i++;
            }
            if (!await _bll.UpdateSorter(sortList))
            {
                TipFormHelper.ShowError("修改排序失败!");
                return false;
            }
            return true;
            //  this.catalogViewModelBindingSource.ResetBindings(false);
        }
 
        private TreeListNode GetTargetNode(DragEventArgs e)
        {
            var point = new Point(e.X, e.Y);
            var clientPoint = this.treeList1.PointToClient(point);
            var targetNode = this.treeList1.GetNodeAt(clientPoint);
            return targetNode;
        }
 
        private SysCatalogViewModel GetViewModel(TreeListNode node)
        {
            if (node == null)
                return default;
            var vm = this.treeList1.GetDataRecordByNode(node) as SysCatalogViewModel;
            return vm;
        }
 
        private void barCheckSorter_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (barCheckSorter.Checked)
            {
                this.treeList1.OptionsDragAndDrop.DragNodesMode = DragNodesMode.Single;
            }
            else
            {
                this.treeList1.OptionsDragAndDrop.DragNodesMode = DragNodesMode.None;
            }
        }
 
        //返回节点下所有子节点个数
        private int GetDataCountByNodeID(long nodeID)
        {
            TreeListNode node = treeList1.FindNodeByFieldValue("ID", nodeID);
            if (node == null)
            {
                return 0;
            }
            return node.Nodes.Count;
        }
 
        private CurrentTreeList GetTreeModel(TreeListNode node)
        {
            if (node == null)
                return default;
 
            var vm = GetViewModel(node);
            var model = new CurrentTreeList();
            model.ID = vm.ID;
            if (node.Nodes != null && node.Nodes.Any())
            {
                model.Children = new List<CurrentTreeList>();
                foreach (TreeListNode childNode in node.Nodes.Cast<TreeListNode>())
                {
                    var childModel = GetTreeModel(childNode);
                    model.Children.Add(childModel);
                }
            }
            return model;
        }
 
        private class CurrentTreeList
        {
            public CurrentTreeList()
            {
            }
 
            public long ID { get; set; }
            public List<CurrentTreeList> Children { get; set; }
        }
 
        private int GetIndexOf(SysCatalogViewModel vm, List<SysCatalogViewModel> dataSourse)
        {
            int i = 0;
            foreach (var item in dataSourse)
            {
                if (item.ID == vm.ID)
                {
                    return i;
                }
                i++;
            }
            return i;
        }
    }
}