using DevExpress.Utils.DragDrop; using DevExpress.XtraEditors; using Yw.WinFrmUI; namespace PBS.WinFrmUI { public partial class FacilityMgrPage : DocumentPage { public FacilityMgrPage() { InitializeComponent(); this.gridView1.SetNormalView(30); behaviorManager1.Detach(gridView1); } private List _allBindingList; private BLL.Facility _facilityBll = new(); public override void InitialDataSource() { SetBindingData(); } private async void SetBindingData() { _allBindingList = new List(); 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) { 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) { 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); 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(this.gridView1); } else { behaviorManager1.Detach(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(); 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(); 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(); 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; } } }