duheng
2025-04-18 d2b91c9d98f38ccbd657f2adff1473aba29f66b2
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using DevExpress.Utils.DragDrop;
using DevExpress.XtraEditors;
using HStation.WinFrmUI;
using Yw;
using Yw.WinFrmUI;
 
namespace PBS.WinFrmUI
{
    public partial class FacilityMgrPage : DocumentPage
    {
        public FacilityMgrPage()
        {
            InitializeComponent();
            this.gridView1.SetNormalView(30);
            this.gridView1.Columns["PlaceID"].GroupIndex = 0;
            this.gridView1.GroupFormat = "{1}";
            behaviorManager1.Detach<DragDropBehavior>(gridView1);
        }
 
        private List<FacilityViewModel> _allBindingList;
 
        private BLL.Facility _facilityBll = new();
 
        public override void InitialDataSource()
        {
            SetBindingData();
        }
 
        private async void SetBindingData()
        {
            _allBindingList = new List<FacilityViewModel>();
            var allList = await _facilityBll.GetAll();
            if (allList != null)
            {
                foreach (var item in allList)
                {
                    _allBindingList.Add(new FacilityViewModel(item));
                }
            }
            this.facilityViewModelBindingSource.DataSource = _allBindingList;
            this.facilityViewModelBindingSource.ResetBindings(false);
            var allFacility = await new PBS.BLL.Place().GetAll();
            repositoryItemTreeListLookUpEdit1.DataSource = allFacility;
        }
 
        //新增
        private void BtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new AddFacilityDlg();
            dlg.SetBindingData();
            dlg.ReloadDataEvent += async (vmo) =>
           {
               var id = await _facilityBll.Insert(vmo);
               if (id > 0)
               {
                   if (vmo.PackageID > 0)
                   {
                       BLL.PackageCurveFileModel.SaveCurveFile(id, vmo.PackageID);
                   }
                   vmo.ID = id;
                   _allBindingList.Add(new FacilityViewModel(vmo));
                   this.facilityViewModelBindingSource.ResetBindings(false);
                   return true;
               }
               return false;
           };
            dlg.ShowDialog();
        }
 
