using Aspose.Cells; using Aspose.Cells.Rendering; using autuPrint.printService; using OfficeOpenXml.Drawing; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using ZXing; using ZXing.Common; using autuPrint.ORM; using System.Diagnostics; using iWareCommon.Utils; namespace autuPrint { public partial class Form1 : Form { public static string QuerPartStatus = ConfigHelper.GetConfigString("QuerPartStatus");//修改为配置文件 【EditBy shaocx,2022-01-27】 public Form1() { InitializeComponent(); } //自动打印线程 Thread AutoPrint, Colors; /// /// //线程锁防止并发 /// public static Mutex single = new Mutex(); private void Form1_Load(object sender, EventArgs e) { //检测是否已经开启 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; } } } AutoPrint = new Thread(autoPrint); AutoPrint.Start();//开启自动打印线程 var doAutoPrint = new Thread(do_autoPrint); doAutoPrint.Start();//开启自动打印线程 new Thread(DeleteData).Start(); Colors = new Thread(changeColor); Colors.Start(); string path = @"C:\autoPrint\" + "Setup.txt"; if (!Directory.Exists(Path.GetDirectoryName(path))) Directory.CreateDirectory(Path.GetDirectoryName(path)); if (!File.Exists(path)) File.WriteAllText(path, string.Empty); string org = File.ReadAllText(path); string[] getSetUP = org.Split('#'); if (getSetUP.Length == 2) { textBox1.Text = getSetUP[0]; textBox2.Text = getSetUP[1]; } } /// /// 自动打印-准备数据 /// public void autoPrint() { while (true) { Thread.Sleep(1000); if (single.WaitOne())//增加线程锁 { try { using (dbModel1 mod = new dbModel1()) { //List needPrint = mod.vBase_ProductPosition.Where(x => x.PositionName == "收货暂存区" && x.ProductStorage > 0 && x.PartStatus == "1").ToList(); List needPrint = mod.vBase_ProductPosition.Where(x => x.PositionName == "收货暂存区" && x.ProductStorage > 0 && x.PartStatus == QuerPartStatus).ToList(); //needPrint = needPrint.Where(x => x.ProductStorage>0).ToList(); if (needPrint.Count > 0) { foreach (var item in needPrint) { var isQrCode = false;//是不是二维码 #region 单个处理标签打印 Thread.Sleep(1000); #region 生成 标签条码 string barCode = ""; if (!string.IsNullOrEmpty(item.qrCode_guid)) { isQrCode = true; barCode = item.qrCode_guid; } else { barCode = DateTime.Now.ToString("yyMMddHHmmss"); } var existCount = mod.Base_PositionPrint.Where(x => x.ExtendField06 == barCode).Count(); if (existCount >= 1) {//说明标签条码重复了,需要直接返回 【Editby shaocx,2022-10-20】 Log4NetHelper.WriteInfoLog(LogType.AutoHandlerPrint, "说明标签条码重复了,需要直接返回:" + barCode); //必须要保存数据才对! //注意:保存 item.PartStatus = "0";//赋值为0,表示已经打印完毕 【EditBy shaocx,2022-10-15】 //为什么会将库存清零?我不明白,我给注释掉了.因为清零,会导致我单独上架时库存为0的问题 【Editby shaocx,2024-09-12】 //item.ProductStorage = 0M; var dbObj = mod.Base_ProductPosition.Where(x => x.ProductPosition_Id == item.ProductPosition_Id).FirstOrDefault(); if (dbObj != null) { dbObj.ExtendField06 = barCode;//记录标签码 dbObj.BarCode = barCode;//记录标签码 } int pp = mod.SaveChanges(); Log4NetHelper.WriteInfoLog(LogType.AutoHandlerPrint, "说明标签条码重复了,需要直接返回SaveChanges,直接返回:" + pp); break;//直接跳出本次循环 } #endregion #region 写入打印记录,该表用于界面【码盘上架】扫码查询使用 try { Base_PositionPrint printRecord = new Base_PositionPrint(); printRecord.ExtendField06 = barCode;//标签条码 printRecord.PoCode = item.PoCode == null ? "" : item.PoCode;//采购单号 printRecord.ItemNumber = item.ItemNumber == null ? "" : item.ItemNumber;//采购项号 printRecord.SaleCode = item.SaleCode == null ? "" : item.SaleCode;//销售单号 printRecord.ExtendField08 = item.ExtendField08 == null ? "" : item.ExtendField08;//销售项号 printRecord.ExtendField04 = item.ExtendField04 == null ? "" : item.ExtendField04;//跟踪号 printRecord.ProductCode = item.ProductCode;//物料编号 printRecord.ProductName = item.ProductName;//物料名称 printRecord.Quantity = (decimal?)item.OrignStorage;//标签上物料数量 修复 异常可空对象必须要有一个值 【Editby shaocx,2023-07-11】 printRecord.SmallUnit = item.SmallUnit;//库存单位 printRecord.InStorageDate = (DateTime)item.InStorageDate;//收货日期 printRecord.LimitDate = item.LimitDate;//限用日期 printRecord.ProviderShortName = item.ProviderShortName;//供应商名称 printRecord.CreateDate = DateTime.Now; //不为空垃圾字段 printRecord.CreateID = 1; printRecord.Creator = !isQrCode ? "自动打印" : "二维码,不打印"; printRecord.PlatUser_Id = 1; printRecord.UserProduct_Id = 1007; printRecord.isNeedPrint = 1;//需要打印 【EditBy shaocx,2022-10-20】 printRecord.printDevice = Convert.ToInt32(QuerPartStatus);//打印设备 【EditBy shaocx,2022-10-20】 if (isQrCode) {//二维码,不需要打印 printRecord.isNeedPrint = 0;//打印完成 } mod.Base_PositionPrint.Add(printRecord); //注意:保存 item.PartStatus = "0";//赋值为0,表示已经打印完毕 【EditBy shaocx,2022-10-15】 //为什么会将库存清零?我不明白,我给注释掉了.因为清零,会导致我单独上架时库存为0的问题 【Editby shaocx,2024-09-12】 //item.ProductStorage = 0M; var dbObj = mod.Base_ProductPosition.Where(x => x.ProductPosition_Id == item.ProductPosition_Id).FirstOrDefault(); if (dbObj != null) { dbObj.ExtendField06 = barCode;//记录标签码 dbObj.BarCode = barCode;//记录标签码 } int i = mod.SaveChanges(); Log4NetHelper.WriteInfoLog(LogType.AutoHandlerPrint, "SaveChanges,直接返回:" + i); } catch (Exception ex) { Log4NetHelper.WriteErrorLog(LogType.AutoHandlerPrint, "类名: Form1 方法名: autoPrint 标签自动打印,存打印记录表失败 " + ex.ToString(), ex); } #endregion #endregion } } else { Log4NetHelper.WriteInfoLog(LogType.AutoHandlerPrint, "没有要处理的数据呵呵"); } } } catch (Exception ex) { Log4NetHelper.WriteErrorLog(LogType.AutoHandlerPrint, "类名: Service1 方法名: autoPrint 自动打印方法参数转换失败 \r\n " + ex.ToString(), ex); } finally { single.ReleaseMutex(); }//一轮结束 } } } /// /// 自动打印-行动 /// public void do_autoPrint() { while (true) { Thread.Sleep(1000); try { using (dbModel1 mod = new dbModel1()) { var queryQuerPartStatus = Convert.ToInt32(QuerPartStatus); List needPrint = mod.Base_PositionPrint.Where(x => x.isNeedPrint == 1 && x.printDevice == queryQuerPartStatus).ToList(); if (needPrint.Count > 0) { foreach (var item in needPrint) { #region 单个处理标签打印 Thread.Sleep(1000); labelEntity label = new labelEntity(); int pringTime = 1;//打印次数 label.purchaseOrder = item.PoCode == null ? "" : item.PoCode;//采购单号 label.purchaseOrderItem = item.ItemNumber == null ? "" : item.ItemNumber;//采购项号 label.saleCode = item.SaleCode == null ? "" : item.SaleCode;//销售单号 label.saleCodeItem = item.ExtendField08 == null ? "" : item.ExtendField08;//销售项号 label.trackNumber = item.ExtendField04 == null ? "" : item.ExtendField04;//跟踪号 label.materailCode = item.ProductCode;//物料编号 label.materarilName = item.ProductName;//物料名称 //保留两位小数 [EditBy shaocx,2022-06-20] decimal zz = (item.Quantity == null ? 0 : (decimal)item.Quantity); label.quantity = pringTime == 1 ? Math.Round(zz, 2) : 0;//收货数量 label.Unit = item.SmallUnit;//库存单位 label.receivDate = (DateTime)item.InStorageDate;//收货日期 label.expireDate = item.LimitDate;//限用日期 label.supplierName = item.ProviderShortName;//供应商名称 string barCode = item.ExtendField06;//标签条码 Image img = QrCodeAndBarCode.printBr(barCode); bool saveSucceed = creatExcelPrint.creatEXcel(img, label, barCode); if (saveSucceed) { if (File.Exists(@"D:\临时.xlsx")) { bool printSucceed = true; //发起打印 for (int i = 0; i < pringTime; i++) { printSucceed = print.printExcel(@"D:\临时.xlsx"); } if (printSucceed) { //打印成功 删除临时文件,并改状态 FileInfo myfile = new FileInfo(@"D:\临时.xlsx");//PartStatus myfile.Delete(); item.isNeedPrint = 0;//打印完成 int isSave = mod.SaveChanges(); if (isSave < 1) { Log4NetHelper.WriteErrorLog(LogType.Print, "类名: Form1 方法名: do_autoPrint 修改库存自动打印状态失败 ", null); } } else { Log4NetHelper.WriteErrorLog(LogType.Print, "最终打印失败", null); } } else { Log4NetHelper.WriteErrorLog(LogType.Print, "临时.xlsx不存在", null); } } else { Log4NetHelper.WriteErrorLog(LogType.Print, "创建creatEXcel失败", null); } #endregion } } else { Log4NetHelper.WriteInfoLog(LogType.Print, "没有要打印的数据"); } } } catch (Exception ex) { Log4NetHelper.WriteErrorLog(LogType.Print, "类名: Service1 方法名: do_autoPrint 自动打印方法参数转换失败 \r\n " + ex.ToString(), ex); } } } public void changeColor() { bool isChange = true; while (true) { Thread.Sleep(1000); if (isChange) { label1.BackColor = Color.Yellow; } else { label1.BackColor = Color.Pink; } isChange = !isChange; } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Environment.Exit(0);//退出本进程所有线程 } private void textBox2_TextChanged(object sender, EventArgs e) { string path = @"C:\autoPrint\" + "Setup.txt"; if (!Directory.Exists(Path.GetDirectoryName(path))) Directory.CreateDirectory(Path.GetDirectoryName(path)); if (!File.Exists(path)) File.WriteAllText(path, string.Empty); string info = textBox1.Text + "#" + textBox2.Text; File.WriteAllText(path, info); } private void textBox1_TextChanged(object sender, EventArgs e) { string path = @"C:\autoPrint\" + "Setup.txt"; if (!Directory.Exists(Path.GetDirectoryName(path))) Directory.CreateDirectory(Path.GetDirectoryName(path)); if (!File.Exists(path)) File.WriteAllText(path, string.Empty); string info = textBox1.Text + "#" + textBox2.Text; File.WriteAllText(path, info); } #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 } }