using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Threading;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using ZJ_BYD.Common;
using ZJ_BYD.DB;
using ZJ_BYD.Model;
using ZJ_BYD.Untils;
using ZJ_BYD.ViewModel;
namespace ZJ_BYD.UserControls.PlcPoint
{
public partial class PlcPointConfig : XtraUserControl
{
private int pagesize = 30; //每页显示行数
private int totalCount = 0; //总记录数
private int pageCurrent = 1; //当前页号
public PlcPointConfig()
{
InitializeComponent();
gridcontrol.CustomDrawRowIndicator += GridView1_CustomDrawRowIndicator;
gridPage1.williamPagerEvent += GridPage1_williamPagerEvent;
gridcontrol.IndicatorWidth = 35;
}
private void PlcPointConfig_Load(object sender, EventArgs e)
{
BindCombox.BindStation(cmbStation);
GetData();
}
private void GetData(int pageCurrent = 1)
{
var list = DataSource();
this.plcgrid.DataSource = list;
gridPage1.RefreshPager(pagesize, totalCount, pageCurrent);
}
///
/// 数据源
///
///
///
private List DataSource(bool isPage = true)
{
var selectedStation = cmbStation.SelectedItem as ListItem;
var keyWord = this.txtkeyword.Text.Trim();
var data = PlcPointHelper.QueryPlcPoints()
.InnerJoin((a, b) => a.StationCode == b.StationCode && !a.IsDeleted && !b.IsDeleted)
.Select((a, b) => new PlcPointVM
{
Id = a.Id,
IpcId = a.IpcId,
LineCode = a.LineCode,
StationCode = a.StationCode,
StationName = b.StationName,
PointCode = a.PointCode,
PointName = a.PointName,
PointStartAddress = a.PointStartAddress,
PointLength = a.PointLength,
PointDataType = a.PointDataType,
StrIsShowMain = a.IsShowMain ? "是" : "否",
StrIsSaveDb = a.IsSaveDb ? "是" : "否",
ResultField = a.ResultField,
StrIsTestItem = a.IsTestItem ? "是" : "否",
StrIsResponseData = a.IsResponseData ? "是" : "否",
StrIsSub = a.IsSub ? "是" : "否",
StrIsSystem = a.IsSystem ? "是" : "否",
SortIndex = a.SortIndex,
Remark = a.Remark,
StrIsActive = a.IsActive ? "是" : "否",
CreatedTime = a.CreatedTime
}).Distinct().OrderBy(a => a.StationCode);
if (!string.IsNullOrWhiteSpace(keyWord))
{
data.Where(a => a.PointCode.ToLower().Contains(keyWord.ToLower())
|| a.PointName.ToLower().Contains(keyWord.ToLower()));
}
if (selectedStation != null && !string.IsNullOrWhiteSpace(selectedStation.Value))
{
data.Where(a => a.StationCode == selectedStation.Value);
}
if (isPage)
{
return data.ToPageList(pageCurrent, pagesize, ref totalCount);
}
return data.ToList();
}
private void btnsearch_Click(object sender, EventArgs e)
{
splashScreenManager1.ShowWaitForm();
GetData();
splashScreenManager1.CloseWaitForm();
}
private void btnAdd_Click(object sender, EventArgs e)
{
EditPlcPoint editPlcPoint = new EditPlcPoint();
var result = editPlcPoint.ShowDialog();
if (result == DialogResult.OK)
{
GetData();
}
}
private void plcgrid_Load(object sender, EventArgs e)
{
this.repositoryItemHyperLinkEdit1.ButtonClick += new ButtonPressedEventHandler(repositoryItemHyperLinkEdit_ButtonClick);
}
private void repositoryItemHyperLinkEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
{
var rowHandle = gridcontrol.FocusedRowHandle;
var model = gridcontrol.GetRow(rowHandle) as PlcPointVM;
EditPlcPoint editPlcPoint = new EditPlcPoint(model.Id);
var result = editPlcPoint.ShowDialog();
if (result == DialogResult.OK)
{
GetData();
}
}
private void btndel_Click(object sender, EventArgs e)
{
var ids = new List();
var rowsNumber = this.gridcontrol.GetSelectedRows();
if (rowsNumber.Length <= 0)
{
XtraMessageBox.Show("请选择数据行!");
return;
}
splashScreenManager1.ShowWaitForm();
foreach (var rownumber in rowsNumber)
{
var maskCode = this.gridcontrol.GetRow(rownumber) as PlcPointVM;
ids.Add(maskCode.Id);
}
if (ids.Count > 0)
{
var rows = PlcPointHelper.DoDelPlcPointsByIds(ids);
if (rows > 0)
{
splashScreenManager1.CloseWaitForm();
XtraMessageBox.Show("操作成功!");
GetData();
}
else
{
splashScreenManager1.CloseWaitForm();
XtraMessageBox.Show("操作失败!");
}
}
else
{
splashScreenManager1.CloseWaitForm();
}
}
private void btnimport_Click(object sender, EventArgs e)
{
var importThread = new Thread(new ThreadStart(ImportDialog));
importThread.SetApartmentState(ApartmentState.STA); //重点
importThread.Start();
}
private void btnExport_Click(object sender, EventArgs e)
{
var importThread = new Thread(new ThreadStart(ExportDialog));
importThread.SetApartmentState(ApartmentState.STA); //重点
importThread.Start();
}
///
/// 导出
///
private void ExportDialog()
{
try
{
FolderBrowserDialog path = new FolderBrowserDialog();
if (path.ShowDialog() == DialogResult.OK)
{
splashScreenManager1.ShowWaitForm();
var list = DataSource(false);
var filePath = string.Format("{0}\\{1}.xlsx", path.SelectedPath, DateTime.Now.ToString("yyyyMMddHHmmss"));
#region 设置列名称
var mappingColumns = new List();
var co1 = new ExcelColumns { field = "StationCode", title = "工位编码" };
var co2 = new ExcelColumns { field = "PointName", title = "点位名称" };
var co3 = new ExcelColumns { field = "PointCode", title = "点位编码" };
var co4 = new ExcelColumns { field = "PointStartAddress", title = "点位起始地址" };
var co5 = new ExcelColumns { field = "PointLength", title = "长度" };
var co6 = new ExcelColumns { field = "PointDataType", title = "数据类型" };
var co7 = new ExcelColumns { field = "StrIsShowMain", title = "是否在主页面显示" };
var co8 = new ExcelColumns { field = "StrIsSaveDb", title = "是否存数据库" };
var co9 = new ExcelColumns { field = "ResultField", title = "存库字段" };
var co10 = new ExcelColumns { field = "StrIsTestItem", title = "是否测试项点位" };
var co11 = new ExcelColumns { field = "StrIsResponseData", title = "是否反馈数据点位" };
var co12 = new ExcelColumns { field = "StrIsSub", title = "是否组件条码点位" };
var co13 = new ExcelColumns { field = "StrIsSystem", title = "是否系统内置点位" };
var co14 = new ExcelColumns { field = "StrIsActive", title = "是否启用" };
var co15 = new ExcelColumns { field = "SortIndex", title = "排序索引" };
var co16 = new ExcelColumns { field = "Remark", title = "备注" };
//var co1 = new ExcelColumns { field = "StationCode", title = "工位编码" };
//var co2 = new ExcelColumns { field = "StationName", title = "工位名称" };
//var co3 = new ExcelColumns { field = "PointCode", title = "点位编码" };
//var co4 = new ExcelColumns { field = "PointName", title = "点位名称" };
//var co5 = new ExcelColumns { field = "PointStartAddress", title = "点位起始地址" };
//var co6 = new ExcelColumns { field = "PointLength", title = "长度" };
//var co7 = new ExcelColumns { field = "PointDataType", title = "数据类型" };
//var co8 = new ExcelColumns { field = "StrIsShowMain", title = "是否在主页面显示" };
//var co9 = new ExcelColumns { field = "StrIsSaveDb", title = "是否存数据库" };
//var co10 = new ExcelColumns { field = "ResultField", title = "存库字段" };
//var co11 = new ExcelColumns { field = "StrIsTestItem", title = "是否测试项" };
//var co12 = new ExcelColumns { field = "StrIsResponseData", title = "是否反馈点位" };
//var co13 = new ExcelColumns { field = "StrIsSub", title = "是否组件条码" };
//var co14 = new ExcelColumns { field = "StrIsSystem", title = "是否系统内置点位" };
//var co15 = new ExcelColumns { field = "SortIndex", title = "排序索引" };
//var co16 = new ExcelColumns { field = "StrIsActive", title = "是否启用" };
//var co17 = new ExcelColumns { field = "Remark", title = "备注" };
mappingColumns.Add(co1);
mappingColumns.Add(co2);
mappingColumns.Add(co3);
mappingColumns.Add(co4);
mappingColumns.Add(co5);
mappingColumns.Add(co6);
mappingColumns.Add(co7);
mappingColumns.Add(co8);
mappingColumns.Add(co9);
mappingColumns.Add(co10);
mappingColumns.Add(co11);
mappingColumns.Add(co12);
mappingColumns.Add(co13);
mappingColumns.Add(co14);
mappingColumns.Add(co15);
mappingColumns.Add(co16);
//mappingColumns.Add(co17);
#endregion
if (list.Count <= 0)
{
Invoke(new Action(() => { XtraMessageBox.Show("无数据!", "提示"); }));
splashScreenManager1.CloseWaitForm();
return;
}
ExcelHelper.ToExcel(list, filePath, mappingColumns, true);
Invoke(new Action(() => { XtraMessageBox.Show("导出成功!", "提示"); }));
splashScreenManager1.CloseWaitForm();
}
}
catch (Exception ex)
{
Invoke(new Action(() => { XtraMessageBox.Show("导出数据异常!", "提示"); }));
LogHelper.WriteErrorLog("导出数据异常!", ex);
}
}
private void ImportDialog()
{
using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Excel Workbook|*.xlsx|Excel 97-2003 Workbook|*.xls" })
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
splashScreenManager1.ShowWaitForm();
var filePath = openFileDialog.FileName;
var dt = ExcelHelper.GetExcel(filePath, 0, out var maxColumnNum);
if (dt.Rows.Count <= 0)
{
Invoke(new Action(() => { XtraMessageBox.Show("文件不能为空!", "提示"); }));
splashScreenManager1.CloseWaitForm();
return;
}
var result = CheckData(dt);
if (!result.isOk)
{
Invoke(new Action(() => { XtraMessageBox.Show("存在不合法数据,请检查!", "提示"); }));
splashScreenManager1.CloseWaitForm();
return;
}
if (PlcPointHelper.AddPlcPoints(result.list))
{
Invoke(new Action(() => { XtraMessageBox.Show("操作成功!", "提示"); }));
Invoke(new Action(() => { GetData(); }));
}
else
{
Invoke(new Action(() => { XtraMessageBox.Show("操作失败!", "提示"); }));
}
splashScreenManager1.CloseWaitForm();
}
}
}
///
/// 校验上传数据
///
///
///
private (bool isOk, List list) CheckData(DataTable dt)
{
var isOk = true;
var t_PlcPoints = new List();
for (int rowindex = 0; rowindex < dt.Rows.Count; rowindex++)
{
if (string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_1"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_2"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_3"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_4"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_5"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_6"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_7"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_10"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_11"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_12"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_13"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_14"].ToString())
|| string.IsNullOrWhiteSpace(dt.Rows[rowindex]["col_15"].ToString()))
{
isOk = false;
continue;
}
else if (!StationHelper.CheckStationCode(dt.Rows[rowindex]["col_1"].ToString()))
{
isOk = false;
continue;
}
else
{
var t_PlcPoint = new T_PlcPoint
{
IpcId = Program.CurrentIpcId,
LineCode = Program.CurrentLineCode,
StationCode = dt.Rows[rowindex]["col_1"].ToString(),
PointName = dt.Rows[rowindex]["col_2"].ToString(),
PointCode = dt.Rows[rowindex]["col_3"].ToString(),
PointStartAddress = dt.Rows[rowindex]["col_4"].ToString(),
PointLength = dt.Rows[rowindex]["col_5"].ToString(),
PointDataType = dt.Rows[rowindex]["col_6"].ToString(),
IsShowMain = dt.Rows[rowindex]["col_7"].ToString() == "是",
IsSaveDb = dt.Rows[rowindex]["col_8"].ToString() == "是",
ResultField = dt.Rows[rowindex]["col_9"].ToString(),
IsTestItem = dt.Rows[rowindex]["col_10"].ToString() == "是",
IsResponseData = dt.Rows[rowindex]["col_11"].ToString() == "是",
IsSub = dt.Rows[rowindex]["col_12"].ToString() == "是",
IsSystem = dt.Rows[rowindex]["col_13"].ToString() == "是",
IsActive = dt.Rows[rowindex]["col_14"].ToString() == "是",
SortIndex = int.Parse(dt.Rows[rowindex]["col_15"].ToString()),
Remark = dt.Rows[rowindex]["col_16"].ToString(),
CreatedTime = DateTime.Now,
CreatedBy = CurrentUser.UserName
};
t_PlcPoints.Add(t_PlcPoint);
}
}
return (isOk, t_PlcPoints);
}
private void btnDown_Click(object sender, EventArgs e)
{
var downThread = new Thread(new ThreadStart(DownDialog));
downThread.SetApartmentState(ApartmentState.STA); //重点
downThread.Start();
}
private void DownDialog()
{
var fileName = "PlcPoint.xlsx";
var filePath = Path.Combine(Application.StartupPath, "FileTemplate/" + fileName);
if (!File.Exists(filePath))
{
Invoke(new Action(() => { XtraMessageBox.Show("模板文件不存在!", "提示"); }));
return;
}
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
{
splashScreenManager1.ShowWaitForm();
var localPath = "";
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
localPath = Path.Combine(folderBrowserDialog.SelectedPath, fileName);
FileHelper.DownloadFile(filePath, localPath);
if (File.Exists(localPath))
{
Invoke(new Action(() => { XtraMessageBox.Show("下载成功!", "提示"); }));
}
else
{
Invoke(new Action(() => { XtraMessageBox.Show("下载失败!", "提示"); }));
}
splashScreenManager1.CloseWaitForm();
}
else
{
splashScreenManager1.CloseWaitForm();
}
}
}
///
/// 分页事件
///
///
///
private void GridPage1_williamPagerEvent(int curPage, int pageSize)
{
pageCurrent = curPage;
pagesize = pageSize;
GetData(curPage);
}
///
/// 行号
///
///
///
private void GridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle > -1)
{
e.Info.DisplayText = (e.RowHandle + 1).ToString();
}
}
}
}