using DevExpress.XtraEditors; using DevExpress.XtraEditors.Controls; using System; using System.Collections.Generic; 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.UserControls.BranchInfo; using ZJ_BYD.ViewModel; namespace ZJ_BYD.UserControls.MaskCode { public partial class MskCode : XtraUserControl { private int pagesize = 30; //每页显示行数 private int totalCount = 0; //总记录数 private int pageCurrent = 1; //当前页号 public MskCode() { InitializeComponent(); gridView1.CustomDrawRowIndicator += GridView1_CustomDrawRowIndicator; gridPage1.williamPagerEvent += GridPage1_williamPagerEvent; gridView1.IndicatorWidth = 35; this.repositoryItemHyperLinkEdit1.ButtonClick += new ButtonPressedEventHandler(repositoryItemHyperLinkEdit_ButtonClick); } private void MskCode_Load(object sender, EventArgs e) { BindCombox.BindStation(cmbStation); GetData(); } private void repositoryItemHyperLinkEdit_ButtonClick(object sender, ButtonPressedEventArgs e) { var rowHandle = gridView1.FocusedRowHandle; var model = gridView1.GetRow(rowHandle) as MaskCodeVM; if (e.Button.Caption == "编辑") { UpdateMaskCode editMaskCode = new UpdateMaskCode(model.Id); var result = editMaskCode.ShowDialog(); if (result == DialogResult.OK) { GetData(); } } else if (e.Button.Caption == "删除") { DialogResult dialogResult = XtraMessageBox.Show("确认删除吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (dialogResult == DialogResult.OK) { splashScreenManager1.ShowWaitForm(); var rows = MskCodeHelper.DelMaskCodeByIds(model.Id); if (rows > 0) { splashScreenManager1.CloseWaitForm(); XtraMessageBox.Show("操作成功!"); GetData(); } else { splashScreenManager1.CloseWaitForm(); XtraMessageBox.Show("操作失败!"); } } } else//支线信息 { var stationCode = model.StationCode; var stationName = model.StationName; var productSfcCode = model.ProductSfcCode; EditBranch editBranch = new EditBranch(stationCode, stationName, productSfcCode, false); editBranch.ShowDialog(); } } private void GetData(int pageCurrent = 1) { var list = DataSource(); this.GridMak.DataSource = list; gridPage1.RefreshPager(pagesize, totalCount, pageCurrent); } /// /// 数据源 /// /// /// private List DataSource(bool isPage = true) { var selectedStation = cmbStation.SelectedItem as ListItem; var keyWord = this.txtProductSfcCode.Text.Trim(); var data = MskCodeHelper.QueryMskCodes() .InnerJoin((a, b) => a.LineCode == b.LineCode && a.Category == b.Category && a.StationCode == b.StationCode && !b.IsDeleted) .Select((a, b) => new MaskCodeVM { Id = a.Id, LineCode = a.LineCode, Category = a.Category, StationCode = a.StationCode, StationName = b.StationName, ProductSfcCode = a.ProductSfcCode, SfcDsp = a.SfcDsp, ItemCode = a.ItemCode, ItemVersion = a.ItemVersion, ItemType = a.ItemType, Sub1SfcCode = a.Sub1SfcCode, Sub1SfcDsp = a.Sub1SfcDsp, Sub1SfcItemCode = a.Sub1SfcItemCode, Sub1ItemVersion = a.Sub1ItemVersion, Sub1ItemType = a.Sub1ItemType, Sub2SfcCode = a.Sub2SfcCode, Sub2SfcDsp = a.Sub2SfcDsp, Sub2SfcItemCode = a.Sub2SfcItemCode, Sub2ItemVersion = a.Sub2ItemVersion, Sub2ItemType = a.Sub2ItemType, Sub3SfcCode = a.Sub3SfcCode, Sub3SfcDsp = a.Sub3SfcDsp, Sub3SfcItemCode = a.Sub3SfcItemCode, Sub3ItemVersion = a.Sub3ItemVersion, Sub3ItemType = a.Sub3ItemType, Sub4SfcCode = a.Sub4SfcCode, Sub4SfcDsp = a.Sub4SfcDsp, Sub4SfcItemCode = a.Sub4SfcItemCode, Sub4ItemVersion = a.Sub4ItemVersion, Sub4ItemType = a.Sub4ItemType, Sub5SfcCode = a.Sub5SfcCode, Sub5SfcDsp = a.Sub5SfcDsp, Sub5SfcItemCode = a.Sub5SfcItemCode, Sub5ItemVersion = a.Sub5ItemVersion, Sub5ItemType = a.Sub5ItemType, WeightStandar = a.WeightStandar, CarMode = a.CarMode, Site = a.Site, Resource = a.Resource, Procedure = a.Procedure, NgCode = a.NgCode, BarCodeChar = a.BarCodeChar, SortIndex = a.SortIndex, StrIsUsed = a.IsUsed ? "使用中" : "未使用", SoftCode = a.SoftCode, GuideVersion = a.GuideVersion, VoltageMax = a.VoltageMax, VoltageMin = a.VoltageMin, LastStationSite = a.LastStationSite, LastStationResource = a.LastStationResource, LastStationProcedure = a.LastStationProcedure, PrintBarcode = a.PrintBarcode, ProgramBlockNum = a.ProgramBlockNum, IsUseStator = a.IsUseStator, SoftVersionLen = a.SoftVersionLen, IsSerialNum = a.IsSerialNum, StrIsSerialNum = a.IsSerialNum ? "是" : "否", PartCode = a.PartCode }).OrderBy(a => a.Category); if (selectedStation != null && !string.IsNullOrWhiteSpace(selectedStation.Value)) { data.Where(a => a.StationCode == selectedStation.Value); } if (isPage) { return data.WhereIF(!string.IsNullOrWhiteSpace(keyWord), a => a.ProductSfcCode.Contains(keyWord)) .ToPageList(pageCurrent, pagesize, ref totalCount); } return data.WhereIF(!string.IsNullOrWhiteSpace(keyWord), m => m.ProductSfcCode.Contains(keyWord)) .ToList(); } /// /// 点击查询按钮 /// /// /// private void btnsearch_Click(object sender, EventArgs e) { splashScreenManager1.ShowWaitForm(); GetData(); splashScreenManager1.CloseWaitForm(); } /// /// 点击新增按钮 /// /// /// private void btnadd_Click(object sender, EventArgs e) { UpdateMaskCode editMaskCode = new UpdateMaskCode(); var result = editMaskCode.ShowDialog(); if (result == DialogResult.OK) { GetData(); } } /// /// 点击导出按钮 /// /// /// private void btnexport_Click(object sender, EventArgs e) { var importThread = new Thread(new ThreadStart(ExportDialog)); importThread.SetApartmentState(ApartmentState.STA); //重点 importThread.Start(); } /// /// 点击模板下载按钮 /// /// /// private void btnDownload_Click(object sender, EventArgs e) { var downThread = new Thread(new ThreadStart(DownDialog)); downThread.SetApartmentState(ApartmentState.STA); //重点 downThread.Start(); } /// /// 点击上传Excel按钮 /// /// /// private void btnimport_Click(object sender, EventArgs e) { var importThread = new Thread(new ThreadStart(ImportDialog)); 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 = "LineCode", title = "线编码" }; var co2 = new ExcelColumns { field = "StationCode", title = "工位编码" }; var co3 = new ExcelColumns { field = "ProductSfcCode", title = "机壳SFC掩码" }; var co4 = new ExcelColumns { field = "SfcDsp", title = "SFC_DSP描述" }; var co5 = new ExcelColumns { field = "ItemCode", title = "ITEM物料编码" }; var co6 = new ExcelColumns { field = "ItemVersion", title = "ITEM_VISION版本" }; var co7 = new ExcelColumns { field = "ItemType", title = "ITEM_TYPE内外部批次" }; var co8 = new ExcelColumns { field = "Sub1SfcCode", title = "组件1SFC掩码" }; var co9 = new ExcelColumns { field = "Sub1SfcDsp", title = "组件1SFC_DSP描述" }; var co10 = new ExcelColumns { field = "Sub1SfcItemCode", title = "组件1ITEM物料编码" }; var co11 = new ExcelColumns { field = "Sub1ItemVersion", title = "组件1ITEM_VISION版本" }; var co12 = new ExcelColumns { field = "Sub1ItemType", title = "组件1ITEM_TYPE内外部批次" }; var co13 = new ExcelColumns { field = "Sub2SfcCode", title = "组件2SFC掩码" }; var co14 = new ExcelColumns { field = "Sub2SfcDsp", title = "组件2SFC_DSP描述" }; var co15 = new ExcelColumns { field = "Sub2SfcItemCode", title = "组件2ITEM物料编码" }; var co16 = new ExcelColumns { field = "Sub2ItemVersion", title = "组件2ITEM_VISION版本" }; var co17 = new ExcelColumns { field = "Sub2ItemType", title = "组件2ITEM_TYPE内外部批次" }; var co18 = new ExcelColumns { field = "Sub3SfcCode", title = "组件3SFC掩码" }; var co19 = new ExcelColumns { field = "Sub3SfcDsp", title = "组件3SFC_DSP描述" }; var co20 = new ExcelColumns { field = "Sub3SfcItemCode", title = "组件3ITEM物料编码" }; var co21 = new ExcelColumns { field = "Sub3ItemVersion", title = "组件3ITEM_VISION版本" }; var co22 = new ExcelColumns { field = "Sub3ItemType", title = "组件3ITEM_TYPE内外部批次" }; var co23 = new ExcelColumns { field = "Sub4SfcCode", title = "组件4SFC掩码" }; var co24 = new ExcelColumns { field = "Sub4SfcDsp", title = "组件4SFC_DSP描述" }; var co25 = new ExcelColumns { field = "Sub4SfcItemCode", title = "组件4ITEM物料编码" }; var co26 = new ExcelColumns { field = "Sub4ItemVersion", title = "组件4ITEM_VISION版本" }; var co27 = new ExcelColumns { field = "Sub4ItemType", title = "组件4ITEM_TYPE内外部批次" }; var co28 = new ExcelColumns { field = "Sub5SfcCode", title = "组件5SFC掩码" }; var co29 = new ExcelColumns { field = "Sub5SfcDsp", title = "组件5SFC_DSP描述" }; var co30 = new ExcelColumns { field = "Sub5SfcItemCode", title = "组件5ITEM物料编码" }; var co31 = new ExcelColumns { field = "Sub5ItemVersion", title = "组件5ITEM_VISION版本" }; var co32 = new ExcelColumns { field = "Sub5ItemType", title = "组件5ITEM_TYPE内外部批次" }; var co33 = new ExcelColumns { field = "WeightStandar", title = "垫圈重量标准G" }; var co34 = new ExcelColumns { field = "CarMode", title = "车型" }; var co35 = new ExcelColumns { field = "Site", title = "站点" }; var co36 = new ExcelColumns { field = "Resource", title = "资源" }; var co37 = new ExcelColumns { field = "Procedure", title = "工序" }; var co38 = new ExcelColumns { field = "NgCode", title = "不合格代码" }; var co39 = new ExcelColumns { field = "SortIndex", title = "排序索引" }; var co40 = new ExcelColumns { field = "BarCodeChar", title = "打印条码字符" }; var co41 = new ExcelColumns { field = "Category", title = "线路" }; var co42 = new ExcelColumns { field = "SoftCode", title = "232软件编码" }; var co43 = new ExcelColumns { field = "GuideVersion", title = "232引导版本号" }; var co44 = new ExcelColumns { field = "VoltageMax", title = "232电压上限" }; var co45 = new ExcelColumns { field = "VoltageMin", title = "232电压下限" }; var co46 = new ExcelColumns { field = "LastStationSite", title = "上工位站点" }; var co47 = new ExcelColumns { field = "LastStationResource", title = "上工位资源" }; var co48 = new ExcelColumns { field = "LastStationProcedure", title = "上工位工序" }; var co49 = new ExcelColumns { field = "PrintBarcode", title = "打印条码组成字符" }; var co50 = new ExcelColumns { field = "ProgramBlockNum", title = "程序块编码" }; var co51 = new ExcelColumns { field = "SoftVersionLen", title = "软件版本号长度" }; var co52 = new ExcelColumns { field = "StrIsSerialNum", title = "是否写入序列号" }; var co53 = new ExcelColumns { field = "PartCode", 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); mappingColumns.Add(co18); mappingColumns.Add(co19); mappingColumns.Add(co20); mappingColumns.Add(co21); mappingColumns.Add(co22); mappingColumns.Add(co23); mappingColumns.Add(co24); mappingColumns.Add(co25); mappingColumns.Add(co26); mappingColumns.Add(co27); mappingColumns.Add(co28); mappingColumns.Add(co29); mappingColumns.Add(co30); mappingColumns.Add(co31); mappingColumns.Add(co32); mappingColumns.Add(co33); mappingColumns.Add(co34); mappingColumns.Add(co35); mappingColumns.Add(co36); mappingColumns.Add(co37); mappingColumns.Add(co38); mappingColumns.Add(co39); mappingColumns.Add(co40); mappingColumns.Add(co41); mappingColumns.Add(co42); mappingColumns.Add(co43); mappingColumns.Add(co44); mappingColumns.Add(co45); mappingColumns.Add(co46); mappingColumns.Add(co47); mappingColumns.Add(co48); mappingColumns.Add(co49); mappingColumns.Add(co50); mappingColumns.Add(co51); mappingColumns.Add(co52); mappingColumns.Add(co53); #endregion if (list.Count <= 0) { splashScreenManager1.CloseWaitForm(); Invoke(new Action(() => { XtraMessageBox.Show("无数据!", "提示"); })); return; } ExcelHelper.ToExcel(list, filePath, mappingColumns, true); splashScreenManager1.CloseWaitForm(); Invoke(new Action(() => { XtraMessageBox.Show("导出成功!", "提示"); })); } } catch (Exception ex) { splashScreenManager1.CloseWaitForm(); 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("文件不能为空!", "提示"); })); return; } var t_MaskCodes = new List(); var hasError = false; var errorMsg = "文件中第【"; for (int rowindex = 0; rowindex < dt.Rows.Count; rowindex++) { var strSortIdnex = dt.Rows[rowindex]["col_39"].ToString(); var sortIndex = 0; if (!string.IsNullOrWhiteSpace(strSortIdnex)) { int.TryParse(strSortIdnex, out sortIndex); } var stationCode = dt.Rows[rowindex]["col_2"].ToString(); if (!StationHelper.CheckStationCode(stationCode)) { hasError = true; errorMsg += string.Format("{0}、", rowindex); } #region 给model赋值 var t_MaskCode = new T_MaskCode { LineCode = dt.Rows[rowindex]["col_1"].ToString(), StationCode = stationCode, ProductSfcCode = dt.Rows[rowindex]["col_3"].ToString(), SfcDsp = dt.Rows[rowindex]["col_4"].ToString(), ItemCode = dt.Rows[rowindex]["col_5"].ToString(), ItemVersion = dt.Rows[rowindex]["col_6"].ToString(), ItemType = dt.Rows[rowindex]["col_7"].ToString(), Sub1SfcCode = dt.Rows[rowindex]["col_8"].ToString(), Sub1SfcDsp = dt.Rows[rowindex]["col_9"].ToString(), Sub1SfcItemCode = dt.Rows[rowindex]["col_10"].ToString(), Sub1ItemVersion = dt.Rows[rowindex]["col_11"].ToString(), Sub1ItemType = dt.Rows[rowindex]["col_12"].ToString(), Sub2SfcCode = dt.Rows[rowindex]["col_13"].ToString(), Sub2SfcDsp = dt.Rows[rowindex]["col_14"].ToString(), Sub2SfcItemCode = dt.Rows[rowindex]["col_15"].ToString(), Sub2ItemVersion = dt.Rows[rowindex]["col_16"].ToString(), Sub2ItemType = dt.Rows[rowindex]["col_17"].ToString(), Sub3SfcCode = dt.Rows[rowindex]["col_18"].ToString(), Sub3SfcDsp = dt.Rows[rowindex]["col_19"].ToString(), Sub3SfcItemCode = dt.Rows[rowindex]["col_20"].ToString(), Sub3ItemVersion = dt.Rows[rowindex]["col_21"].ToString(), Sub3ItemType = dt.Rows[rowindex]["col_22"].ToString(), Sub4SfcCode = dt.Rows[rowindex]["col_23"].ToString(), Sub4SfcDsp = dt.Rows[rowindex]["col_24"].ToString(), Sub4SfcItemCode = dt.Rows[rowindex]["col_25"].ToString(), Sub4ItemVersion = dt.Rows[rowindex]["col_26"].ToString(), Sub4ItemType = dt.Rows[rowindex]["col_27"].ToString(), Sub5SfcCode = dt.Rows[rowindex]["col_28"].ToString(), Sub5SfcDsp = dt.Rows[rowindex]["col_29"].ToString(), Sub5SfcItemCode = dt.Rows[rowindex]["col_30"].ToString(), Sub5ItemVersion = dt.Rows[rowindex]["col_31"].ToString(), Sub5ItemType = dt.Rows[rowindex]["col_32"].ToString(), WeightStandar = dt.Rows[rowindex]["col_33"].ToString(), CarMode = dt.Rows[rowindex]["col_34"].ToString(), Site = dt.Rows[rowindex]["col_35"].ToString(), Resource = dt.Rows[rowindex]["col_36"].ToString(), Procedure = dt.Rows[rowindex]["col_37"].ToString(), NgCode = dt.Rows[rowindex]["col_38"].ToString(), SortIndex = sortIndex, BarCodeChar = dt.Rows[rowindex]["col_40"].ToString(), Category = dt.Rows[rowindex]["col_41"].ToString(), SoftCode = dt.Rows[rowindex]["col_42"].ToString(), GuideVersion = dt.Rows[rowindex]["col_43"].ToString(), VoltageMax = dt.Rows[rowindex]["col_44"].ToString(), VoltageMin = dt.Rows[rowindex]["col_45"].ToString(), LastStationSite = dt.Rows[rowindex]["col_46"].ToString(), LastStationResource = dt.Rows[rowindex]["col_47"].ToString(), LastStationProcedure = dt.Rows[rowindex]["col_48"].ToString(), PrintBarcode = dt.Rows[rowindex]["col_49"].ToString(), ProgramBlockNum = int.Parse(dt.Rows[rowindex]["col_50"].ToString()), SoftVersionLen = int.Parse(dt.Rows[rowindex]["col_51"].ToString()) }; #endregion if (string.IsNullOrWhiteSpace(t_MaskCode.StationCode) || string.IsNullOrWhiteSpace(t_MaskCode.ProductSfcCode) || string.IsNullOrWhiteSpace(t_MaskCode.Site) || string.IsNullOrWhiteSpace(t_MaskCode.Resource) || string.IsNullOrWhiteSpace(t_MaskCode.Procedure) || string.IsNullOrWhiteSpace(t_MaskCode.NgCode) || string.IsNullOrWhiteSpace(t_MaskCode.BarCodeChar) || string.IsNullOrWhiteSpace(t_MaskCode.Category)) { hasError = true; errorMsg += string.Format("{0}、", rowindex); } else { t_MaskCodes.Add(t_MaskCode); } } errorMsg = errorMsg.TrimEnd('、'); errorMsg += "】行数据异常!"; splashScreenManager1.CloseWaitForm(); if (hasError) { Invoke(new Action(() => { XtraMessageBox.Show(errorMsg, "提示"); })); return; } if (MskCodeHelper.AddMaskCodes(t_MaskCodes)) { Invoke(new Action(() => { XtraMessageBox.Show("操作成功!", "提示"); })); Invoke(new Action(() => { GetData(); })); } else { Invoke(new Action(() => { XtraMessageBox.Show("操作失败!", "提示"); })); } } } } private void DownDialog() { var fileName = "ProductConfig.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); splashScreenManager1.CloseWaitForm(); if (File.Exists(localPath)) { Invoke(new Action(() => { XtraMessageBox.Show("下载成功!", "提示"); })); } else { Invoke(new Action(() => { XtraMessageBox.Show("下载失败!", "提示"); })); } } 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(); } } /// /// 启用支线查询复选框change事件 /// /// /// private void repositoryItemCheckEdit2_CheckedChanged(object sender, EventArgs e) { int index = this.gridView1.FocusedRowHandle; var stationCode = this.gridView1.GetRowCellValue(index, "StationCode").ToString(); var strMskId = this.gridView1.GetRowCellValue(index, "Id").ToString(); int.TryParse(strMskId, out var mskId); var stationName = this.gridView1.GetRowCellValue(index, "StationName").ToString(); var productSfcCode = this.gridView1.GetRowCellValue(index, "ProductSfcCode").ToString(); var checkBox = (CheckEdit)sender; if (checkBox.Checked) { var updateRows = MskCodeHelper.UpdateMskCodeIsUsedStator(mskId, true); if (updateRows <= 0) { XtraMessageBox.Show("更新掩码表是否启用支线查询字段时失败!"); return; } EditBranch editBranch = new EditBranch(stationCode, stationName, productSfcCode); var diaLogResult = editBranch.ShowDialog(); if (diaLogResult == DialogResult.OK) { XtraMessageBox.Show("操作成功!"); return; } else if (diaLogResult == DialogResult.No) { XtraMessageBox.Show("操作失败!"); return; } else { GetData(); } } else { var result = BranchInfoHelper.delBranchInfo(stationCode, productSfcCode); if (!result) { XtraMessageBox.Show("操作失败!"); } else { XtraMessageBox.Show("操作成功!"); } GetData(); } } } }