tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Utils;
using TProduct.Model;
using DevExpress.XtraEditors.ColorPick.Picker;
using TProduct.WinFrmUI.Core;
using DevExpress.XtraPrinting;
using TProduct.Common;
 
namespace TProduct.WinFrmUI.Data4Owner
{
    /// <summary>
    /// 电机
    /// </summary>
    public partial class ProductMotorAndPartMgrPage : DocumentPage
    {
        public ProductMotorAndPartMgrPage()
        {
            InitializeComponent();
 
            this.PageTitle.Caption = "电机";
            this._pageOperateInfo = "电机列表";
            this.gridView1.SetNormalView();
            this.gridView1.SetGridMianViewColor();
            this.gridView1.RegistCustomDrawRowIndicator();
            this.gridView1.OptionsView.ShowViewCaption = false;
            this.gridView1.OptionsView.ShowDetailButtons = true;
 
 
            this.gridView2.SetNormalView();
            this.gridView1.SetGridSubViewColor();
 
            this.colSdName.Visible = TProduct.WinFrmUI.Setting.Disp.IsShowSender;
            this.colMfName.Visible = TProduct.WinFrmUI.Setting.Disp.IsShowManufacturer;
        }
 
        #region 当前视图
 
        public class CurrentViewModel : Model.ProductMainExMotor
        {
            public CurrentViewModel() { }
            public CurrentViewModel(Model.ProductMainExMotor rhs) : base(rhs) 
            {
                this.RatedPower = rhs.RatedPower<0?null: rhs.RatedPower;
                this.PowerFactor = rhs.PowerFactor < 0 ? null : rhs.PowerFactor;
                this.RatedI = rhs.RatedI < 0 ? null : rhs.RatedI;
                this.RatedU = rhs.RatedU < 0 ? null : rhs.RatedU;
                this.Ratedn = rhs.Ratedn < 0 ? null : rhs.Ratedn;
                this.PhaseNum = rhs.PhaseNum;
            }
 
            public override void Reset(Model.ProductMainExMotor rhs)
            {
                base.Reset(rhs);
            }
 
            public List<CurrentViewModelSub> CurrentViewModelSubList { get; set; }
 
            public string UpadteName { get; set; }
            public string CreateName { get; set; }
            public string strCreateTime
            {
                get
                {
                    var A = DateTime.Compare(this.CreateTime, Convert.ToDateTime("2008-8-8"));
                    if (A > 0)
                    {
                        return this.CreateTime.ToString();
                    }
                    else
                        return "";
                }
            }
            public string strUpdateTime
            {
                get
                {
                    var A = DateTime.Compare(this.UpdateTime, Convert.ToDateTime("2008-8-8"));
                    if (A > 0)
                    {
                        return this.UpdateTime.ToString();
                    }
                    else
                        return "";
                }
            }
 
            public string strE_Self
            {
                get
                {
                    return Model.RatedParas4Motor.ToModel(RatedParas).E_Self > 0 ? Model.RatedParas4Motor.ToModel(RatedParas).E_Self.ToString() : "";
                }
            }
            public string strE_PLC
            {
                get
                {
                    return Model.RatedParas4Motor.ToModel(RatedParas).E_PLC > 0 ? Model.RatedParas4Motor.ToModel(RatedParas).E_PLC.ToString() : "";
                }
            }
 
            public bool IsFrequency
            {
                get
                {
                    return Model.RatedParas4Motor.ToModel(RatedParas).IsFrequency;
                }
            }
        }
 
        public class CurrentViewModelSub : Model.PartBase
        {
            public CurrentViewModelSub(Model.PartBase rhs) : base(rhs) { }
 
            public override void Reset(Model.PartBase rhs)
            {
                base.Reset(rhs);
            }
            #region 产品界面参数
            public string strLastTestTime
            {
                get
                {
                    if (this.LastTestTime != null)
                    {
                        return this.LastTestTime.Value.ToString("yyyy-MM-dd");
                    }
                    else
                        return "";
                }
            }
            public string PartCreateTime
            {
                get
                {
                    var A = DateTime.Compare(this.CreateTime, Convert.ToDateTime("2008-8-8"));
                    if (A > 0)
                    {
                        return this.CreateTime.ToString("yyyy-MM-dd HH:mm");
                    }
                    else
                        return "";
                }
            }
            public string PartUpdateTime
            {
                get
                {
                    var A = DateTime.Compare(this.UpdateTime, Convert.ToDateTime("2008-8-8"));
                    if (A > 0)
                    {
                        return this.UpdateTime.ToString("yyyy-MM-dd HH:mm");
                    }
                    else
                        return "";
                }
            }
            public string MfName { get; set; }
            public string SdName { get; set; }
            public string LastRealName { get; set; }
            public string UpadteName { get; set; }
            public string CreateName { get; set; }
 
