using HStation.WinFrmUI.Xhs.PumpProduct; namespace HStation.WinFrmUI { public partial class EditPumpPartPropDlg : DevExpress.XtraEditors.XtraForm { public EditPumpPartPropDlg() { InitializeComponent(); this.gridView1.Columns["PropGroupName"].Group(); this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon; this.pumpProductListBox1.MouseUp += PumpProductListBox1_MouseUp; } private BLL.AssetsPumpPartMain _bll = null; private List _propList = null; private List _allBindingList = new List(); private Vmo.AssetsPumpMainAndPartMapVmo _partMapping = null; private Vmo.AssetsPumpMainVmo _pumpMain; private Vmo.AssetsPumpPartMainVmo _pumpPart = null; private long _catalogID; //分类ID private Vmo.AssetsPumpSeriesVmo _series; //系列ID private List _allPropList = null; private Yw.BLL.SysPropMapping _sysPropMapping; //回调事件 public event Func, Vmo.AssetsPumpMainAndPartMapVmo, Task> ReloadEvent; //数据绑定 public async void SetBindingData(Vmo.AssetsPumpMainVmo AssetsPumpMain) { this.pumpProductListBox1.SetBindingData(AssetsPumpMain); _pumpMain = AssetsPumpMain; this.pumpProductListBox1.SelectReloadEvent += () => { ListBoxPart_SelectedIndexChanged(); }; this.pumpProductListBox1.AddReloadEvent += () => { BarBtnAddPumpPart_ItemClick(); }; _bll = new BLL.AssetsPumpPartMain(); var allpartlist = await _bll.GetByPumpMainID(AssetsPumpMain.ID); //获取所有产品 var series = await new BLL.AssetsPumpSeries().GetByID(AssetsPumpMain.PumpSeriesID); //获取系列 _pumpPart = new Vmo.AssetsPumpPartMainVmo(); _series = series; _pumpPart.SeriesID = _series.ID; _catalogID = series.CatalogID; _propList = new List(); _partMapping = new Vmo.AssetsPumpMainAndPartMapVmo(); _partMapping.PumpID = AssetsPumpMain.ID; _partMapping.SeriesID = _series.ID; this.propGroupChoiceViewModelBindingSource.DataSource = _propList; this.propGroupChoiceViewModelBindingSource.ResetBindings(false); } //分类选择变换 private async void SetPropSelectedValue(CurrentPartMainViewModel partmain) { layoutControAddPart.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never; var model = await _bll.GetByID(partmain.ID); if (model == null) return; this.TextEditNo.Text = partmain.NO.Trim(); this.TextEditProductCode.Text = partmain.Code.Trim(); this.TextEditProductName.Text = partmain.Name.Trim(); _propList.Clear(); var bll = new Yw.BLL.SysPropStruct(); var catlog = await bll.GetByCatalogID(_catalogID); if (catlog == null) { this.propGroupChoiceViewModelBindingSource.ResetBindings(false); return; } _propList.Clear(); _propList.Add(new PropGroupChoiceViewModel() { PropGroupName = "铭牌参数", PropName = "流量", Value = _pumpMain.RatedFlow.ToString(), KeyWorldType = "Basic_Flow" }); _propList.Add(new PropGroupChoiceViewModel() { PropGroupName = "铭牌参数", PropName = "扬程", Value = _pumpMain.RatedHead.ToString(), KeyWorldType = "Basic_Head" }); _propList.Add(new PropGroupChoiceViewModel() { PropGroupName = "铭牌参数", PropName = "功率", Value = _pumpMain.RatedPower.ToString(), KeyWorldType = "Basic_Power" }); _propList.Add(new PropGroupChoiceViewModel() { PropGroupName = "铭牌参数", PropName = "转速", Value = _pumpMain.RatedSpeed.ToString(), KeyWorldType = "Basic_Speed" }); foreach (var item in catlog) { foreach (var prop in item.PropList) { _propList.Add(new PropGroupChoiceViewModel() { PropGroupName = item.Name, PropName = prop.Name, ID = prop.ID, KeyWorldType = "Prop" }); } } var propbll = new BLL.PumpPartPropContent(); var alllist = await propbll.GetByPumpPartID(model.ID); _allPropList = alllist; foreach (var item in alllist) { var selectProp = _propList.Find(x => x.ID == item.PropID); if (selectProp != null) { selectProp.Value = item.PropValue; } } this.propGroupChoiceViewModelBindingSource.ResetBindings(false); this.gridView1.ExpandAllGroups(); } //产品列表聚焦改变 private void ListBoxPart_SelectedIndexChanged() { var vm = this.pumpProductListBox1.GetCurrentVm(); if (vm == null) return; SetPropSelectedValue(vm); } //确认编辑 private async void BtnOk_Click(object sender, EventArgs e) { var vm = this.pumpProductListBox1.GetCurrentVm(); if (vm == null) return; var updatepart = new Vmo.AssetsPumpPartMainVmo(); updatepart.ID = vm.ID; updatepart.Name = TextEditProductName.Text; updatepart.NO = TextEditNo.Text; updatepart.Code = TextEditProductCode.Text; updatepart.SeriesID = _series.ID; var updateproplist = new List(); var propList = _propList.Where(x => x.KeyWorldType == "Prop").ToList(); foreach (var item in propList) { if (_allPropList != null) { // 查找 _allPropList 中是否存在与 item.ID 匹配的项 var prop = _allPropList.FirstOrDefault(x => x.PropID == item.ID); // 如果找到匹配的项,则更新其 PropValue if (prop != null) { prop.PropValue = item.Value; } else { _allPropList.Add(new Vmo.AssetsPumpPropContentVmo() { SeriesID = _allPropList.First().SeriesID, PartID = _allPropList.First().PartID, PropID = item.ID, PropValue = item.Value }); } } } var basicFlow = _propList.Find(x => x.KeyWorldType == "Basic_Flow"); var basicHead = _propList.Find(x => x.KeyWorldType == "Basic_Head"); var basicSpeed = _propList.Find(x => x.KeyWorldType == "Basic_Speed"); var basicPower = _propList.Find(x => x.KeyWorldType == "Basic_Power"); if (double.TryParse(basicFlow.Value, out double flow)) { _pumpMain.RatedFlow = flow; } if (double.TryParse(basicHead.Value, out double head)) { _pumpMain.RatedHead = head; } if (double.TryParse(basicSpeed.Value, out double speed)) { _pumpMain.RatedSpeed = speed; } if (double.TryParse(basicPower.Value, out double power)) { _pumpMain.RatedPower = power; } var pumpMainBll = new BLL.AssetsPumpMain(); await pumpMainBll.Update(_pumpMain); if (await _bll.UpdateEx(updatepart, _allPropList)) { vm.Reset(updatepart); this.pumpProductListBox1.Refresh(); TipFormHelper.ShowSucceed("修改成功!"); } else { TipFormHelper.ShowError("修改失败!"); } this.DialogResult = DialogResult.OK; this.Close(); } //左侧树右击菜单事件 private void PumpProductListBox1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { Point screenPoint = Cursor.Position; popupPumpPartMenu.ShowPopup(screenPoint); } } private bool Vaild() { this.dxErrorProvider1.ClearErrors(); if (TextEditProductName.Text == null) { this.dxErrorProvider1.SetError(TextEditProductName, "必填项"); return false; } return true; } //添加产品 private async void BtnAddPumpPart_Click(object sender, EventArgs e) { if (!Vaild()) return; _pumpPart.Name = TextEditProductName.Text; _pumpPart.NO = TextEditNo.Text; _pumpPart.Code = TextEditProductCode.Text; var AssetsPumpPropContent = new List(); foreach (var item in _propList) { AssetsPumpPropContent.Add(new Vmo.AssetsPumpPropContentVmo { PropID = item.ID, PropValue = item.Value, SeriesID = _pumpPart.SeriesID }); } var id = await _bll.InsertEx(_pumpPart, AssetsPumpPropContent, _partMapping); if (id > 0) { var pumppart = await _bll.GetByID(id); this.pumpProductListBox1._allBindingList.Add(new CurrentPartMainViewModel(pumppart)); this.pumpProductListBox1.Refresh(); TipFormHelper.ShowSucceed("添加成功!"); } else { TipFormHelper.ShowError("添加失败!"); } this.DialogResult = DialogResult.OK; this.Close(); } #region 菜单功能 //添加产品 private async void BarBtnAddPumpPart_ItemClick() { layoutControAddPart.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always; TextEditProductName.Text = string.Empty; TextEditNo.Text = string.Empty; TextEditProductCode.Text = string.Empty; var bll = new Yw.BLL.SysPropStruct(); try { var catlog = await bll.GetByCatalogID(_catalogID); if (catlog == null) { this.propGroupChoiceViewModelBindingSource.ResetBindings(false); return; } _propList.Clear(); foreach (var item in catlog) { foreach (var prop in item.PropList) { _propList.Add(new PropGroupChoiceViewModel() { PropGroupName = item.Name, PropName = prop.Name, ID = prop.ID, Value = prop.DefaultValue }); } } this.propGroupChoiceViewModelBindingSource.ResetBindings(false); } catch { } this.TextEditProductName.Properties.NullText = "在此输入产品名称"; // 尝试改变焦点以触发控件更新 this.TextEditProductName.Focus(); // this.TextEditProductName.SelectAll(); this.TextEditNo.Properties.NullText = "在此输入产品编号"; this.TextEditProductCode.Properties.NullText = "在此输入产品图号"; } // 删除产品 private async void BarBtnDeletePart_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.pumpProductListBox1.GetCurrentVm(); if (vm == null) return; if (!MessageBoxHelper.IsClickOk("确定删除数据行")) { if (await _bll.DeleteEx(vm.ID)) { this.pumpProductListBox1._allBindingList.Remove(vm); this.pumpProductListBox1.Refresh(); MessageBoxHelper.ShowSuccess("删除成功!"); } else { MessageBoxHelper.ShowError("删除失败!"); } } } #endregion 菜单功能 //增加属性 private void simpleLabelItemAddProp_Click(object sender, EventArgs e) { var dlg = new AddPumpPropDlg(); dlg.SetBindingData(_series.CatalogID); dlg.ReloadDataEvent += async (rhs, groupName, propValue) => { var bll = new Yw.BLL.SysProp(); var id = await bll.Insert(rhs); if (id > 0) { _sysPropMapping = new Yw.BLL.SysPropMapping(); var allList = await _sysPropMapping.GetHaveListByCatalogID(_series.CatalogID); var list = new List(); foreach (var item in allList) { foreach (var prop in item.PropList) { if (prop.Have) { list.Add(new Yw.Vmo.SysPropMappingSetterVmo { PropID = prop.ID, UnitName = prop.UnitName, IsNull = prop.IsNull, DefaultValue = prop.DefaultValue, ChoiceIds = prop.ChoiceList.Select(x => x.ID).ToList(), }); } } } list.Add(new Yw.Vmo.SysPropMappingSetterVmo { PropID = id, UnitName = rhs.UnitName, IsNull = rhs.IsNull }); await _sysPropMapping.SetByCatalogID(_series.CatalogID, list); var model = await bll.GetByID(id); var partMain = new PropGroupChoiceViewModel() { PropGroupName = groupName, PropName = model.Name, ID = model.ID, Value = propValue, KeyWorldType = "Prop" }; _propList.Add(partMain); this.propGroupChoiceViewModelBindingSource.ResetBindings(false); return true; } return false; }; dlg.ShowDialog(); } } }