private List <ProductionOrderWorkSchedule> CalculateWorkTimeWithParents(List <ProductionOrderWorkSchedule> schedules) { foreach (var schedule in schedules) { schedule.WorkTimeWithParents = GetRemainTimeFromParents(schedule); } _context.UpdateRange(schedules); _context.SaveChanges(); return(schedules); }
public Task <bool> DoAtStart(int time) { var pows = _context.ProductionOrderWorkSchedules.Single(a => a.Id == ProductionOrderWorkScheduleId); pows.ProducingState = ProducingState.Producing; var demandProvider = _context.ProductionOrderWorkSchedules.Single(a => a.Id == ProductionOrderWorkScheduleId).ProductionOrder .DemandProviderProductionOrders; if (demandProvider != null && demandProvider.Any()) { foreach (var provider in demandProvider) { provider.State = State.Producing; } _context.ProductionOrderWorkSchedules.Update(pows); } if (HasHierarchyChildren(pows)) { _context.SaveChanges(); return(null); } //set bom to be finished var boms = _context.ProductionOrderBoms.Where(a => a.ProductionOrderParentId == ProductionOrderId); foreach (var bom in boms) { bom.State = State.Finished; var requester = bom.DemandProductionOrderBoms; foreach (var req in requester.Where(a => a.State != State.Finished)) { //find DemandProviderStock to set them ready foreach (var provider in req.DemandProvider.OfType <DemandProviderStock>()) { provider.State = State.Finished; } req.State = State.Finished; } } _context.UpdateRange(boms); var pobs = _context.ProductionOrderBoms.Where(a => a.ProductionOrderParentId == ProductionOrderId); foreach (var pob in pobs) { foreach (var dpob in pob.DemandProductionOrderBoms) { var stock = _context.Stocks.Single(a => a.ArticleForeignKey == dpob.ArticleId); stock.Current -= dpob.Quantity; _context.StockExchanges.Add(new StockExchange() { ExchangeType = ExchangeType.Withdrawal, Quantity = dpob.Quantity, StockId = stock.Id, RequiredOnTime = _context.ProductionOrders.Single(a => a.Id == ProductionOrderId).Duetime, Time = time }); _context.Stocks.Update(stock); _context.SaveChanges(); } } _context.SaveChanges(); return(null); }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="simulationId"></param> /// <param name="simulationType"></param> /// <param name="simulationNumber"></param> /// <param name="final"></param> /// <param name="time"></param> public static void CalculateMachineUtilization(ProductionDomainContext context, int simulationId, SimulationType simulationType, int simulationNumber, bool final, int time) { var simConfig = context.SimulationConfigurations.Single(a => a.Id == simulationId); //get working time var content = final ? context.SimulationWorkschedules.Where(a => a.Start >= simConfig.SettlingStart && a.End <= simConfig.SimulationEndTime && a.End >= simConfig.SettlingStart && a.SimulationNumber == simulationNumber && a.SimulationType == simulationType && a.SimulationConfigurationId == simulationId) .Select(x => new { x.Start, x.End, x.Machine }).Distinct().ToList() : context.SimulationWorkschedules.Where(a => a.Start >= time - simConfig.DynamicKpiTimeSpan && a.End <= time && a.SimulationNumber == simulationNumber && a.SimulationType == simulationType && a.SimulationConfigurationId == simulationId) .Select(x => new { x.Start, x.End, x.Machine }).Distinct().ToList(); //get SimulationTime var simulationTime = final ? simConfig.SimulationEndTime - simConfig.SettlingStart : simConfig.DynamicKpiTimeSpan; var kpis = content.GroupBy(x => x.Machine).Select(g => new Kpi() { Value = Math.Round((double)(g.Sum(x => x.End) - g.Sum(x => x.Start)) / simulationTime, 2), Name = g.Key, IsKpi = final, KpiType = KpiType.MachineUtilization, SimulationConfigurationId = simulationId, SimulationType = simulationType, SimulationNumber = simulationNumber, Time = time, IsFinal = final }).ToList(); context.Kpis.AddRange(kpis); context.SaveChanges(); if (!final) { return; } var allKpis = context.Kpis.Where(a => a.KpiType == KpiType.MachineUtilization && a.SimulationConfigurationId == simulationId && a.SimulationNumber == simulationNumber && a.SimulationType == simulationType && !a.IsFinal && a.Time > simConfig.SettlingStart && a.Time < simConfig.SimulationEndTime).ToList(); //for each machine for (var i = 0; i < kpis.Count(); i++) { var list = allKpis.Where(a => a.Name == kpis[i].Name).ToList(); if (list.Count == 0 || list.Count == 1) { continue; } kpis[i].Count = list.Sum(item => Math.Pow(item.Value - kpis[i].Value, 2)) / (list.Count - 1.00); kpis[i].ValueMin = kpis[i].Count / list.Count; } context.UpdateRange(kpis); context.SaveChanges(); }