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;
|
}
|
|
}
|
}
|