schangxiang@126.com
2025-09-17 ff43ddf18764629ff875478e4e47a7281cbd230a
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
using Admin.NET.Core;
using iWareCommon;
using iWareCommon.Utils;
using iWareSql.MyDbContext;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
 
namespace iWareCC
{
    /// <summary>
    /// 打印处理的线程
    /// </summary>
    public class PrintHandler
    {
        public static void Handler()
        {
            var errMsg = "";
            while (true)
            {
                try
                {
                    SystemWarningMsg.lbl_Alert_ReceivingPoOrderHandler = "循环开始...";
                    if (SystemValue.isAllowRuning_PrintHandler && SystemValue.isStartedModel)
                    {
                        errMsg = "";//重置
                        try
                        {
                            Do(ref errMsg);
                            if (!string.IsNullOrEmpty(errMsg))
                            {
                                SystemWarningMsg.lbl_Alert_ReceivingPoOrderHandler = errMsg;
                            }
                        }
                        catch (Exception ex)
                        {
                            Log4NetHelper.WriteErrorLog(LogType.PrintHandler, "打印处理的线程=>" + errMsg + ",异常:" + ex.Message, ex);
                            SystemWarningMsg.lbl_Alert_ReceivingPoOrderHandler = "打印处理的线程=>" + errMsg + ",异常:" + ex.Message;
                        }
                    }
                }
                catch (Exception ex)
                {
                    SystemWarningMsg.lbl_Alert_ReceivingPoOrderHandler = "出现异常:" + ex.Message;
                    Log4NetHelper.WriteErrorLog(LogType.PrintHandler, "PrintHandler 出现异常:" + 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 static void Do(ref string errMsg)
        {
            using (MyDbContext mycontext = new MyDbContext())
            {
                var query_PrintStatuEnum = (int)PrintStatuEnum.未打印;
                var list = mycontext.wms_record_sncode_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 = default(PrintClassifyEnum);
                    var isRight = GetPrintClassifyEnum(item, mycontext, ref errMsg, ref printClassifyEnum);
                    if (!isRight)
                    {
                        return;
                    }
 
                    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("cardNo", item.KanBanCardNo ?? "");
                        data.Add("ToAreaName", item.ToAreaName ?? "");
 
                        data.Add("bar", item.KanBanCardNo);
                        data.Add("material", item.MaterialCode ?? "" + "/" + item.MaterialName ?? "");
                        data.Add("supplier", item.SupplierName ?? "");
                        data.Add("unit", item.MaterialUnit ?? "");
                        data.Add("qty", item.Quantity);
                        data.Add("date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                    else
                    {
                        data.Add("bar", item.SNCode);
                        data.Add("material", item.MaterialCode ?? "" + "/" + item.MaterialName ?? "");
                        data.Add("supplier", item.SupplierName ?? "");
                        data.Add("unit", item.MaterialUnit ?? "");
                        data.Add("qty", item.Quantity);
                        data.Add("date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
 
 
                    //调用打印接口
                    bool isSuccess = false;
                    using (PrintBaseService.PrintWcfServiceClient client = new 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 = $"打印错误,跟踪码{item.SNCode},打印机名称{_PrinterName},错误信息:(打印服务返回)" + errMsg;
                        Log4NetHelper.WriteErrorLog(LogType.PrintHandler, errMsg, null);
                        return;
                    }
 
                    Thread.Sleep(2000);
                }
 
            }
        }
 
 
        private static bool GetPrintClassifyEnum(wms_record_sncode_print item, MyDbContext mycontext, ref string errMsg, ref PrintClassifyEnum printClassifyEnum)
        {
            if (item.PrintType == (int)PrintTypeEnum.看板卡)
            {
                printClassifyEnum = PrintClassifyEnum.看板卡;
            }
            else
            {
                wms_config_sncode_rule rule = null;
                var ruleList = mycontext.wms_config_sncode_rule.Where(x => (x.RuleName == "ASN一维码" || x.RuleName == "ASN二维码") && x.IsDisabled == false).ToList();
                if (ruleList != null && ruleList.Count > 1)
                {
                    errMsg = "条码生成规则配置中,ASN一维码和ASN二维码只能启用其中一个!";
                    return false;
                }
                if (ruleList == null || ruleList.Count == 0)
                {
                    errMsg = "条码生成规则配置中,ASN一维码和ASN二维码两个都没有配置启用!";
                    return false;
                }
                rule = ruleList.First();
 
                printClassifyEnum = default(PrintClassifyEnum);
                if (rule.RuleName == "ASN一维码")
                {
                    printClassifyEnum = PrintClassifyEnum.一维跟踪码;
                }
                else
                {
                    printClassifyEnum = PrintClassifyEnum.二维跟踪码;
                }
            }
 
            return true;
 
        }
 
    }
}