private void button_Sign_Click(object sender, EventArgs e) { var examId = new Guid(textBox_ExamId.Text); var user = new EduEntities().Base_User.Take(int.Parse(numericUpDown_TotalCount.Text)); var rst = ExamData.BulkUserToExam(examId, int.Parse(numericUpDown_PageCount.Text), user.Select(d => d.UserId).ToArray()); MessageBox.Show(rst); }
public static string BulkUserToExam(Guid examId, int maxSignCount = 1000, params string[] userIds) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int totalPaperCount = 0; int totalPaperDetailCount = 0; using (var eduContext = new EduEntities()) { var examPapers = eduContext.Exam_Papers.Where(d => d.ExamId == examId).ToList(); //试卷 var examPaperJson = new Dictionary <Guid, PaperJson>(); //试卷内容 foreach (var paper in examPapers) { examPaperJson.Add(paper.PaperId, JsonConvert.DeserializeObject <PaperJson>(paper.PaperFormJson)); } var random = new Random(Guid.NewGuid().GetHashCode()); var lastExamNum = GetExamLastNum(examId); //最后的考号 for (int pageIndex = 0; pageIndex <= (userIds.Length / maxSignCount); pageIndex++) { var pageUser = userIds.Skip(pageIndex * maxSignCount).Take(maxSignCount).ToList(); //每页数量 var pageStudentPapers = new List <Exam_StudentPapers>(); //考生试卷 var pageStudentPaperDetails = new List <Exam_StudentPaperDetail>(); //考生答题记录 foreach (var userId in pageUser) { var selectPaper = examPapers[random.Next(0, examPapers.Count)]; //选中的试卷 var studentPaper = new Exam_StudentPapers() { StudentPaperId = Guid.NewGuid(), ExamId = examId, PaperId = selectPaper.PaperId, StudentPaperFormJson = selectPaper.PaperFormJson, UserId = new Guid(userId), CreatedDate = DateTime.Now, ExamNumber = lastExamNum++ }; pageStudentPapers.Add(studentPaper); pageStudentPaperDetails.AddRange(GetExamStudentPaperDetailsFromPaperJson(studentPaper.StudentPaperId, examPaperJson[selectPaper.PaperId])); } try { totalPaperCount += eduContext.BulkInsert(pageStudentPapers); } catch (Exception e) { throw new Exception($"批量保存试卷出错!{e.Message}"); } try { totalPaperDetailCount += eduContext.BulkInsert(pageStudentPaperDetails); } catch (Exception e) { throw new Exception($"批量保存答题记录出错!{e.Message}"); } } } stopwatch.Stop(); return($"共{totalPaperCount}条试卷 共{totalPaperDetailCount}条答题记录 耗时{stopwatch.ElapsedMilliseconds}"); }