1
huangxiaoqiang
4 天以前 85d9ca4ec972ce4d020db046d930e8991709ae2d
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
using AngleSharp.Dom;
using MailKit.Search;
using Masuit.Tools;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SixLabors.Fonts;
using SqlSugar;
using System;
using System.Data;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Xml.Linq;
using WIDESEA_Cache;
using WIDESEA_Core.Const;
using WIDESEA_DTO.WMS;
using WIDESEA_IOrderRepository;
using WIDESEA_IOrderServices;
using WIDESEA_IServices;
using WIDESEA_Model.Models.ERP;
using WIDESEA_Model.Models.Order;
using WIDESEA_OrderRepository;
using WIDESEAWCS_BasicInfoRepository;
 
namespace WIDESEA_StorageTaskServices
{
    public class MyBackgroundService : IHostedService, IDisposable
    {
        private readonly ILogger<MyBackgroundService> _logger;
        private readonly IStockInfoRepository _stockInfoRepository;
        private readonly IDt_TaskRepository _taskRepository;
        private readonly ISys_ConfigService _configService;
        private readonly ILocationInfoRepository _locationRepository;
        private readonly IDt_InboundOrderRepository _inboundOrderRepository;
        private readonly IDt_ProductionOutboundOrderRepository _productionoutboundorderRepository;
        private readonly IDt_AllocateOutboundOrderRepository _allocateoutboundorderRepository;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IDt_ProductionOutboundOrderService _productionOutboundOrderService;
        private readonly IDt_AllocateOutboundOrderService _allocateOutboundOrderService;
        private Timer _timer;
 
        public MyBackgroundService(ILogger<MyBackgroundService> logger, ILocationInfoRepository locationRepository, IStockInfoRepository stockInfoRepository,IDt_TaskRepository taskRepository,ISys_ConfigService configService,IDt_InboundOrderRepository inboundOrderRepository,IDt_AllocateOutboundOrderRepository allocateoutboundorderRepository,IDt_ProductionOutboundOrderRepository productionOutboundOrderRepository,IUnitOfWorkManage unitOfWorkManage, IDt_ProductionOutboundOrderService productionOutboundOrderService, IDt_AllocateOutboundOrderService allocateOutboundOrderService)
        {
            _logger = logger;
            _locationRepository = locationRepository;
            _stockInfoRepository = stockInfoRepository;
            _taskRepository = taskRepository;
            _configService = configService;
            _inboundOrderRepository = inboundOrderRepository;
            _allocateoutboundorderRepository = allocateoutboundorderRepository;
            _productionoutboundorderRepository = productionOutboundOrderRepository;
            _unitOfWorkManage = unitOfWorkManage;
            _productionOutboundOrderService = productionOutboundOrderService;
            _allocateOutboundOrderService = allocateOutboundOrderService;
        }
 
        public Task StartAsync(CancellationToken cancellationToken)
        {
            _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
            return Task.CompletedTask;
        }
 
