using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using Dm; using Model.ModelDto.BllSoDto; using SqlSugar; using WMS.BLL.LogServer; using WMS.DAL; using WMS.Entity.BllSoEntity; using WMS.Entity.Context; using WMS.Entity.DataEntity; using WMS.Entity.SysEntity; using WMS.IBLL.IBllSoServer; namespace WMS.BLL.BllSoServer { public class ExportNoticeDetailServer: DbHelper,IExportNoticeDetailServer { private static readonly SqlSugarScope Db = DataContext.Db; public ExportNoticeDetailServer():base(Db) { } public List GetExportNoticeDetailList(string soNo, int page, int limit, out int count) { try { if (string.IsNullOrWhiteSpace(soNo)) { throw new Exception("出库单号为空"); } var notice = DataContext.Db.Queryable().Where(m => m.IsDel == "0" && m.SONo == soNo).ToList().FirstOrDefault(); if (notice == null) { throw new Exception("出库单号为空"); } var total = 0; var data = GetAllWhereAsync(a=>a.SONo == soNo) .LeftJoin((a,b)=> a.PackagNo == b.PackagNo) .LeftJoin((a,b,c)=> a.CreateUser == c.Id) .LeftJoin((a,b,c,d)=> a.UpdateUser == d.Id) .Select((a, b, c, d) => new ExportNoticeDetailDto() { Id = a.Id, Status = a.Status, Origin = notice.Origin, SONo = a.SONo, SkuNo = a.SkuNo, SkuName = a.SkuName, Standard = a.Standard, LotNo = a.LotNo, LotText = a.LotText, Qty = a.Qty, AllotQty = a.AllotQty, FactQty = a.FactQty, CompleteQty = a.CompleteQty, PackagNo = a.PackagNo, PackagName = b.PackagName, Price = a.Price, Money = a.Money, IsBale = a.IsBale, IsBelt = a.IsBelt, SupplierLot = a.SupplierLot, IsWave = a.IsWave, WaveNo = a.WaveNo, CreateUserName = c.RealName, UpdateUserName = d.RealName, CreateTime = a.CreateTime, UpdateTime = a.UpdateTime }).ToOffsetPage(page,limit,ref total); count = total; return data; } catch (Exception e) { throw new Exception(e.Message); } } public bool DelExportNoticeDetail(int id, int userId) { try { //出库单明细 var noticeDetail = Db.Queryable().Where(m => m.IsDel == "0" && m.Id == id).ToList().FirstOrDefault(); //出库单明细 var notice = Db.Queryable().Where(m => m.IsDel == "0" && m.SONo == noticeDetail.SONo).ToList().FirstOrDefault(); if (noticeDetail == null) { throw new Exception("未查询到出库单明细信息"); } if (notice == null) { throw new Exception("未查询到出库单信息"); } if (notice.Status != "0") { throw new Exception("当前出库单状态不是等待执行,不能删除"); } if (noticeDetail.AllotQty > 0) { throw new Exception("当前出库单明细已被分配,不能删除"); } var count = Db.Queryable().Count(d => d.SONo == noticeDetail.SONo && d.IsDel == "0" && d.Id != noticeDetail.Id); Db.BeginTran();//开启事务 try { var stock = Db.Queryable().Where(s => s.SkuNo == noticeDetail.SkuNo).ToList(); if (!string.IsNullOrWhiteSpace(noticeDetail.LotNo)) { stock = stock.Where(s => s.LotNo == noticeDetail.LotNo).ToList(); } else { stock = stock.Where(s => string.IsNullOrWhiteSpace(s.LotNo)).ToList(); } var sd = stock.FirstOrDefault(); if (sd == null) { throw new Exception($"未找到物料{noticeDetail.SkuNo}、批次{noticeDetail.LotNo} 的库存信息"); } sd.LockQty -= noticeDetail.Qty; noticeDetail.IsDel = "1"; noticeDetail.UpdateUser = userId; noticeDetail.UpdateTime = DateTime.Now; var n = 0; var j = 0; if (count < 1) { notice.IsDel = "1"; notice.UpdateUser = userId; notice.UpdateTime = DateTime.Now; n = Db.Updateable(notice).ExecuteCommand(); j = 1; } var m = Db.Updateable(sd).UpdateColumns(it => new { it.LockQty }).ExecuteCommand(); var i = Db.Updateable(noticeDetail).ExecuteCommand(); //添加操作日志记录 var k = new OperationSOServer().AddLogOperationSo("出库作业", "出库单据", notice.SONo, "删除", $"删除了单据号为{notice.SONo}、物料为{noticeDetail.SkuNo}、批次号为{noticeDetail.LotNo}的单据明细信息", userId); if (j>0) { if (n > 0 && m > 0 && i > 0 && k) { Db.CommitTran(); return true; } } else { if ( m > 0 && i > 0 && k) { Db.CommitTran(); return true; } } Db.RollbackTran(); return false; } catch (Exception e) { Db.RollbackTran(); throw new Exception(e.Message); } } catch (Exception e) { throw new Exception("删除出库单明细失败:"+e.Message); } } } }