コード例 #1
0
ファイル: ClinicalService.cs プロジェクト: solracph/SCRA
        public async Task <Result> UpdateRule(Rule rule)
        {
            RuleEntity newruleEntity      = _clinicalFactory.MappingToRuleEntity.DefaultContext.Mapper.Map <RuleEntity>(rule);
            RuleEntity originalRuleEntity = ClinicalDbService.RuleRepository.GetById(newruleEntity.RuleId);

            originalRuleEntity.Description = newruleEntity.Description;

            #region Update Segments ------------------------------------------------------------------------------------

            IEnumerable <int> originalSegmentsId = originalRuleEntity.Segments.Select(s => s.SegmentId);
            IEnumerable <int> editedSegmentsId   = newruleEntity.Segments.Select(s => s.SegmentId);

            IList <SegmentEntity> segmentToAdd    = new List <SegmentEntity>();
            IEnumerable <int>     segmentsIdToAdd = editedSegmentsId.Except(originalSegmentsId);
            foreach (int segmentId in segmentsIdToAdd)
            {
                SegmentEntity segment = ClinicalDbService.SegmentRepository.GetById(segmentId);
                if (!segmentToAdd.Contains(segment))
                {
                    segmentToAdd.Add(segment);
                }
            }

            segmentToAdd.ToList().ForEach(s => originalRuleEntity.Segments.Add(s));


            IList <SegmentEntity> segmentToRemove  = new List <SegmentEntity>();
            IEnumerable <int>     segmentsIdRemove = originalSegmentsId.Except(editedSegmentsId);

            foreach (int segmentId in segmentsIdRemove)
            {
                SegmentEntity segment = originalRuleEntity.Segments.FirstOrDefault(s => s.SegmentId == segmentId);
                if (!segmentToRemove.Contains(segment))
                {
                    segmentToRemove.Add(segment);
                }
            }

            segmentToRemove.ToList().ForEach(s => originalRuleEntity.Segments.Remove(s));
            #endregion

            #region Update Contracts -----------------------------------------------------------------------------------

            IEnumerable <int> originalContractsId = originalRuleEntity.Contracts.Select(c => c.ContractId);
            IEnumerable <int> editedContractsId   = newruleEntity.Contracts.Select(c => c.ContractId);

            IList <ContractEntity> contractToAdd    = new List <ContractEntity>();
            IEnumerable <int>      contractsIdToAdd = editedContractsId.Except(originalContractsId);
            foreach (int contractId in contractsIdToAdd)
            {
                ContractEntity contract = ClinicalDbService.ContractRepository.GetById(contractId);
                if (!contractToAdd.Contains(contract))
                {
                    contractToAdd.Add(contract);
                }
            }

            contractToAdd.ToList().ForEach(c => originalRuleEntity.Contracts.Add(c));

            IList <ContractEntity> contractToRemove  = new List <ContractEntity>();
            IEnumerable <int>      contractsIdRemove = originalContractsId.Except(editedContractsId);

            foreach (int contractId in contractsIdRemove)
            {
                ContractEntity contract = originalRuleEntity.Contracts.FirstOrDefault(s => s.ContractId == contractId);
                if (!contractToRemove.Contains(contract))
                {
                    contractToRemove.Add(contract);
                }
            }

            contractToRemove.ToList().ForEach(c => originalRuleEntity.Contracts.Remove(c));
            #endregion

            #region Update Pbp -----------------------------------------------------------------------------------------

            IEnumerable <int> originalPbpId = originalRuleEntity.Pbp.Select(c => c.PbpId);
            IEnumerable <int> editedPbpId   = newruleEntity.Pbp.Select(c => c.PbpId);

            IList <PbpEntity> pbpToAdd    = new List <PbpEntity>();
            IEnumerable <int> pbpsIdToAdd = editedPbpId.Except(originalPbpId);
            foreach (int pbpId in pbpsIdToAdd)
            {
                PbpEntity pbp = ClinicalDbService.PbpRepository.GetById(pbpId);
                if (!pbpToAdd.Contains(pbp))
                {
                    pbpToAdd.Add(pbp);
                }
            }

            pbpToAdd.ToList().ForEach(c => originalRuleEntity.Pbp.Add(c));

            IList <PbpEntity> pbpToRemove  = new List <PbpEntity>();
            IEnumerable <int> pbpsIdRemove = originalPbpId.Except(editedPbpId);

            foreach (int pbpId in pbpsIdRemove)
            {
                PbpEntity pbp = originalRuleEntity.Pbp.FirstOrDefault(s => s.PbpId == pbpId);
                if (!pbpToRemove.Contains(pbp))
                {
                    pbpToRemove.Add(pbp);
                }
            }

            pbpToRemove.ToList().ForEach(c => originalRuleEntity.Pbp.Remove(c));
            #endregion

            #region Update TIN -----------------------------------------------------------------------------------------

            IEnumerable <int> originalTinId = originalRuleEntity.Tin.Select(c => c.TinId);
            IEnumerable <int> editedTinId   = newruleEntity.Tin.Select(c => c.TinId);

            IList <TinEntity> tinToAdd    = new List <TinEntity>();
            IEnumerable <int> tinsIdToAdd = editedTinId.Except(originalTinId);
            foreach (int tinId in tinsIdToAdd)
            {
                TinEntity tin = ClinicalDbService.TinRepository.GetById(tinId);
                if (!tinToAdd.Contains(tin))
                {
                    tinToAdd.Add(tin);
                }
            }

            tinToAdd.ToList().ForEach(c => originalRuleEntity.Tin.Add(c));

            IList <TinEntity> tinToRemove  = new List <TinEntity>();
            IEnumerable <int> tinsIdRemove = originalTinId.Except(editedTinId);

            foreach (int tinId in tinsIdRemove)
            {
                TinEntity tin = originalRuleEntity.Tin.FirstOrDefault(s => s.TinId == tinId);
                if (!tinToRemove.Contains(tin))
                {
                    tinToRemove.Add(tin);
                }
            }

            tinToRemove.ToList().ForEach(c => originalRuleEntity.Tin.Remove(c));
            #endregion

            #region Update Measures ------------------------------------------------------------------------------------

            IEnumerable <int> originalMeasuresId = originalRuleEntity.Measures.Select(c => c.MeasureId);
            IEnumerable <int> editedMeasuresId   = newruleEntity.Measures.Select(c => c.MeasureId);

            IList <MeasureEntity> measuresToAdd   = new List <MeasureEntity>();
            IEnumerable <int>     measuresIdToAdd = editedMeasuresId.Except(originalMeasuresId);
            foreach (int measureId in measuresIdToAdd)
            {
                MeasureEntity measure = ClinicalDbService.MeasureRepository.GetById(measureId);
                if (!measuresToAdd.Contains(measure))
                {
                    measuresToAdd.Add(measure);
                }
            }

            measuresToAdd.ToList().ForEach(c => originalRuleEntity.Measures.Add(c));

            IList <MeasureEntity> measuresToRemove = new List <MeasureEntity>();
            IEnumerable <int>     measuresIdRemove = originalMeasuresId.Except(editedMeasuresId);

            foreach (int measureId in measuresIdRemove)
            {
                MeasureEntity measure = originalRuleEntity.Measures.FirstOrDefault(s => s.MeasureId == measureId);
                if (!measuresToRemove.Contains(measure))
                {
                    measuresToRemove.Add(measure);
                }
            }

            measuresToRemove.ToList().ForEach(c => originalRuleEntity.Measures.Remove(c));
            #endregion

            #region Update Applications --------------------------------------------------------------------------------

            IEnumerable <int> originalApplicationsId = originalRuleEntity.Applications.Select(c => c.ApplicationId);
            IEnumerable <int> editedApplicationsId   = newruleEntity.Applications.Select(c => c.ApplicationId);

            IList <ApplicationEntity> applicationsToAdd   = new List <ApplicationEntity>();
            IEnumerable <int>         applicationsIdToAdd = editedApplicationsId.Except(originalApplicationsId);
            foreach (int applicationId in applicationsIdToAdd)
            {
                ApplicationEntity application = ClinicalDbService.ApplicationRepository.GetById(applicationId);
                if (!applicationsToAdd.Contains(application))
                {
                    applicationsToAdd.Add(application);
                }
            }

            applicationsToAdd.ToList().ForEach(c => originalRuleEntity.Applications.Add(c));

            IList <ApplicationEntity> applicationsToRemove = new List <ApplicationEntity>();
            IEnumerable <int>         applicationsIdRemove = originalApplicationsId.Except(editedApplicationsId);

            foreach (int applicationId in applicationsIdRemove)
            {
                ApplicationEntity application = originalRuleEntity.Applications.FirstOrDefault(s => s.ApplicationId == applicationId);
                if (!applicationsToRemove.Contains(application))
                {
                    applicationsToRemove.Add(application);
                }
            }

            applicationsToRemove.ToList().ForEach(c => originalRuleEntity.Applications.Remove(c));
            #endregion

            ClinicalDbService.RuleRepository.Update(originalRuleEntity);

            await ClinicalDbService.SaveAsync();

            return(Result.New(originalRuleEntity));
        }
