333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
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;
        /// <summary>
        /// //线程锁防止并发
        /// </summary>
        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];
            }
 
        }
        /// <summary>
        /// 自动打印-准备数据
        /// </summary>
        public void autoPrint()
        {
            while (true)
            {
                Thread.Sleep(1000);
                if (single.WaitOne())//增加线程锁
                {
                    try
                    {
                        using (dbModel1 mod = new dbModel1())
                        {
                            //List<vBase_ProductPosition> needPrint = mod.vBase_ProductPosition.Where(x => x.PositionName == "收货暂存区" && x.ProductStorage > 0 && x.PartStatus == "1").ToList();
                            List<vBase_ProductPosition> 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(); }//一轮结束
                }
            }
        }
 
        /// <summary>
        /// 自动打印-行动
        /// </summary>
        public void do_autoPrint()
        {
            while (true)
            {
                Thread.Sleep(1000);
 
                try
                {
                    using (dbModel1 mod = new dbModel1())
                    {
                        var queryQuerPartStatus = Convert.ToInt32(QuerPartStatus);
                        List<Base_PositionPrint> 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 定时删除数据
 
        /// <summary>
        /// 定时删除数据
        /// </summary>
        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
 
    }
}