zongzhibin
2024-11-29 1b6830d5f8671e48bdc13d7155b848aa938283f5
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
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 iWareSql.WmsDBModel;
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<string, string> cellheader = new Dictionary<string, string> {
                    { "MaterialCode", "物料号" },
                    { "MaterialName", "物料名称" },
                    { "Vendor", "供应商" },
                    { "PO", "PO" },
                    { "Qty", "数量" }
                };
 
                // 1.2解析文件,存放到一个List集合里
                StringBuilder errorMsg = new StringBuilder(); // 错误信息
                string tableDesc = "", tableName = "";
                List<MaterialEntity> enlist = ExcelHelper.ExcelToEntityListForCreateTable<MaterialEntity>(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;
            }
        }
 
        /// <summary>
        /// 自动打印-行动
        /// </summary>
        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秒
            }
        }
 
 
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="model"></param>
        /// <param name="Lane"></param>
        /// <param name="flag"></param>
        /// <param name="errMsg"></param>
        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<string, object> data = new Dictionary<string, object>();
                    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 finsiList = mycontext.mes_package_gather.Where(x => x.Info5 == item.Info5 && x.UpiStatus >= (int)UpiStatusEnum.已合包);
                        double leijisaomiaomj = 0;
                        foreach (var aa in finsiList)
                        {
                            if (!string.IsNullOrEmpty(aa.Info13))
                            {
                                leijisaomiaomj += Convert.ToDouble(aa.Info13);
                            }
                        }
                        if (!string.IsNullOrEmpty(item.Info13))
                        {
                            leijisaomiaomj += Convert.ToDouble(item.Info13);
                        }
                        data.Add("leijisaomiaomj", leijisaomiaomj);//累计扫描面积
 
                        //查询下 包数据
                        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 定时删除数据
 
        /// <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
    }
}