|
@@ -15,6 +15,213 @@ namespace IES.ExamServer.Services
|
|
|
public class ManageService
|
|
|
{
|
|
|
|
|
|
+
|
|
|
+ public static (List<EvaluationStudentPaper> roundStudentPapers, List<EvaluationMember> members, List<EvaluationStudentResult> results, int code, string msg)
|
|
|
+ AssignStudentPaper(EvaluationClient evaluationClient, EvaluationRoundSetting setting, CenterServiceConnectionService _connectionService,LiteDBFactory _liteDBFactory,ILogger _logger)
|
|
|
+ {
|
|
|
+ int code = 200;
|
|
|
+ string msg = string.Empty;
|
|
|
+ List<EvaluationStudentPaper> roundStudentPapers = new List<EvaluationStudentPaper>();
|
|
|
+ List<EvaluationMember> members = new List<EvaluationMember>();
|
|
|
+ List<EvaluationStudentResult> results = new List<EvaluationStudentResult>();
|
|
|
+ string packagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "package");
|
|
|
+ string evaluationPath = Path.Combine(packagePath, evaluationClient.id!);
|
|
|
+ string evaluationDataPath = Path.Combine(evaluationPath, "data");
|
|
|
+ string path_groupList = Path.Combine(evaluationDataPath, "groupList.json");
|
|
|
+ if (System.IO.File.Exists(path_groupList))
|
|
|
+ {
|
|
|
+ JsonNode? jsonNode = System.IO.File.ReadAllText(path_groupList).ToObject<JsonNode>();
|
|
|
+ if (jsonNode!=null && jsonNode["groupList"]!=null)
|
|
|
+ {
|
|
|
+ List<EvaluationGroupList>? groupList = jsonNode["groupList"]?.ToObject<List<EvaluationGroupList>>();
|
|
|
+ if (groupList!=null)
|
|
|
+ {
|
|
|
+ bool isAllContained = setting.groupList.All(x => groupList.Any(y => y.id == x.id));
|
|
|
+ if (isAllContained)
|
|
|
+ {
|
|
|
+ foreach (var item in setting.groupList)
|
|
|
+ {
|
|
|
+ EvaluationGroupList? groupListItem = groupList.Find(x => x.id == item.id);
|
|
|
+ if (groupListItem!=null)
|
|
|
+ {
|
|
|
+ groupListItem.members.ForEach(x =>
|
|
|
+ {
|
|
|
+ x.schoolId=_connectionService?.serverDevice?.school?.id;
|
|
|
+ x.evaluationId=evaluationClient.id;
|
|
|
+ x.classId= groupListItem.id;
|
|
|
+ x.periodId= groupListItem.periodId;
|
|
|
+ x.roundId=setting.id;
|
|
|
+ x.className= groupListItem.name;
|
|
|
+ x.year= groupListItem.year;
|
|
|
+ });
|
|
|
+ members.AddRange(groupListItem.members);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var database = _liteDBFactory.GetLiteDatabase();
|
|
|
+ try
|
|
|
+ {
|
|
|
+
|
|
|
+ var transaction = database.BeginTrans();
|
|
|
+
|
|
|
+ //清空数据库,重新插入
|
|
|
+ database.GetCollection<EvaluationMember>().DeleteAll();
|
|
|
+ //插入
|
|
|
+ database.GetCollection<EvaluationMember>().Upsert(members);
|
|
|
+ foreach (var subject in evaluationClient.subjects)
|
|
|
+ {
|
|
|
+ var studentPaperIds = members.Select(x => ShaHashHelper.GetSHA1($"{evaluationClient.id}{subject.examId}{subject.subjectId}{x.id}")).ToList() ;
|
|
|
+ List<EvaluationStudentPaper> evaluationStudentPapers = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentPaper>()
|
|
|
+ .Find(x => studentPaperIds.Contains(x.id!)).ToList();
|
|
|
+ List<EvaluationStudentPaper> studentPapers = new List<EvaluationStudentPaper>();
|
|
|
+ int paperIndex = 0;
|
|
|
+ int paperCount = subject.papers.Count();
|
|
|
+ //先把试卷顺序打乱
|
|
|
+ subject.papers =subject.papers.OrderBy(x => Guid.NewGuid().ToString()).ToList();
|
|
|
+ //将学生顺序打乱
|
|
|
+ members = members.OrderBy(x => Guid.NewGuid().ToString()).ToList();
|
|
|
+ foreach (var member in members)
|
|
|
+ {
|
|
|
+ SubjectExamPaper subjectExamPaper = subject.papers[paperIndex];
|
|
|
+ string studentPaperId = ShaHashHelper.GetSHA1($"{evaluationClient.id}{subject.examId}{subject.subjectId}{member.id}");
|
|
|
+ var paper = evaluationStudentPapers.Find(x => x.id!.Equals(studentPaperId));
|
|
|
+ if (paper==null)
|
|
|
+ {
|
|
|
+ EvaluationStudentPaper studentPaper = new EvaluationStudentPaper
|
|
|
+ {
|
|
|
+ studentId=member.id,
|
|
|
+ studentName=member.name,
|
|
|
+ classId=member.classId,
|
|
|
+ className=member.className,
|
|
|
+ evaluationId=evaluationClient.id,
|
|
|
+ examId=subject.examId,
|
|
|
+ examName=subject.examName,
|
|
|
+ subjectId=subject.subjectId,
|
|
|
+ subjectName=subject.subjectName,
|
|
|
+ paperId=subjectExamPaper.paperId,
|
|
|
+ paperName=subjectExamPaper.paperName,
|
|
|
+ questionCount=subjectExamPaper.questionCount,
|
|
|
+ id=studentPaperId,
|
|
|
+ };
|
|
|
+ studentPapers.Add(studentPaper);
|
|
|
+ // 移动到下一个试卷
|
|
|
+ paperIndex = (paperIndex + 1) % paperCount;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (studentPapers.Count>0)
|
|
|
+ {
|
|
|
+ _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentPaper>().Upsert(studentPapers);
|
|
|
+ roundStudentPapers.AddRange(studentPapers);
|
|
|
+ }
|
|
|
+ if (evaluationStudentPapers!=null && evaluationStudentPapers.Count()>0)
|
|
|
+ {
|
|
|
+ roundStudentPapers.AddRange(evaluationStudentPapers);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
|
|
+ IEnumerable<EvaluationStudentResult> studentResults = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>()
|
|
|
+ .Find(x => members.Select(x => x.id).Contains(x.studentId)&&!string.IsNullOrWhiteSpace(x.evaluationId) && x.evaluationId.Equals(evaluationClient.id));
|
|
|
+ foreach (var member in members)
|
|
|
+ {
|
|
|
+ EvaluationStudentResult? studentResult = null;
|
|
|
+ //sha1(evaluationId-schoolId-studentId)
|
|
|
+ string resultId = ShaHashHelper.GetSHA1(evaluationClient.id+_connectionService?.serverDevice?.school?.id+member.id);
|
|
|
+ var result = studentResults.Where(x => x.id!.Equals(resultId) && !string.IsNullOrWhiteSpace(x.studentId) && x.studentId.Equals(member.id));
|
|
|
+ if (result==null || result.Count()==0)
|
|
|
+ {
|
|
|
+ studentResult = new EvaluationStudentResult()
|
|
|
+ {
|
|
|
+ id = resultId,
|
|
|
+ evaluationId = evaluationClient.id,
|
|
|
+ schoolId = _connectionService?.serverDevice?.school?.id,
|
|
|
+ studentId = member.id,
|
|
|
+ studentName = member.name,
|
|
|
+ classId = member.classId,
|
|
|
+ className = member.className,
|
|
|
+ ownerId= evaluationClient.ownerId,
|
|
|
+ scope= evaluationClient.scope,
|
|
|
+ type= evaluationClient.type,
|
|
|
+ pid= evaluationClient.pid,
|
|
|
+ };
|
|
|
+ var studentPapers = roundStudentPapers.FindAll(x => !string.IsNullOrWhiteSpace(x.studentId)
|
|
|
+ && x.studentId.Equals(member.id) &&!string.IsNullOrWhiteSpace(x.evaluationId) && x.evaluationId.Equals(evaluationClient.id));
|
|
|
+ if (studentPapers.IsNotEmpty())
|
|
|
+ {
|
|
|
+ foreach (var studentPaper in studentPapers)
|
|
|
+ {
|
|
|
+ studentResult.subjectResults.Add(new EvaluationSubjectResult()
|
|
|
+ {
|
|
|
+ id = ShaHashHelper.GetSHA1(evaluationClient.id+studentPaper.examId+studentPaper.subjectId+member.id),
|
|
|
+ evaluationId = studentPaper.evaluationId,
|
|
|
+ examId = studentPaper.examId,
|
|
|
+ examName = studentPaper.examName,
|
|
|
+ subjectId = studentPaper.subjectId,
|
|
|
+ subjectName = studentPaper.subjectName,
|
|
|
+ paperId = studentPaper.paperId,
|
|
|
+ paperName = studentPaper.paperName,
|
|
|
+ questionCount=studentPaper.questionCount,
|
|
|
+ createTime=now
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>().Upsert(studentResult);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ studentResult = result.First();
|
|
|
+ studentResult.studentName = member.name;
|
|
|
+ studentResult.classId = member.classId;
|
|
|
+ studentResult.className = member.className;
|
|
|
+ var studentPapers = roundStudentPapers.FindAll(x => !string.IsNullOrWhiteSpace(x.studentId)
|
|
|
+ && x.studentId.Equals(member.id) &&!string.IsNullOrWhiteSpace(x.evaluationId) && x.evaluationId.Equals(evaluationClient.id));
|
|
|
+ if (studentPapers.IsNotEmpty())
|
|
|
+ {
|
|
|
+ foreach (var studentPaper in studentPapers)
|
|
|
+ {
|
|
|
+ string subjectResultId = ShaHashHelper.GetSHA1(evaluationClient.id+studentPaper.examId+studentPaper.subjectId+member.id);
|
|
|
+ var subjectResult = studentResult.subjectResults.Where(x => x.id!.Equals(subjectResultId)).FirstOrDefault();
|
|
|
+ if (subjectResult==null)
|
|
|
+ {
|
|
|
+ subjectResult= new EvaluationSubjectResult()
|
|
|
+ {
|
|
|
+ id = ShaHashHelper.GetSHA1(evaluationClient.id+studentPaper.examId+studentPaper.subjectId+member.id),
|
|
|
+ evaluationId = studentPaper.evaluationId,
|
|
|
+ examId = studentPaper.examId,
|
|
|
+ examName = studentPaper.examName,
|
|
|
+ subjectId = studentPaper.subjectId,
|
|
|
+ subjectName = studentPaper.subjectName,
|
|
|
+ paperId = studentPaper.paperId,
|
|
|
+ paperName = studentPaper.paperName,
|
|
|
+ questionCount= studentPaper.questionCount,
|
|
|
+ createTime=now
|
|
|
+ };
|
|
|
+ }
|
|
|
+ studentResult.subjectResults.Add(subjectResult);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (studentResult!=null)
|
|
|
+ {
|
|
|
+ results.Add(studentResult);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (results.Count>0)
|
|
|
+ {
|
|
|
+ _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationStudentResult>().Upsert(results);
|
|
|
+ }
|
|
|
+ database.Commit();
|
|
|
+
|
|
|
+ } catch (Exception ex) {
|
|
|
+ database.Rollback();
|
|
|
+ _logger.LogError($"Transaction failed: {ex.Message}");
|
|
|
+ throw; // 重新抛出异常
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return (roundStudentPapers, members, results, code, msg);
|
|
|
+ }
|
|
|
public async static Task<(EvaluationClient? evaluationCloud, string centerCode, string centerMsg)> GetEvaluationFromCenter(string? x_auth_token, IConfiguration _configuration,IHttpClientFactory _httpClientFactory, string shortCode, string evaluationId)
|
|
|
{
|
|
|
EvaluationClient? evaluationCloud = null;
|