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, 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(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().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(str); if (box.Qty == null) { box.Qty = 0; } box.Qty = box.Qty + model.Qty; var package = Db.Queryable().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().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().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 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> item = Expressionable.Create() .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((a, b) => a.CreateUser == b.Id) .LeftJoin((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 boxList = new List(); 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 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(sql); return data; } catch (Exception e) { throw new Exception(e.Message); } } } }