using Admin.NET.Application; using Admin.NET.Core.WareHouse.Enum; using autuPrint.printService; using Excel2SQL; using iWareCommon; using iWareCommon.Utils; using iWareSql; using iWareSql.DataAccess; using iWareSql.MyDbContext; using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; using WindowsFormsApplication1.Common; namespace WindowsFormsApplication1 { public partial class Form1 : Form { //自动打印线程 Thread AutoPrint, Colors; public Form1() { InitializeComponent(); } private void btn_SelectFile_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = true; fileDialog.Title = "请选择文件"; fileDialog.Filter = "所有文件(*xls*)|*.xls*"; //设置要选择的文件的类型 if (fileDialog.ShowDialog() == DialogResult.OK) { string file = fileDialog.FileName;//返回文件的完整路径 this.tb_FilePath.Text = file; } } private void button2_Click(object sender, EventArgs e) { try { this.tb_Msg.Text = "打印:"; string filePath = this.tb_FilePath.Text; if (filePath == string.Empty) { MessageBox.Show("请选择Excel!"); return; } Dictionary cellheader = new Dictionary { { "MaterialCode", "物料号" }, { "MaterialName", "物料名称" }, { "Vendor", "供应商" }, { "PO", "PO" }, { "Qty", "数量" } }; // 1.2解析文件,存放到一个List集合里 StringBuilder errorMsg = new StringBuilder(); // 错误信息 string tableDesc = "", tableName = ""; List enlist = ExcelHelper.ExcelToEntityListForCreateTable(cellheader, filePath, out tableDesc, out tableName, out errorMsg); if (!string.IsNullOrEmpty(errorMsg.ToString())) { MessageBox.Show("错误:" + errorMsg.ToString()); return; } enlist = enlist.Where(x => !string.IsNullOrEmpty(x.MaterialCode)).ToList(); this.tb_Msg.Text = "处理条数:" + enlist.Count; if (enlist != null && enlist.Count > 0) { foreach (var item in enlist) { if (string.IsNullOrEmpty(item.MaterialName)) { continue; } this.do_autoPrint(item); } //MessageBox.Show("批量打印成功成功!"); return; } MessageBox.Show("无打印内容!"); } catch (Exception ex) { MessageBox.Show("错误:" + ex.Message.ToString()); this.tb_Msg.Text = "异常:" + ex.Message; } } /// /// 自动打印-行动 /// public void do_autoPrint(MaterialEntity material) { try { #region 单个处理标签打印 string barCode = material.MaterialName;//标签条码 Image img = QrCodeAndBarCode.printQr(barCode); var errMsg = ""; ware_material_print_history ware_Material_Print_History = new ware_material_print_history() { Code = material.MaterialCode, Name = material.MaterialName, SupplierNo = material.Vendor, PurchaseNo = material.PO, Quantity = Convert.ToDecimal(material.Qty) }; bool saveSucceed = creatExcelPrint.creatEXcel(img, ware_Material_Print_History, ref errMsg); if (saveSucceed) { if (File.Exists(SystemConst.TEMPORARY_FILEPATH)) { bool printSucceed = true; printSucceed = print.printExcel(SystemConst.TEMPORARY_FILEPATH, SystemConst.Default_LeftMargin, SystemConst.Default_TopMargin); if (printSucceed) { //打印成功 删除临时文件,并改状态 FileInfo myfile = new FileInfo(SystemConst.TEMPORARY_FILEPATH);//PartStatus myfile.Delete(); int isSave = 1; if (isSave < 1) { Log4NetHelper.WriteErrorLog(LogType.Print, "类名: Form1 方法名: do_autoPrint 修改库存自动打印状态失败,修改条数小于1 ", null); } } //*/ } } else { this.tb_Msg.Text = "失败,失败内容:" + errMsg; } #endregion } catch (Exception ex) { Log4NetHelper.WriteErrorLog(LogType.Print, "类名: Service1 方法名: do_autoPrint 自动打印方法参数转换失败 \r\n " + ex.ToString(), ex); this.tb_Msg.Text = "失败异常 :" + ex.Message; } } private void Form1_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; //检测是否已经开启 Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes) { if (process.Id != current.Id) { if (process.MainModule.FileName == current.MainModule.FileName) { Environment.Exit(0); return; } } } Colors = new Thread(changeColor); Colors.Start(); AutoPrint = new Thread(DBAutoPrint); AutoPrint.Start(); new Thread(DeleteData).Start(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Environment.Exit(0);//退出本进程所有线程 } public void changeColor() { bool isChange = true; while (true) { Thread.Sleep(1000); if (isChange) { label1.BackColor = Color.Yellow; } else { label1.BackColor = Color.Pink; } isChange = !isChange; } } #region 数据库打印 public void DBAutoPrint() { var errMsg = ""; while (true) { try { this.tb_Msg.Text = "循环开始..."; errMsg = "";//重置 try { Do(ref errMsg); if (!string.IsNullOrEmpty(errMsg)) { this.tb_Msg.Text = errMsg; } } catch (Exception ex) { Log4NetHelper.WriteErrorLog(LogType.Print, "打印处理的线程=>" + errMsg + ",异常:" + ex.Message, ex); this.tb_Msg.Text = "打印处理的线程=>" + errMsg + ",异常:" + ex.Message; } } catch (Exception ex) { this.tb_Msg.Text = "出现异常:" + ex.Message; Log4NetHelper.WriteErrorLog(LogType.Print, "Print 出现异常:" + ex.Message, ex); } Thread.Sleep(2 * 1000);//休眠2秒 } } /// /// 打印 /// /// /// /// /// private void Do(ref string errMsg) { using (WmsDBModel mycontext = new WmsDBModel()) { var query_PrintStatuEnum = (int)PrintStatuEnum.未打印; var list = mycontext.wms_record_print.Where(x => (x.IsDelete == false) && x.PrintStatus == query_PrintStatuEnum) .OrderBy(x => x.Id).ToList(); wms_config_print printConfig = null; string _PrinterName = ""; if (list.Count > 0) { } else { errMsg = $"没有可打印的内容!"; return; } foreach (var item in list) { PrintClassifyEnum printClassifyEnum = PrintClassifyEnum.包装条码; printConfig = mycontext.wms_config_print.Where(x => x.PrintClassify == (int)printClassifyEnum).FirstOrDefault(); if (printConfig == null) { errMsg = $"没有找到{printClassifyEnum.ToString()}的打印配置!"; return; } //设置打印机,注意:这里只获取第一个,以后可能要改! _PrinterName = printConfig.PrinterList.Split(',')[0]; //根据配置名字查询 打印机名字 //var printConfigData = mycontext.SysDictData.Where(x => x.Name == _PrinterName).FirstOrDefault(); //if (printConfigData == null) //{ // errMsg = $"没有找到{_PrinterName.ToString()}的打印机名字!"; // return; //} //_PrinterName = printConfigData.Value; Dictionary data = new Dictionary(); if (printClassifyEnum == PrintClassifyEnum.包装条码) { data.Add("Info4", item.Info4);//合同单号 data.Add("Info5", item.Info5 ?? "");//生产单号 data.Add("Info6", "第" + (item.Info6 ?? "") + "包");//第几包 data.Add("bar", item.PackageCode);//二维码(包号) data.Add("Info7", item.Info7 ?? "");//经销店 data.Add("Info8", item.Info8 ?? "");//产品 data.Add("Info9", item.PackageCode ?? "");//包装编码 data.Add("Info10", item.Info10 ?? "");//客户名称 data.Add("Info11", item.Info11 ?? "");//自提or发货 data.Add("Info12", "包装明细 部件总数" + item.Info12 ?? "");//部件明细 data.Add("Info13", item.Info13 ?? "");//当前包面积 data.Add("printTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //查询下 包数据 var mylist = mycontext.mes_batchOrderUPI_new.Where(x => x.PackageCode == item.PackageCode).ToList(); var gg = mylist.GroupBy(x => x.DetailName); int i = 0; foreach (var g in gg) { i++; data.Add("detail_" + i, g.Key ?? "");//部件名称 data.Add("detail_" + i + "_length", g.ToList().First().Info16 ?? "");//部件尺寸 data.Add("detail_" + i + "_number", g.ToList().Count());//部件数量 } for (int p = (i + 1); p <= 6; p++) { data.Add("detail_" + p, "");//部件名称 data.Add("detail_" + p + "_length", "");//部件尺寸 data.Add("detail_" + p + "_number", "");//部件数量 } } else { } //调用打印接口 bool isSuccess = false; using (iWarePrintService.PrintBaseService.PrintWcfServiceClient client = new iWarePrintService.PrintBaseService.PrintWcfServiceClient()) { //isSuccess = client.Print3(data, 1, @"D:\打印程序\跟踪码打印模板\跟踪码打印模板_一维码.btw", "ZDesigner ZD888-203dpi ZPL", out errMsg); //isSuccess = client.Print3(data, 1, @"D:\打印程序\跟踪码打印模板\跟踪码打印模板_二维码.btw", "ZDesigner ZD888-203dpi ZPL", out errMsg); isSuccess = client.Print3(data, item.PrintSheetNum, printConfig.PrintTemplateAddr, _PrinterName, out errMsg); } if (isSuccess) { item.PrintStatus = 1;//打印状态(0:未打印 1:已打印) item.PrintNum = (item.PrintNum) + 1; mycontext.SaveChanges(); } else { errMsg = $"打印错误 打印机名称{_PrinterName},错误信息:(打印服务返回)" + errMsg; Log4NetHelper.WriteErrorLog(LogType.Print, errMsg, null); return; } Thread.Sleep(2000); } } } #endregion #region 定时删除数据 /// /// 定时删除数据 /// public void DeleteData() { while (true) { try { LogTextHelper.BatchDeleteLog(); Thread.Sleep(8 * 60 * 60 * 1000);//每天8小时一次 } catch (Exception ex) { Log4NetHelper.WriteErrorLog(iWareCommon.Utils.LogType.Sys_DeleteLog, "定时删除数据 出现异常", ex); } finally { } } } #endregion } }