Пример #1
0
        public override int UpdateView <T>(T objModalSrc)
        {
            var pollingView = (PollingView)(IViewModel)objModalSrc;
            // 在后台根据传过来的值区分哪些option or question是插入哪些是更新

            // 先从后台拉一下当前的数据,组一个oldPollingView
            var oldPollingView = new PollingView();

            oldPollingView.ConvertAPIModel(Repository.Entities.AsNoTracking().FirstOrDefault(a => a.Id == pollingView.Id && a.IsDeleted != true));
            oldPollingView.PollingQuestions = _pollingQuestionService.GetPollingQuestion(pollingView.Id);
            foreach (var oldQuestion in oldPollingView.PollingQuestions)
            {
                oldQuestion.PollingOptionEntities = _pollingOptionService.GetPollingOptions(oldQuestion.Id);
            }

            // 因为直接更新会级联更新,所以这里把孩子都删了更新,然后再加上孩子们
            var newQuestions = pollingView.PollingQuestions;

            pollingView.PollingQuestions = null;
            base.UpdateView(pollingView, new List <string> {
                "Name", "Type", "StartDateTime", "EndDateTime", "StandardScore", "ReplyMessage"
            });
            pollingView.PollingQuestions = newQuestions;

            var allUpdates = newQuestions.AsParallel().Where(x => oldPollingView.PollingQuestions.AsParallel().Any(y => x.Id == y.Id && (x.Score != y.Score || string.Compare(x.RightAnswers, y.RightAnswers, StringComparison.OrdinalIgnoreCase) != 0))).Select(x => new { x.Id, x.Score, x.RightAnswers }).ToList();

            if (allUpdates.Any())
            {
                var ids        = allUpdates.Select(x => x.Id).ToList();
                var oldAnswers = _pollingAnswerService.Repository.Entities.Where(x => ids.Contains(x.QuestionId) && x.IsDeleted != true).ToList();
                var updateScoreQuestionList       = allUpdates.AsParallel().Where(x => oldPollingView.PollingQuestions.AsParallel().Any(y => x.Id == y.Id && x.Score != y.Score)).Select(x => new { x.Id, x.Score }).ToList();
                var updateRightAnswerQuestionList = allUpdates.AsParallel().Where(x => oldPollingView.PollingQuestions.AsParallel().Any(y => x.Id == y.Id && string.Compare(x.RightAnswers, y.RightAnswers, StringComparison.OrdinalIgnoreCase) != 0)).Select(x => new { x.Id, x.RightAnswers }).ToList();

                if (updateScoreQuestionList.Any() && oldAnswers.Any())
                {
                    Parallel.ForEach(oldAnswers, x =>
                    {
                        var newQuestion = updateScoreQuestionList.AsParallel().First(y => y.Id == x.QuestionId);
                        x.Score         = newQuestion.Score.GetValueOrDefault();
                    });
                    oldAnswers.ForEach(x => _pollingAnswerService.Repository.Update(x, new List <string> {
                        "Score"
                    }));
                }

                if (updateRightAnswerQuestionList.Any() && oldAnswers.Any())
                {
                    Parallel.ForEach(oldAnswers, x =>
                    {
                        var newQuestion = updateRightAnswerQuestionList.AsParallel().First(y => y.Id == x.QuestionId);

                        x.Status      = string.Compare(x.SelectAnswer, newQuestion.RightAnswers, StringComparison.OrdinalIgnoreCase) == 0;
                        x.RightAnswer = newQuestion.RightAnswers;
                    });
                    oldAnswers.ForEach(x => _pollingAnswerService.Repository.Update(x, new List <string> {
                        "RightAnswer", "Status"
                    }));
                }
            }


            // 检查OldPollingView相对于NewPollingView删除了哪些
            foreach (var oldQuestion in oldPollingView.PollingQuestions)
            {
                // 如果id不在新polling里,就是删掉了
                var sameQuestionInNew = pollingView.PollingQuestions.FirstOrDefault(a => a.Id == oldQuestion.Id && a.IsDeleted != true);
                if (sameQuestionInNew == null)
                {
                    _pollingQuestionService.Repository.Delete(oldQuestion.Id);
                    // 如果删除了question的话,会自动删除option
                    // 我们再次手动删除answer表里相关的option
                    _pollingResultService.DeleteByQuestionId(oldQuestion.Id);
                    PollingQuestionView question = oldQuestion;
                    _pollingAnswerService.Repository.Entities.Where(x => x.QuestionId == question.Id).Delete();

                    continue;
                }
                // 如果没删,检查option。
                // 如果option被删了,同时去删除用户填写过答案的answer
                foreach (var oldOption in oldQuestion.PollingOptionEntities)
                {
                    if (sameQuestionInNew.PollingOptionEntities == null || sameQuestionInNew.PollingOptionEntities.FirstOrDefault(a => a.Id == oldOption.Id && a.IsDeleted != true) == null)
                    {
                        _pollingOptionService.Repository.Delete(oldOption.Id);
                        _pollingResultService.DeleteByOptionId(oldOption.Id);
                        PollingOptionView option = oldOption;
                        _pollingAnswerService.Repository.Entities.Where(x => x.QuestionId == option.Id).Delete();
                    }
                }
            }

            // 除了删除的,就是添加和更新的
            // 检查NewPollingView相对于OldPollingView添加了哪些

            foreach (var newQuestion in pollingView.PollingQuestions)
            {
                // 如果id是0,肯定新加的,执行级联添加
                if (newQuestion.Id == 0)
                {
                    newQuestion.PollingId = pollingView.Id;
                    _pollingQuestionService.InsertView(newQuestion);
                    continue;
                }
                // 如果不是0,更新自己,并且检查option
                var options = newQuestion.PollingOptionEntities;
                newQuestion.PollingOptionEntities = null;
                _pollingQuestionService.UpdateView(newQuestion);
                newQuestion.PollingOptionEntities = options;

                if (newQuestion.PollingOptionEntities != null && newQuestion.PollingOptionEntities.Any())
                {
                    foreach (var newOption in newQuestion.PollingOptionEntities)
                    {
                        if (newOption.Id == 0)
                        {
                            newOption.QuestionId = newQuestion.Id;
                            _pollingOptionService.InsertView(newOption);
                        }
                        else
                        {
                            _pollingOptionService.UpdateView(newOption, new List <string> {
                                "OptionName", "Picture", "Type"
                            });
                        }
                    }
                }
            }

            return(1);
        }
