using DevExpress.XtraCharts.Designer; using HStation.Vmo; using HStation.WinFrmUI.Assets; using System.IO; using System.Windows.Media.Animation; using Yw; namespace HStation.WinFrmUI { public partial class ImportPumpExcel : DevExpress.XtraEditors.XtraForm { public ImportPumpExcel() { InitializeComponent(); this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon; this.generalOkAndCancelCtrl1.OkEvent += GeneralOkAndCancelCtrl1_OkEvent; } private List _Pumps; private long _seriesId; public void SetBindingData(long seriesId) { _seriesId = seriesId; } public event Action ReloadDataEvent; private void btnExcelFilePath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { if (e.Button.Tag.ToString() == "Import") { var dlg = new System.Windows.Forms.OpenFileDialog(); dlg.Filter = "EXCEL 文件(*.xls;*.xlsx)|*.xls;*.xlsx"; dlg.CheckFileExists = true; if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; this.btnExcelFilePath.Text = dlg.FileName; _Pumps = new List(); var PumpExcelHelper = new PumpExcelHelper(); var result = PumpExcelHelper.ParsePumpExcel(dlg.FileName, out _Pumps); if (result != null && !string.IsNullOrEmpty(result)) { TipFormHelper.ShowWarn(result); return; } _Pumps.ForEach(x => x.SeriesID = _seriesId); this.gridControl1.DataSource = _Pumps; } else if (e.Button.Tag.ToString() == "Download") { var dlg = new SaveFileDialog(); dlg.Title = "模板导出路径"; dlg.Filter = "Excel文件|*.xls"; if (dlg.ShowDialog() == DialogResult.OK) { var fileName = dlg.FileName; PumpExcelHelper.ExportPumpTemplate(fileName); TipFormHelper.ShowSucceed("导出成功!"); } } } //验证 private bool Valid() { this.dxErrorProvider1.ClearErrors(); if (string.IsNullOrEmpty(this.btnExcelFilePath.Text.Trim())) { this.dxErrorProvider1.SetError(this.btnExcelFilePath, "必填项"); return false; } return true; } //确定 private async void GeneralOkAndCancelCtrl1_OkEvent() { if (!Valid()) { return; } if (_Pumps.Count < 0 || _Pumps == null) { TipFormHelper.ShowError("导入信息为空!"); return; } var bol = await BLLFactory.Instance.Inserts(_Pumps); if (bol) { this.ReloadDataEvent?.Invoke(); TipFormHelper.ShowSucceed("导入成功!"); } else { TipFormHelper.ShowError("导入失败!"); } this.DialogResult = DialogResult.OK; this.Close(); } //批量导入泵曲线(根据文件名匹配) private async void btnImportCurve_Click(object sender, EventArgs e) { FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog(); if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { var allPumpList = await new BLL.AssetsPumpMain().GetAll(); string folderpath = folderBrowserDialog.SelectedPath; string[] excelFilePath = Directory.GetFiles(folderpath); try { foreach (string excelFile in excelFilePath) { string fileName = Path.GetFileNameWithoutExtension(excelFile); var vmo = ExcelConvertHelper.ParsePumpExcel(excelFile); if (vmo != null) { var select = allPumpList.Find(x => x.Name == fileName); if (select != null) { vmo.Name = select.Name; var diagramId = await BLLFactory.Instance.Insert(vmo); if (diagramId < 1) { TipFormHelper.ShowError("图表信息保存失败!"); return; } var vmoRelation = new PhartDiagramRelationVmo(); vmoRelation.ObjectType = HStation.Assets.DataType.PumpMain; vmoRelation.ObjectID = select.ID; vmoRelation.DiagramID = diagramId; vmoRelation.OtherName = select.Name; vmoRelation.Importance = 0; var relationId = await BLLFactory.Instance.Insert(vmoRelation); if (relationId < 1) { TipFormHelper.ShowError("图表关联信息保存失败!"); return; } } } } } catch (Exception ex) { TipFormHelper.ShowError(ex.Message); return; } TipFormHelper.ShowSucceed("导入成功!"); } } } }