using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Model.ModelDto.BllSoDto; using Model.ModelVm.BllSoVm; using SqlSugar; using WMS.BLL.LogServer; using WMS.DAL; using WMS.Entity.BllSoEntity; using WMS.Entity.Context; using WMS.Entity.DataEntity; using WMS.Entity.LogEntity; using WMS.Entity.SysEntity; using WMS.IBLL.IBllSoServer; namespace WMS.BLL.BllSoServer { public class ExportAllotServer : DbHelper, IExportAllotServer { private static readonly SqlSugarScope Db = DataContext.Db; public ExportAllotServer() : base(Db) { } //查询出库分配表信息 public async Task> GetExportAllotList(GetExportAllotVm model, RefAsync count) { var strList = new List(); if (!string.IsNullOrWhiteSpace(model.BoxNo)) { var detailList = Db.Queryable().Where(m => m.IsDel == "0" && m.BoxNo.Contains(model.BoxNo.Trim())).Select(m => m.ExportAllotId).Distinct().ToList(); strList = detailList; } Expression> item = Expressionable.Create() .AndIF(!string.IsNullOrWhiteSpace(model.SoNo), it => it.SONo.Contains(model.SoNo.Trim())) .AndIF(!string.IsNullOrWhiteSpace(model.WaveNo), it => it.WaveNo.Contains(model.WaveNo.Trim())) .AndIF(!string.IsNullOrWhiteSpace(model.PalletNo), it => it.PalletNo.Contains(model.PalletNo.Trim())) .AndIF(!string.IsNullOrWhiteSpace(model.SkuNo), it => it.SkuNo.Contains(model.SkuNo.Trim())) .AndIF(!string.IsNullOrWhiteSpace(model.SkuName), it => it.SkuName.Contains(model.SkuName.Trim())) .AndIF(!string.IsNullOrWhiteSpace(model.LotNo), it => it.LotNo.Contains(model.LotNo.Trim())) .AndIF(!string.IsNullOrWhiteSpace(model.Status), it => it.Status == model.Status) .AndIF(!string.IsNullOrWhiteSpace(model.BoxNo), m => strList.Contains(m.Id)) .ToExpression();//注意 这一句 不能少 var total = 0; var data = await GetAllWhereAsync(item) .LeftJoin((a, b) => a.LogisticsId == b.Id) .LeftJoin((a, b, c) => a.CreateUser == c.Id) .LeftJoin((a, b, c, d) => a.CreateUser == d.Id) .Select((a, b, c, d) => new ExportAllotDto() { Id = a.Id, SONo = a.SONo, WaveNo = a.WaveNo, SODetailNo = a.SODetailNo, StockId = a.StockId, TaskNo = a.TaskNo, LotNo = a.LotNo, LotText = a.LotText, SupplierLot = a.SupplierLot, SkuNo = a.SkuNo, SkuName = a.SkuName, Standard = a.Standard, PalletNo = a.PalletNo, IsBale = a.IsBale, IsBelt = a.IsBelt, Qty = a.Qty, CompleteQty = a.CompleteQty, Status = a.Status, LogisticsId = a.LogisticsId, LogisticsName = b.CarrierName, IsAdvance = a.IsAdvance, OutMode = a.OutMode, LoadingAddre = a.LoadingAddre, UnstackingMode = a.UnstackingMode, CreateUserName = c.RealName, UpdateUserName = d.RealName, CreateTime = a.CreateTime, UpdateTime = a.UpdateTime }).OrderByDescending(a => a.CreateTime).OrderBy(a => a.Status).OrderBy(a => a.SkuNo) .ToPageListAsync(model.Page, model.Limit, count); count = total; return data; } //撤销分配(删除) public void DelExportAllot(int id, int userId) { try { var allot = GetOneById(id); if (allot == null) { throw new Exception("未查询到要撤销的分配信息"); } if (allot.Status != "0") { throw new Exception("要撤销的分配信息状态不是待下发,不能撤销"); } //出库单 var notice = Db.Queryable().First(m => m.IsDel == "0" && m.SONo == allot.SONo); //出库单明细 var noticeDe = Db.Queryable().First(m => m.IsDel == "0" && m.Id == allot.SODetailNo); //库存明细 var stockDe = Db.Queryable().First(m => m.IsDel == "0" && m.Id == allot.StockId); //库存总单 var stock = Db.Queryable().First(a => a.IsDel == "0" && a.SkuNo == stockDe.SkuNo); //任务 var task = Db.Queryable().First(a => a.IsDel == "0" && a.PalletNo == allot.PalletNo && a.Status == "4"); if (notice == null) { throw new Exception("未查询到出库单信息"); } if (noticeDe == null) { throw new Exception("未查询到出库单明细信息"); } if (stockDe == null) { throw new Exception("未查询到要分配的库存明细信息"); } if (stock == null) { throw new Exception("未查询到要分配的库存总量信息"); } // 若任务已下发,判断任务状态 if (task != null) { //验证任务状态是否为正在执行 if (task.Status == "0" || task.Status == "1" || task.Status == "2") { throw new Exception("当前任务等待执行或正在执行,请取消后删除"); } } Db.BeginTran(); var time = DateTime.Now; //修改任务信息 //task.IsSend = 0; //是否可下发 //task.UpdateTime = time; //task.UpdateUser = userId; //删除出库分配信息 allot.IsDel = "1"; allot.UpdateTime = time; allot.UpdateUser = userId; //allot.TaskNo = ""; //修改出库单明细 noticeDe.AllotQty -= allot.Qty; noticeDe.UpdateTime = time; noticeDe.UpdateUser = userId; //修改库存明细 stockDe.LockQty -= allot.Qty; if (notice.Status == "2") { notice.Status = "1"; } if (noticeDe.AllotQty == 0) { noticeDe.Status = "0"; var count = Db.Queryable().Count(m => m.IsDel == "0" && m.Id != noticeDe.Id && m.SONo == allot.SONo && m.Status != "0"); if (count == 0) { notice.Status = "0"; } } if (stockDe.LockQty == 0) { stockDe.Status = "0"; } else if (stockDe.Status == "2") { stockDe.Status = "1"; } //stock.LockQty -= allot.Qty; Db.Updateable(notice).ExecuteCommand(); Db.Updateable(noticeDe).ExecuteCommand(); Db.Updateable(allot).ExecuteCommand(); Db.Updateable(stockDe).ExecuteCommand(); //Db.Updateable(task).ExecuteCommand(); //Db.Updateable(stock).ExecuteCommand(); //添加操作日志记录 var k = new OperationSOServer().AddLogOperationSo("出库作业", "拣货明细", notice.SONo, "撤销", $"撤销了单据号为{notice.SONo}、托盘码为:{allot.PalletNo}的单据分配信息", userId); Db.CommitTran(); } catch (Exception e) { Db.RollbackTran(); throw new Exception(e.Message); } } #region 归档数据 //查询出库分配表信息 public List GetArchivingExportAllotList(string no, string waveNo, string palletNo, string skuNo, string skuName, string lotNo, string status, string boxNo, int page, int limit, out int count) { string sqlString = string.Empty; string sqlCount = string.Empty; string sqlPub = string.Empty; try { sqlCount += "SELECT DISTINCT COUNT(tb1.ID) FROM ArchivingExportAllot AS tb1 "; sqlString += "SELECT DISTINCT tb1.Id,tb1.SONo,tb1.WaveNo,tb1.SODetailNo,tb1.StockId,tb1.LotNo,tb1.TaskNo, "; sqlString += "tb1.LotText,tb1.SupplierLot,tb1.SkuNo,tb1.SkuName,tb1.Standard,tb1.PalletNo,tb1.IsBale,tb1.IsBelt,"; sqlString += "tb1.Qty,tb1.CompleteQty,tb1.Status,tb1.LogisticsId,tb2.CarrierName as LogisticsName,tb1.IsAdvance,"; sqlString += "tb1.OutMode,tb1.LoadingAddre,tb1.UnstackingMode,"; sqlString += "tb3.RealName as CreateUserName,tb4.RealName as UpdateUserName,tb1.CreateTime,tb1.UpdateTime "; sqlString += " FROM ArchivingExportAllot AS tb1 "; sqlPub += "LEFT JOIN SysLogisticsInfo AS tb2 ON tb1.LogisticsId = tb2.Id "; sqlPub += "LEFT JOIN SysUserInfor AS tb3 ON tb1.CreateUser = tb3.Id "; sqlPub += "LEFT JOIN SysUserInfor AS tb4 ON tb1.UpdateUser = tb4.Id "; sqlPub += $"WHERE tb1.SONo LIKE '%{no}%' AND tb1.LotNo LIKE '%{lotNo}%' and tb1.IsDel = '0' "; sqlPub += $"and tb1.waveNo LIKE '%{waveNo}%' AND tb1.palletNo LIKE '%{palletNo}%' "; sqlPub += $"and tb1.skuNo LIKE '%{skuNo}%' AND tb1.SkuName LIKE '%{skuName}%' "; if (!string.IsNullOrWhiteSpace(boxNo)) { sqlPub += $"and tb1.id in (select ExportAllotId from ArchivingCompleteDetail where isdel = '0' and BoxNo like '%{boxNo}%') "; } if (!string.IsNullOrWhiteSpace(status)) { sqlPub += $"AND tb1.Status = '{status}' "; } sqlCount += sqlPub; sqlPub += " order by tb1.CreateTime desc ,tb1.Status,tb1.SkuNo asc "; if (page == 0) { page = 1; } sqlString += sqlPub + $" offset {((page - 1) * limit)} rows fetch next {limit} rows only;"; var com = new Common(); count = com.GetRowCount(sqlCount); var modelList = Db.Ado.SqlQuery(sqlString); return modelList; } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion } }