using Model.InterFaceModel; using Newtonsoft.Json; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using Utility.Tools; using WMS.BLL.LogServer; using WMS.DAL; using WMS.Entity.BllAsnEntity; using WMS.Entity.BllQualityEntity; using WMS.Entity.BllSoEntity; using WMS.Entity.Context; using WMS.Entity.DataEntity; using WMS.Entity.LogEntity; using WMS.Entity.SysEntity; using WMS.IBLL.IBllTransServer; using static Model.InterFaceModel.RCSModel; namespace WMS.BLL.BllTransServer { public class NoticeServer : DbHelper, INoticeServer { private static readonly SqlSugarScope Db = DataContext.Db; public NoticeServer() : base(Db) { } /// /// 下发入库单 /// /// /// public ErpModel CreateAsnWork(AsnInfo model) { try { var resultModel = new ErpModel() { Success = -1, Message = "" }; if (string.IsNullOrEmpty(model.OrderCode)) { resultModel.Message = "上游系统单号不可为空!"; return resultModel; } if (model.AsnDetails.Count <= 0) { resultModel.Message = "入库单明细不可为空!"; return resultModel; } // 入库总表信息 string asnNo = new Common().GetMaxNo("ASN"); var asnModel = new BllArrivalNotice() { ASNNo = asnNo, Status = "0",//等待执行 Type = "0",//原料入库 Origin = string.IsNullOrEmpty(model.Origin) ? "MES" : model.Origin, CustomerNo = model.Customer, CustomerName = "", OrderCode = model.OrderCode }; // 入库明细表信息 List detailModels = new List(); foreach (AsnDetail asnDetailModel in model.AsnDetails) { if (string.IsNullOrEmpty(asnDetailModel.SkuNo)) { resultModel.Message = "物料编码不可为空!"; return resultModel; } if (string.IsNullOrEmpty(asnDetailModel.SkuName)) { resultModel.Message = "物料名称不可为空!"; return resultModel; } if (asnDetailModel.Qty <= 0) { resultModel.Message = "数量应大于0!"; return resultModel; } if (string.IsNullOrEmpty(asnDetailModel.LotNo)) { resultModel.Message = "批次号不可为空!"; return resultModel; } var detailModel = new BllArrivalNoticeDetail() { ASNNo = asnNo, OrderDetailCode = asnDetailModel.OrderDetailCode, SkuNo = asnDetailModel.SkuNo, SkuName = asnDetailModel.SkuName, LotNo = asnDetailModel.LotNo, LotText = "", Qty = (decimal)asnDetailModel.Qty,//这里是桶数量 FactQty = 0, CompleteQty = 0, SupplierLot = asnDetailModel.SupplierLot, Status = "0", IsSampling = "0", InspectStatus = "0", CreateUser = 0 }; detailModels.Add(detailModel); } Db.BeginTran(); // 插入入库单总表 Db.Insertable(asnModel).ExecuteCommand(); // 插入入库单明细表 Db.Insertable(detailModels).ExecuteCommand(); Db.CommitTran(); resultModel.Success = 0; resultModel.Message = "成功"; return resultModel; } catch (Exception ex) { Db.RollbackTran(); throw ex; } } /// /// 任务完成 /// /// /// /// public ErpModel RCSFinishTask(string TaskNo,string Status) { try { #region 条件判断 var resultModel = new ErpModel() { Success = -1, Message = "" }; if (string.IsNullOrEmpty(TaskNo)) { resultModel.Message = "任务号不可为空!"; return resultModel; } //任务信息 var taskInfo = Db.Queryable().First(w => w.TaskNo == TaskNo); if (taskInfo == null) { resultModel.Message = $"任务号为:{TaskNo}的任务不存在!"; return resultModel; } if (taskInfo.Status != "1") { resultModel.Message = $"任务号为:{TaskNo}的任务状态异常!"; return resultModel; } #endregion //开启事务 Db.BeginTran(); var comTime = DateTime.Now; if (Status == "0") { taskInfo.Status = "3";//异常结束 //修改任务状态 Db.Updateable(taskInfo).ExecuteCommand(); resultModel.Success = 0; resultModel.Message = "成功"; //提交事务 Db.CommitTran(); return resultModel; } switch (taskInfo.OrderType) { #region 入库完成 case "0": //目标储位信息 var locatInfo = Db.Queryable().First(w => w.LocatNo == taskInfo.EndLocat && w.IsDel == "0"); if (locatInfo == null) { resultModel.Message = "目标储位信息不存在!"; return resultModel; } //库存明细 var sd1 = Db.Queryable().First(w => w.PalletNo == taskInfo.PalletNo); if (sd1 == null) { resultModel.Message = $"该桶对应的库存信息不存在,桶号:{taskInfo.PalletNo}!"; return resultModel; } else { sd1.Qty = 1; sd1.LockQty = 0; sd1.FrozenQty = 0; sd1.InspectQty = 0; sd1.WareHouseNo = locatInfo.WareHouseNo;//所属仓库 sd1.RoadwayNo = locatInfo.RoadwayNo;//所属巷道 sd1.AreaNo = locatInfo.AreaNo;//所属区域 sd1.LocatNo = locatInfo.LocatNo;//储位地址 sd1.CompleteTime = comTime; sd1.InspectStatus = "0";//待检验 sd1.PalletStatus = "2";//半成品 //修改库存明细 Db.Updateable(sd1).ExecuteCommand(); } locatInfo.Status = "1";//有物品 //更新储位状态 Db.Updateable(locatInfo).ExecuteCommand(); //入库单明细信息 var noticeDetail = Db.Queryable().First(w => w.Id == taskInfo.NoticeDetailNo); if (noticeDetail != null) { //入库单信息 var notice = Db.Queryable().First(w => w.ASNNo == noticeDetail.ASNNo); if (notice == null) { resultModel.Message = "该任务对应的入库单不存在!"; return resultModel; } if (notice.Status == "3" || notice.Status == "4") { resultModel.Message = "该任务对应的入库单已执行完成!"; return resultModel; } noticeDetail.CompleteQty += 1;//完成数量 noticeDetail.Status = "1";//0:等待执行 1:正在执行 2:执行完成 if (noticeDetail.CompleteQty >= noticeDetail.Qty) { noticeDetail.Status = "2"; noticeDetail.CompleteTime = comTime; } noticeDetail.UpdateUser = 0; noticeDetail.UpdateTime = comTime; //更新入库单明细 Db.Updateable(noticeDetail).ExecuteCommand(); notice.UpdateUser = 0; notice.UpdateTime = comTime; if (notice.Status == "0") { notice.Status = "1"; } var asnDetailNum = Db.Queryable().Count(m => m.IsDel == "0" && m.ASNNo == noticeDetail.ASNNo && m.Status != "2"); if (asnDetailNum == 0) { notice.Status = "2"; notice.CompleteTime = comTime;//完成时间 } //更新入库单 Db.Updateable(notice).ExecuteCommand(); } #region 质检请验,去掉MES //var qualityRequest = Db.Queryable().First(m => m.IsDel == "0" && m.SkuNo == noticeDetail.SkuNo && m.LotNo == noticeDetail.LotNo && m.SupplierLot == noticeDetail.SupplierLot); //if (qualityRequest == null) //{ // //质检请验单信息 // string qcNo = new Common().GetMaxNo("QC"); // qualityRequest = new BllQualityInspectionRequest(); // qualityRequest.QcNo = qcNo; // qualityRequest.Status = "0"; // qualityRequest.SkuNo = noticeDetail.SkuNo; // qualityRequest.SkuName = noticeDetail.SkuName; // qualityRequest.LotNo = noticeDetail.LotNo; // qualityRequest.SupplierLot = noticeDetail.SupplierLot; // qualityRequest.Qty = noticeDetail.Qty; // qualityRequest.SamplingQty = 0; // qualityRequest.ASNNo = noticeDetail.ASNNo; // qualityRequest.CreateUser = 0; // qualityRequest.CreateTime = comTime; // // 记录任务日志 // var taskNo = new Common().GetMaxNo("TK"); // var exTask = new LogTask() // { // TaskNo = taskNo, // Sender = "WMS", // Receiver = "Limes", // IsSuccess = 0,//是否下发成功 0失败 1成功 // StartLocat = "",//起始位置 // PalletNo = "",//托盘码 // IsSend = 1,//是否可再次下发 // IsCancel = 1,//是否可取消 // IsFinish = 1,//是否可完成 // Status = "0",//任务状态0:等待执行1正在执行2执行完成 // OrderType = "5",//0 入库单 1 出库单 2 盘点单 3 移库单 4 取样出库单 5 其他 // EndLocat = "",//目标位置 // Type = "3",//任务类型 0 入库任务 1 出库任务 2 移库任务 // Msg = "请验任务" // }; // // 插入任务日志 // Db.Insertable(exTask).ExecuteCommand(); // // 调用Limes接口发起请验 // var sendModel = new SendLimesModel() // { // QcNo = qualityRequest.QcNo, // SkuNo = qualityRequest.SkuNo, // Qty = qualityRequest.Qty.ToString(), // LotNo = qualityRequest.LotNo, // SupplierLot = qualityRequest.SupplierLot, // RequestUser = "", // 请验人 // }; // var jsonData = JsonConvert.SerializeObject(sendModel); // string response = ""; // try // { // var time1 = DateTime.Now;//发送时间 .ToString("yyyy-MM-dd HH:mm:ss") // //response = HttpHelper.DoPost(url, jsonData, "上传Limes系统发起请验", "Limes"); // var time2 = DateTime.Now;//返回时间 .ToString("yyyy-MM-dd HH:mm:ss") // ////解析返回数据 // //var limesModel = JsonConvert.DeserializeObject(response); // List list1 = new List(); // list1.Add(taskNo); // //if (limesModel.Success == 0) // //{ // //更改任务的发送返回时间// // new TaskServer().EditTaskIssueOk(list1, time1, time2); // // 更新请验单信息 // qualityRequest.Status = "1"; // qualityRequest.SamplingQty = 10;//decimal.Parse(limesModel.SamplingQty); // qualityRequest.RequestUser = 0; // qualityRequest.RequestTime = DateTime.Now; // qualityRequest.UpdateTime = DateTime.Now; // qualityRequest.UpdateUser = 0; // //添加质检请验单 // Db.Insertable(qualityRequest).ExecuteCommand(); // //} // //if (limesModel.Success == -1) // //{ // // new TaskServer().EditTaskIssueNo(list1, time1, time2,limesModel.Message); // // throw new Exception(limesModel.Message); // //} // } // catch (Exception ex) // { // throw new Exception(ex.Message); // } //} #endregion break; #endregion #region 出库完成 case "1": //起始储位信息 var startLocatInfo = Db.Queryable().First(w => w.LocatNo == taskInfo.StartLocat && w.IsDel == "0"); if (startLocatInfo == null) { resultModel.Message = "起始储位信息不存在!"; return resultModel; } //库存明细 var sd2 = Db.Queryable().First(w => w.PalletNo == taskInfo.PalletNo); if (sd2 == null) { resultModel.Message = "库存信息不存在!"; return resultModel; } sd2.Qty = 0; sd2.LockQty = 0; sd2.FrozenQty = 0; sd2.InspectQty = 0; sd2.ASNNo = ""; sd2.ASNDetailNo = null; sd2.WareHouseNo = "";//所属仓库 sd2.RoadwayNo = "";//所属巷道 sd2.AreaNo = "";//所属区域 sd2.LocatNo = "";//储位地址 sd2.CompleteTime = comTime; //修改库存明细 Db.Updateable(sd2).ExecuteCommand(); startLocatInfo.Status = "0";//空储位 //修改起始储位状态 Db.Updateable(startLocatInfo).ExecuteCommand(); //出库单明细信息 var exNoticeDetail = Db.Queryable().First(w => w.Id == taskInfo.NoticeDetailNo); if (exNoticeDetail != null) { //出库单信息 var exNotice = Db.Queryable().First(w => w.SONo == exNoticeDetail.SONo); if (exNotice == null) { resultModel.Message = "该任务对应的出库单不存在!"; return resultModel; } if (exNotice.Status == "4" || exNotice.Status == "5") { resultModel.Message = "该任务对应的出库单已执行完成!"; return resultModel; } exNotice.UpdateUser = 0; exNotice.UpdateTime = comTime; if (exNotice.Status == "0" || exNotice.Status == "1" || exNotice.Status == "2") { exNotice.Status = "3";//更改为正在执行 } var num = Db.Queryable().Count(m => m.IsDel == "0" && m.SONo == exNoticeDetail.SONo && m.CompleteQty < m.Qty); if (num <= 0) { exNotice.Status = "4"; //更改为执行完成 } //修改出库单信息 Db.Updateable(exNotice).ExecuteCommand(); //修改出库单明细拣货数量 exNoticeDetail.CompleteQty += sd2.Qty; Db.Updateable(exNoticeDetail).ExecuteCommand(); } break; #endregion #region 移库完成 case "3": //库存明细 var sd3 = Db.Queryable().First(w => w.PalletNo == taskInfo.PalletNo); if (sd3 == null) { resultModel.Message = "库存信息不存在!"; return resultModel; } if (taskInfo.Type == "0")//入库任务 { //目标储位信息 var endLocatInfo2 = Db.Queryable().First(w => w.LocatNo == taskInfo.EndLocat && w.IsDel == "0"); if (endLocatInfo2 == null) { resultModel.Message = "目标储位信息不存在!"; return resultModel; } endLocatInfo2.Status = "1";//有物品 //修改目标储位状态 Db.Updateable(endLocatInfo2).ExecuteCommand(); sd3.WareHouseNo = endLocatInfo2.WareHouseNo;//所属仓库 sd3.RoadwayNo = endLocatInfo2.RoadwayNo;//所属巷道 sd3.AreaNo = endLocatInfo2.AreaNo;//所属区域 sd3.LocatNo = endLocatInfo2.LocatNo;//储位地址 if (endLocatInfo2.AreaNo.Contains("01"))//净桶区 { sd3.PalletStatus = "0"; } else if (endLocatInfo2.AreaNo.Contains("02"))//预混区 { sd3.PalletStatus = "1"; } else if (endLocatInfo2.AreaNo.Contains("03"))//半成品区 { sd3.PalletStatus = "2"; } else if (endLocatInfo2.AreaNo.Contains("04"))//脏桶区 { sd3.PalletStatus = "3"; sd3.LotNo = "";//批次 sd3.SkuNo = ""; sd3.SkuName = ""; sd3.InspectStatus = "0";//待检验 } } else if (taskInfo.Type == "1")//出库任务 { //起始储位信息 var startLocatInfo2 = Db.Queryable().First(w => w.LocatNo == taskInfo.StartLocat && w.IsDel == "0"); if (startLocatInfo2 == null) { resultModel.Message = "起始储位信息不存在!"; return resultModel; } startLocatInfo2.Status = "0";//空储位 //修改起始储位状态 Db.Updateable(startLocatInfo2).ExecuteCommand(); sd3.WareHouseNo = "";//所属仓库 sd3.RoadwayNo = "";//所属巷道 sd3.AreaNo = "";//所属区域 sd3.LocatNo = "";//储位地址 } else if (taskInfo.Type == "2")//移库任务 { //起始储位信息 var startLocatInfo2 = Db.Queryable().First(w => w.LocatNo == taskInfo.StartLocat && w.IsDel == "0"); if (startLocatInfo2 == null) { resultModel.Message = "起始储位信息不存在!"; return resultModel; } startLocatInfo2.Status = "0";//空储位 //修改起始储位状态 Db.Updateable(startLocatInfo2).ExecuteCommand(); //目标储位信息 var endLocatInfo2 = Db.Queryable().First(w => w.LocatNo == taskInfo.EndLocat && w.IsDel == "0"); if (endLocatInfo2 == null) { resultModel.Message = "目标储位信息不存在!"; return resultModel; } endLocatInfo2.Status = "1";//有物品 //修改目标储位状态 Db.Updateable(endLocatInfo2).ExecuteCommand(); sd3.WareHouseNo = endLocatInfo2.WareHouseNo;//所属仓库 sd3.RoadwayNo = endLocatInfo2.RoadwayNo;//所属巷道 sd3.AreaNo = endLocatInfo2.AreaNo;//所属区域 sd3.LocatNo = endLocatInfo2.LocatNo;//储位地址 sd3.Status = "0"; if (endLocatInfo2.AreaNo.Contains("01"))//洁净区 { sd3.PalletStatus = "0"; sd3.Status = "0"; } else if (endLocatInfo2.AreaNo.Contains("02"))//预混区 { sd3.PalletStatus = "1"; } else if (endLocatInfo2.AreaNo.Contains("03"))//半成品区 { sd3.PalletStatus = "2"; } else if (endLocatInfo2.AreaNo.Contains("04"))//脏桶区 { sd3.PalletStatus = "3"; sd3.LotNo = "";//批次 sd3.SkuNo = ""; sd3.SkuName = ""; sd3.InspectStatus = "0";//待检验 } } //修改库存明细 Db.Updateable(sd3).ExecuteCommand(); #region 去掉MES //回传桶状态给MES //BackPalletStatus(taskInfo.PalletNo, sd3.PalletStatus); #endregion break; #endregion } taskInfo.Status = "2";//执行完成 //修改任务状态 Db.Updateable(taskInfo).ExecuteCommand(); //提交事务 Db.CommitTran(); resultModel.Success = 0; resultModel.Message = "成功"; return resultModel; } catch (Exception ex) { //回滚事务 Db.RollbackTran(); throw ex; } } /// /// 回传桶状态给MES /// /// 桶号 /// 0:净桶 1:预混 2:半成品 3:脏桶 /// public void BackPalletStatus(string PalletNo, string Status,string url="") { try { var obj = new { PalletNo = PalletNo, Status = Status }; string jsonReq = JsonConvert.SerializeObject(obj); var response = HttpHelper.DoPost(url, jsonReq, "回传MES桶状态", "MES").ToString(); var result = JsonConvert.DeserializeObject(response);//解析返回数据 } catch (Exception ex) { throw ex; } } /// /// 下发出库单 /// /// /// public SoResInfo CreateSoWork(SoInfo model) { try { if (string.IsNullOrEmpty(model.OrderCode)) { throw new Exception("上游系统单号不可为空!"); } if (model.SoDetails.Count <= 0) { throw new Exception("出库单明细不可为空!"); } //返回信息 SoResInfo result = new SoResInfo(); var skuNos = model.SoDetails.Select(a => a.SkuNo).Distinct().ToList(); //获取库存明细 var stockDetailList = Db.Queryable().Where(s => skuNos.Contains(s.SkuNo) && (s.Qty - s.FrozenQty - s.LockQty) > 0 && s.InspectStatus == "1").ToList(); var billNo = ""; var bl = true; do { //获取自增单据号 billNo = new Common().GetMaxNo("SO"); var no = billNo; bl = Db.Queryable().Any(m => m.SONo == no); } while (bl); List soDetailList = new List(); //开启事务 Db.BeginTran(); var list = new List(); //添加出库单 foreach (var d in model.SoDetails) { if (d.Qty < 1) { throw new Exception("出库数量必须大于0"); } if (string.IsNullOrWhiteSpace(d.LotNo)) { throw new Exception("批次不可为空!"); } //库存明细 List stockDetails = stockDetailList.Where(s => s.SkuNo == d.SkuNo && s.LotNo == d.LotNo).OrderByDescending(o => o.CompleteTime).ToList(); if (stockDetails.Count < 1) { throw new Exception($"库存中未查询到出库物料信息:{d.SkuNo}"); } //判断数量 var qty = stockDetails.Sum(s => s.Qty - s.FrozenQty - s.LockQty); if (d.Qty > qty) { throw new Exception($"库存中出库物料信息:{d.SkuNo}、{d.LotNo} 库存数量不足"); } //添加出库单明细 var noticeDetail = new BllExportNoticeDetail() { SONo = billNo, SkuNo = d.SkuNo, SkuName = d.SkuName, Standard = "", LotNo = d.LotNo, LotText = "", Qty = d.Qty, AllotQty = 0, FactQty = 0, CompleteQty = 0, PackagNo = "", Price = 0, Money = 0, IsBale = "", IsBelt = "", SupplierLot = "", IsWave = "0", WaveNo = "", IsIssueLotNo = "1", OrderDetailCode = d.OrderDetailCode, CreateUser = 0, }; list.Add(noticeDetail); //锁定库存数量 decimal lockQty = 0; foreach (var item in stockDetails) { if (lockQty >= d.Qty) { break; } if (item.Qty - item.LockQty - item.FrozenQty <= d.Qty - lockQty) { item.LockQty += item.Qty - item.LockQty - item.FrozenQty; lockQty += (decimal)(item.Qty - item.LockQty - item.FrozenQty); } else { item.LockQty += d.Qty - lockQty; lockQty += d.Qty - lockQty; } Db.Updateable(item).UpdateColumns(it => new { it.LockQty }).ExecuteCommand(); } SoDetailInfo soDetail = new SoDetailInfo(); soDetail.OrderDetailCode = d.OrderDetailCode; soDetail.LockQty = d.Qty; soDetail.LotNo = d.LotNo; soDetailList.Add(soDetail); } //出库单信息 var notice = new BllExportNotice() { SONo = billNo, OrderCode = model.OrderCode, Type = model.SoType, Status = "0", Origin = "WMS", CustomerNo = model.Customer, CustomerName = "", LogisticsId = null, IsWave = "0", WaveNo = "", IsDespatch = "0", CreateUser = 0, }; var n = Db.Insertable(notice).ExecuteCommand(); var m = Db.Insertable(list).ExecuteCommand(); if (n <= 0 || m <= 0) { Db.RollbackTran(); throw new Exception("操作失败"); } //提交事务 Db.CommitTran(); //回传上游系统锁定数量信息 result.Success = "0"; result.Message = "操作成功"; result.SoDetails = soDetailList; return result; } catch (Exception ex) { Db.RollbackTran(); throw new Exception(ex.Message); } } } }