bklLiudl
2024-05-25 484e5129e4c9a671c5660a556a24bd306f1fdd9b
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 Model.InterFaceModel;
using Model.ModelDto;
using Model.ModelVm;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using WMS.BLL.LogServer;
using WMS.DAL;
using WMS.Entity.BllAsnEntity;
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IBLL.IBllAsnServer;
 
namespace WMS.BLL.BllAsnServer
{
    public class BllBoxInfoServer : DbHelper<BllBoxInfo>, IBllBoxInfoServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
 
        public BllBoxInfoServer() : base(Db)
        {
        }
 
        //添加箱码信息
        public string AddBoxInfo(BoxInfoVm model)
        {
            string strMessage = "";
            string sqlString = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(model.BoxNo))
                {
                    strMessage = "-1:箱码不可为空!";
                    return strMessage;
                }
                //if (string.IsNullOrEmpty(model.BoxNo2))
                //{
                //    strMessage = "-1:盒码不可为空!";
                //    return strMessage;
                //}
                if (string.IsNullOrEmpty(model.BoxNo3))
                {
                    strMessage = "-1:支码不可为空!";
                    return strMessage;
                }
                if (string.IsNullOrEmpty(model.SkuNo))
                {
                    strMessage = "-1:物料编码不可为空!";
                    return strMessage;
                }
                if (string.IsNullOrEmpty(model.LotNo))
                {
                    strMessage = "-1:批次号不可为空!";
                    return strMessage;
                }
                if (model.Qty == null || model.Qty <= 0)
                {
                    strMessage = "-1:实际数量必须大于0!";
                    return strMessage;
                }
                if (string.IsNullOrEmpty(model.ProductionTime))
                {
                    strMessage = "-1:生产日期不可为空!";
                    return strMessage;
                }
 
                // 验证是否重复 支/袋码
                sqlString += $@"select count(id) from BllBoxInfo where BoxNo3 = '{model.BoxNo3}' and IsDel = 0 ";
                int rowCount = Db.Ado.GetInt(sqlString);
                if (rowCount > 0)
                {
                    strMessage = "-1:重复数据!";
                    return strMessage;
                }
                // 获取物料信息
                sqlString = "select tb1.SkuNo,tb1.SkuName,tb1.Warranty, ";
                sqlString += "case tb2.level when 5 then tb2.L4Num when 4 then tb2.L3Num when 3 then tb2.L2Num else 0 end as FullQty ";
                sqlString += "from SysMaterials as tb1 left join SysPackag as tb2 on tb1.PackagNo = tb2.PackagNo ";
                sqlString += $"where SkuNo = '{model.SkuNo}' and tb1.IsDel = '0';";
                var materialModel = Db.Ado.SqlQuery<BoxInfoVm>(sqlString);
                if (materialModel.Count <= 0)
                {
                    strMessage = "-1:该物料信息不存在!";
                    return strMessage;
                }
                DateTime Ptime = DateTime.Parse(model.ProductionTime);
                double dayNum = (int)materialModel[0].Warranty;
                model.ExpirationTime = Ptime.AddDays(dayNum);
                if (string.IsNullOrEmpty(model.InspectMark))
                {
                    model.InspectMark = "0";
                }
                if (string.IsNullOrEmpty(model.BitBoxMark))
                {
                    model.BitBoxMark = "0";
                }
 
                var sku = Db.Queryable<SysMaterials>().First(m => m.SkuNo == model.SkuNo);
                if (sku == null)
                {
                    throw new Exception("未查询到物料信息");
                }
 
                #region 包装信息
                string str = $"select SUM(Qty) Qty from BllBoxInfo where IsDel = '0' and BoxNo = '{model.BoxNo}'";
                //获取箱码信息
                var box = Db.Ado.SqlQuerySingle<BoxInfoVm>(str);
                if (box.Qty == null)
                {
                    box.Qty = 0;
                }
                box.Qty = box.Qty + model.Qty;
                var package = Db.Queryable<SysPackag>().Where(m => m.IsDel == "0");
                var pack = package.First(m => m.IsDel == "0" && m.PackagNo == sku.PackagNo);
                var pNum = 0;//物品包装数量
                if (pack.L5Num.HasValue)
                {
                    pNum = (int)pack.L5Num;
                }
                if (pack.L4Num.HasValue)
                {
                    pNum = (int)pack.L4Num;
                }
                if (pack.L3Num.HasValue)
                {
                    pNum = (int)pack.L3Num;
                }
                if (pack.L2Num.HasValue)
                {
                    pNum = (int)pack.L2Num;
                    if (pack.L2Name == "箱")
                    {
                        if (box.Qty > (int)pack.L2Num)
                        {
                            throw new Exception($"绑定失败,箱支{model.BoxNo}实际数量大于物品包装数量!");
                        }
                    }
                }
                if (pack.L1Num.HasValue)
                {
                    pNum = (int)pack.L1Num;
                    if (pack.L1Name == "支" || pack.L1Name == "盒" || pack.L1Name == "袋" || pack.L1Name == "根" || pack.L1Name == "块")
                    {
                        if (model.Qty > (int)pack.L1Num)
                        {
                            throw new Exception($"绑定失败,箱支{model.BoxNo3}实际数量大于物品包装数量!");
                        }
                    }
                }
 