コード例 #2
0
ファイル: ClinicalService.cs プロジェクト: solracph/SCRA.Core
        public async Task <Result> UpdateRule(Rule rule)
        {
            RuleEntity newruleEntity      = _clinicalFactory.MappingToRuleEntity.DefaultContext.Mapper.Map <RuleEntity>(rule);
            RuleEntity originalRuleEntity = _GetReleById(rule.RuleId);

            originalRuleEntity.Description = newruleEntity.Description;

            #region Update Segments ------------------------------------------------------------------------------------

            IEnumerable <int> originalSegmentsId = originalRuleEntity.RuleSegment.Select(s => s.Segment).Select(s => s.SegmentId);
            IEnumerable <int> editedSegmentsId   = newruleEntity.RuleSegment.Select(s => s.Segment).Select(s => s.SegmentId);

            IList <SegmentEntity> segmentToAdd    = new List <SegmentEntity>();
            IEnumerable <int>     segmentsIdToAdd = editedSegmentsId.Except(originalSegmentsId);
            foreach (int segmentId in segmentsIdToAdd)
            {
                SegmentEntity segment = ClinicalDbService.SegmentRepository.GetById(segmentId);
                if (!segmentToAdd.Contains(segment))
                {
                    segmentToAdd.Add(segment);
                }
            }

            segmentToAdd.ToList().ForEach(s => originalRuleEntity.RuleSegment.Add(new RuleSegmentEntity {
                RuleId  = originalRuleEntity.RuleId,
                Segment = s
            }));


            IList <SegmentEntity> segmentToRemove  = new List <SegmentEntity>();
            IEnumerable <int>     segmentsIdRemove = originalSegmentsId.Except(editedSegmentsId);

            foreach (int segmentId in segmentsIdRemove)
            {
                SegmentEntity segment = originalRuleEntity.RuleSegment.Select(s => s.Segment).FirstOrDefault(s => s.SegmentId == segmentId);
                if (!segmentToRemove.Contains(segment))
                {
                    segmentToRemove.Add(segment);
                }
            }


            segmentToRemove.ToList().ForEach(s => originalRuleEntity.RuleSegment.Remove(
                                                 originalRuleEntity.RuleSegment.Select(r => r).Where(r => r.SegmentId == s.SegmentId).First()
                                                 ));
            #endregion

            #region Update Contracts -----------------------------------------------------------------------------------

            IEnumerable <int> originalContractsId = originalRuleEntity.RuleContract.Select(c => c.Contract).Select(c => c.ContractId);
            IEnumerable <int> editedContractsId   = newruleEntity.RuleContract.Select(c => c.Contract).Select(c => c.ContractId);

            IList <ContractEntity> contractToAdd    = new List <ContractEntity>();
            IEnumerable <int>      contractsIdToAdd = editedContractsId.Except(originalContractsId);
            foreach (int contractId in contractsIdToAdd)
            {
                ContractEntity contract = ClinicalDbService.ContractRepository.GetById(contractId);
                if (!contractToAdd.Contains(contract))
                {
                    contractToAdd.Add(contract);
                }
            }

            contractToAdd.ToList().ForEach(c => originalRuleEntity.RuleContract.Add(new RuleContractEntity
            {
                RuleId   = originalRuleEntity.RuleId,
                Contract = c
            }));

            IList <ContractEntity> contractToRemove  = new List <ContractEntity>();
            IEnumerable <int>      contractsIdRemove = originalContractsId.Except(editedContractsId);

            foreach (int contractId in contractsIdRemove)
            {
                ContractEntity contract = originalRuleEntity.RuleContract.Select(c => c.Contract).FirstOrDefault(s => s.ContractId == contractId);
                if (!contractToRemove.Contains(contract))
                {
                    contractToRemove.Add(contract);
                }
            }

            contractToRemove.ToList().ForEach(c => originalRuleEntity.RuleContract.Remove(
                                                  originalRuleEntity.RuleContract.Select(rc => rc).Where(rc => rc.ContractId == c.ContractId).First()
                                                  ));
            #endregion

            #region Update Pbp -----------------------------------------------------------------------------------------
            IEnumerable <int> originalContractPbpId = originalRuleEntity.RulePbp.Select(p => p.ContractPbp).Select(cp => cp.ContractPBPId);
            IEnumerable <int> editedContractPbpId   = newruleEntity.RulePbp.Select(p => p.ContractPbp).Select(cp => cp.ContractPBPId);

            IList <ContractPbpEntity> contractPbpToAdd   = new List <ContractPbpEntity>();
            IEnumerable <int>         contractPbpIdToAdd = editedContractPbpId.Except(originalContractPbpId);

            foreach (int contractPbpId in contractPbpIdToAdd)
            {
                ContractPbpEntity contractPbp = ClinicalDbService.ContractPbpRepository.GetById(contractPbpId);
                if (!contractPbpToAdd.Contains(contractPbp))
                {
                    contractPbpToAdd.Add(contractPbp);
                }
            }

            contractPbpToAdd.ToList().ForEach(cp => originalRuleEntity.RulePbp.Add(new RulePbpEntity {
                RuleId      = originalRuleEntity.RuleId,
                ContractPbp = cp
            }));

            IList <ContractPbpEntity> contractPbpToRemove   = new List <ContractPbpEntity>();
            IEnumerable <int>         contractPbpIdToRemove = originalContractPbpId.Except(editedContractPbpId);

            foreach (int contractPbpId in contractPbpIdToRemove)
            {
                ContractPbpEntity contractPbp = originalRuleEntity.RulePbp.Select(c => c.ContractPbp).FirstOrDefault(s => s.ContractPBPId == contractPbpId);
                if (!contractPbpToRemove.Contains(contractPbp))
                {
                    contractPbpToRemove.Add(contractPbp);
                }
            }

            contractPbpToRemove.ToList().ForEach(c => originalRuleEntity.RulePbp.Remove(
                                                     originalRuleEntity.RulePbp.Select(rc => rc).Where(rc => rc.ContractPBPId == c.ContractPBPId).First()
                                                     ));

            #endregion

            #region Update TIN -----------------------------------------------------------------------------------------

            IEnumerable <int> originalTinId = originalRuleEntity.RuleTin.Select(t => t.Tin).Select(c => c.TinId);
            IEnumerable <int> editedTinId   = newruleEntity.RuleTin.Select(t => t.Tin).Select(c => c.TinId);

            IList <TinEntity> tinToAdd    = new List <TinEntity>();
            IEnumerable <int> tinsIdToAdd = editedTinId.Except(originalTinId);
            foreach (int tinId in tinsIdToAdd)
            {
                TinEntity tin = ClinicalDbService.TinRepository.GetById(tinId);
                if (!tinToAdd.Contains(tin))
                {
                    tinToAdd.Add(tin);
                }
            }

            tinToAdd.ToList().ForEach(t => originalRuleEntity.RuleTin.Add(
                                          new RuleTinEntity {
                RuleId = originalRuleEntity.RuleId,
                Tin    = t
            }
                                          ));

            IList <TinEntity> tinToRemove  = new List <TinEntity>();
            IEnumerable <int> tinsIdRemove = originalTinId.Except(editedTinId);

            foreach (int tinId in tinsIdRemove)
            {
                TinEntity tin = originalRuleEntity.RuleTin.Select(t => t.Tin).FirstOrDefault(s => s.TinId == tinId);
                if (!tinToRemove.Contains(tin))
                {
                    tinToRemove.Add(tin);
                }
            }

            tinToRemove.ToList().ForEach(t => originalRuleEntity.RuleTin.Remove(
                                             originalRuleEntity.RuleTin.Select(rt => rt).Where(rt => rt.TinId == t.TinId).First()
                                             ));
            #endregion

            #region Update Measures ------------------------------------------------------------------------------------

            IEnumerable <int> originalMeasuresId = originalRuleEntity.RuleMeasure.Select(rm => rm.Measure).Select(c => c.MeasureId);
            IEnumerable <int> editedMeasuresId   = newruleEntity.RuleMeasure.Select(rm => rm.Measure).Select(c => c.MeasureId);

            IList <MeasureEntity> measuresToAdd   = new List <MeasureEntity>();
            IEnumerable <int>     measuresIdToAdd = editedMeasuresId.Except(originalMeasuresId);
            foreach (int measureId in measuresIdToAdd)
            {
                MeasureEntity measure = ClinicalDbService.MeasureRepository.GetById(measureId);
                if (!measuresToAdd.Contains(measure))
                {
                    measuresToAdd.Add(measure);
                }
            }

            measuresToAdd.ToList().ForEach(m => originalRuleEntity.RuleMeasure.Add(
                                               new RuleMeasureEntity {
                RuleId  = originalRuleEntity.RuleId,
                Measure = m
            }
                                               ));

            IList <MeasureEntity> measuresToRemove = new List <MeasureEntity>();
            IEnumerable <int>     measuresIdRemove = originalMeasuresId.Except(editedMeasuresId);

            foreach (int measureId in measuresIdRemove)
            {
                MeasureEntity measure = originalRuleEntity.RuleMeasure.Select(rm => rm.Measure).FirstOrDefault(s => s.MeasureId == measureId);
                if (!measuresToRemove.Contains(measure))
                {
                    measuresToRemove.Add(measure);
                }
            }

            measuresToRemove.ToList().ForEach(m => originalRuleEntity.RuleMeasure.Remove(
                                                  originalRuleEntity.RuleMeasure.Select(rm => rm).Where(rm => rm.MeasureId == m.MeasureId).First()
                                                  ));
            #endregion

            #region Update Applications --------------------------------------------------------------------------------

            IEnumerable <int> originalApplicationsId = originalRuleEntity.RuleApplication.Select(ra => ra.Application).Select(a => a.ApplicationId);
            IEnumerable <int> editedApplicationsId   = newruleEntity.RuleApplication.Select(ra => ra.Application).Select(c => c.ApplicationId);

            IList <ApplicationEntity> applicationsToAdd   = new List <ApplicationEntity>();
            IEnumerable <int>         applicationsIdToAdd = editedApplicationsId.Except(originalApplicationsId);
            foreach (int applicationId in applicationsIdToAdd)
            {
                ApplicationEntity application = ClinicalDbService.ApplicationRepository.GetById(applicationId);
                if (!applicationsToAdd.Contains(application))
                {
                    applicationsToAdd.Add(application);
                }
            }

            applicationsToAdd.ToList().ForEach(a => originalRuleEntity.RuleApplication.Add(
                                                   new RuleApplicationEntity {
                RuleId      = originalRuleEntity.RuleId,
                Application = a
            }
                                                   ));

            IList <ApplicationEntity> applicationsToRemove = new List <ApplicationEntity>();
            IEnumerable <int>         applicationsIdRemove = originalApplicationsId.Except(editedApplicationsId);

            foreach (int applicationId in applicationsIdRemove)
            {
                ApplicationEntity application = originalRuleEntity.RuleApplication.Select(ra => ra.Application).FirstOrDefault(s => s.ApplicationId == applicationId);
                if (!applicationsToRemove.Contains(application))
                {
                    applicationsToRemove.Add(application);
                }
            }

            applicationsToRemove.ToList().ForEach(a => originalRuleEntity.RuleApplication.Remove(
                                                      originalRuleEntity.RuleApplication.Select(ra => ra).Where(ra => ra.ApplicationId == a.ApplicationId).First()
                                                      ));
            #endregion

            ClinicalDbService.RuleRepository.Update(originalRuleEntity);

            await ClinicalDbService.SaveAsync();



            return(Result.New(originalRuleEntity));
        }