public void SetActualMachine(int id) { var context = GetContext(); if (context.ActualMachine.Id != id) { context.ActualMachine = context.AllMachines.FirstOrDefault(w => w.Id == id); if (context.ActualMachine?.LastUpdate != null) { var lastUpdate = context.ActualMachine.LastUpdate.Value; context.ActualPeriod = PeriodService.GetPeriodModel(lastUpdate.Date, lastUpdate, enAggregation.Day); } } SetContext(context); }
public bool InitializeMachineLevel(int?machineId = null) { var context = GetContext(); context.ActualPage = enPage.Machine; if (context.User.Role == enRole.Administrator) { context.AllPlants = _mesService.GetAllPlantsMachines(); } else if (context.User.Role == enRole.Demo) { context.AllPlants = _mesService.GetAllPlantsRole(enRole.Demo); } else { context.AllPlants = _mesService.GetUserPlants(context.User.ID); } if (context.AllPlants.Any()) { if (machineId != null) { var machine = _machineService.GetMachineInfo(machineId.Value); if (machine == null) { return(false); } context.ActualPlant = context.AllPlants.FirstOrDefault(w => w.Id == machine.PlantId); if (context.User.Role == enRole.Administrator) { context.AllMachines = _machineService.GetAllMachines(); } else if (context.User.Role == enRole.Demo) { context.AllMachines = _machineService.GetRoleMachines(enRole.Demo); } else { context.AllMachines = _machineService.GetUserMachines(context); } if (context.AllMachines.Count == 0) { return(false); } context.ActualMachine = context.AllMachines.FirstOrDefault(w => w.Id == machineId.Value); } else { if (context.ActualPlant == null) { context.ActualPlant = context.AllPlants.FirstOrDefault(); } if (context.User.Role == enRole.Administrator) { context.AllMachines = _machineService.GetAllMachines(); } else if (context.User.Role == enRole.Demo) { context.AllMachines = _machineService.GetRoleMachines(enRole.Demo); } else { context.AllMachines = _machineService.GetUserMachines(context); } if (context.AllMachines.Count == 0) { return(false); } if (context.ActualMachine == null) { context.ActualMachine = context.AllMachines.FirstOrDefault(); } } } else { if (context.User.Role == enRole.Assistance || context.User.Role == enRole.RandD) { if (context.AssistanceMachineId != null) { var all = new List <MachineInfoModel>() { _machineService.GetMachineInfo((int)context.AssistanceMachineId) }; context.AllMachines = all; } } else { context.AllMachines = _machineService.GetUserMachines(context); } if (context.AllMachines.Count == 0) { return(false); } if (machineId != null) { context.ActualMachine = context.AllMachines.FirstOrDefault(w => w.Id == machineId.Value); } else { context.ActualMachine = context.AllMachines.FirstOrDefault(); } } if (context.ActualMachine?.LastUpdate != null) { context.ActualPeriod = PeriodService.GetPeriodModel(context.ActualMachine?.LastUpdate?.Date ?? DateTime.UtcNow.Date, context.ActualMachine?.LastUpdate?.Date.AddDays(1).AddTicks(-1) ?? DateTime.UtcNow.Date.AddDays(1).AddTicks(-1), enAggregation.Day); } SetContext(context); return(true); }