bklLiudl
2025-04-02 1bbbbc8bb49411b544626996a1370788142300e0
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using Model.ModelDto;
using Model.ModelVm;
using SqlSugar;
using WMS.BLL.LogServer;
using WMS.DAL;
using WMS.Entity.BllAsnEntity;
using WMS.Entity.Context;
using WMS.Entity.DataEntity;
using WMS.Entity.SysEntity;
using WMS.IBLL.IBllAsnServer;
 
namespace WMS.BLL.BllAsnServer
{
    public class AuditLogServer : DbHelper<BllAuditLog>, IAuditLogServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
 
        public AuditLogServer() : base(Db)
        {
        }
 
        //获取审核记录信息
        public List<AuditLogDto> GetAuditLogList(AuditLogVm model, out int count)
        {
            try
            {
                Expression<Func<BllAuditLog, bool>> item = Expressionable.Create<BllAuditLog>()
                    .AndIF(!string.IsNullOrWhiteSpace(model.AsnNo), it => it.OrderNo.Contains(model.AsnNo.Trim()))
                    .AndIF(!string.IsNullOrWhiteSpace(model.Status), it => it.Status == model.Status)
                    .AndIF(!string.IsNullOrWhiteSpace(model.StartTime), it => it.CreateTime >= Convert.ToDateTime(model.StartTime))
                    .AndIF(!string.IsNullOrWhiteSpace(model.EndTime), it => it.CreateTime <= Convert.ToDateTime(model.EndTime).AddDays(1))
                    .And(it => it.IsDel == "0")
                    .ToExpression();
                var total = 0;
                var data = GetAllWhereAsync(item)
                    .LeftJoin<SysUserInfor>((a, b) => a.CreateUser == b.Id)
                    .LeftJoin<SysUserInfor>((a, b, c) => a.AuditUser == c.Id)
                    .Select((a, b, c) => new AuditLogDto()
                    {
                        Id = a.Id,
                        OrderNo = a.OrderNo,
                        PalletNo = a.PalletNo,
 
                        Msg = a.Msg,
                        Status = a.Status,
                        Reason = a.Reason,
                        Opinion = a.Opinion,
                        StartStatus = a.StartStatus,
                        EndStatus = a.EndStatus,
                        FunctionCode = a.FunctionCode,
 
                        CreateUserName = b.RealName,
                        AuditUserName = c.RealName,
                        CreateTime = a.CreateTime,
                        AuditTime = a.AuditTime
                    }).OrderByDescending(a => a.CreateTime).OrderBy(a => a.Status).ToOffsetPage(model.Page, model.Limit, ref total);
                count = total;
                return data;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        //编辑审核
        public void EditAudit(int id, string status, string opinion, int userId)
        {
            try
            {
                var log = Db.Queryable<BllAuditLog>().First(m => m.Id == id && m.IsDel == "0");
                if (log == null)
                {
                    throw new Exception("未查询到审核信息,请核实");
                }
                if (log.Status != "0")
                {
                    throw new Exception("当亲审核信息已审核,不可重复审核");
                }
                Db.BeginTran();
                try
                {
                    if (status == "1")
                    {
                        if (log.FunctionCode == "001")//入库单撤销
                        {
                            //查询是否有当前单据的其它撤销审核
                            var logList = Db.Queryable<BllAuditLog>()
                                .Where(m => m.IsDel == "0" && m.Status == "0" && m.OrderNo == log.OrderNo && m.Id != id).ToList();
                            if (logList.Count > 0)
                            {
                                throw new Exception("请优先完成当前单据其它托盘绑定撤销");
                            }
                            //库存托盘明细
                            var stockDetail = Db.Queryable<DataStockDetail>().Where(m => m.ASNNo == log.OrderNo);
                            //if (stockDetail.Count() == 0)
                            //{
                            //    throw new Exception("未查询到库存托盘明细信息");
                            //}
                            var stockDetailToList = stockDetail.ToList();
                            if (stockDetail.Count(m => m.Status != "0" || m.LockQty > 0) > 0)
                            {
                                throw new Exception("库存托盘明细存在出库或分配的货物,无法撤销");
                            }
                            //入库单
                            var notice = Db.Queryable<BllArrivalNotice>().First(m => m.IsDel == "0" && m.ASNNo == log.OrderNo);
                            if (notice == null)
                            {
                                throw new Exception("未查询到该托盘的单据信息");
                            }
                            //入库单明细
                            var noticeDetailList = Db.Queryable<BllArrivalNoticeDetail>().Where(m => m.IsDel == "0" && m.ASNNo == log.OrderNo);
                            if (noticeDetailList.Count() == 0)
                            {
                                throw new Exception("未查询到该托盘的单据明细信息");
                            }
 
                            var noticeDetailListToList = noticeDetailList.ToList();
                            //判断单据库存数量和单据数量是否对应的上
                            var asnQty = noticeDetailList.GroupBy(m => m.ASNNo)
                                .Select(m => SqlFunc.AggregateSum(m.CompleteQty)).First();
                            var stockQty = stockDetail.GroupBy(m => m.ASNNo).Select(m => SqlFunc.AggregateSum(m.Qty))
                                .First();
                            if (asnQty != stockQty && asnQty != null && stockQty != null)
                            {
                                throw new Exception("库存托盘明细存在出库或分配的货物,无法撤销");
                            }
                            //托盘绑定
                            var bindList = Db.Queryable<BllPalletBind>().Where(m =>
                                m.IsDel == "0" && m.ASNNo == log.OrderNo);
                            if (bindList.Count() == 0)
                            {
                                throw new Exception("未查询到单据的托盘绑定信息");
                            }
 
                            if (bindList.Count(m => m.Status == "1") > 0)
                            {
                                throw new Exception("当前入库单据的托盘绑定信息有正在执行的,请稍后完成后再撤销");
                            }
 
                            if (log.EndStatus == "等待执行")
                            {
                                foreach (var item in stockDetailToList)
                                {
                                    //总库存
                                    var stock = Db.Queryable<DataStock>()
                                        .First(m => m.SkuNo == item.SkuNo && m.LotNo == item.LotNo);
                                    //库存箱支明细
                                    var stockInfo = Db.Queryable<DataBoxInfo>().Where(m => m.StockDetailId == item.Id).ToList();
                                    //储位
                                    var locate = Db.Queryable<SysStorageLocat>()
                                        .First(m => m.IsDel == "0" && m.LocatNo == item.LocatNo);
                                    if (item.Qty != null)
                                    {
                                        if (item.LockQty > 0)
                                        {
                                            throw new Exception("当前物料批次已被分配,无法撤销");
                                        }
                                        stock.Qty -= (int)item.Qty;
                                        if (stock.Qty == 0)
                                        {
                                            Db.Deleteable(stock).ExecuteCommand();
                                        }
                                        else
                                        {
                                            Db.Updateable(stock).ExecuteCommand();
                                        }
                                    }
                                    Db.Deleteable(item).ExecuteCommand();
                                    Db.Deleteable(stockInfo).ExecuteCommand();
                                    if (locate != null)
                                    {
                                        locate.Flag = "1";//储位标识修改为屏蔽
                                        Db.Updateable(locate).ExecuteCommand();
                                    }
                                }
 
                                notice.Status = "0";
                                Db.Updateable(notice).ExecuteCommand(); //修改入库单
 
                                foreach (var demo in noticeDetailListToList)
                                {
                                    demo.Status = "0";
                                    demo.FactQty = 0;
                                    demo.CompleteQty = 0;
                                    Db.Updateable(demo).ExecuteCommand(); //修改入库单明细
                                }
                                //箱支明细更改
                                foreach (var demo in bindList.ToList())
                                {
                                    var boxInfo = Db.Queryable<BllBoxInfo>().Where(m => m.BindNo == demo.Id).ToList();
                                    foreach (var demo2 in boxInfo)
                                    {
                                        if (demo2.Origin != "WMS")
                                        {
                                            if (notice.Type != "1" && notice.Type != "4")
                                            {
                                                demo2.ASNNo = null;
                                                demo2.ASNDetailNo = null;
                                            }
                                        }
                                        demo2.BindNo = null;
                                        demo2.PalletNo = null;
                                        demo2.Status = "0";
                                        demo2.CompleteTime = null;
                                        Db.Updateable(demo2).ExecuteCommand();
                                    }
 
                                    var pallet = Db.Queryable<SysPallets>().First(m => m.PalletNo == demo.PalletNo);
                                    if (pallet.Status == "1")
                                    {
                                        pallet.Status = "0";
                                        Db.Updateable(pallet).ExecuteCommand();
                                    }
                                }
                                Db.Deleteable<BllPalletBind>(bindList.ToList()).ExecuteCommand(); //删除组托信息
 
                            }
 
                            if (log.EndStatus == "正在执行")
                            {
                                foreach (var item in stockDetailToList)
                                {
                                    //总库存
                                    var stock = Db.Queryable<DataStock>()
                                        .First(m => m.SkuNo == item.SkuNo && m.LotNo == item.LotNo);
                                    //库存箱支明细
                                    var stockInfo = Db.Queryable<DataBoxInfo>().Where(m => m.StockDetailId == item.Id).ToList();
                                    //储位
                                    var locate = Db.Queryable<SysStorageLocat>()
                                        .First(m => m.IsDel == "0" && m.LocatNo == item.LocatNo);
 
                                    if (item.Qty != null)
                                    {
                                        if (item.LockQty > 0)
                                        {
                                            throw new Exception("当前物料批次已被分配,无法撤销");
                                        }
                                        stock.Qty -= (int)item.Qty;
                                        if (stock.Qty == 0)
                                        {
                                            Db.Deleteable(stock).ExecuteCommand();
                                        }
                                        else
                                        {
                                            Db.Updateable(stock).ExecuteCommand();
                                        }
 
                                    }
 
                                    Db.Deleteable(item).ExecuteCommand();
                                    Db.Deleteable(stockInfo).ExecuteCommand();
                                    if (locate != null)
                                    {
                                        locate.Flag = "1";//储位标识修改为屏蔽
                                        Db.Updateable(locate).ExecuteCommand();
                                    }
                                }
 
                                //修改入库单
                                notice.Status = "1";
                                Db.Updateable(notice).ExecuteCommand();
                                //修改入库单明细
                                foreach (var demo in noticeDetailListToList)
                                {
                                    if (demo.Status == "0")
                                    {
                                        continue;
                                    }
                                    demo.Status = "1";
                                    demo.CompleteQty = 0;
                                    Db.Updateable(demo).ExecuteCommand();
 
                                    #region 采购计划
                                    if (notice.Type == "1")
                                    {
                                        //采购单明细
                                        var planDetail = Db.Queryable<BllProcurePlanNoticeDetail>().First(it => it.OrderDetailCode == demo.OrderDetailCode && it.IsDel == "0");
                                        if (planDetail != null)
                                        {
                                            planDetail.CompleteQty -= demo.CompleteQty;
                                            planDetail.Status = "1";
                                            Db.Updateable(planDetail).ExecuteCommand();
                                        }
                                    }
                                    #endregion
                                }
                                #region 采购计划
                                if (notice.Type == "1")
                                {
                                    //采购总单信息
                                    var planOrd = Db.Queryable<BllProcurePlanNotice>().First(it => it.OrderCode == notice.OrderCode && it.IsDel == "0");
                                    if (planOrd != null)
                                    {
                                        planOrd.Status = "1";
                                        Db.Updateable(notice).ExecuteCommand();
                                    }
                                }
                                #endregion
                                //修改托盘绑定       
                                foreach (var demo in bindList.ToList())
                                {
                                    if (demo.Status == "0")
                                    {
                                        continue;
                                    }
                                    var boxInfo = Db.Queryable<BllBoxInfo>().Where(m => m.BindNo == demo.Id).ToList();
                                    foreach (var demo2 in boxInfo)
                                    {
                                        demo2.Status = "1";
                                        demo2.CompleteTime = null;
                                        Db.Updateable(demo2).ExecuteCommand();
                                    }
                                    demo.Status = "0";
                                    demo.LocatNo = "";
                                    demo.TaskNo = "";
                                    demo.CompleteTime = null;
                                    Db.Updateable(demo).ExecuteCommand();
                                }
 
                                #region 初始逻辑版本
 
                                //foreach (var item in stockDetail.ToList())
                                //{
                                //    //入库单明细
                                //    var noticeDetail = Db.Queryable<BllArrivalNoticeDetail>().First(m => m.IsDel == "0" && m.ASNNo == log.OrderNo && m.Id == item.ASNDetailNo);
                                //    if (noticeDetail == null)
                                //    {
                                //        throw new Exception("未查询到该托盘的单据明细信息");
                                //    }
                                //    //总库存
                                //    var stock = Db.Queryable<DataStock>()
                                //        .First(m => m.SkuNo == item.SkuNo && m.LotNo == item.LotNo);
                                //    //库存箱支明细
                                //    var stockInfo = Db.Queryable<DataBoxInfo>().Where(m => m.StockDetailId == item.Id).ToList();
                                //    //储位
                                //    var locate = Db.Queryable<SysStorageLocat>()
                                //        .First(m => m.IsDel == "0" && m.LocatNo == item.LocatNo);
                                //    //托盘绑定
                                //    var bind = Db.Queryable<BllPalletBind>().First(m =>
                                //        m.IsDel == "0" && m.PalletNo == log.PalletNo && m.ASNNo == log.OrderNo);
                                //    if (bind == null)
                                //    {
                                //        throw new Exception("未查询到托盘绑定信息");
                                //    }
                                //    if (bind.Status != "2")
                                //    {
                                //        throw new Exception("托盘绑定信息状态不是执行完成,无法撤销");
                                //    }
                                //    if (item.Qty != null)
                                //    {
                                //        stock.Qty -= (int)item.Qty;
                                //        Db.Updateable(stock).ExecuteCommand();
                                //    }
 
                                //    Db.Deleteable(item).ExecuteCommand();
                                //    Db.Deleteable(stockInfo).ExecuteCommand();
                                //    if (locate != null)
                                //    {
                                //        locate.Flag = "1";//储位标识修改为屏蔽
                                //        Db.Updateable(locate).ExecuteCommand();
                                //    }
 
                                //    notice.Status = "1";
                                //    Db.Updateable(notice).ExecuteCommand();
 
                                //    noticeDetail.Status = "1";
                                //    noticeDetail.CompleteQty -= bind.Qty;
                                //    Db.Updateable(noticeDetail).ExecuteCommand();
 
                                //    bind.Status = "0";
                                //    bind.LocatNo = "";
                                //    bind.TaskNo = "";
                                //    bind.CompleteTime = null;
                                //    Db.Updateable(bind).ExecuteCommand();
                                //}
 
                                #endregion
                            }
 
                        }
                        else if (log.FunctionCode == "002")//托盘绑定撤销
                        {
                            //库存托盘明细
                            var stockDetail = Db.Queryable<DataStockDetail>()
                                .First(m => m.PalletNo == log.PalletNo && m.ASNNo == log.OrderNo);
                            if (stockDetail == null)
                            {
                                throw new Exception("未查询到库存托盘明细信息");
                            }
 
                            if (stockDetail.Status != "0" || stockDetail.LockQty > 0)
                            {
                                throw new Exception("库存托盘明细存在出库或分配的货物,无法撤销");
                            }
                            //入库单
                            var notice = Db.Queryable<BllArrivalNotice>().First(m => m.IsDel == "0" && m.ASNNo == log.OrderNo);
                            if (notice == null)
                            {
                                throw new Exception("未查询到该托盘的单据信息");
                            }
                            //入库单明细
                            var noticeDetail = Db.Queryable<BllArrivalNoticeDetail>().First(m => m.IsDel == "0" && m.ASNNo == log.OrderNo && m.Id == stockDetail.ASNDetailNo);
                            if (noticeDetail == null)
                            {
                                throw new Exception("未查询到该托盘的单据明细信息");
                            }
                            //总库存
                            var stock = Db.Queryable<DataStock>()
                                .First(m => m.SkuNo == stockDetail.SkuNo && m.LotNo == stockDetail.LotNo);
                            //库存箱支明细
                            var stockInfo = Db.Queryable<DataBoxInfo>().Where(m => m.StockDetailId == stockDetail.Id).ToList();
                            //储位
                            var locate = Db.Queryable<SysStorageLocat>()
                                .First(m => m.IsDel == "0" && m.LocatNo == stockDetail.LocatNo);
                            //托盘绑定
                            var bind = Db.Queryable<BllPalletBind>().First(m =>
                                m.IsDel == "0" && m.PalletNo == log.PalletNo && m.ASNNo == log.OrderNo);
                            if (bind == null)
                            {
                                throw new Exception("未查询到托盘绑定信息");
                            }
                            if (bind.Status != "2")
                            {
                                throw new Exception("托盘绑定信息状态不是执行完成,无法撤销");
                            }
 
                            if (stockDetail.Qty != null)
                            {
                                stock.Qty -= (int)stockDetail.Qty;
 
                            }
 
                            if (stock.Qty == 0)
                            {
                                Db.Deleteable(stock).ExecuteCommand();
                            }
                            else
                            {
                                Db.Updateable(stock).ExecuteCommand();
                            }
 
                            Db.Deleteable(stockDetail).ExecuteCommand();
                            Db.Deleteable(stockInfo).ExecuteCommand();
                            if (locate != null)
                            {
                                locate.Flag = "1";//储位标识修改为屏蔽
                                Db.Updateable(locate).ExecuteCommand();
                            }
 
                            notice.Status = "1";
                            noticeDetail.Status = "1";
                            noticeDetail.CompleteQty -= bind.Qty;
                            Db.Updateable(notice).ExecuteCommand();
                            Db.Updateable(noticeDetail).ExecuteCommand();
 
                            bind.Status = "0";
                            bind.LocatNo = "";
                            bind.TaskNo = "";
                            bind.CompleteTime = null;
                            Db.Updateable(bind).ExecuteCommand();
 
                        }
                        else
                        {
                            throw new Exception("记录的功能编号异常");
                        }
                    }
 
                    log.Status = status;
                    log.Opinion = opinion;
                    log.AuditTime = DateTime.Now;
                    log.AuditUser = userId;
                    Db.Updateable(log).ExecuteCommand();
                    new OperationASNServer().AddLogOperationAsn("入库作业", "审核记录", log.PalletNo, "编辑", $"编辑了单据号为{log.OrderNo}的审核记录", userId);
 
                    Db.CommitTran();
                }
                catch (Exception e)
                {
                    Db.RollbackTran();
                    throw new Exception(e.Message);
                }
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
 
    }
}