public bool CourtArchiveCommittee_SaveData(CourtArchiveCommittee model, List <int> lawUnitIds) { try { if (model.Id > 0) { //Update var saved = repo.GetById <CourtArchiveCommittee>(model.Id); saved.Label = model.Label; saved.Description = model.Description; saved.DateStart = model.DateStart; saved.DateEnd = model.DateEnd; repo.Update(saved); } else { //Insert repo.Add <CourtArchiveCommittee>(model); } DateTime fromDate = DateTime.Now; DateTime toDate = DateTime.Now.AddSeconds(-1); var lawUnitList = repo.All <CourtArchiveCommitteeLawUnit>().Where(x => x.CourtArchiveCommitteeId == model.Id).ToList(); lawUnitList.ForEach(x => x.DateTo = toDate); foreach (var itemId in lawUnitIds) { var units = lawUnitList.Where(x => x.LawUnitId == itemId).FirstOrDefault(); if (units != null) { units.DateTo = null; } else { CourtArchiveCommitteeLawUnit newLawUnit = new CourtArchiveCommitteeLawUnit(); newLawUnit.CourtArchiveCommitteeId = model.Id; newLawUnit.LawUnitId = itemId; newLawUnit.DateFrom = fromDate; repo.Add <CourtArchiveCommitteeLawUnit>(newLawUnit); } } repo.SaveChanges(); return(true); } catch (Exception ex) { logger.LogError(ex, $"Грешка при запис на CourtArchiveCommittee Id={ model.Id }"); return(false); } }
private bool migrate_ArchiveCommitteeLawunit(MigrationData item) { var data = item.Data.Split('|'); try { var model = new CourtArchiveCommitteeLawUnit() { DateFrom = DefaultDateFrom }; var committee = repo.AllReadonly <CourtArchiveCommittee>().Where(x => x.CourtId == item.CourtId && x.Description == item.ParentCode).FirstOrDefault(); var uic = data[0]; var lawunit = repo.AllReadonly <CourtLawUnit>() .Include(x => x.LawUnit) .Where(x => x.CourtId == item.CourtId && x.LawUnit.Uic == uic && x.PeriodTypeId == NomenclatureConstants.PeriodTypes.Appoint) .Select(x => x.LawUnit).FirstOrDefault(); if ((committee == null) || (lawunit == null)) { item.Message = $"Ненамерена комисия {item.ParentCode} или лице {uic} {data[1]}"; return(false); } if (repo.AllReadonly <CourtArchiveCommitteeLawUnit>().Where(x => x.CourtArchiveCommitteeId == committee.Id && x.LawUnitId == lawunit.Id).Any()) { item.Message = $"Лицето {uic} {data[1]} вече съществува"; return(false); } model.CourtArchiveCommitteeId = committee.Id; model.LawUnitId = lawunit.Id; repo.Add(model); repo.SaveChanges(); return(true); } catch (Exception ex) { item.Message = ex.Message; return(false); } }