public void UpdateAssociations(Plant plant) { try { var pl = _ctx.Plants .Include(x => x.SectorsPlants) .ThenInclude(x => x.JobsSectorsPlants) .Where(x => x.PlantID == plant.PlantID) .FirstOrDefault(); var listSectorsPlants = new List <SectorsPlants>(); foreach (var sector in plant.Sectors) { var sectorPlant = pl.SectorsPlants.Where(x => x.SectorID == sector.Id && x.PlantID == plant.PlantID).FirstOrDefault(); if (sectorPlant == null) { sectorPlant = new SectorsPlants() { Plant = pl, Sector = _ctx.Sectors.Where(x => x.SectorID == sector.Id).FirstOrDefault(), JobsSectorsPlants = new List <JobsSectorsPlants>() }; } if (sector.jobs != null && sector.jobs.Count > 0) { sectorPlant.JobsSectorsPlants = this.GetUpdatedJobsOfSector(sector.jobs, sectorPlant); } listSectorsPlants.Add(sectorPlant); } pl.SectorsPlants = listSectorsPlants; _ctx.Update(pl); _ctx.SaveChanges(); } catch (DbUpdateException ex) { throw new DbUpdateException("No puede actualizarse ya que existen registros asociados a la Planta/Sector/Puesto", ex); } catch (Exception ex) { throw new Exception("No puede actualizarse ya que existen registros asociados a la Planta/Sector/Puesto", ex); } }
public GetUpdatedJobsOfSectorTest() { var optionsBuilder = new DbContextOptionsBuilder <SQLHoshinCoreContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; _ctx = new SQLHoshinCoreContext(optionsBuilder); jobs = new List <Domain.Job.Job>(); sectorsPlants = new SectorsPlants(); jobsDbSet = new List <Jobs> { new Jobs { JobID = 1 }, new Jobs { JobID = 2 }, new Jobs { JobID = 3 }, new Jobs { JobID = 4 }, new Jobs { JobID = 5 }, new Jobs { JobID = 6 }, new Jobs { JobID = 7 }, new Jobs { JobID = 8 }, new Jobs { JobID = 9 }, new Jobs { JobID = 10 } }; _ctx.Jobs.AddRange(jobsDbSet); _ctx.SaveChanges(); sectorsPlants.JobsSectorsPlants = new List <JobsSectorsPlants>(); sectorsPlants.JobsSectorsPlants.Add(new JobsSectorsPlants { PlantID = 1, SectorID = 1, JobID = 1, }); sectorsPlants.JobsSectorsPlants.Add(new JobsSectorsPlants { PlantID = 1, SectorID = 1, JobID = 2, }); sectorsPlants.JobsSectorsPlants.Add(new JobsSectorsPlants { PlantID = 1, SectorID = 1, JobID = 3, }); sectorsPlants.JobsSectorsPlants.Add(new JobsSectorsPlants { PlantID = 1, SectorID = 1, JobID = 4, }); }
public ICollection <JobsSectorsPlants> GetUpdatedJobsOfSector(List <Domain.Job.Job> jobs, SectorsPlants sectorsPlants) { var jobsId = jobs.Select(y => y.Id).ToList(); var existingJobs = sectorsPlants.JobsSectorsPlants.Where(x => jobsId.Contains(x.JobID)); var jobsToAdd = jobsId.Except(existingJobs.Select(x => x.JobID)); var jobsAdded = new List <JobsSectorsPlants>(); foreach (var job in jobsToAdd) { var jobSectorPlant = new JobsSectorsPlants { Job = _ctx.Jobs.Where(x => x.JobID == job).FirstOrDefault(), SectorPlant = sectorsPlants }; jobsAdded.Add(jobSectorPlant); } var allJobsOfSector = jobsAdded.Union(existingJobs); return(allJobsOfSector.ToList()); }
public bool Update(Audit audit) { using (var scope = _serviceProvider.CreateScope()) { var _mapper = scope.ServiceProvider.GetService(typeof(IMapper)) as IMapper; var _ctx = scope.ServiceProvider.GetService(typeof(SQLHoshinCoreContext)) as SQLHoshinCoreContext; var _emailSender = scope.ServiceProvider.GetService(typeof(IEmailSender)) as IEmailSender; var _userRepository = scope.ServiceProvider.GetService(typeof(Application.Repositories.IUserRepository)) as Application.Repositories.IUserRepository; var auditResult = _ctx.Audits.Include(x => x.AuditStandards).ThenInclude(x => x.Standard).Include(x => x.SectorPlant).Include(x => x.Auditor) .Include(x => x.AuditReschedulingHistories).Include(x => x.AuditType).Include(x => x.AuditState).Where(x => x.AuditID == audit.AuditID).FirstOrDefault(); SectorsPlants sectorsPlants = _ctx.SectorsPlants.Include(x => x.Plant).Include(x => x.Sector).Where(x => x.SectorID == audit.SectorID && x.PlantID == audit.PlantID).SingleOrDefault(); auditResult.SectorPlant = sectorsPlants; auditResult.AuditInitDate = audit.AuditInitDate; AuditReschedulingHistory auditReschedulingHistory = new AuditReschedulingHistory(); auditReschedulingHistory.AuditID = audit.AuditID; auditReschedulingHistory.DateRescheduling = DateTime.Now; auditResult.AuditReschedulingHistories.Add(auditReschedulingHistory); List <string> sectorPlantReferrents = _sectorPlantRepository.GetSectorPlantReferredEmail(auditResult.PlantID, auditResult.SectorID); sectorPlantReferrents.AddRange(_sectorPlantRepository.GetSectorPlantReferredEmail(audit.PlantID, audit.SectorID)); List <string> userRoles = _ctx.UserRoles.Include(x => x.User).Include(x => x.Role).Where(x => x.Role.Name.ToLower() == "responsable sgc").Select(x => x.User.Email).ToList(); sectorPlantReferrents.AddRange(userRoles); #region Envio de mail if (audit.AuditorID != null) { if (auditResult.AuditorID != null) { var email = _userRepository.Get(audit.AuditorID).Email; if (auditResult.Auditor.Email == email) { sectorPlantReferrents.Add(auditResult.Auditor.Email); } else { sectorPlantReferrents.Add(email); sectorPlantReferrents.Add(auditResult.Auditor.Email); } } else { List <string> auditorInterno = _ctx.UserRoles.Include(x => x.User).Include(x => x.Role).Where(x => x.Role.Name.ToLower() == "auditor interno").Select(x => x.User.Email).ToList(); sectorPlantReferrents.AddRange(auditorInterno); } } else if (audit.AuditTypeID == 10) { if (auditResult.AuditTypeID == 1) { sectorPlantReferrents.Add(auditResult.Auditor.Email); } else { List <string> auditorInterno = _ctx.UserRoles.Include(x => x.User).Include(x => x.Role).Where(x => x.Role.Name.ToLower() == "auditor interno").Select(x => x.User.Email).ToList(); sectorPlantReferrents.AddRange(auditorInterno); } } if (sectorPlantReferrents.Count > 0) { //sectorPlantReferrents = sectorPlantReferrents.Distinct().ToList(); SendEmailAuditRescheduling(sectorPlantReferrents.ToArray(), new string[0], new string[0], audit); } #endregion if (auditResult.AuditState.Code == "PLA" || auditResult.AuditState.Code == "PRE") { auditResult.AuditInitTime = audit.AuditInitTime; auditResult.AuditFinishTime = audit.AuditFinishTime; auditResult.AuditFinishDate = audit.AuditFinishDate; auditResult.DocumentAnalysisDuration = audit.DocumentAnalysisDuration; auditResult.DocumentsAnalysisDate = audit.DocumentsAnalysisDate; auditResult.CloseMeetingDate = audit.CloseMeetingDate; auditResult.CloseMeetingDuration = audit.CloseMeetingDuration; auditResult.CloseDate = audit.CloseDate; } if (audit.AuditStateID == 1) { auditResult.AuditTypeID = audit.AuditTypeID; auditResult.AuditStandards = audit.AuditStandardsID.Select(x => new AuditStandard { AuditID = audit.AuditID, StandardID = x }).ToList(); auditResult.AuditorID = audit.AuditorID; auditResult.ExternalAuditor = audit.ExternalAuditor; } _ctx.Update(auditResult); _ctx.SaveChanges(); } return(true); }