Пример #2
0
        public override int UpdateView <T>(T objModalSrc)
        {
            int iRet;
            var pollingView = (PollingView)(IViewModel)objModalSrc;
            // 在后台根据传过来的值区分哪些option or question是插入哪些是更新

            // 先从后台拉一下当前的数据,组一个oldPollingView
            PollingView oldPollingView = new PollingView();

            oldPollingView.ConvertAPIModel(Repository.Entities.AsNoTracking().FirstOrDefault(a => a.Id == pollingView.Id && a.IsDeleted != true));
            oldPollingView.PollingQuestions = _pollingQuestionService.GetPollingQuestion(pollingView.Id);
            foreach (var oldQuestion in oldPollingView.PollingQuestions)
            {
                oldQuestion.PollingOptionEntities = _pollingOptionService.GetPollingOptions(oldQuestion.Id);
            }

            // 因为直接更新会级联更新,所以这里把孩子都删了更新,然后再加上孩子们
            var newQuestions = pollingView.PollingQuestions;

            pollingView.PollingQuestions = null;
            base.UpdateView(pollingView, new List <string> {
                "Name", "Type", "StartDateTime", "EndDateTime", "StandardScore", "ReplyMessage"
            });
            pollingView.PollingQuestions = newQuestions;

            // 检查OldPollingView相对于NewPollingView删除了哪些
            foreach (var oldQuestion in oldPollingView.PollingQuestions)
            {
                // 如果id不在新polling里,就是删掉了
                var sameQuestionInNew = pollingView.PollingQuestions.FirstOrDefault(a => a.Id == oldQuestion.Id && a.IsDeleted != true);
                if (sameQuestionInNew == null)
                {
                    _pollingQuestionService.Repository.Delete(oldQuestion.Id);
                    // 如果删除了question的话,会自动删除option
                    // 我们再次手动删除answer表里相关的option
                    var answers = _pollingResultService.DeleteByQuestionId(oldQuestion.Id);

                    continue;
                }
                // 如果没删,检查option。
                // 如果option被删了,同时去删除用户填写过答案的answer
                else
                {
                    foreach (var oldOption in oldQuestion.PollingOptionEntities)
                    {
                        if (sameQuestionInNew.PollingOptionEntities == null || sameQuestionInNew.PollingOptionEntities.FirstOrDefault(a => a.Id == oldOption.Id && a.IsDeleted != true) == null)
                        {
                            _pollingOptionService.Repository.Delete(oldOption.Id);
                            _pollingResultService.DeleteByOptionId(oldOption.Id);
                            continue;
                        }
                    }
                }
            }

            // 除了删除的,就是添加和更新的
            // 检查NewPollingView相对于OldPollingView添加了哪些
            foreach (var newQuestion in pollingView.PollingQuestions)
            {
                // 如果id是0,肯定新加的,执行级联添加
                if (newQuestion.Id == 0)
                {
                    newQuestion.PollingId = pollingView.Id;
                    _pollingQuestionService.InsertView(newQuestion);
                    continue;
                }
                // 如果不是0,更新自己,并且检查option
                else
                {
                    var options = newQuestion.PollingOptionEntities;
                    newQuestion.PollingOptionEntities = null;
                    _pollingQuestionService.UpdateView(newQuestion);
                    newQuestion.PollingOptionEntities = options;

                    if (newQuestion.PollingOptionEntities != null && newQuestion.PollingOptionEntities.Any())
                    {
                        foreach (var newOption in newQuestion.PollingOptionEntities)
                        {
                            if (newOption.Id == 0)
                            {
                                newOption.QuestionId = newQuestion.Id;
                                _pollingOptionService.InsertView(newOption);
                            }
                            else
                            {
                                _pollingOptionService.UpdateView(newOption, new List <string> {
                                    "OptionName", "Picture", "Type"
                                });
                            }
                        }
                    }
                }
            }

            return(1);
        }