public new bool Add(string SortingLineCode, string DeliverLineCodes, string orderDate) { if (orderDate == string.Empty || orderDate == null) { orderDate = DateTime.Now.ToString("yyyyMMdd"); } else { orderDate = Convert.ToDateTime(orderDate).ToString("yyyyMMdd"); } var sortOder = SortOrderRepository.GetQueryable().Where(s => DeliverLineCodes.Contains(s.DeliverLineCode) && s.OrderDate == orderDate) .GroupBy(s => new { s.DeliverLineCode, s.OrderDate }) .Select(s => new { DeliverLineCode = s.Key.DeliverLineCode, OrderDate = s.Key.OrderDate }); foreach (var item in sortOder.ToArray()) { var sortOrderDispatch = new SortOrderDispatch(); sortOrderDispatch.SortingLineCode = SortingLineCode; sortOrderDispatch.DeliverLineCode = item.DeliverLineCode; sortOrderDispatch.WorkStatus = "1"; sortOrderDispatch.OrderDate = item.OrderDate; sortOrderDispatch.IsActive = "1"; sortOrderDispatch.UpdateTime = DateTime.Now; SortOrderDispatchRepository.Add(sortOrderDispatch); } SortOrderDispatchRepository.SaveChanges(); return(true); }
public object GetDetails(string orderDate) { if (orderDate == string.Empty || orderDate == null) { orderDate = DateTime.Now.ToString("yyyyMMdd"); } else { orderDate = Convert.ToDateTime(orderDate).ToString("yyyyMMdd"); } IQueryable <SortOrder> sortOrderQuery = SortOrderRepository.GetQueryable(); IQueryable <SortOrderDispatch> SortOrderDispatchQuery = SortOrderDispatchRepository.GetQueryable(); var sortorderDisp = SortOrderDispatchQuery.Where(s => s.OrderDate.Contains(orderDate)); var sortOrder = sortOrderQuery.Where(s => s.OrderDate.Contains(orderDate) && !sortorderDisp.Any(d => d.DeliverLineCode == s.DeliverLineCode)) //.Join(SortOrderDispatchQuery, //so => new { so.OrderDate, so.DeliverLineCode }, //sd => new { sd.OrderDate, sd.DeliverLineCode }, //(so, sd) => new { so.OrderDate, so.DeliverLineCode, so.DeliverLine.DeliverLineName, so.QuantitySum, so.DetailNum, sd.SortingLine, so.AmountSum }) .GroupBy(s => new { s.OrderDate, s.DeliverLineCode, s.DeliverLine }) .Select(s => new { DeliverLineCode = s.Key.DeliverLineCode, DeliverLineName = s.Key.DeliverLine.DeliverLineName, OrderDate = s.Key.OrderDate, //SortingLineCode = s.Key.SortingLine.SortingLineCode, //SortingLineName = s.Key.SortingLine.SortingLineName, QuantitySum = s.Sum(p => p.QuantitySum), AmountSum = s.Sum(p => p.AmountSum), DetailNum = s.Sum(p => p.DetailNum), IsActive = "可用" }); return(sortOrder.ToArray()); }
public object GetDetails(int page, int rows, string OrderID, string orderDate, string productCode) { if (orderDate == string.Empty || orderDate == null) { orderDate = DateTime.Now.ToString("yyyyMMdd"); } else { orderDate = Convert.ToDateTime(orderDate).ToString("yyyyMMdd"); } IQueryable <SortOrder> sortOrderQuery = SortOrderRepository.GetQueryable(); IQueryable <SortOrderDetail> sortOrderDetailQuery = SortOrderDetailRepository.GetQueryable(); var sortOrder = sortOrderQuery.Join(sortOrderDetailQuery, m => m.OrderID, d => d.OrderID, (m, d) => new { m, d }); if (productCode != string.Empty && productCode != null) { sortOrder = sortOrder.Where(s => s.d.ProductCode == productCode && s.d.RealQuantity > 0); } sortOrder = sortOrder.Where(s => s.m.OrderDate == orderDate); if (OrderID != string.Empty && OrderID != null) { sortOrder = sortOrder.Where(s => s.m.OrderID.Contains(OrderID)); } var temp = sortOrder.AsEnumerable().OrderBy(t => t.m.OrderID).Select(s => new { s.m.OrderID, s.m.CompanyCode, s.m.SaleRegionCode, s.m.OrderDate, s.m.OrderType, s.m.CustomerCode, s.m.CustomerName, s.m.QuantitySum, s.m.DeliverLineCode, s.m.AmountSum, s.m.DetailNum, s.m.DeliverOrder, s.m.DeliverDate, IsActive = s.m.IsActive == "1" ? "可用" : "不可用", UpdateTime = s.m.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss"), s.m.Description }); int total = temp.Count(); temp = temp.Skip((page - 1) * rows).Take(rows); return(new { total, rows = temp.ToArray() }); }
public new bool Add(string SortingLineCode, string DeliverLineCodes) { var sortOder = SortOrderRepository.GetQueryable().Where(s => DeliverLineCodes.Contains(s.DeliverLineCode)) .GroupBy(s => new { s.DeliverLineCode, s.OrderDate }) .Select(s => new { DeliverLineCode = s.Key.DeliverLineCode, OrderDate = s.Key.OrderDate }); foreach (var item in sortOder.ToArray()) { var sortOrderDispatch = new SortOrderDispatch(); sortOrderDispatch.SortingLineCode = SortingLineCode; sortOrderDispatch.DeliverLineCode = item.DeliverLineCode; sortOrderDispatch.WorkStatus = "1"; sortOrderDispatch.OrderDate = item.OrderDate; sortOrderDispatch.IsActive = "1"; sortOrderDispatch.UpdateTime = DateTime.Now; SortOrderDispatchRepository.Add(sortOrderDispatch); } SortOrderDispatchRepository.SaveChanges(); return(true); }
public object GetWorkStatus() { IQueryable <SortOrderDispatch> sortDispatchQuery = SortOrderDispatchRepository.GetQueryable(); IQueryable <SortOrder> sortOrderQuery = SortOrderRepository.GetQueryable(); var temp = sortDispatchQuery.Where(s => s.WorkStatus == "1").ToArray().AsEnumerable() .Join(sortOrderQuery, dp => new { dp.OrderDate, dp.DeliverLineCode }, om => new { om.OrderDate, om.DeliverLineCode }, (dp, om) => new { dp.ID, dp.OrderDate, dp.SortingLine, dp.DeliverLine, dp.IsActive, dp.UpdateTime, dp.WorkStatus, om.QuantitySum } ).Select(r => new { r.ID, r.OrderDate, r.SortingLine, r.DeliverLine, r.WorkStatus, r.IsActive, r.UpdateTime, r.QuantitySum }) .GroupBy(g => new { g.ID, g.OrderDate, g.DeliverLine, g.SortingLine, g.IsActive, g.UpdateTime, g.WorkStatus }) .Select(r => new { r.Key.ID, r.Key.OrderDate, r.Key.SortingLine.SortingLineCode, r.Key.SortingLine.SortingLineName, r.Key.DeliverLine.DeliverLineCode, r.Key.DeliverLine.DeliverLineName, IsActive = r.Key.IsActive == "1" ? "可用" : "不可用", UpdateTime = r.Key.UpdateTime.ToString("yyyy-MM-dd"), WorkStatus = r.Key.WorkStatus == "1" ? "未作业" : "已作业", QuantitySum = r.Sum(q => q.QuantitySum) }); return(temp.ToArray()); }
public void Dispatch(string connectionId, Model.ProgressState ps, CancellationToken cancellationToken, string workDispatchId, string userName) { Locker.LockKey = workDispatchId; ConnectionId = connectionId; ps.State = StateType.Start; NotifyConnection(ps.Clone()); IQueryable <SortOrderDispatch> sortOrderDispatchQuery = SortOrderDispatchRepository.GetQueryable(); IQueryable <SortOrder> sortOrderQuery = SortOrderRepository.GetQueryable(); IQueryable <SortOrderDetail> sortOrderDetailQuery = SortOrderDetailRepository.GetQueryable(); IQueryable <OutBillMaster> outBillMasterQuery = OutBillMasterRepository.GetQueryable(); IQueryable <OutBillDetail> outBillDetailQuery = OutBillDetailRepository.GetQueryable(); IQueryable <MoveBillMaster> moveBillMasterQuery = MoveBillMasterRepository.GetQueryable(); IQueryable <MoveBillDetail> moveBillDetailQuery = MoveBillDetailRepository.GetQueryable(); IQueryable <SortingLowerlimit> sortingLowerlimitQuery = SortingLowerlimitRepository.GetQueryable(); IQueryable <SortingLine> sortingLineQuery = SortingLineRepository.GetQueryable(); IQueryable <Storage> storageQuery = StorageRepository.GetQueryable(); IQueryable <SortWorkDispatch> sortWorkDispatchQuery = SortWorkDispatchRepository.GetQueryable(); workDispatchId = workDispatchId.Substring(0, workDispatchId.Length - 1); int[] work = workDispatchId.Split(',').Select(s => Convert.ToInt32(s)).ToArray(); //调度表未作业的数据 var temp = sortOrderDispatchQuery.Where(s => work.Any(w => w == s.ID) && s.WorkStatus == "1") .Join(sortOrderQuery, dp => new { dp.OrderDate, dp.DeliverLineCode }, om => new { om.OrderDate, om.DeliverLineCode }, (dp, om) => new { dp.OrderDate, dp.SortingLine, dp.DeliverLineCode, om.OrderID } ).Join(sortOrderDetailQuery, dm => new { dm.OrderID }, od => new { od.OrderID }, (dm, od) => new { dm.OrderDate, dm.SortingLine, od.Product, od.UnitCode, od.Price, od.RealQuantity } ).GroupBy(r => new { r.OrderDate, r.SortingLine, r.Product, r.UnitCode, r.Price }) .Select(r => new { r.Key.OrderDate, r.Key.SortingLine, r.Key.Product, r.Key.UnitCode, r.Key.Price, SumQuantity = r.Sum(p => p.RealQuantity * r.Key.Product.UnitList.Unit02.Count) }) .GroupBy(r => new { r.OrderDate, r.SortingLine }) .Select(r => new { r.Key.OrderDate, r.Key.SortingLine, Products = r }) .ToArray(); var employee = EmployeeRepository.GetQueryable().FirstOrDefault(i => i.UserName == userName); string operatePersonID = employee != null?employee.ID.ToString() : ""; if (employee == null) { ps.State = StateType.Error; ps.Errors.Add("未找到当前用户,或当前用户不可用!"); NotifyConnection(ps.Clone()); return; } decimal sumAllotQuantity = 0; decimal sumAllotLineQuantity = 0; MoveBillMaster lastMoveBillMaster = null; foreach (var item in temp) { try { if (cancellationToken.IsCancellationRequested) { return; } bool hasError = false; ps.State = StateType.Info; ps.Messages.Add("开始调度" + item.SortingLine.SortingLineName); NotifyConnection(ps.Clone()); using (var scope = new TransactionScope()) { if (item.Products.Count() > 0) { if (cancellationToken.IsCancellationRequested) { return; } if (lastMoveBillMaster != null && lastMoveBillMaster.WarehouseCode != item.SortingLine.Cell.WarehouseCode) { if (MoveBillCreater.CheckIsNeedSyncMoveBill(lastMoveBillMaster.WarehouseCode)) { MoveBillCreater.CreateSyncMoveBillDetail(lastMoveBillMaster); } } sumAllotLineQuantity = 0; if (cancellationToken.IsCancellationRequested) { return; } MoveBillMaster moveBillMaster = MoveBillCreater.CreateMoveBillMaster(item.SortingLine.Cell.WarehouseCode, item.SortingLine.MoveBillTypeCode, operatePersonID); moveBillMaster.Origin = "2"; moveBillMaster.Description = "分拣调度生成!"; lastMoveBillMaster = moveBillMaster; foreach (var product in item.Products.ToArray()) { if (cancellationToken.IsCancellationRequested) { return; } decimal sumBillQuantity = temp.Sum(t => t.Products.Sum(p => p.SumQuantity)); sumAllotQuantity += product.SumQuantity; decimal sumBillProductQuantity = item.Products.Sum(p => p.SumQuantity); sumAllotLineQuantity += product.SumQuantity; ps.State = StateType.Processing; ps.TotalProgressName = "分拣作业调度"; ps.TotalProgressValue = (int)(sumAllotQuantity / sumBillQuantity * 100); ps.CurrentProgressName = "正在调度:" + item.SortingLine.SortingLineName; ps.CurrentProgressValue = (int)(sumAllotLineQuantity / sumBillProductQuantity * 100); NotifyConnection(ps.Clone()); if (cancellationToken.IsCancellationRequested) { return; } //获取分拣线下限数据 var sortingLowerlimitQuantity = sortingLowerlimitQuery.Where(s => s.ProductCode == product.Product.ProductCode && s.SortingLineCode == product.SortingLine.SortingLineCode); decimal lowerlimitQuantity = 0; if (sortingLowerlimitQuantity.Count() > 0) { lowerlimitQuantity = sortingLowerlimitQuantity.Sum(s => s.Quantity); } if (cancellationToken.IsCancellationRequested) { return; } //获取分拣备货区库存 var storageQuantity = storageQuery.Where(s => s.ProductCode == product.Product.ProductCode) .Join(sortingLineQuery, s => s.Cell, l => l.Cell, (s, l) => new { l.SortingLineCode, s.Quantity } ) .Where(r => r.SortingLineCode == product.SortingLine.SortingLineCode); decimal storQuantity = 0; if (storageQuantity.Count() > 0) { storQuantity = storageQuantity.Sum(s => s.Quantity); } //获取当前这个卷烟库存数量 string[] areaTypes = new string[] { "1", "2", "4" }; var areaSumQuantitys = storageQuery.Where(s => areaTypes.Any(t => t == s.Cell.Area.AreaType) && s.ProductCode == product.Product.ProductCode).ToArray(); decimal areaQuantiy = 0; if (areaSumQuantitys.Count() > 0) { areaQuantiy = areaSumQuantitys.Sum(s => s.Quantity - s.OutFrozenQuantity); } if (cancellationToken.IsCancellationRequested) { return; } //获取移库量(按整件计)出库量加上下限量减去备货区库存量取整 decimal quantity = 0; quantity = Math.Ceiling((product.SumQuantity + lowerlimitQuantity - storQuantity) / product.Product.Unit.Count) * product.Product.Unit.Count; if (areaQuantiy < quantity)//判断当前这个卷烟库存是否小于移库量 { //出库量减去备货区库存量取整 quantity = Math.Ceiling((product.SumQuantity - storQuantity) / product.Product.Unit.Count) * product.Product.Unit.Count; if (areaQuantiy < quantity) { //出库量减去备货区库存量 quantity = product.SumQuantity - storQuantity; } } if (quantity > 0) { if (cancellationToken.IsCancellationRequested) { return; } AlltoMoveBill(moveBillMaster, product.Product, item.SortingLine.Cell, ref quantity, cancellationToken, ps, item.SortingLine.Cell.CellCode); } if (quantity > 0) { //生成移库不完整,可能是库存不足; hasError = true; ps.State = StateType.Error; ps.Errors.Add(product.Product.ProductCode + " " + product.Product.ProductName + " 库存不足!"); NotifyConnection(ps.Clone()); } } if (!hasError) { if (cancellationToken.IsCancellationRequested) { return; } OutBillMaster outBillMaster = OutBillCreater.CreateOutBillMaster(item.SortingLine.Cell.WarehouseCode, item.SortingLine.OutBillTypeCode, operatePersonID); outBillMaster.Origin = "2"; outBillMaster.Description = "分拣调度生成!"; //添加出库单细单 foreach (var product in item.Products.ToArray()) { if (cancellationToken.IsCancellationRequested) { return; } OutBillCreater.AddToOutBillDetail(outBillMaster, product.Product, product.Price, product.SumQuantity); } if (cancellationToken.IsCancellationRequested) { return; } //添加出库、移库主单和作业调度表 SortWorkDispatch sortWorkDisp = AddSortWorkDispMaster(moveBillMaster, outBillMaster, item.SortingLine.SortingLineCode, item.OrderDate); //修改线路调度作业状态和作业ID var sortDispTemp = sortOrderDispatchQuery.Where(s => work.Any(w => w == s.ID) && s.OrderDate == item.OrderDate && s.SortingLineCode == item.SortingLine.SortingLineCode); foreach (var sortDisp in sortDispTemp.ToArray()) { if (cancellationToken.IsCancellationRequested) { return; } sortDisp.SortWorkDispatchID = sortWorkDisp.ID; sortDisp.WorkStatus = "2"; } if (cancellationToken.IsCancellationRequested) { return; } SortWorkDispatchRepository.SaveChanges(); scope.Complete(); ps.Messages.Add(item.SortingLine.SortingLineName + " 调度成功!"); } else { ps.State = StateType.Info; ps.Messages.Add(item.SortingLine.SortingLineName + " 调度失败!"); NotifyConnection(ps.Clone()); return; } } } if (cancellationToken.IsCancellationRequested) { return; } if (MoveBillCreater.CheckIsNeedSyncMoveBill(lastMoveBillMaster.WarehouseCode)) { MoveBillCreater.CreateSyncMoveBillDetail(lastMoveBillMaster); } MoveBillMasterRepository.SaveChanges(); } catch (Exception e) { ps.State = StateType.Info; ps.Errors.Add(item.SortingLine.SortingLineName + "作业调度失败! 详情:" + e.Message); NotifyConnection(ps.Clone()); return; } } ps.State = StateType.Info; ps.Messages.Add("调度完成!"); NotifyConnection(ps.Clone()); }
public bool DownSortOrder(string beginDate, string endDate, out string errorInfo) { errorInfo = string.Empty; bool result = false; string sortOrderStrs = ""; string sortOrderList = ""; try { var sortOrderIds = SortOrderRepository.GetQueryable().Where(s => s.OrderID == s.OrderID).Select(s => new { s.OrderID }).ToArray(); for (int i = 0; i < sortOrderIds.Length; i++) { sortOrderStrs += sortOrderIds[i].OrderID + ","; } SortOrder[] SortOrders = SortingDownService.GetSortOrder(beginDate, endDate, sortOrderStrs); foreach (var item in SortOrders) { var sortOrder = new SortOrder(); sortOrder.OrderID = item.OrderID; sortOrder.CompanyCode = item.CompanyCode; sortOrder.SaleRegionCode = item.SaleRegionCode; sortOrder.OrderDate = item.OrderDate; sortOrder.OrderType = item.OrderType; sortOrder.CustomerCode = item.CustomerCode; sortOrder.CustomerName = item.CustomerName; sortOrder.DeliverLineCode = item.DeliverLineCode; sortOrder.QuantitySum = item.QuantitySum; sortOrder.AmountSum = item.AmountSum; sortOrder.DetailNum = item.DetailNum; sortOrder.DeliverOrder = item.DeliverOrder; sortOrder.DeliverDate = item.DeliverDate; sortOrder.Description = item.Description; sortOrder.IsActive = item.IsActive; sortOrder.UpdateTime = item.UpdateTime; SortOrderRepository.Add(sortOrder); sortOrderList += item.OrderID + ","; } if (sortOrderList != string.Empty) { SortOrderDetail[] SortOrderDetails = null; //SortingDownService.GetSortOrderDetail(sortOrderList); foreach (var detail in SortOrderDetails) { var sortOrderDetail = new SortOrderDetail(); var product = ProductRepository.GetQueryable().FirstOrDefault(p => p.ProductCode == detail.ProductCode); sortOrderDetail.OrderDetailID = detail.OrderDetailID; sortOrderDetail.OrderID = detail.OrderID; sortOrderDetail.ProductCode = detail.ProductCode; sortOrderDetail.ProductName = detail.ProductName; sortOrderDetail.UnitCode = detail.UnitCode; sortOrderDetail.UnitName = detail.UnitName; sortOrderDetail.DemandQuantity = detail.DemandQuantity * product.UnitList.Unit02.Count; sortOrderDetail.RealQuantity = detail.RealQuantity * product.UnitList.Unit02.Count; sortOrderDetail.Price = detail.Price; sortOrderDetail.Amount = detail.Amount; sortOrderDetail.UnitQuantity = product.UnitList.Quantity02; SortOrderDetailRepository.Add(sortOrderDetail); } } SortOrderRepository.SaveChanges(); result = true; } catch (Exception e) { errorInfo = "出错,原因:" + e.Message; } return(result); }
public void Dispatch(string connectionId, Model.ProgressState ps, CancellationToken cancellationToken, string workDispatchId, string userName) { Locker.LockKey = workDispatchId; ConnectionId = connectionId; ps.State = StateType.Start; NotifyConnection(ps.Clone()); IQueryable <SortOrderDispatch> sortOrderDispatchQuery = SortOrderDispatchRepository.GetQueryable(); IQueryable <SortOrder> sortOrderQuery = SortOrderRepository.GetQueryable(); IQueryable <SortOrderDetail> sortOrderDetailQuery = SortOrderDetailRepository.GetQueryable(); IQueryable <OutBillMaster> outBillMasterQuery = OutBillMasterRepository.GetQueryable(); IQueryable <OutBillDetail> outBillDetailQuery = OutBillDetailRepository.GetQueryable(); IQueryable <MoveBillMaster> moveBillMasterQuery = MoveBillMasterRepository.GetQueryable(); IQueryable <MoveBillDetail> moveBillDetailQuery = MoveBillDetailRepository.GetQueryable(); IQueryable <SortingLowerlimit> sortingLowerlimitQuery = SortingLowerlimitRepository.GetQueryable(); IQueryable <SortingLine> sortingLineQuery = SortingLineRepository.GetQueryable(); IQueryable <Storage> storageQuery = StorageRepository.GetQueryable(); IQueryable <SortWorkDispatch> sortWorkDispatchQuery = SortWorkDispatchRepository.GetQueryable(); IQueryable <THOK.Authority.DbModel.SystemParameter> systemParQuery = SystemParameterRepository.GetQueryable(); var IsUselowerlimit = systemParQuery.FirstOrDefault(s => s.ParameterName == "IsUselowerlimit"); //查询调度是否使用下限 0 否 1是 var isRoundingTray = systemParQuery.FirstOrDefault(s => s.ParameterName == "isRoundingTray"); //查询立式机总量大于20件的是否取整托盘 0表示不取整托盘。其它任意数字表示大于的总数 workDispatchId = workDispatchId.Substring(0, workDispatchId.Length - 1); int[] work = workDispatchId.Split(',').Select(s => Convert.ToInt32(s)).ToArray(); //调度表未作业的数据 var temp = sortOrderDispatchQuery.Join(sortOrderQuery, dp => new { dp.OrderDate, dp.DeliverLineCode }, om => new { om.OrderDate, om.DeliverLineCode }, (dp, om) => new { dp.ID, dp.WorkStatus, dp.OrderDate, dp.SortingLine, dp.DeliverLineCode, om.OrderID } ).Join(sortOrderDetailQuery, dm => new { dm.OrderID }, od => new { od.OrderID }, (dm, od) => new { dm.ID, dm.WorkStatus, dm.OrderDate, dm.SortingLine, od.Product, od.UnitCode, od.Price, od.RealQuantity } ).WhereIn(s => s.ID, work) .Where(s => s.WorkStatus == "1") .GroupBy(r => new { r.OrderDate, r.SortingLine, r.Product, r.UnitCode, r.Price }) .Select(r => new { r.Key.OrderDate, r.Key.SortingLine, r.Key.Product, r.Key.UnitCode, r.Key.Price, SumQuantity = r.Sum(p => p.RealQuantity * r.Key.Product.UnitList.Unit02.Count) }) .AsParallel() .GroupBy(r => new { r.OrderDate, r.SortingLine }) .Select(r => new { r.Key.OrderDate, r.Key.SortingLine, Products = r }) .OrderBy(s => s.SortingLine.SortingLineCode) //如果取整托盘多余的量是1号线就倒序排序,目前多余的量放入2号线,所以先调度一号线 .ToArray(); var temp1 = sortingLowerlimitQuery.GroupBy(r => new { r.Product, r.SortType }) .Select(s => new { s.Key.Product, s.Key.SortType }).ToArray(); var temp2 = sortOrderDispatchQuery.Join(sortOrderQuery, dp => new { dp.OrderDate, dp.DeliverLineCode }, om => new { om.OrderDate, om.DeliverLineCode }, (dp, om) => new { dp.ID, dp.WorkStatus, dp.OrderDate, om.OrderID } ).Join(sortOrderDetailQuery, dm => new { dm.OrderID }, od => new { od.OrderID }, (dm, od) => new { dm.ID, dm.WorkStatus, od.Product, od.RealQuantity } ).WhereIn(s => s.ID, work) .GroupBy(r => new { r.Product }) .Select(s => new { s.Key.Product, Quantity = s.Sum(p => p.RealQuantity * s.Key.Product.UnitList.Unit02.Count) }) .ToArray(); Dictionary <string, decimal> proQuan = new Dictionary <string, decimal>(); var employee = EmployeeRepository.GetQueryable().FirstOrDefault(i => i.UserName == userName); string operatePersonID = employee != null?employee.ID.ToString() : ""; if (employee == null) { ps.State = StateType.Error; ps.Errors.Add("未找到当前用户,或当前用户不可用!"); NotifyConnection(ps.Clone()); return; } decimal sumAllotQuantity = 0; decimal sumAllotLineQuantity = 0; MoveBillMaster lastMoveBillMaster = null; foreach (var item in temp) { try { if (cancellationToken.IsCancellationRequested) { return; } bool hasError = false; ps.State = StateType.Info; ps.Messages.Add("开始调度" + item.SortingLine.SortingLineName); NotifyConnection(ps.Clone()); //using (var scope = new TransactionScope()) //{ if (item.Products.Count() > 0) { if (cancellationToken.IsCancellationRequested) { return; } if (lastMoveBillMaster != null && lastMoveBillMaster.WarehouseCode != item.SortingLine.Cell.WarehouseCode) { if (MoveBillCreater.CheckIsNeedSyncMoveBill(lastMoveBillMaster.WarehouseCode)) { MoveBillCreater.CreateSyncMoveBillDetail(lastMoveBillMaster); } } sumAllotLineQuantity = 0; if (cancellationToken.IsCancellationRequested) { return; } MoveBillMaster moveBillMaster = MoveBillCreater.CreateMoveBillMaster(item.SortingLine.Cell.WarehouseCode, item.SortingLine.MoveBillTypeCode, operatePersonID); moveBillMaster.Origin = "2"; moveBillMaster.Description = item.SortingLine.SortingLineCode + " 分拣调度生成!"; lastMoveBillMaster = moveBillMaster; foreach (var product in item.Products.ToArray()) { if (product.SumQuantity > 0) { if (cancellationToken.IsCancellationRequested) { return; } decimal sumBillQuantity = temp.Sum(t => t.Products.Sum(p => p.SumQuantity)); sumAllotQuantity += product.SumQuantity; decimal sumBillProductQuantity = item.Products.Sum(p => p.SumQuantity); sumAllotLineQuantity += product.SumQuantity; ps.State = StateType.Processing; ps.TotalProgressName = "分拣作业调度"; ps.TotalProgressValue = (int)(sumAllotQuantity / sumBillQuantity * 100); ps.CurrentProgressName = "正在调度:" + item.SortingLine.SortingLineName; ps.CurrentProgressValue = (int)(sumAllotLineQuantity / sumBillProductQuantity * 100); NotifyConnection(ps.Clone()); if (cancellationToken.IsCancellationRequested) { return; } //获取分拣线下限数据 var sortingLowerlimitQuantity = sortingLowerlimitQuery.Where(s => s.ProductCode == product.Product.ProductCode && s.SortingLineCode == product.SortingLine.SortingLineCode); decimal lowerlimitQuantity = 0; if (sortingLowerlimitQuantity.Count() > 0) { lowerlimitQuantity = sortingLowerlimitQuantity.Sum(s => s.Quantity); } if (cancellationToken.IsCancellationRequested) { return; } //获取分拣备货区库存 var storageQuantity = storageQuery.Where(s => s.ProductCode == product.Product.ProductCode) .Join(sortingLineQuery, s => s.Cell, l => l.Cell, (s, l) => new { l.SortingLineCode, s.Quantity } ) .Where(r => r.SortingLineCode == product.SortingLine.SortingLineCode); decimal storQuantity = 0; if (storageQuantity.Count() > 0) { storQuantity = storageQuantity.Sum(s => s.Quantity); } //获取当前这个卷烟库存数量 string[] areaTypes = new string[] { "1", "2", "4" }; var areaSumQuantitys = storageQuery.Where(s => areaTypes.Any(t => t == s.Cell.Area.AreaType) && s.ProductCode == product.Product.ProductCode).ToArray(); decimal areaQuantiy = 0; if (areaSumQuantitys.Count() > 0) { areaQuantiy = areaSumQuantitys.Sum(s => s.Quantity - s.OutFrozenQuantity); } //是否使用下限 if (IsUselowerlimit != null && IsUselowerlimit.ParameterValue == "0") { lowerlimitQuantity = 0; } if (cancellationToken.IsCancellationRequested) { return; } //获取移库量(按整件计)出库量加上下限量减去备货区库存量取整 decimal quantity = 0; quantity = Math.Ceiling((product.SumQuantity + lowerlimitQuantity - storQuantity) / product.Product.Unit.Count) * product.Product.Unit.Count; //立式机大于20件的取整托盘 //查询这个卷烟是否是立式机的卷烟 if (isRoundingTray != null && Convert.ToInt32(isRoundingTray.ParameterValue) > 0) { var temp3 = temp1.FirstOrDefault(s => s.Product.ProductCode == product.Product.ProductCode && s.SortType == "1"); if (temp3 != null && temp.Count() >= 2 && quantity > 0) { //查询这个订单在分拣当中是否存在.大于20件取整托盘, var SumlowerlimitQuantity = temp2.FirstOrDefault(s => s.Product.ProductCode == temp3.Product.ProductCode); if (SumlowerlimitQuantity != null && SumlowerlimitQuantity.Quantity > (Convert.ToInt32(isRoundingTray.ParameterValue) * product.Product.Unit.Count)) { decimal WholeCare = 0; //托盘数 decimal SumSortingQuantity = 0; //整托盘的数量 decimal Quantity1 = 0; decimal Quantity2 = 0; //取2条线数量总和取整托盘 WholeCare = Math.Ceiling(SumlowerlimitQuantity.Quantity / (product.Product.CellMaxProductQuantity * product.Product.Unit.Count)); SumSortingQuantity = Convert.ToDecimal(WholeCare * (product.Product.Unit.Count * product.Product.CellMaxProductQuantity)); if (item.SortingLine.SortingLineCode == "1") { //总订单量减去当前分拣线订单量,这里是另一条线的量 Quantity1 = SumlowerlimitQuantity.Quantity - product.SumQuantity; if (Quantity1 > 0) { //整托盘数量减去另一条线的量, Quantity1 = Math.Ceiling(Quantity1 / product.Product.Unit.Count) * product.Product.Unit.Count; Quantity2 = SumSortingQuantity - Quantity1; if (Quantity2 >= quantity) { quantity = Quantity2; proQuan.Add(product.Product.ProductCode, SumSortingQuantity - quantity); } } } else { if (proQuan.Keys.Contains(product.Product.ProductCode)) { if (proQuan[product.Product.ProductCode] >= quantity) { quantity = proQuan[product.Product.ProductCode]; } } } } } } //取整托盘,查询这个卷烟是否是通道机的卷烟 decimal wholeTray = 0; var temp4 = temp1.FirstOrDefault(s => s.Product.ProductCode == product.Product.ProductCode && s.SortType == "2"); if (product.Product.IsRounding == "2" || temp4 != null) { wholeTray = Math.Ceiling(quantity / (product.Product.CellMaxProductQuantity * product.Product.Unit.Count)); quantity = Convert.ToDecimal(wholeTray * (product.Product.Unit.Count * product.Product.CellMaxProductQuantity)); } if (areaQuantiy < quantity) //判断当前这个卷烟库存是否小于移库量 { //出库量减去备货区库存量取整 quantity = Math.Ceiling((product.SumQuantity - storQuantity) / product.Product.Unit.Count) * product.Product.Unit.Count; if (areaQuantiy < quantity) { //出库量减去备货区库存量 quantity = product.SumQuantity - storQuantity; } } //不取整的烟直接出库。 if (product.Product.IsRounding == "1") { quantity = product.SumQuantity - storQuantity; } if (quantity > 0) { if (cancellationToken.IsCancellationRequested) { return; } AlltoMoveBill(moveBillMaster, product.Product, item.SortingLine.Cell, ref quantity, cancellationToken, ps, item.SortingLine.Cell.CellCode); } if (quantity > 0) { //生成移库不完整,可能是库存不足; hasError = true; ps.State = StateType.Error; ps.Errors.Add(item.SortingLine.SortingLineCode + "线," + product.Product.ProductCode + " " + product.Product.ProductName + ",库存不足!当前总量:" + Convert.ToDecimal(product.SumQuantity / product.Product.UnitList.Unit02.Count) + "(条),缺少:" + Convert.ToDecimal(quantity / product.Product.UnitList.Unit02.Count) + "(条)"); NotifyConnection(ps.Clone()); } } } if (!hasError) { if (cancellationToken.IsCancellationRequested) { return; } OutBillMaster outBillMaster = OutBillCreater.CreateOutBillMaster(item.SortingLine.Cell.WarehouseCode, item.SortingLine.OutBillTypeCode, operatePersonID); outBillMaster.Origin = "2"; outBillMaster.Description = item.SortingLine.SortingLineCode + " 分拣调度生成!"; //添加出库单细单 foreach (var product in item.Products.ToArray()) { if (cancellationToken.IsCancellationRequested) { return; } OutBillCreater.AddToOutBillDetail(outBillMaster, product.Product, product.Price, product.SumQuantity); } if (cancellationToken.IsCancellationRequested) { return; } //添加出库、移库主单和作业调度表 SortWorkDispatch sortWorkDisp = AddSortWorkDispMaster(moveBillMaster, outBillMaster, item.SortingLine.SortingLineCode, item.OrderDate); //修改线路调度作业状态和作业ID var sortDispTemp = sortOrderDispatchQuery.WhereIn(s => s.ID, work) .Where(s => s.OrderDate == item.OrderDate && s.SortingLineCode == item.SortingLine.SortingLineCode); foreach (var sortDisp in sortDispTemp.ToArray()) { if (cancellationToken.IsCancellationRequested) { return; } sortDisp.SortWorkDispatchID = sortWorkDisp.ID; sortDisp.WorkStatus = "2"; } if (cancellationToken.IsCancellationRequested) { return; } SortWorkDispatchRepository.SaveChanges(); //scope.Complete(); ps.Messages.Add(item.SortingLine.SortingLineName + " 调度成功!"); } else { ps.State = StateType.Info; ps.Messages.Add(item.SortingLine.SortingLineName + " 调度失败!"); NotifyConnection(ps.Clone()); return; } } //} } catch (Exception e) { ps.State = StateType.Info; ps.Errors.Add(item.SortingLine.SortingLineName + "作业调度失败! 详情:" + e.Message); NotifyConnection(ps.Clone()); return; } } if (cancellationToken.IsCancellationRequested) { return; } if (MoveBillCreater.CheckIsNeedSyncMoveBill(lastMoveBillMaster.WarehouseCode)) { MoveBillCreater.CreateSyncMoveBillDetail(lastMoveBillMaster); } MoveBillMasterRepository.SaveChanges(); ps.State = StateType.Info; ps.Messages.Add("调度完成!"); NotifyConnection(ps.Clone()); }