Shuxia Ning
2024-11-19 a01861a95ede48fa4979a47b24f21616e362e534
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
using DevExpress.Utils.DragDrop;
 
namespace HStation.WinFrmUI.Basic
{
    public partial class SysTypeManageMainPanel : DocumentPage
    {
        public SysTypeManageMainPanel()
        {
            InitializeComponent();
            this.gridView1.SetNormalView();
            this.gridView1.RegistCustomDrawRowIndicator();
            this.PageTitle.Caption = "类型管理";
            this.PageTitle.HeaderSvgImage = this.svgImage32[0];
            this.PageTitle.SvgImageSize = new Size(24, 24);
            this.moduleTreeListCtrl1.FocusedChangedEvent += ModuleTreeListCtrl1_FocusedChangedEvent;
        }
 
        private List<SysTypeViewModel> _allBindingList = new List<SysTypeViewModel>();
 
        private int _LastRowIndex;
 
        private Yw.BLL.SysType _bll = null;
 
        public override void InitialDataSource()
        {
            SetBindingData();
        }
 
        //聚焦切换
        private async void ModuleTreeListCtrl1_FocusedChangedEvent(long moduleid)
        {
            var alllist = await _bll.GetByModuleID(moduleid);
            _allBindingList.Clear();
            foreach (var item in alllist)
            {
                _allBindingList.Add(new SysTypeViewModel(item));
            }
            this.typeViewModelBindingSource.ResetBindings(false);
        }
 
        private async void SetBindingData()
        {
            this.moduleTreeListCtrl1.SetBindingData();
            _bll = new Yw.BLL.SysType();
            this.typeViewModelBindingSource.DataSource = _allBindingList;
            this.typeViewModelBindingSource.ResetBindings(false);
        }
 
        //添加
        private void BtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new AddSysTypeDlg();
            var moduleid = this.moduleTreeListCtrl1.GetCurrentID();
            if (moduleid == 0)
            {
                MessageBoxHelper.ShowWarning("请选择数据行!");
                return;
            }
            dlg.SetBindingData(moduleid);
            dlg.ReloadDataEvent += async (rhs) =>
            {
                var id = await _bll.Insert(rhs);
                if (id > 0)
                {
                    var model = await _bll.GetByID(id);
                    _allBindingList.Add(new SysTypeViewModel(model));
                    this.typeViewModelBindingSource.ResetBindings(false);
                    return true;
                }
 
                return false;
            };
 
            dlg.ShowDialog();
        }
 
        //编辑
        private void barBtnEditPumpCurve_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new EditSysTypeDlg();
            var vm = this.gridView1.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.typeViewModelBindingSource.ResetBindings(false);
                    return true;
                }
                return false;
            };
            dlg.ShowDialog();
        }
 
        //删除
        private async void BtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var currentVm = this.gridView1.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.typeViewModelBindingSource.ResetBindings(false);
                    MessageBoxHelper.ShowSuccess($"删除成功!");
                }
                else
                {
                    MessageBoxHelper.ShowError($"删除失败!");
                    return;
                }
            }
            catch (Yw.Dto.InternalException ex)
            {
                MessageBoxHelper.ShowError(ex.ErrorMsg);
                return;
            }
        }
 
        //允许拖拽排序
        private void barCheckItem1_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (barCheckSorter.Checked)
            {
                behaviorManager1.Attach<DragDropBehavior>(gridView1, behavior =>
                {
                    behavior.BeginDragDrop += Behavior_BeginDragDrop;
                    behavior.DragDrop += Behavior_DragDrop;
                    behavior.EndDragDrop += Behavior_EndDragDrop;
                });
            }
            else
            {
                behaviorManager1.Detach<DragDropBehavior>(gridView1);
            }
        }
 
        private void Behavior_DragDrop(object sender, DragDropEventArgs e)
        {
            //behaviorManager1.Detach<DragDropBehavior>(gridView1);
            return;
        }
 
        private void Behavior_DragEnter(object sender, DragEnterEventArgs e)
        {
            return;
        }
 
        private void Behavior_BeginDragDrop(object sender, BeginDragDropEventArgs e)
        {
            _LastRowIndex = gridView1.FocusedRowHandle;
            var currentCellValue = gridView1.GetFocusedValue();
        }
 
        //拖拽排序操作
        private async void Behavior_EndDragDrop(object sender, EndDragDropEventArgs e)
        {
            var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
            var sorterList = new List<Yw.Vmo.Sorter>();
            int i = 1;
            foreach (var item in _allBindingList)
            {
                sorterList.Add(new Yw.Vmo.Sorter() { ID = item.ID, SortCode = i });
                i++;
            }
            if (await _bll.UpdateSorter(sorterList))
            {
                this.typeViewModelBindingSource.ResetBindings(false);
            }
            else
            {
                gridView1.FocusedRowHandle = _LastRowIndex;
                TipFormHelper.ShowError("修改排序失败!");
                return;
            }
        }
    }
}