コード例 #1
0
ファイル: NpoiHelper.cs プロジェクト: wochizhuzhua/Guoli
        /// <summary>
        /// 将试题文件及问题集合插入数据库中
        /// </summary>
        private static object InsertQuestionsToDb(ExamFiles examFiles, List <KeyValuePair <ExamQuestion, List <ExamAnswers> > > list)
        {
            var examFilesBll = new ExamFilesBll();

            examFilesBll.Insert(examFiles);

            if (examFiles.Id > 0)
            {
                DataUpdateLog.SingleUpdate(typeof(ExamFiles).Name, examFiles.Id, DataUpdateType.Insert);

                var questionBll = new ExamQuestionBll();
                var answerBll   = new ExamAnswersBll();

                // 在插入之前获取两表的最大id,以便在插入完成之后做数据库更新记录
                var maxQuestionId = (int)questionBll.GetMaxId();
                var maxAnswerId   = (int)answerBll.GetMaxId();

                try
                {
                    foreach (var keyPair in list)
                    {
                        var question = keyPair.Key;
                        var answers  = keyPair.Value;
                        question.ExamFileId = examFiles.Id;

                        // 将问题与答案放到事务中一起插入,保证数据的准确性
                        questionBll.ExecuteTransation(() => {
                            questionBll.Insert(question);
                            if (question.Id > 0)
                            {
                                answers.ForEach(answer => answer.QuestionId = question.Id);
                                answerBll.BulkInsert(answers);

                                return(true);
                            }

                            return(false);
                        });
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                // 数据库更新记录上传
                DataUpdateLog.BulkUpdate(typeof(ExamQuestion).Name, maxQuestionId, DataUpdateType.Insert);
                DataUpdateLog.BulkUpdate(typeof(ExamAnswers).Name, maxAnswerId, DataUpdateType.Insert);

                return(ErrorModel.OperateSuccess);
            }

            return(ErrorModel.OperateFailed);
        }
コード例 #2
0
        /// <summary>
        /// 当新增搜索结果时,将其写入对应关键字的搜索结果文件中(每个关键字对应一个搜索结果文件)
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="results"></param>
        private static void UpdateTraficKeywordsSearchResult(TraficKeywords keywords, IEnumerable <TraficSearchResult> results)
        {
            EnsureSearchResultFileExists(keywords);

            var filename  = PathExtension.MapPath(keywords.ResultPath);
            var seperator = "Guoli.Admin.SearchHelper.Seperator";

            using (var fs = new FileStream(filename, FileMode.Append, FileAccess.Write))
            {
                using (var writer = new StreamWriter(fs, Encoding.UTF8))
                {
                    foreach (var r in results)
                    {
                        var json = JsonHelper.Serialize(r);
                        writer.Write(json);
                        writer.Write(seperator);
                    }
                }
            }

            DataUpdateLog.SingleUpdate(nameof(TraficKeywords), keywords.Id, DataUpdateType.Update);
        }