        //编辑
        private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                TipFormHelper.ShowWarn("请选择数据行");
            }
            var dlg = new EditFacilityDlg();
            dlg.SetBindingData(vm.Vmo);
            dlg.ReloadDataEvent += async (vmo) =>
            {
                var bol = await _facilityBll.Update(vmo);
                if (bol)
                {
                    if (vmo.PackageID > 0)
                    {
                        BLL.PackageCurveFileModel.SaveCurveFile(vmo.ID, vmo.PackageID);
                    }
                    vm.Resert(vmo);
                    this.facilityViewModelBindingSource.ResetBindings(false);
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        //删除
        private async void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                return;
            }
            var result = XtraMessageBox.Show("请问确认删除当前数据吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes;
            if (!result)
            {
                return;
            }
            var bol = await _facilityBll.DeleteByID(vm.ID);
            if (!bol)
            {
                TipFormHelper.ShowError("删除失败!");
                return;
            }
            _allBindingList.Remove(vm);
            if (vm.PackageID > 0)
            {
                BLL.PackageCurveFileModel.DeleteCurveFile(vm.ID);
            }
            this.facilityViewModelBindingSource.ResetBindings(false);
            TipFormHelper.ShowSucceed("删除成功!");
        }
 
        //刷新
        private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetBindingData();
        }
 
        //查询
        private void barCekSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView1.OptionsFind.AlwaysVisible = !this.gridView1.OptionsFind.AlwaysVisible;
        }
 
        private void barCkDrag_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (barCkDrag.Checked)
            {
                behaviorManager1.Attach<DragDropBehavior>(this.gridView1);
            }
            else
            {
                behaviorManager1.Detach<DragDropBehavior>(gridView1);
            }
        }
 
        private async void dragDropEvents1_DragDrop(object sender, DevExpress.Utils.DragDrop.DragDropEventArgs e)
        {
            var indexs = e.Data as int[];
            if (indexs == null || indexs.Length < 1)
            {
                e.Handled = true;
                return;
            }
            var sourceIndex = indexs[0];
            var sourceObj = this.gridView1.GetRow(sourceIndex) as FacilityViewModel;
            if (sourceObj == null)
            {
                e.Handled = true;
                return;
            }
 
            var destIndex = this.gridView1.GetRowHandleByCP(e.Location);
            if (destIndex < 0)
            {
                e.Handled = true;
                return;
            }
 
            var destObj = this.gridView1.GetRow(destIndex) as FacilityViewModel;
            if (destObj == null)
            {
                e.Handled = true;
                return;
            }
 
            var sorters = new List<Yw.Vmo.Sorter>();
 
            if (e.InsertType == InsertType.Before)
            {
                sorters.Add(new Yw.Vmo.Sorter() { ID = sourceObj.ID, SortCode = destObj.SortCode });
                _allBindingList.ForEach(x =>
                {
                    if (x.SortCode >= destObj.SortCode)
                    {
                        if (x != sourceObj)
                        {
                            sorters.Add(new Yw.Vmo.Sorter() { ID = x.ID, SortCode = x.SortCode + 1 });
                        }
                    }
                });
            }
            else if (e.InsertType == InsertType.After)
            {
                sorters.Add(new Yw.Vmo.Sorter() { ID = sourceObj.ID, SortCode = destObj.SortCode + 1 });
                _allBindingList.ForEach(x =>
                {
                    if (x.SortCode > destObj.SortCode)
                    {
                        if (x != sourceObj)
                        {
                            sorters.Add(new Yw.Vmo.Sorter() { ID = x.ID, SortCode = x.SortCode + 1 });
                        }
                    }
                });
            }
            else
            {
                e.Handled = true;
                return;
            }
 
            var bol = await _facilityBll.UpdateSorter(sorters);
            if (!bol)
            {
                e.Handled = true;
                return;
            }
 
            _allBindingList.ForEach(x =>
            {
                var sorter = sorters.Find(t => t.ID == x.ID);
                if (sorter != null)
                {
                    x.SortCode = sorter.SortCode;
                }
            });
        }
 
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Up();
        }
 
        //上移
        private async void Up()
        {
            var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                return;
            }
            var rowHandle = this.gridView1.FocusedRowHandle;
            if (rowHandle == 0)
            {
                TipFormHelper.ShowWarn("上移失败!");
                return;
            }
            var current = this.gridView1.GetRow(rowHandle) as FacilityViewModel;
            if (current == null)
            {
                return;
            }
            var prevHandle = rowHandle - 1;
            var prev = this.gridView1.GetRow(prevHandle) as FacilityViewModel;
            if (prev == null)
            {
                return;
            }
            var sorters = new List<Yw.Vmo.Sorter>();
            sorters.Add(new Yw.Vmo.Sorter() { ID = current.ID, SortCode = prev.SortCode });
            sorters.Add(new Yw.Vmo.Sorter() { ID = prev.ID, SortCode = current.SortCode });
            var bol = await _facilityBll.UpdateSorter(sorters);
            if (!bol)
            {
                TipFormHelper.ShowError("上移失败!");
                return;
            }
            var sortCode = current.SortCode;
            current.SortCode = prev.SortCode;
            prev.SortCode = sortCode;
            _allBindingList = _allBindingList.OrderBy(x => x.SortCode).ToList();
            this.facilityViewModelBindingSource.DataSource = _allBindingList;
            this.facilityViewModelBindingSource.ResetBindings(false);
            this.gridView1.FocusedRowHandle = prevHandle;
        }
 
        private void barBtnDown_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Down();
        }
 
        //下移
        private async void Down()
        {
            var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                return;
            }
            var rowHandle = this.gridView1.FocusedRowHandle;
            if (rowHandle == _allBindingList.Count - 1)
            {
                TipFormHelper.ShowWarn("下移失败!");
                return;
            }
            var current = this.gridView1.GetRow(rowHandle) as FacilityViewModel;
            if (current == null)
            {
                return;
            }
            var nextHandle = rowHandle + 1;
            var next = this.gridView1.GetRow(nextHandle) as FacilityViewModel;
            if (next == null)
            {
                return;
            }
            var sorters = new List<Yw.Vmo.Sorter>();
            sorters.Add(new Yw.Vmo.Sorter() { ID = current.ID, SortCode = next.SortCode });
            sorters.Add(new Yw.Vmo.Sorter() { ID = next.ID, SortCode = current.SortCode });
            var bol = await _facilityBll.UpdateSorter(sorters);
            if (!bol)
            {
                TipFormHelper.ShowError("下移失败!");
                return;
            }
            var sortCode = current.SortCode;
            current.SortCode = next.SortCode;
            next.SortCode = sortCode;
            _allBindingList = _allBindingList.OrderBy(x => x.SortCode).ToList();
            this.facilityViewModelBindingSource.DataSource = _allBindingList;
            this.facilityViewModelBindingSource.ResetBindings(false);
            this.gridView1.FocusedRowHandle = nextHandle;
        }
 
        private void barBtnUpdateUseStatus_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                return;
            }
            var dlg = new SetUseStatusDlg();
            dlg.SetBindingData((Yw.Model.eUseStatus)vm.UseStatus);
            dlg.ReloadDataEvent += async (status) =>
            {
                var bol = await new PBS.BLL.Facility().UpdateUseStatus(vm.ID, status);
                if (bol)
                {
                    vm.UseStatus = (Yw.Vmo.eUseStatus)(int)status;
                    this.gridView1.RefreshRow(this.gridView1.FocusedRowHandle);
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
    }
}