/// <summary> /// 紀錄問卷填答資料至問卷答題主檔與答題明細 /// </summary> /// <param name="questEntity">問卷資料</param> /// <param name="calculateResult">問卷得分與填答資料</param> /// <param name="userId">用戶ID</param> /// <returns>問卷答題資料</returns> private QuestionnaireAnswerDO SaveQuestionAnswer(QuestionnaireEntity questEntity, CalculateScoreEntity calculateResult, string userId) { QuestionnaireAnswerDO questionnaireAnswerDO = null; if (questEntity == null) { throw new ArgumentNullException("questEntity"); } else if (calculateResult == null) { throw new ArgumentNullException("calculateResult"); } else if (String.IsNullOrEmpty(userId)) { throw new ArgumentNullException("userId"); } DateTime currentTiem = DateTime.Now; using (TransactionScope scope = new TransactionScope()) { questionnaireAnswerDO = new QuestionnaireAnswerDO() { Uid = Guid.NewGuid(), QuestUid = questEntity.Uid, QuestAnswerId = currentTiem.ToString("yyMMddHHmmssfff"), TesteeId = userId, QuestScore = questEntity.QuestScore, ActualScore = calculateResult.ActualScore, TesteeSource = _testeeSource, CreateUserId = userId, CreateTime = currentTiem, ModifyUserId = null, ModifyTime = null, }; IEnumerable <QuestionnaireAnswerDetailDO> questAnswerDetailList = calculateResult.FullAnswerDetailList.Select(answerDetail => new QuestionnaireAnswerDetailDO() { Uid = Guid.NewGuid(), AnswerUid = questionnaireAnswerDO.Uid, QuestionUid = answerDetail.QuestionUid, AnswerCode = answerDetail.AnswerCode, OtherAnswer = answerDetail.OtherAnswer, Score = answerDetail.Score, CreateUserId = userId, CreateTime = currentTiem, ModifyUserId = null, ModifyTime = null, }); new QuestionnaireAnswerDAO().Insert(questionnaireAnswerDO); new QuestionnaireAnswerDetailDAO().Insert(questAnswerDetailList); scope.Complete(); } return(questionnaireAnswerDO); }
/// <summary> /// 計算問卷填答得分 /// </summary> /// <param name="answer">問卷填答資料</param> /// <returns>問卷填答評分結果</returns> public QuestionnaireResultEntity Calculate(QuestionnaireAnswerEntity answer) { QuestionnaireResultEntity questionResultEntity = null; QuestionnaireAnswerDO questionAnswerDO = null; if (answer == null) { throw new ArgumentNullException("沒有提供問卷填答資料"); } QuestionnaireEntity questionEntity = GetQuestionnaire(answer.QuestUid); DateTime currentTime = DateTime.Now; if ((questionEntity == null) || (questionEntity.Ondate >= currentTime) || (questionEntity.Offdate != null) && (questionEntity.Offdate <= currentTime)) { var ex = new InvalidOperationException("問卷資料不存在或沒有有效的問卷資料"); ex.Data["QuestionUis"] = answer.QuestUid; throw ex; } Dictionary <string, string> validateResult = ValidateRule(answer, questionEntity); if (validateResult == null) { throw new InvalidOperationException("validateResult not found"); } Dictionary <string, string> riskResult = null; string dialogMsg = null; if (validateResult.Count == 0) { CalculateScoreEntity calculateResult = CalculateScore(answer, questionEntity); if (calculateResult == null) { throw new InvalidOperationException("calculateResult not found"); } riskResult = calculateResult.FullAnswerDetailList. Where(x => !String.IsNullOrEmpty(x.AnswerCode)). GroupBy(x => x.QuestionId). Select(group => new { group.Key, Value = String.Join(",", group.Select(item => item.AnswerCode)) }).ToDictionary(x => x.Key, x => x.Value); if (riskResult == null) { throw new InvalidOperationException("riskResult not found"); } questionAnswerDO = SaveQuestionAnswer(questionEntity, calculateResult, answer.UserId); if (questionAnswerDO == null) { throw new InvalidOperationException("questionAnswerDO not found"); } if (questionEntity.NeedScore != "Y") { dialogMsg = "您的問卷己填答完畢,謝謝您的參與"; } } questionResultEntity = new QuestionnaireResultEntity() { QuestionnaireEntity = questionEntity, AnswerDetailEntities = answer.AnswerDetailEntities, ValidateFailInfo = validateResult, RiskResult = riskResult, QuestionnaireMessage = dialogMsg, }; if (questionAnswerDO != null) { questionResultEntity.Uid = questionAnswerDO.Uid; questionResultEntity.QuestUid = questionAnswerDO.QuestUid; questionResultEntity.QuestAnswerId = questionAnswerDO.QuestAnswerId; questionResultEntity.TesteeId = questionAnswerDO.TesteeId; questionResultEntity.QuestScore = questionAnswerDO.QuestScore; questionResultEntity.ActualScore = questionAnswerDO.ActualScore; questionResultEntity.TesteeSource = questionAnswerDO.TesteeSource; questionResultEntity.CreateUserId = questionAnswerDO.CreateUserId; questionResultEntity.CreateTime = questionAnswerDO.CreateTime; questionResultEntity.ModifyUserId = questionAnswerDO.ModifyUserId; questionResultEntity.ModifyTime = questionAnswerDO.ModifyTime; } return(questionResultEntity); }