                if (pNum == 0)
                {
                    throw new Exception($"绑定失败,{sku.SkuNo}物品包装未找到!");
                }
 
                #endregion
 
                // 插入信息
                sqlString = "Insert into BllBoxInfo (BoxNo,BoxNo2,BoxNo3,SkuNo,SkuName,LotNo,LotText,";
                sqlString += "Qty,FullQty,ProductionTime,ExpirationTime,InspectMark,InspectStatus,BitBoxMark,Origin,Status,CreateUser) values ( ";
                sqlString += $"'{model.BoxNo}','{model.BoxNo2}','{model.BoxNo3}','{model.SkuNo}', ";
                sqlString += $"'{materialModel[0].SkuName}','{model.LotNo}','{model.LotText}','{model.Qty}','{materialModel[0].FullQty}',";
                sqlString += $"'{model.ProductionTime}','{model.ExpirationTime}','{model.InspectMark}','{sku.IsInspect}','{model.BitBoxMark}','{model.Origin}','0','{model.CreateUser}');";
 
                rowCount = Db.Ado.ExecuteCommand(sqlString);
                if (rowCount <= 0)
                {
                    strMessage = "-1:添加失败!";
                }
                else
                {
                    if (model.Origin == "录入")
                    {
                        new OperationASNServer().AddLogOperationAsn("入库作业", "箱支录入", model.BoxNo3, "添加", $"添加了箱码:{model.BoxNo}、追溯码:{model.BoxNo3}的箱支物料信息", Convert.ToInt32(model.CreateUser));
                    }
                }
 
