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, IAuditLogServer { private static readonly SqlSugarScope Db = DataContext.Db; public AuditLogServer() : base(Db) { } //获取审核记录信息 public List GetAuditLogList(AuditLogVm model, out int count) { try { Expression> item = Expressionable.Create() .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((a, b ) => a.CreateUser == b.Id) .LeftJoin((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().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() .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().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().First(m => m.IsDel == "0" && m.ASNNo == log.OrderNo); if (notice == null) { throw new Exception("未查询到该托盘的单据信息"); } //入库单明细 var noticeDetailList = Db.Queryable().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().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() .First(m => m.SkuNo == item.SkuNo && m.LotNo == item.LotNo); //库存箱支明细 var stockInfo = Db.Queryable().Where(m => m.StockDetailId == item.Id).ToList(); //储位 var locate = Db.Queryable() .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().Where(m => m.BindNo == demo.Id).ToList(); foreach (var demo2 in boxInfo) { 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().First(m => m.PalletNo == demo.PalletNo); if (pallet.Status == "1") { pallet.Status = "0"; Db.Updateable(pallet).ExecuteCommand(); } } Db.Deleteable(bindList.ToList()).ExecuteCommand(); //删除组托信息 } if (log.EndStatus == "正在执行") { foreach (var item in stockDetailToList) { //总库存 var stock = Db.Queryable() .First(m => m.SkuNo == item.SkuNo && m.LotNo == item.LotNo); //库存箱支明细 var stockInfo = Db.Queryable().Where(m => m.StockDetailId == item.Id).ToList(); //储位 var locate = Db.Queryable() .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(); } //修改托盘绑定 foreach (var demo in bindList.ToList()) { if (demo.Status == "0") { continue; } var boxInfo = Db.Queryable().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().First(m => m.IsDel == "0" && m.ASNNo == log.OrderNo && m.Id == item.ASNDetailNo); // if (noticeDetail == null) // { // throw new Exception("未查询到该托盘的单据明细信息"); // } // //总库存 // var stock = Db.Queryable() // .First(m => m.SkuNo == item.SkuNo && m.LotNo == item.LotNo); // //库存箱支明细 // var stockInfo = Db.Queryable().Where(m => m.StockDetailId == item.Id).ToList(); // //储位 // var locate = Db.Queryable() // .First(m => m.IsDel == "0" && m.LocatNo == item.LocatNo); // //托盘绑定 // var bind = Db.Queryable().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() .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().First(m => m.IsDel == "0" && m.ASNNo == log.OrderNo); if (notice == null) { throw new Exception("未查询到该托盘的单据信息"); } //入库单明细 var noticeDetail = Db.Queryable().First(m => m.IsDel == "0" && m.ASNNo == log.OrderNo && m.Id == stockDetail.ASNDetailNo); if (noticeDetail == null) { throw new Exception("未查询到该托盘的单据明细信息"); } //总库存 var stock = Db.Queryable() .First(m => m.SkuNo == stockDetail.SkuNo && m.LotNo == stockDetail.LotNo); //库存箱支明细 var stockInfo = Db.Queryable().Where(m => m.StockDetailId == stockDetail.Id).ToList(); //储位 var locate = Db.Queryable() .First(m => m.IsDel == "0" && m.LocatNo == stockDetail.LocatNo); //托盘绑定 var bind = Db.Queryable().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); } } } }