public object GetArrangedTaskOrderPutAndWarehouse([FromQuery] int wId, DateTime startTime, DateTime endTime, DateTime deliveryTime, bool all) { var result = new SmartTaskOrderNeedOrderTimeResult(); if (startTime == default(DateTime) || endTime == default(DateTime)) { return(result); } var schedules = SmartTaskOrderScheduleHelper.GetSmartTaskOrderSchedule(wId, startTime, endTime); var tasks = SmartTaskOrderHelper.GetAllArrangedButNotDoneSmartTaskOrderDetails(wId, deliveryTime, all); var taskIds = tasks.Select(x => x.Id).Concat(schedules.Select(y => y.TaskOrderId)).Distinct(); if (!taskIds.Any()) { return(result); } tasks = SmartTaskOrderHelper.GetAllArrangedButNotDoneSmartTaskOrderDetails(wId, taskIds); var taskNeeds = SmartTaskOrderNeedHelper.GetSmartTaskOrderNeedsByTaskOrderIds(wId, taskIds, true); var orders = taskNeeds.GroupBy(y => new { y.PId, y.Order, y.Process, y.CategoryId }).Select(z => new SmartTaskOrderNeedWithOrder { Id = z.Key.PId, Process = z.Key.Process, CategoryId = z.Key.CategoryId, Order = z.Key.Order }); result.StartTime = startTime; result.EndTime = endTime; result.Orders.AddRange(orders.OrderBy(z => z.Order)); //设备型号数量 var deviceList = SmartDeviceHelper.GetNormalSmartDevices(wId); //人员等级数量 var operatorList = SmartOperatorHelper.GetNormalSmartOperators(wId); var modelCount = deviceList.GroupBy(x => new { x.CategoryId }).Select(y => new SmartDeviceModelCount { CategoryId = y.Key.CategoryId, Count = y.Count() }); var operatorCount = operatorList.GroupBy(x => new { x.ProcessId }).Select(y => new SmartOperatorCount { ProcessId = y.Key.ProcessId, Count = y.Count() }); //if (schedules.Any()) //{ // var productIds = schedules.Select(x => x.ProductId); // // 任务单计划号 // var products = SmartProductHelper.Instance.GetByIds<SmartProduct>(productIds); // var capacityIds = products.Select(x => x.CapacityId); // // 产能设置 // var smartCapacityLists = SmartCapacityListHelper.Instance.GetAllSmartCapacityListsWithOrder(capacityIds); // //if () // //{ // //} // ////产能配置 // //var capacityList = smartCapacityLists.Where(x => x.CapacityId == task.CapacityId); // ////工序单日产能配置 // //var cList = capacityList.FirstOrDefault(x => x.ProcessId == y.ProcessId); // //sc.Index //} var ts = taskNeeds.Where(n => tasks.Any(task => task.Id == n.TaskOrderId)).Select(x => { var task = tasks.First(t => t.Id == x.TaskOrderId); var need = new SmartTaskOrderScheduleSumInfoResult { Id = x.Id, TaskOrderId = x.TaskOrderId, TaskOrder = x.TaskOrder, ProductId = x.ProductId, Product = x.Product, DeliveryTime = task.DeliveryTime, ArrangedTime = task.ArrangedTime, PId = x.PId, ProcessId = x.ProcessId, Process = x.Process, Order = x.Order, Put = x.Put, HavePut = x.HavePut, Target = x.Target, DoneTarget = x.DoneTarget, }; var schedule = schedules.Where(sc => sc.TaskOrderId == need.TaskOrderId && sc.ProcessId == need.ProcessId && sc.PId == need.PId).ToList(); for (var i = 0; i < (result.EndTime - result.StartTime).TotalDays + 1; i++) { var t = result.StartTime.AddDays(i); if (schedule.All(p => p.ProcessTime != t)) { schedule.Add(new SmartTaskOrderScheduleDetail() { ProcessTime = t }); } } schedule = schedule.OrderBy(sc => sc.ProcessTime).ToList(); need.Schedules.AddRange(schedule.Select(y => new SmartTaskOrderScheduleInfoResult { Id = y.Id, ProductType = y.ProductType, ProcessTime = y.ProcessTime, Put = y.Put, HavePut = y.HavePut, Target = y.Target, DoneTarget = y.DoneTarget, })); return(need); }); result.datas.AddRange(ts.OrderBy(x => x.DeliveryTime).ThenBy(x => x.ArrangedTime)); var indexes = SmartTaskOrderScheduleIndexHelper.GetSmartTaskOrderScheduleIndex(wId, startTime, endTime).ToList(); var arrangeIndexes = new List <SmartTaskOrderScheduleIndex>(); foreach (var order in result.Orders) { for (var i = 0; i < (result.EndTime - result.StartTime).TotalDays + 1; i++) { var t = result.StartTime.AddDays(i); if (!indexes.Any(p => p.ProcessTime == t && p.PId == order.Id)) { arrangeIndexes.Add(new SmartTaskOrderScheduleIndex { ProcessTime = t, PId = order.Id, }); } else { var ins = indexes.Where(p => p.ProcessTime == t && p.PId == order.Id); decimal index = 0; if (ins.Any()) { var cnt = ins.First().ProductType == 0 ? (modelCount.FirstOrDefault(x => x.CategoryId == order.CategoryId)?.Count ?? 0) : (operatorCount.FirstOrDefault(x => x.ProcessId == order.Id)?.Count ?? 0); index = cnt == 0 ? 0 : (ins.Sum(x => x.Index) / cnt).ToRound(2); } arrangeIndexes.Add(new SmartTaskOrderScheduleIndex { ProcessTime = t, PId = order.Id, Index = index }); } } } result.Indexes.AddRange(arrangeIndexes.OrderBy(x => result.Orders.FirstOrDefault(y => y.Id == x.PId)?.Order ?? 0).ThenBy(z => z.ProcessTime)); return(result); }
public object PostSmartSchedulePreview([FromBody] IEnumerable <SmartTaskOrderCapacity> taskOrders) { if (taskOrders == null) { return(Result.GenError <Result>(Error.ParamError)); } var taskIds = taskOrders.GroupBy(x => x.Id).Select(y => y.Key); if (taskIds.Count() != taskOrders.Count()) { return(Result.GenError <Result>(Error.SmartTaskOrderDuplicate)); } var result = new SmartTaskOrderNeedWithOrderPreviewResult(); var wId = taskOrders.FirstOrDefault()?.WorkshopId ?? 0; if (taskOrders.Any()) { var tOrders = SmartTaskOrderHelper.Instance.GetByIds <SmartTaskOrderCapacity>(taskIds); if (taskIds.Count() != tOrders.Count()) { result.errno = Error.SmartTaskOrderNotExist; result.datas.AddRange(taskOrders.Where(x => tOrders.All(y => y.Id != x.Id)).Select(x => x.TaskOrder.IsNullOrEmpty() ? x.Id.ToString() : x.TaskOrder)); return(result); } foreach (var task in taskOrders) { var t = tOrders.FirstOrDefault(x => x.Id == task.Id); if (t == null) { result.errno = Error.SmartTaskOrderNotExist; result.datas.Add(task.TaskOrder); } else { task.TaskOrder = t.TaskOrder; task.Target = t.Target; task.DeliveryTime = t.DeliveryTime; task.ProductId = t.ProductId; task.CapacityId = t.CapacityId; } } } if (result.errno != Error.Success) { return(result); } var productIds = taskOrders.GroupBy(x => x.ProductId).Select(y => y.Key); if (!productIds.Any()) { return(Result.GenError <Result>(Error.SmartProductNotExist)); } var products = SmartProductHelper.Instance.GetByIds <SmartProduct>(productIds); if (products.Count() != productIds.Count()) { return(Result.GenError <Result>(Error.SmartProductNotExist)); } var productCapacities = SmartProductCapacityHelper.GetSmartProductCapacities(productIds); if (!productCapacities.Any()) { return(Result.GenError <Result>(Error.SmartProductCapacityNotExist)); } var capacityIds = products.GroupBy(x => x.CapacityId).Select(y => y.Key); if (!capacityIds.Any()) { return(Result.GenError <Result>(Error.SmartCapacityNotExist)); } var capacityLists = SmartCapacityListHelper.GetSmartCapacityListsWithOrder(capacityIds); foreach (var productId in productIds) { var tasks = taskOrders.Where(x => x.ProductId == productId); var product = products.FirstOrDefault(x => x.Id == productId); var pCapacities = productCapacities.Where(x => x.ProductId == productId); var cLists = capacityLists.Where(x => x.CapacityId == product.CapacityId); if (cLists.Count() != pCapacities.Count()) { result.errno = Error.SmartProductCapacityNotExist; result.datas.AddRange(tasks.Select(x => x.TaskOrder)); } } if (result.errno != Error.Success) { return(result); } //设备型号 var deviceModels = SmartDeviceModelHelper.Instance.GetAll <SmartDeviceModel>(); //设备型号数量 var deviceList = SmartDeviceHelper.GetNormalSmartDevices(wId); var modelCounts = deviceList.GroupBy(x => new { x.CategoryId, x.ModelId }).Select(y => new SmartDeviceModelCount { CategoryId = y.Key.CategoryId, ModelId = y.Key.ModelId, Count = y.Count() }); //人员等级 var operatorLevels = SmartOperatorLevelHelper.Instance.GetAll <SmartOperatorLevel>(); //人员等级数量 var operatorList = SmartOperatorHelper.GetNormalSmartOperators(wId); var operatorCounts = operatorList.GroupBy(x => new { x.ProcessId, x.LevelId }).Select(y => new SmartOperatorCount { ProcessId = y.Key.ProcessId, LevelId = y.Key.LevelId, Count = y.Count() }); var taskNeeds = SmartTaskOrderNeedHelper.GetSmartTaskOrderNeedsByTaskOrderIds(wId, taskIds); foreach (var task in taskOrders) { var needs = taskNeeds.Where(need => need.TaskOrderId == task.Id); if (task.Needs.Any()) { task.Needs = task.Needs.Select(x => { var need = needs.FirstOrDefault(y => y.TaskOrderId == x.TaskOrderId && y.PId == x.PId); if (need != null) { x.DoneTarget = need.DoneTarget; x.HavePut = need.HavePut; } return(x); }).ToList(); } var oldNeeds = task.Needs.ToList(); task.Needs.Clear(); var productId = task.ProductId; var product = products.FirstOrDefault(x => x.Id == productId); task.Product = product.Product; var pCapacities = productCapacities.Where(x => x.ProductId == productId); var cLists = capacityLists.Where(x => x.CapacityId == product.CapacityId); var target = task.Target; foreach (var cList in cLists.Reverse()) { var need = oldNeeds.FirstOrDefault(x => x.Id == cList.Id); var stock = need?.Stock ?? 0; var doneTarget = need?.DoneTarget ?? 0; var havePut = need?.HavePut ?? 0; if (target < stock) { stock = target; target = 0; } else { target -= stock; } if (!task.All) { target = target > doneTarget ? target - doneTarget : 0; } var pCapacity = pCapacities.FirstOrDefault(x => x.ProcessId == cList.ProcessId); var rate = 0m; var y = pCapacity; if (y.DeviceList.Any()) { rate = y.DeviceList.First().Rate; } else if (y.OperatorList.Any()) { rate = y.OperatorList.First().Rate; } var put = rate != 0 ? (int)Math.Ceiling((target) * 100 / rate) : 0; var newNeed = new SmartTaskOrderCapacityNeed { Id = cList.Id, TaskOrderId = task.Id, ProductId = productId, ProcessId = cList.ProcessId, CategoryId = cList.CategoryId, PId = cList.PId, Target = target, DoneTarget = doneTarget, Stock = stock, Rate = rate, Put = put, HavePut = havePut, Process = cList.Process, Order = cList.Order }; if (need != null) { newNeed.Devices = need.Devices; newNeed.NeedDCapacity = newNeed.DCapacity != 0 ? ((decimal)put / newNeed.DCapacity).ToRound() : 0; foreach (var device in need.DeviceList) { var modelCount = modelCounts.FirstOrDefault(x => x.ModelId == device.Key)?.Count ?? 0; newNeed.TotalDCapacity += modelCount * device.Value.Item1 * device.Value.Item2; } newNeed.HaveDCapacity = newNeed.DCapacity != 0 ? ((decimal)newNeed.TotalDCapacity / newNeed.DCapacity).ToRound() : 0; newNeed.Operators = need.Operators; newNeed.NeedOCapacity = newNeed.OCapacity != 0 ? ((decimal)put / newNeed.OCapacity).ToRound() : 0; foreach (var op in need.OperatorsList) { var operatorCount = operatorCounts.FirstOrDefault(x => x.LevelId == op.Key)?.Count ?? 0; newNeed.TotalOCapacity += operatorCount * op.Value.Item1 * op.Value.Item2; } newNeed.HaveOCapacity = newNeed.OCapacity != 0 ? ((decimal)newNeed.TotalOCapacity / newNeed.OCapacity).ToRound() : 0; } task.Needs.Insert(0, newNeed); target = put; } } result.datas.AddRange(taskOrders); var orders = taskOrders.SelectMany(x => x.Needs).GroupBy(y => new { y.PId, y.Order, y.Process }).Select(z => new SmartTaskOrderNeedWithOrderPreview { Id = z.Key.PId, Process = z.Key.Process, Order = z.Key.Order }).ToList(); foreach (var task in taskOrders) { foreach (var newNeed in task.Needs) { var order = orders.FirstOrDefault(x => x.Id == newNeed.PId); if (order != null) { order.Stock += newNeed.Stock; order.Put += newNeed.Put; order.Target += newNeed.Target; if (newNeed.DeviceList.Any()) { foreach (var device in newNeed.DeviceList) { var modelDevice = order.Devices.FirstOrDefault(x => x.Id == device.Key); if (modelDevice == null) { order.Devices.Add(new SmartTaskOrderNeedCapacityPreview { Id = device.Key, Name = deviceModels.FirstOrDefault(x => x.Id == device.Key)?.Model ?? "", }); } modelDevice = order.Devices.FirstOrDefault(x => x.Id == device.Key); modelDevice.NeedCapacity += newNeed.NeedDCapacity; var modelCount = modelCounts.FirstOrDefault(x => x.ModelId == device.Key)?.Count ?? 0; modelDevice.HaveCapacity = modelCount; } } if (newNeed.OperatorsList.Any()) { foreach (var op in newNeed.OperatorsList) { var levelOp = order.Operators.FirstOrDefault(x => x.Id == op.Key); if (levelOp == null) { order.Operators.Add(new SmartTaskOrderNeedCapacityPreview { Id = op.Key, Name = operatorLevels.FirstOrDefault(x => x.Id == op.Key)?.Level ?? "", }); } levelOp = order.Operators.FirstOrDefault(x => x.Id == op.Key); levelOp.NeedCapacity += newNeed.NeedOCapacity; var operatorCount = operatorCounts.FirstOrDefault(x => newNeed.PId == x.ProcessId && x.LevelId == op.Key)?.Count ?? 0; levelOp.HaveCapacity = operatorCount; } } } } } result.Orders.AddRange(orders.OrderBy(z => z.Order).Select(x => { x.Devices = x.Devices.OrderBy(y => y.Id).ToList(); x.Operators = x.Operators.OrderBy(y => y.Id).ToList(); return(x); })); return(result); }
public object GetArrangedTaskOrderPutIndex([FromQuery] int wId, DateTime time, int pId) { var result = new DataResult(); var data = SmartTaskOrderScheduleIndexHelper.GetSmartTaskOrderScheduleIndex(wId, time, default(DateTime), pId) .Select(ClassExtension.ParentCopyToChild <SmartTaskOrderScheduleIndex, SmartTaskOrderScheduleIndexDetail>).Where(x => x.Index > 0).ToList(); if (!data.Any()) { return(result); } //设备型号数量 var deviceList = SmartDeviceHelper.GetNormalSmartDevices(wId); //人员等级数量 var operatorList = SmartOperatorHelper.GetNormalSmartOperators(wId); var modelCount = deviceList.GroupBy(x => new { x.CategoryId, x.ModelId }).Select(y => new SmartDeviceModelCount { CategoryId = y.Key.CategoryId, ModelId = y.Key.ModelId, Count = y.Count() }); var operatorCount = operatorList.GroupBy(x => new { x.ProcessId, x.LevelId }).Select(y => new SmartOperatorCount { ProcessId = y.Key.ProcessId, LevelId = y.Key.LevelId, Count = y.Count() }); var order = SmartProcessHelper.Instance.Get <SmartProcess>(pId); var productType = data.First().ProductType; if (productType == 0) { var devices = deviceList.Where(x => x.CategoryId == order.CategoryId); foreach (var device in devices) { if (data.All(p => p.DealId != device.Id)) { data.Add(new SmartTaskOrderScheduleIndexDetail { WorkshopId = wId, ProductType = productType, ProcessTime = time, PId = pId, DealId = device.Id }); } } } else if (productType == 1) { var operators = operatorList.Where(x => x.ProcessId == order.Id); foreach (var op in operators) { if (data.All(p => p.DealId != op.Id)) { data.Add(new SmartTaskOrderScheduleIndexDetail { WorkshopId = wId, ProductType = productType, ProcessTime = time, PId = pId, DealId = op.Id }); } } } foreach (var x in data) { if (x.ProductType == 0) { x.Code = deviceList.FirstOrDefault(z => z.Id == x.DealId)?.Code ?? ""; } else if (x.ProductType == 1) { x.Name = operatorList.FirstOrDefault(z => z.Id == x.DealId)?.Name ?? ""; } } result.datas.AddRange(data); return(result); }