You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
14 KiB
C#

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
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.Station
{
public partial class MstStation : XtraUserControl
{
private int pagesize = 30; //每页显示行数
private int totalCount = 0; //总记录数
private int pageCurrent = 1; //当前页号
public MstStation()
{
InitializeComponent();
gridView1.CustomDrawRowIndicator += GridView1_CustomDrawRowIndicator;
gridPage1.williamPagerEvent += GridPage1_williamPagerEvent;
gridView1.IndicatorWidth = 35;
BindCombox.BindCategoryForLookUp(lkupCategory);
GetData();
}
private void MstStation_Load(object sender, EventArgs e)
{
this.repositoryItemHyperLinkEdit1.ButtonClick += new ButtonPressedEventHandler(repositoryItemHyperLinkEdit_ButtonClick);
}
private void repositoryItemHyperLinkEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
{
var rowHandle = gridView1.FocusedRowHandle;
var model = gridView1.GetRow(rowHandle) as QueryStationVM;
EditStation editStation = new EditStation(model.Id);
var result = editStation.ShowDialog();
if (result == DialogResult.OK)
{
GetData();
}
}
private void btnsearch_Click(object sender, EventArgs e)
{
splashScreenManager1.ShowWaitForm();
GetData();
splashScreenManager1.CloseWaitForm();
}
private void btnadd_Click(object sender, EventArgs e)
{
EditStation editStation = new EditStation();
var result = editStation.ShowDialog();
if (result == DialogResult.OK)
{
GetData();
}
}
private void btndel_Click(object sender, EventArgs e)
{
var ids = new List<int>();
var rowsNumber = this.gridView1.GetSelectedRows();
if (rowsNumber.Length <= 0)
{
XtraMessageBox.Show("请选择数据行!");
return;
}
splashScreenManager1.ShowWaitForm();
foreach (var rownumber in rowsNumber)
{
var t_Station = this.gridView1.GetRow(rownumber) as QueryStationVM;
ids.Add(t_Station.Id);
}
if (ids.Count > 0)
{
var rows = StationHelper.DoDelStationByIds(ids);
if (rows > 0)
{
splashScreenManager1.CloseWaitForm();
XtraMessageBox.Show("操作成功!");
GetData();
}
else
{
splashScreenManager1.CloseWaitForm();
XtraMessageBox.Show("操作失败!");
}
}
else
{
splashScreenManager1.CloseWaitForm();
}
}
private void GetData(int pageCurrent = 1)
{
var list = DataSource();
this.GridMak.DataSource = list;
gridPage1.RefreshPager(pagesize, totalCount, pageCurrent);
}
/// <summary>
/// 数据源
/// </summary>
/// <param name="isPage"></param>
/// <returns></returns>
private List<QueryStationVM> DataSource(bool isPage = true)
{
var category = lkupCategory.EditValue?.ToString();
var keyWord = this.txtProductSfcCode.Text.Trim();
var data = StationHelper.QueryActiveStationVMs().OrderBy(m => m.Category).OrderBy(m => m.SortIndex);
if (isPage)
{
return data.WhereIF(!string.IsNullOrWhiteSpace(keyWord), m => m.StationCode.ToLower().Contains(keyWord.ToLower()) || m.StationName.ToLower().Contains(keyWord.ToLower()))
.WhereIF(!string.IsNullOrWhiteSpace(category), m => m.Category == category)
.ToPageList(pageCurrent, pagesize, ref totalCount);
}
return data.WhereIF(!string.IsNullOrWhiteSpace(keyWord), m => m.StationCode.ToLower().Contains(keyWord.ToLower()) || m.StationName.ToLower().Contains(keyWord.ToLower()))
.WhereIF(!string.IsNullOrWhiteSpace(category), m => m.Category == category)
.ToList();
}
/// <summary>
/// 分页事件
/// </summary>
/// <param name="curPage"></param>
/// <param name="pageSize"></param>
private void GridPage1_williamPagerEvent(int curPage, int pageSize)
{
pageCurrent = curPage;
pagesize = pageSize;
GetData(curPage);
}
/// <summary>
/// 行号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
private void btnimport_station_Click(object sender, EventArgs e)
{
var importThread = new Thread(new ThreadStart(ImportDialog_station));
importThread.SetApartmentState(ApartmentState.STA);
importThread.Start();
}
private void btnExport_station_Click(object sender, EventArgs e)
{
var ExportThread = new Thread(new ThreadStart(ExportDialog_station));
ExportThread.SetApartmentState(ApartmentState.STA);
ExportThread.Start();
}
/// <summary>
/// 导出
/// </summary>
private void ExportDialog_station()
{
try
{
FolderBrowserDialog path_station = new FolderBrowserDialog();
if (path_station.ShowDialog() == DialogResult.OK)
{
splashScreenManager1.ShowWaitForm();
var list_station = DataSource(false);
var filePath_station = string.Format("{0}\\{1}.xlsx", path_station.SelectedPath, DateTime.Now.ToString("yyyyMMddHHmmss"));
#region 设置列名称
var mappingColumns_station = new List<ExcelColumns>();
var co1 = new ExcelColumns { field = "IpcId", title = "工控机标识" };
var co2 = new ExcelColumns { field = "LineCode", title = "产线编码" };
var co3 = new ExcelColumns { field = "Category", title = "线路" };
var co4 = new ExcelColumns { field = "StationCode", title = "工位编码" };
var co5 = new ExcelColumns { field = "StationName", title = "工位名称" };
var co6 = new ExcelColumns { field = "StrIsShowOrderBtn", title = "是否显示工单绑定按钮" };
var co7 = new ExcelColumns { field = "StrIsShowPrintBtn", title = "是否显示手动打印按钮" };
var co8 = new ExcelColumns { field = "StrIsSearchAssemble", title = "是否查询离散装配绑定结果" };
var co9 = new ExcelColumns { field = "StrIsBindAssembleCode", title = "是否启用离散装配条码绑定" };
var co10 = new ExcelColumns { field = "StrIsSearchLastStation", title = "是否查询上工位数据" };
var co11 = new ExcelColumns { field = "DatabaseIp", title = "上工位数据库IP地址" };
var co12 = new ExcelColumns { field = "SortIndex", title = "排序索引" };
//var co13 = new ExcelColumns { field = "IsDeleted", title = "是否被删除" };
mappingColumns_station.Add(co1);
mappingColumns_station.Add(co2);
mappingColumns_station.Add(co3);
mappingColumns_station.Add(co4);
mappingColumns_station.Add(co5);
mappingColumns_station.Add(co6);
mappingColumns_station.Add(co7);
mappingColumns_station.Add(co8);
mappingColumns_station.Add(co9);
mappingColumns_station.Add(co10);
mappingColumns_station.Add(co11);
mappingColumns_station.Add(co12);
//mappingColumns_station.Add(co13);
#endregion
if (list_station.Count <= 0)
{
Invoke(new Action(() => { XtraMessageBox.Show("无数据!", "提示"); }));
splashScreenManager1.CloseWaitForm();
return;
}
ExcelHelper.ToExcel(list_station, filePath_station, mappingColumns_station, true);
Invoke(new Action(() => { XtraMessageBox.Show("导出成功!", "提示"); }));
splashScreenManager1.CloseWaitForm();
}
}
catch (Exception ex)
{
Invoke(new Action(() => { XtraMessageBox.Show("导出数据异常!", "提示"); }));
LogHelper.WriteErrorLog("导出数据异常!", ex);
}
}
/// <summary>
/// 导入
/// </summary>
private void ImportDialog_station()
{
using (OpenFileDialog openFileDialog_station = new OpenFileDialog() { Filter = "Excel Workbook|*.xlsx|Excel 97-2003 Workbook|*.xls" })
{
if (openFileDialog_station.ShowDialog() == DialogResult.OK)
{
splashScreenManager1.ShowWaitForm();
var filePath_station = openFileDialog_station.FileName;
var dt_station = ExcelHelper.GetExcel(filePath_station, 0, out var maxColumnNum);
if (dt_station.Rows.Count <= 0)
{
Invoke(new Action(() => { XtraMessageBox.Show("文件不能为空!", "提示"); }));
splashScreenManager1.CloseWaitForm();
return;
}
var result = CheckData_station(dt_station);
if (!result.isOK)
{
Invoke(new Action(() => { XtraMessageBox.Show("存在不合法数据,请检查!", "提示"); }));
splashScreenManager1.CloseWaitForm();
return;
}
if (StationHelper.AddStations(result.list))
{
Invoke(new Action(() => { XtraMessageBox.Show("操作成功!", "提示"); }));
Invoke(new Action(() => { GetData(); }));
}
else
{
Invoke(new Action(() => { XtraMessageBox.Show("操作失败!", "提示"); }));
}
splashScreenManager1.CloseWaitForm();
}
}
}
/// <summary>
/// 校验上传数据
/// </summary>
/// <param name="dt_station"></param>
/// <returns></returns>
private (bool isOK, List<T_Station> list) CheckData_station(DataTable dt_station)
{
var isOK = true;
var t_Stations = new List<T_Station>();
for (int rowindex = 0; rowindex < dt_station.Rows.Count; rowindex++)
{
if (string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_1"].ToString())
|| string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_2"].ToString())
|| string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_3"].ToString())
|| string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_4"].ToString())
|| string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_5"].ToString())
|| string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_6"].ToString())
|| string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_7"].ToString())
|| string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_8"].ToString())
|| string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_9"].ToString())
|| string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_10"].ToString())
//|| string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_11"].ToString())
|| string.IsNullOrWhiteSpace(dt_station.Rows[rowindex]["col_12"].ToString()))
{
isOK = false;
continue;
}
else
{
var t_Station = new T_Station
{
IpcId = dt_station.Rows[rowindex]["col_1"].ToString(),
LineCode = dt_station.Rows[rowindex]["col_2"].ToString(),
Category = dt_station.Rows[rowindex]["col_3"].ToString(),
StationCode = dt_station.Rows[rowindex]["col_4"].ToString(),
StationName = dt_station.Rows[rowindex]["col_5"].ToString(),
IsShowOrderBtn = dt_station.Rows[rowindex]["col_6"].ToString() == "是",
IsShowPrintBtn = dt_station.Rows[rowindex]["col_7"].ToString() == "是",
IsSearchAssemble = dt_station.Rows[rowindex]["col_8"].ToString() == "是",
IsBindAssembleCode = dt_station.Rows[rowindex]["col_9"].ToString() == "是",
IsSearchLastStation = dt_station.Rows[rowindex]["col_10"].ToString() == "是",
DatabaseIp = dt_station.Rows[rowindex]["col_11"].ToString(),
SortIndex = int.Parse(dt_station.Rows[rowindex]["col_12"].ToString()),
CreatedTime = DateTime.Now,
CreatedBy = CurrentUser.UserName
};
t_Stations.Add(t_Station);
}
}
return (isOK, t_Stations);
}
}
}