            #endregion
        }
 
 
        #endregion
        private List<CurrentViewModel> _bindList = null;//所有列表 
        private long _seriesID = 0;
 
        /// <summary>
        /// 初始化数据
        /// </summary>
        public override void InitialDataSource()
        {
            this.seriesListCtrl.SetBindData(Model.eProductType.Motor);
        }
 
        public void SetBindData(long SeriesID)
        {
            _bindList = new List<CurrentViewModel>();
            var allMotor = new BLL.ProductMotor().GetExBySeriesID(SeriesID);
            if (allMotor == null)
            {
                this.currentViewModelBindingSource.DataSource = _bindList;
                this.currentViewModelBindingSource.ResetBindings(false);
                return;
            }
 
 
            foreach (var motor in allMotor)
            {
                _bindList.Add(BuildViewModel(new CurrentViewModel(motor)));
            }
            this.currentViewModelBindingSource.DataSource = _bindList;
            this.currentViewModelBindingSource.ResetBindings(false);
        }
 
        #region 基础功能
 
        //添加 
        private void barBtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_seriesID < 1)
            {
                XtraMessageBox.Show("您必须选择一个设备系列才能进行此项操作");
                ShowCmdOperateInfo("您必须选择一个设备系列才能进行此项操作");
                return;
            }
            if (_bindList == null)
                _bindList = new List<CurrentViewModel>();
            int max_sortCode = 1;
            if (_bindList.Count() > 0)
                max_sortCode = _bindList.Max(x => x.SortCode) + 1;
            WaitFrmHelper.ShowWaitForm();
            var dlg = new AddProductMotorDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(_seriesID, max_sortCode);
            dlg.ReloadDataEvent += (rhs) =>
            {
                _allPartList = new BLL.PartBase().GetAll();
                _allProductMainList = new BLL.ProductMain().GetAll();
 
                _bindList.Add(BuildViewModel(new CurrentViewModel(rhs)));
 
                XtraMessageBox.Show("添加成功!");
                ShowCmdOperateInfo("添加成功!");
                this.currentViewModelBindingSource.ResetBindings(false);
                this.gridView1.RefreshData();
            };
            ShowCmdOperateInfo("添加电机信息");
            dlg.ShowDialog();
            ShowCmdOperateInfo(_pageOperateInfo);
        }
        //编辑 
        private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_bindList.Count < 1)
                return;
            var vm = this.gridView1.GetCurrentViewModel(_bindList);
            if (vm == null)
            {
                XtraMessageBox.Show("您必须选择一个电机才能进行此项操作.");
                ShowCmdOperateInfo("您必须选择一个电机才能进行此项操作.");
                return;
            }
            var dlg = new EditProductMotorDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(vm);
            dlg.ReloadDataEvent += (rhs,ok) =>
              {
                  if (ok)
                      vm.Reset(rhs);
                  else 
                  {
                      _allPartList = new BLL.PartBase().GetAll();
                      _allProductMainList = new BLL.ProductMain().GetAll();
                      BuildViewModel(vm);
                  }
                  XtraMessageBox.Show("更新成功!");
                  ShowCmdOperateInfo("更新成功!");
                  this.currentViewModelBindingSource.ResetBindings(false);
                  this.gridView1.RefreshData();
              };
            ShowCmdOperateInfo("编辑电机信息!");
            dlg.ShowDialog();
            ShowCmdOperateInfo(_pageOperateInfo);
        }
 
 
 
 
        //删除 
        private void barBtnDictTypeDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            WaitFrmHelper.ShowWaitForm();
            var row = this.gridView1.GetCurrentViewModel(_bindList);
            if (row == null)
            {
                WaitFrmHelper.ShowWaitForm();
                XtraMessageBox.Show("请选择一行数据!");
                ShowCmdOperateInfo("请选择一行数据!");
                return;
            }
            ShowCmdOperateInfo("删除数据:" + row.Name);
            DialogResult dr = XtraMessageBox.Show("您确定要删除'" + row.Name + "'这条数据吗?\n(注:如果当前选中设备下仍有产品数据则不能将其删除.)", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr != DialogResult.OK)
            {
                ShowCmdOperateInfo(_pageOperateInfo);
                return;
            }
            var partList = new BLL.PartBase().GetByProductMainID(row.ID);
            if (partList.Count() > 0)
            {
                XtraMessageBox.Show("删除失败,当前选中项下仍有产品数据");
                ShowCmdOperateInfo("删除失败,当前选中项下仍有产品数据");
                return;
            }
            if (!new BLL.ProductPump().DeleteEx(row.ID))
            {
                XtraMessageBox.Show("Error:304,删除失败!");
                ShowCmdOperateInfo("Error:304,删除失败!");
                return;
            }
            SetBindData(_seriesID);
            XtraMessageBox.Show("删除成功!"); 
            ShowCmdOperateInfo(_pageOperateInfo);
        }
 
        #endregion
 
        #region GridView  
 
        //设备列表
        private GridView SysDataView = new GridView();
 
        private void gridView1_MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
        {
            this.SysDataView = this.gridView1.GetDetailView(e.RowHandle, e.RelationIndex) as GridView;
            this.SysDataView.HorzScrollStep = this.SysDataView.RowCount;
        }
        private void barBtnExcelDown_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowCmdOperateInfo("导出列表信息到Excel");
            var path = ExcelSaveFilePathHelper.SaveFilePathName();
            ShowCmdOperateInfo(_pageOperateInfo);
            if (string.IsNullOrEmpty(path)) return;
            gridView1.OptionsPrint.PrintDetails = true;//导出明细
            gridView1.OptionsPrint.ExpandAllDetails = true;//导出所有明细,false的话,只会导出展开的明细
            gridControl1.ExportToXlsx(path, new DevExpress.XtraPrinting.XlsxExportOptionsEx() { ExportType = DevExpress.Export.ExportType.WYSIWYG,TextExportMode= DevExpress.XtraPrinting.TextExportMode.Text });
            DialogResult dr = XtraMessageBox.Show("导出成功!是否打开刚刚保存的Excel文件?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr != DialogResult.OK)
                return;
            System.Diagnostics.Process.Start(path);
        }
 
        #endregion
 
        private void productTypeListCtr_FocusedDataChangedEvent(ProductSeries obj)
        {
            if (obj == null)
                return;
            SetBindData(obj.ID);
            _seriesID = obj.ID;
        }
 
        private void ImportMotor()
        {
            ShowCmdOperateInfo("导入电机");
            DialogResult dr = XtraMessageBox.Show("您确定要在当前选择的系列下导入该文件内电机数据吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr != DialogResult.OK)
                return;
            string file = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择要导入的Excel文件";     //弹窗的标题
            dialog.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx";//筛选文件
 
            if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                ShowCmdOperateInfo(_pageOperateInfo);
                return;
            }
            file = dialog.FileName;
 
            string fileDic = file;
            if (fileDic.Length < 1)
                return;
            else
            {
                var D = new Dictionary<string, object>();
                D.Add("Name", "名称");
                D.Add("RatedPower", "功率kW");
                D.Add("PowerFactor", "功率因子");
                D.Add("RatedI", "电流A");
                D.Add("RatedU ", "电压U");
                D.Add("Ratedn", "转速rpm");
                D.Add("PhaseNum", "电机类型(3(三相)或者1(单相))");
                D.Add("E_Self", "电机效率%");
                D.Add("E_PLC", "变频器效率%");
                D.Add("IsFrequency", "是否变频");
 
                var dt = ExcelHelperEx.ExcelToDataTable(fileDic, D);
                List<List<string>> all_contents = new List<List<string>>();
                var list = new DataTabelToList();
                List<MotorExcelImportHelper> entityList = list.TableToEntity<MotorExcelImportHelper>(dt);
                var motorList = new List<Model.ProductMainExMotor>();
                foreach (var item in entityList)
                {
                    var model = new MotorExcelImportHelper().GetMotor(item.Name, item.RatedPower, item.PowerFactor, item.RatedI, item.RatedU, item.Ratedn, item.PhaseNum, item.E_Self, item.E_PLC, item.IsFrequency);
                    model.SeriesID = _seriesID;
                    model.CreateUserID = TProduct.WinFrmUI.GlobeParas.CurrentLoginUser.ID;
                    model.CreateTime = DateTime.Now;
                    model.UpdateUserID = TProduct.WinFrmUI.GlobeParas.CurrentLoginUser.ID;
                    model.UpdateTime = DateTime.Now;
                    motorList.Add(model);
                }
 
                if (motorList == null)
                {
                    XtraMessageBox.Show("Error:文件数据列表为空,导入失败!");
                    return;
                }
                var dlg = new MotorImportDlg();
                dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
                dlg.SetBindData(motorList);
                dlg.ShowDialog();
                SetBindData(_seriesID);
                this.currentViewModelBindingSource.ResetBindings(false);
                this.gridView1.RefreshData();
 
                ShowCmdOperateInfo(_pageOperateInfo);
            }
 
        }
 
        private void barbtnViewMotorTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var strTemplateFile = System.IO.Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Data",
           "MotorTemplate",
           "ImportTemplateExcel_V1.xls"); ;
 
            ShowCmdOperateInfo("查看导入模板");
            if (!System.IO.File.Exists(strTemplateFile))
            {
                MessageBox.Show(strTemplateFile + ": 模板文件被非法删除");
                ShowCmdOperateInfo(strTemplateFile + ": 模板文件被非法删除");
                return;
            }
            DialogResult dr = XtraMessageBox.Show("您要打开并查看模板文件吗?)", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr == DialogResult.OK)
            {
                try
                {
                    System.Diagnostics.Process.Start(strTemplateFile);
                }
                catch (Exception)
                {
                    XtraMessageBox.Show("此文件无法打开", "打开文件错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    ShowCmdOperateInfo("此文件无法打开或者打开文件错误");
                }
                this.Close();
            }
            ShowCmdOperateInfo(_pageOperateInfo);
        }
 
        private void barbtnImportMotorTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ImportMotor();
        }
 
        List<Model.Senderbase> _allSenderList = null;
        List<Model.ManufacturerBase> _allManufacturerList = null;
        List<Model.LoginUser> _allUserList = null;
        List<Model.PartBase> _allPartList = null;
        List<Model.ProductMain> _allProductMainList = null;
        private CurrentViewModel BuildViewModel(CurrentViewModel viewmodel)
        {
            if (_allManufacturerList == null)
            {
                _allManufacturerList = new BLL.ManufacturerBase().GetAll();
            }
            if (_allSenderList == null)
            {
                _allSenderList = new BLL.Senderbase().GetAll();
            }
            if (_allUserList == null)
            {
                _allUserList = new BLL.LoginUser().GetAll();
            }
            if (_allPartList == null)
            {
                _allPartList = new BLL.PartBase().GetAll();
            }
            if (_allProductMainList == null)
                _allProductMainList = new BLL.ProductMain().GetAll();
 
            viewmodel.UpadteName = _allUserList.Find(x => x.ID == viewmodel.UpdateUserID)?.RealName;
            viewmodel.CreateName = _allUserList.Find(x => x.ID == viewmodel.UpdateUserID)?.RealName;
 
            var parts = _allPartList.Where(x => x.ProductMainID == viewmodel.ID);
            if (parts != null && parts.Count() > 0)
            {
                viewmodel.CurrentViewModelSubList = new List<CurrentViewModelSub>();
                foreach (var part in parts)
                {
                    var vm_part = new CurrentViewModelSub(part);
                    if (part.ManufacturerID > 0)
                    {
                        var Manufacturer = _allManufacturerList.Find(m => m.ID == part.ManufacturerID);
                        vm_part.MfName = Manufacturer?.ShortName == "" ? Manufacturer?.FullName : Manufacturer?.ShortName;
                    }
                    if (part.SenderID > 0)
                    {
                        var Sender = _allSenderList.Find(s => s.ID == part.SenderID);
                        vm_part.SdName = Sender?.ShortName == "" ? Sender?.FullName : Sender?.ShortName;
                    }
                    if (part.LastTestUserID > 0)
                        vm_part.LastRealName = _allUserList.Find(x => x.ID == part.LastTestUserID)?.RealName;
                    viewmodel.CurrentViewModelSubList.Add(vm_part);
                }
            }
 
            return viewmodel;
        }
 
    }
}