duheng
2025-02-19 ad8f813f5eddd66740b4e09801e4ea02ddf70a4a
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
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<Vmo.AssetsPumpMainVmo> _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<Vmo.AssetsPumpMainVmo>();
                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<HStation.BLL.AssetsPumpMain>.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<Yw.BLL.PhartDiagramExtensions>.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<HStation.BLL.PhartDiagramRelation>.Instance.Insert(vmoRelation);
                                if (relationId < 1)
                                {
                                    TipFormHelper.ShowError("图表关联信息保存失败!");
                                    return;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    TipFormHelper.ShowError(ex.Message);
                    return;
                }
                TipFormHelper.ShowSucceed("导入成功!");
            }
        }
    }
}