                return strMessage;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 
        //删除箱码信息
        public string DelBoxInfo(BoxInfoVm model)
        {
            string sqlString = string.Empty;
            try
            {
                var boxInfo = Db.Queryable<BllBoxInfo>().First(m => m.IsDel == "0" && m.Id == model.Id);
                if (boxInfo == null)
                {
                    throw new Exception("未查询到信息");
                }
                //开启事务
                Db.Ado.BeginTran();
                // 删除明细单
                sqlString += $"UPDATE BllBoxInfo SET IsDel = '1',";
                sqlString += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' ";
                sqlString += $"WHERE Id = '{model.Id}' and Status = '0';";
 
                int rowCount = Db.Ado.ExecuteCommand(sqlString);
                if (rowCount <= 0)
                {
                    return "-1:状态已变更无法删除!";
                }
                var boxInfoList = Db.Queryable<BllBoxInfo>().Where(m => m.IsDel == "0" && m.BoxNo == boxInfo.BoxNo && m.Id != model.Id).ToList();
                if (boxInfoList.Count != 0)
                {
                    foreach (var item in boxInfoList)
                    {
                        if (item.BitBoxMark == "1" || item.Status != "0")
                        {
                            continue;
                        }
 
                        var sql = $"update BllBoxInfo SET BitBoxMark = '1' Where id = {item.Id}";
                        int rowCount2 = Db.Ado.ExecuteCommand(sql);
                        if (rowCount2 <= 0)
                        {
                            throw new Exception("-1:状态已变更无法删除!");
                        }
                    }
                }
 
                Db.Ado.CommitTran();
                new OperationASNServer().AddLogOperationAsn("入库作业", "箱支录入", boxInfo.BoxNo3, "删除", $"删除了箱码:{boxInfo.BoxNo}、追溯码:{boxInfo.BoxNo3}的箱支物料信息", Convert.ToInt32(model.CreateUser));
                
                return "";
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
 
        //获取箱码信息(未绑定托盘的箱码)
        public List<BoxInfoDto> GetBoxInfoList(BoxInfoVm model, out int count)
        {
            string sqlString = string.Empty;
            string sqlCount = string.Empty;
            string sqlPub = string.Empty;
            try
            {
                if (model.Page == 0)
                {
                    model.Page = 1;
                }
                Expression<Func<BllBoxInfo, bool>> item = Expressionable.Create<BllBoxInfo>()
                  .AndIF(!string.IsNullOrWhiteSpace(model.BoxNo), it => it.BoxNo.Contains(model.BoxNo.Trim()))
                  .AndIF(!string.IsNullOrWhiteSpace(model.BoxNo2), it => it.BoxNo2.Contains(model.BoxNo2.Trim()))
                  .AndIF(!string.IsNullOrWhiteSpace(model.BoxNo3), it => it.BoxNo3.Contains(model.BoxNo3.Trim()))
                  .AndIF(!string.IsNullOrWhiteSpace(model.SkuNo), it => it.SkuNo.Contains(model.SkuNo.Trim()))
                  .AndIF(!string.IsNullOrWhiteSpace(model.LotNo), it => it.LotNo.Contains(model.LotNo.Trim()))
                  .AndIF(!string.IsNullOrWhiteSpace(model.LotText), it => it.LotText.Contains(model.LotText.Trim()))
                  .AndIF(!string.IsNullOrWhiteSpace(model.ProductionTime), it => it.ProductionTime >= Convert.ToDateTime(model.ProductionTime))
                  .AndIF(!string.IsNullOrWhiteSpace(model.InspectMark), it => it.InspectMark == model.InspectMark.Trim())
                  .AndIF(!string.IsNullOrWhiteSpace(model.BitBoxMark), it => it.BitBoxMark == model.BitBoxMark.Trim())
                  .AndIF(!string.IsNullOrWhiteSpace(model.Origin), it => it.Origin == model.Origin.Trim())
                  .And(m => m.SkuNo != "100099" && m.IsDel == "0" && m.Status == "0")
                  .ToExpression();//注意 这一句 不能少
                var total = 0;
                var data = GetAllWhereAsync(item)
                    .LeftJoin<SysUserInfor>((a, b) => a.CreateUser == b.Id)
                    .LeftJoin<SysUserInfor>((a, b, c) => a.CreateUser == c.Id)
                    .Select((a, b, c) => new BoxInfoDto()
                    {
                        Id = a.Id,
                        BoxNo = a.BoxNo,
                        BoxNo2 = a.BoxNo2,
                        BoxNo3 = a.BoxNo3,
                        Status = a.Status,
                        Origin = a.Origin,
                        SkuNo = a.SkuNo,
                        SkuName = a.SkuName,
                        Qty = a.Qty,
                        FullQty = a.FullQty,
                        LotNo = a.LotNo,
                        LotText = a.LotText,
                        SupplierLot = a.SupplierLot,
                        BitBoxMark = a.BitBoxMark,
                        InspectStatus = a.InspectStatus,
                        InspectMark = a.InspectMark,
                        ProductionTime = a.ProductionTime,
                        ExpirationTime = a.ExpirationTime,
                        CreateUserName = b.RealName,
                        UpdateUserName = c.RealName,
                        CreateTime = a.CreateTime,
                        UpdateTime = a.UpdateTime
                    })
                    .OrderByDescending(a => a.CreateTime)
                    .ToOffsetPage(model.Page, model.Limit, ref total);
                count = total;
                return data;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        //导入箱码信息
        public string ImportBoxInfo(BoxInfoVms models)
        {
            string strMessage = "";
            try
            {
                if (models.ListBoxInfo.Count <= 0)
                {
                    strMessage = "-1:文件内无数据请核实!";
                }
                var num = 0;
                int CreateUser = Convert.ToInt32(models.CreateUser);
                List<string> boxList = new List<string>();
                foreach (BoxInfoVm model in models.ListBoxInfo)
                {
                    try
                    {
                        model.CreateUser = models.CreateUser;
                        model.Origin = "导入";
                        var msg = AddBoxInfo(model);
                        strMessage += msg;
                        if (msg == "")
                        {
                            num += 1;
                            var hasBoxItem = boxList.Exists(x => x == model.BoxNo);
                            if (!hasBoxItem)
                            {
                                boxList.Add(model.BoxNo);
                            }
                        }
                    }
                    catch
                    {
                        // ignored
                    }
                }
                if (num > 0)
                {
                    new OperationASNServer().AddLogOperationAsn("入库作业", "箱支录入", $"共导入了{boxList.Count}箱", "导入", $"共导入了{boxList.Count}箱,共导入了{num}支", CreateUser);
                }
                if (strMessage.Contains("-1") && num > 0)
                {
                    return "部分导入成功" + strMessage;
                }
                if (num>0)
                {
                    return "导入成功" + strMessage;
                }
 
                return strMessage;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 
        //根据箱码获取未组盘的箱码信息
        public List<BoxInfoDto> GetBoxInfoList(string boxCode, string isContinue, string boxCode2)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(boxCode))
                {
                    return null;
                }
 
                var sql = $@" select BoxNo,SkuNo,SkuName,LotNo,sum(Qty) as Qty,Status from BllBoxInfo group by BoxNo,SkuNo,SkuName,LotNo,Status,IsDel ";
 
                if (isContinue == "1")
                {
                    if (string.IsNullOrWhiteSpace(boxCode2))
                    {
                        throw new Exception("开启连续组托时,尾箱码信息不可为空!");
                    }
                    sql += $"having BoxNo >= '{boxCode}' and BoxNo <= '{boxCode2}' and Status = '0' and IsDel = '0' ";
                }
                else
                {
                    sql += $"having BoxNo = '{boxCode}' and Status = '0' and IsDel = '0' ";
 
                }
 
                var data = Db.Ado.SqlQuery<BoxInfoDto>(sql);
                return data;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
    }
}