using DevExpress.Utils.DragDrop; using HStation.WinFrmUI.Assets; using Yw; namespace HStation.WinFrmUI.Xhs.PumpProduct { public partial class PumpProductMainPage : DocumentPage { public PumpProductMainPage() { InitializeComponent(); this.PageTitle.Caption = "水泵管理"; this.PageTitle.HeaderSvgImage = this.svgImage32[0]; this.PageTitle.SvgImageSize = new Size(24, 24); this.gridView1.SetNormalView(); this.gridView1.RegistCustomDrawRowIndicator(); this.AssetsPumpSeriesTreeListCtrl1.FocusedChangedEvent += AssetsPumpSeriesTreeListCtrl1_FocusedChangedEvent; this.AssetsPumpSeriesTreeListCtrl1.AddAssetsPumpMain += BtnAdd_ItemClickAsync; this.dockManager1.Style = DevExpress.XtraBars.Docking2010.Views.DockingViewStyle.Light; } private List _allBindingList = new(); private BLL.AssetsPumpMain _bll = null; public override void InitialDataSource() { _bll = new BLL.AssetsPumpMain(); this.AssetsPumpSeriesTreeListCtrl1.SetBindingData(); this.currentViewModelBindingSource.DataSource = _allBindingList; this.currentViewModelBindingSource.ResetBindings(false); } private async void SetBindingData() { _allBindingList.Clear(); _bll = new BLL.AssetsPumpMain(); var alllist = await _bll.GetAll(); foreach (var item in alllist) { _allBindingList.Add(new AssetsPumpMainViewModel(item)); } } //聚焦切换 private async void AssetsPumpSeriesTreeListCtrl1_FocusedChangedEvent(long obj, bool isgroup) { _allBindingList.Clear(); if (isgroup) { var alllist = await _bll.GetByPumpSeriesID(obj); _allBindingList.Clear(); if (alllist != null) { foreach (var item in alllist) { _allBindingList.Add(new AssetsPumpMainViewModel(item)); } } } else { var idlist = await new BLL.AssetsPumpGroupAndMainMapping().GetByGroupID(obj); var alllist = await _bll.GetByIds(idlist); if (alllist != null) { _allBindingList.Clear(); foreach (var item in alllist) { _allBindingList.Add(new AssetsPumpMainViewModel(item)); } } } this.currentViewModelBindingSource.ResetBindings(false); } //增加 private async void BtnAdd_ItemClickAsync(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var dlg = new AddPumpProductMainDlg(); //系列ID var id = this.AssetsPumpSeriesTreeListCtrl1.GetCurrentSeriesID(); if (id <= 0) { return; } var groupid = this.AssetsPumpSeriesTreeListCtrl1.GetCurrentGroupID(); Vmo.AssetsPumpGroupAndMainMapVmo map = null; if (groupid > 0) { map = new Vmo.AssetsPumpGroupAndMainMapVmo(); map.GroupID = groupid; } var AssetsPumpSeries = await new BLL.AssetsPumpSeries().GetByID(id); if (AssetsPumpSeries == null) return; dlg.SetBindingData(AssetsPumpSeries); dlg.ReloadDataEvent += async (main) => { var id = await _bll.InsertEx(main, map); if (id > 0) { var model = await _bll.GetByID(id); _allBindingList.Add(new AssetsPumpMainViewModel(model)); this.currentViewModelBindingSource.ResetBindings(false); return true; } return false; }; dlg.ShowDialog(); } //修改 private async void BtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var currentVm = this.gridView1.GetCurrentViewModel(_allBindingList); if (currentVm == null) { MessageBoxHelper.ShowWarning("请选择数据行!"); return; } var dlg = new EditPumpProductMainDlg(); var model = await new BLL.AssetsPumpMain().GetByID(currentVm.ID); if (model == null) return; dlg.SetBindingData(model); dlg.ReloadDataEvent += async (rhs) => { if (await _bll.Update(rhs)) { currentVm.Reset(rhs); this.currentViewModelBindingSource.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; var groupresult = await _bll.DeleteMapByMainID(currentVm.ID); if (groupresult) { _allBindingList.Remove(currentVm); this.currentViewModelBindingSource.ResetBindings(false); MessageBoxHelper.ShowSuccess($"删除成功!"); } else { MessageBoxHelper.ShowError($"删除失败!"); return; } } //泵曲线编辑 private async void barBtnEditPumpCurve_ItemClickAsync(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var currentVm = this.gridView1.GetCurrentViewModel(_allBindingList); if (currentVm == null) { MessageBoxHelper.ShowWarning("请选择数据行!"); return; } var guid = new PageGuid() { Function = "泵型号曲线", TagName = currentVm.ID.ToString(), Modular = "" }; if (!IsExistPage(guid, true)) { var model = await _bll.GetByID(currentVm.ID); if (model == null) { MessageBoxHelper.ShowWarning($"{currentVm.Name}:AssetsPumpMainDto is null!"); return; } var page = new PumpChartMainPage(); page.SetBindingData(currentVm.Model); page.PageTitle.Caption = guid.Function; page.PageGuid = guid; CreatePage(page, guid); } } #region 右击菜单功能列表 //泵产品编辑 private void BarBtnEidtPumpPart_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var currentVm = this.gridView1.GetCurrentViewModel(_allBindingList); if (currentVm == null) { MessageBoxHelper.ShowWarning("请选择数据行!"); return; } var dlg = new EditPumpPartPropDlg(); // var AssetsPumpMain = currentVm.Model as Vmo.AssetsPumpMainVmo; dlg.SetBindingData(currentVm.Model); dlg.ReloadEvent += async (part, content, map) => { var bll = new BLL.AssetsPumpPartMain(); var id = await bll.InsertEx(part, content, map); if (id > 0) { return true; } return false; }; dlg.ShowDialog(); } //查看泵信息 private void BarBtnPumpinformation_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var dlg = new ViewPumpInformationDlg(); dlg.ShowDialog(); } #endregion 右击菜单功能列表 //表格右击菜单 private void gridView1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { if (this.gridView1.GetCurrentViewModel(_allBindingList) != null) { Point screenPoint = Cursor.Position; popupPump.ShowPopup(screenPoint); } } } //批量导入 private void BtnImportEx_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { //树聚焦ID var seriesId = this.AssetsPumpSeriesTreeListCtrl1.GetFocusID(out bool isGroup); if (seriesId <= 0) { return; } var dlg = new ImportPumpExcel(); dlg.SetBindingData(seriesId); dlg.ReloadDataEvent += () => { AssetsPumpSeriesTreeListCtrl1_FocusedChangedEvent(seriesId, isGroup); }; dlg.ShowDialog(); } private void barCkDrag_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { SetDragEnable(this.barCkDrag.Checked); } //设置拖拽可用性 private void SetDragEnable(bool allowArag) { var be = this.behaviorManager1.GetBehavior(this.gridView1); be.Properties.AllowDrag = allowArag; } //拖拽 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 AssetsPumpMainViewModel; 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 AssetsPumpMainViewModel; 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 bll = BLLFactory.Instance; var bol = await bll.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 barBtnUp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { Up(); } //下移 private void barBtnDown_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { Down(); } //上移 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 AssetsPumpMainViewModel; if (current == null) { return; } var prevHandle = rowHandle - 1; var prev = this.gridView1.GetRow(prevHandle) as AssetsPumpMainViewModel; 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 BLLFactory.Instance.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.currentViewModelBindingSource.DataSource = _allBindingList; this.currentViewModelBindingSource.ResetBindings(false); this.gridView1.FocusedRowHandle = prevHandle; } //下移 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 AssetsPumpMainViewModel; if (current == null) { return; } var nextHandle = rowHandle + 1; var next = this.gridView1.GetRow(nextHandle) as AssetsPumpMainViewModel; 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 BLLFactory.Instance.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.currentViewModelBindingSource.DataSource = _allBindingList; this.currentViewModelBindingSource.ResetBindings(false); this.gridView1.FocusedRowHandle = nextHandle; } } }