        private void DoWork(object state)
        {
            try
            {
                #region 入库单据同步
                List<WMS_采购入成品入库自动打印清单_ST> ERPPrintChecklist = SqlSugarHelper.DBERP.Queryable<WMS_采购入成品入库自动打印清单_ST>().Where(x => true).ToList();
 
                var printCheckList = _inboundOrderRepository.QueryData(x => true);
                List<Dt_InboundOrder> inboundOrderAdd = new List<Dt_InboundOrder>();
                List<Dt_InboundOrder> inboundOrderUpdate = new List<Dt_InboundOrder>();
                foreach (var item in ERPPrintChecklist)
                {
                    var x = printCheckList.Where(x => x.MaterialNo == item.料号 && x.UpperOrderNo == item.单号 && x.WarehouseName == item.入库仓库名称).FirstOrDefault();
                    if (x == null)
                    {
                        Dt_InboundOrder Print = new Dt_InboundOrder()
                        {
                            OrderNo = GetOrderPintCode("OrderNoIn"),
                            UpperOrderNo = item.单号,
                            DemandClassification = item.需求分类,
                            OrderType = item.单据类型,
                            WarehouseName = item.入库仓库名称,
                            Datetime = item.日期.ToString(),
                            LineNumber = item.行号,
                            ProductDrawingNumber = item.产品图号,
                            MaterialNo = item.料号,
                            MaterialName = item.品名,
                            Weight = item.单重,
                            WareHouseId = item.入库仓库编码,
                            Specs = item.规格,
                            Unit = item.单位,
                            Texture = item.用友材质,
                            Quantity = item.入库数量,
                            OrderStatus = item.单据状态
 
                        };
                        inboundOrderAdd.Add(Print);
                    }
                    else
                    {
                        if (x.OrderNo == null || x.OrderNo == "")
                        {
                            x.OrderNo = GetOrderPintCode("OrderNoIn");
                            x.UpperOrderNo = item.单号;
                            x.DemandClassification = item.需求分类;
                            x.OrderType = item.单据类型;
                            x.WarehouseName = item.入库仓库名称;
                            x.Datetime = item.日期.ToString();
                            x.LineNumber = item.行号;
                            x.ProductDrawingNumber = item.产品图号;
                            x.MaterialNo = item.料号;
                            x.MaterialName = item.品名;
                            x.Weight = item.单重;
                            x.Specs = item.规格;
                            x.Unit = item.单位;
                            x.Texture = item.用友材质;
                            x.Quantity = item.入库数量;
                            x.OrderStatus = item.单据状态;
                            x.WareHouseId = item.入库仓库编码;
                            inboundOrderUpdate.Add(x);
                        }
                    }
                }
                #endregion
 
                #region ERP生产领料单同步
 
                var productionOutOrder = _productionoutboundorderRepository.Db.Queryable<Dt_ProductionOutboundOrder>().Includes(x => x.Details).ToList();
 
                List<Dt_ProductionOutboundOrder> productionoutboundOrderAdd = new List<Dt_ProductionOutboundOrder>();
                List<WMS_用友工程发货单一览表_ST> EngineeringERP = SqlSugarHelper.DBERP.Queryable<WMS_用友工程发货单一览表_ST>().Where(x => true).ToList();
                List<WMS_用友生产领料单一览表_ST> productERP = SqlSugarHelper.DBERP.Queryable<WMS_用友生产领料单一览表_ST>().Where(x => true).ToList();
                foreach (var item in EngineeringERP)
                {
                    var x = productionOutOrder.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号).FirstOrDefault();
                    if (x == null)
                    {
                        if (productionoutboundOrderAdd.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号 ).FirstOrDefault() == null)
                        {
                            Dt_ProductionOutboundOrder outOrder = new Dt_ProductionOutboundOrder()
                            {
                                OrderNo = item.单号,
                                UpperOrderNo = item.单号,
                                OrderName = item.订单品名,
                                DemandClassification = item.需求名称,
                                OrderPartNumber = item.订单料号,
                                OrderStatus = (int)OrderStateEmun.未开始,
                                OrderType = (int)OrderTypeEmun.生产领料单,
                                CreateType = (int)OrderCreateTypeEmun.ERP推送,
                                Details = new List<Dt_ProductionOutboundOrderDetail>()
                                {
                                    new Dt_ProductionOutboundOrderDetail()
                                    {
                                        MaterielCode = item.子件料号,
                                        MaterielName = item.子件品名,
                                        Specs = item.子件规格,
                                        Weight = item.子件单重,
                                        ERPOrderId = item.备料单ID,
                                        WareHouseId =item.存储地点ID,
                                        OrderQuantity = item.子件实际用量,
                                        NotOutQuantity = item.子件未发数量,
                                        OverOutQuantity = item.子件发放数量,
                                    }
                                }
                            };
                            productionoutboundOrderAdd.Add(outOrder);
                        }
                        else
                        {
                            var existingOrder = productionoutboundOrderAdd.Where(x => x.UpperOrderNo == item.单号).FirstOrDefault();
                            existingOrder.Details.Add(new Dt_ProductionOutboundOrderDetail()
                            {
                                MaterielCode = item.子件料号,
                                MaterielName = item.子件品名,
                                Specs = item.子件规格,
                                Weight = item.子件单重,
                                ERPOrderId = item.备料单ID,
                                WareHouseId = item.存储地点ID,
                                OrderQuantity = item.子件实际用量,
                                NotOutQuantity = item.子件未发数量,
                                OverOutQuantity = item.子件发放数量,
                            });
                        }
                    }
                }
                foreach (var item in productERP)
                {
                    var x = productionOutOrder.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号).FirstOrDefault();
                    if (x == null)
                    {
                        if (productionoutboundOrderAdd.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号).FirstOrDefault() == null)
                        {
                            Dt_ProductionOutboundOrder outOrder = new Dt_ProductionOutboundOrder()
                            {
                                OrderNo = item.单号,
                                UpperOrderNo = item.单号,
                                OrderName = item.订单品名,
                                DemandClassification = item.需求名称,
                                OrderPartNumber = item.订单料号,
                                OrderStatus = (int)OrderStateEmun.未开始,
                                OrderType = (int)OrderTypeEmun.生产领料单,
                                CreateType = (int)OrderCreateTypeEmun.ERP推送,
                                Details = new List<Dt_ProductionOutboundOrderDetail>()
                                {
                                    new Dt_ProductionOutboundOrderDetail()
                                    {
                                        MaterielCode = item.子件料号,
                                        MaterielName = item.子件品名,
                                        Specs = item.子件规格,
                                        Weight = item.子件单重,
                                        ERPOrderId = item.备料单ID,
                                        WareHouseId =item.存储地点ID,
                                        OrderQuantity = item.子件实际用量,
                                        NotOutQuantity = item.子件未发数量,
                                        OverOutQuantity = item.子件发放数量,
                                    }
                                }
                            };
                            productionoutboundOrderAdd.Add(outOrder);
                        }
                        else
                        {
                            var existingOrder = productionoutboundOrderAdd.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号).FirstOrDefault();
                            existingOrder.Details.Add(new Dt_ProductionOutboundOrderDetail()
                            {
                                MaterielCode = item.子件料号,
                                MaterielName = item.子件品名,
                                Specs = item.子件规格,
                                Weight = item.子件单重,
                                ERPOrderId = item.备料单ID,
                                WareHouseId = item.存储地点ID,
                                OrderQuantity = item.子件实际用量,
                                NotOutQuantity = item.子件未发数量,
                                OverOutQuantity = item.子件发放数量,
                            });
                        }
                    }
 
                }
 
                #endregion
 
                #region ERP调拨出库单同步
 
                var allocateoutboundorder = _allocateoutboundorderRepository.Db.Queryable<Dt_AllocateOutboundOrder>().Includes(x => x.Details).ToList();
 
                List<Dt_AllocateOutboundOrder> allocateoutboundorderAdd = new List<Dt_AllocateOutboundOrder>();
                List<Dt_AllocateOutboundOrder> allocateoutboundorderUpdate = new List<Dt_AllocateOutboundOrder>();
                List<WMS_用友电控柜调拨一览表_ST> ERPallocateOrder = SqlSugarHelper.DBERP.Queryable<WMS_用友电控柜调拨一览表_ST>().Where(x => true).ToList();
                foreach (var item in ERPallocateOrder)
                {
                    var x = allocateoutboundorder.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号).FirstOrDefault();
                    if (x == null)
                    {
                        if (allocateoutboundorderAdd.Where(x => x.UpperOrderNo == item.单号).FirstOrDefault() == null)
                        {
                            Dt_AllocateOutboundOrder outOrder = new Dt_AllocateOutboundOrder()
                            {
                                OrderNo = item.单号,
                                UpperOrderNo = item.单号,
                                OrderName = item.订单品名,
                                DemandClassification = item.需求名称,
                                OrderPartNumber = item.订单料号,
                                OrderStatus = (int)OrderStateEmun.未开始,
                                OrderType = (int)OrderTypeEmun.调拨出库单,
                                CreateType = (int)OrderCreateTypeEmun.ERP推送,
                                Details = new List<Dt_AllocateOutboundOrderDetail>()
                                {
                                    new Dt_AllocateOutboundOrderDetail()
                                    {
                                        MaterielCode = item.子件料号,
                                        MaterielName = item.子件品名,
                                        Specs = item.子件规格,
                                        Weight = item.子件单重,
                                        ERPOrderId = item.备料单ID,
                                        WareHouseId =item.存储地点ID,
                                        OrderQuantity = item.子件实际用量,
                                        NotOutQuantity = item.子件未发数量,
                                        OverOutQuantity = item.子件发放数量,
                                    }
                                }
                            };
                            allocateoutboundorderAdd.Add(outOrder);
                        }
                        else
                        {
                            var existingOrder = allocateoutboundorderAdd.Where(x => x.UpperOrderNo == item.单号 && x.DemandClassification == item.需求名称 && x.OrderPartNumber == item.订单料号).FirstOrDefault();
                            existingOrder.Details.Add(new Dt_AllocateOutboundOrderDetail()
                            {
                                MaterielCode = item.子件料号,
                                MaterielName = item.子件品名,
                                Specs = item.子件规格,
                                Weight = item.子件单重,
                                ERPOrderId = item.备料单ID,
                                WareHouseId = item.存储地点ID,
                                OrderQuantity = item.子件实际用量,
                                NotOutQuantity = item.子件未发数量,
                                OverOutQuantity = item.子件发放数量,
                            });
                        }
                    }
                }
                #endregion
 
                #region 数据处理
 
                if (inboundOrderAdd.Count > 0)
                {
                    //PrintInbound(inboundOrderAdd);
                    _inboundOrderRepository.AddData(inboundOrderAdd);
                }
                if (inboundOrderUpdate.Count > 0)
                {
                    //PrintInbound(inboundOrderUpdate);
                    _inboundOrderRepository.UpdateData(inboundOrderUpdate);
                }
 
                if (productionoutboundOrderAdd.Count > 0)
                {
                    SqlSugarHelper.DbWMS.InsertNav(productionoutboundOrderAdd).Include(x => x.Details).ExecuteCommand();
                }
                if (allocateoutboundorderAdd.Count > 0)
                {
                    SqlSugarHelper.DbWMS.InsertNav(allocateoutboundorderAdd).Include(x => x.Details).ExecuteCommand();
                }
 
                #endregion
 
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteErrorLine($"错误信息:" + ex.Message);
            }
        }
 
        public string GetOrderPintCode(string printCode)
        {
            string PrintCode = "";
            var PrintSetting = SqlSugarHelper.DbWMS.Queryable<Dt_PrintSetting>().Where(x => x.PrintCode == printCode).ToList().FirstOrDefault();
 
            if (PrintSetting.Spare1 == DateTime.Now.ToString("yyyyMMdd"))
            {
                PrintCode = PrintSetting.Spare1 + PrintSetting.PrintNo.ToString().PadLeft(PrintSetting.Spare2, '0');
                PrintSetting.PrintNo = PrintSetting.PrintNo + 1;
            }
            else
            {
                PrintSetting.Spare1 = DateTime.Now.ToString("yyyyMMdd");
                PrintSetting.PrintNo = 2;
                PrintCode = PrintSetting.Spare1 + 1.ToString().PadLeft(PrintSetting.Spare2, '0');
            }
            SqlSugarHelper.DbWMS.Updateable(PrintSetting).ExecuteCommand();
            return PrintCode;
        }
        
        public Task StopAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("MyBackgroundService is stopping.");
            _timer?.Change(Timeout.Infinite, 0);
            return Task.CompletedTask;
        }
 
        public void PrintInbound(List<Dt_InboundOrder> orders)
        {
            var configs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
            var Base = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.PrintIPAddress)?.ConfigValue;
            var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.PrintInboundOrder)?.ConfigValue;
            if (Base == null || ipAddress == null)
            {
                throw new InvalidOperationException("WMS IP 未配置");
            }
            var IpAddress = Base + ipAddress;
            var result = HttpHelper.PostAsync(IpAddress, orders.ToJsonString()).Result;
        }
 
        public void Dispose()
        {
            _timer?.Dispose();
        }
    }
}