using Azure.Storage.Blobs.Models; using IES.ExamServer.Models; using Microsoft.Azure.Cosmos; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json; using System.Text.Json.Nodes; using System.Threading.Tasks; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Models.Cosmos; using TEAMModelOS.SDK.Models.Cosmos.Normal; using TEAMModelOS.SDK.Models.Cosmos.Student; namespace TEAMModelOS.SDK.Models.Service { public sealed class EvaluationSyncInfoService { /// /// 活动数据打包 /// /// /// /// /// /// /// public static async Task PackageEvaluation( string id,string scope, string owner, string type, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage,CoreAPIHttpService _coreAPIHttpService, DingDing _dingDing) { EvaluationSyncInfo evaluationSyncInfo= null; string schoolCode = null; if (scope.Equals("school")) { schoolCode= owner; } var responseEvaluationSyncInfo = await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).ReadItemStreamAsync(id, new PartitionKey("EvaluationSyncInfo")); if (responseEvaluationSyncInfo.IsSuccessStatusCode) { evaluationSyncInfo= JsonDocument.Parse(responseEvaluationSyncInfo.Content).RootElement.Deserialize(); } else { evaluationSyncInfo=new EvaluationSyncInfo { id = id, scope = scope, owner = owner, type = type, pk="EvaluationSyncInfo", code="ActivitySyncInfo", }; } switch (true) { case bool when (type == "Exam"): { string code = $"Exam-{owner}"; var response = await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Common).ReadItemStreamAsync(id, new PartitionKey(code)); if (response.IsSuccessStatusCode) { ExamInfo exam= JsonDocument.Parse(response.Content).RootElement.Deserialize(); evaluationSyncInfo.name=exam.name; evaluationSyncInfo.subjects = exam.subjects?.Select(x=>new IES.ExamServer.Models.SubjectExam { id=x.id,name=x.name,examId=id}).ToList(); evaluationSyncInfo.dataTime= DateTimeOffset.Now.ToUnixTimeMilliseconds(); HashSet grouplist = new HashSet(); if (exam.classes.IsNotEmpty()) { exam.classes.ForEach(x => { grouplist.Add(x); }); } if (exam.stuLists.IsNotEmpty()) { exam.stuLists.ForEach(x => { grouplist.Add(x); }); } evaluationSyncInfo.grouplist=grouplist.ToList() ; evaluationSyncInfo.paperCount=exam.papers.IsNotEmpty()? exam.papers.Count():0; foreach (var group in exam.papers.GroupBy(x=>x.subjectId).Select(x=>new { key = x.Key,list= x.ToList()})) { var subject= exam.subjects.Find(x => x.id.Equals(group.key)); if (subject!=null) { evaluationSyncInfo.subjects.Add(new IES.ExamServer.Models.SubjectExam { id=subject.id, name=subject.name, examId=id, papers= group.list.Select(x=>new SubjectExamPaper {paperId= x.id,subjectId=x.subjectId,paperName=x.name,blob=x.blob }).ToList(), } ); } } } break; } case bool when (type == "Art"): { string code = $"Art-{owner}"; var response = await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Common).ReadItemStreamAsync(id, new PartitionKey(code)); if (response.IsSuccessStatusCode) { ArtEvaluation art = JsonDocument.Parse(response.Content).RootElement.Deserialize(); // evaluationSyncInfo.subjects = art.subjects?.Select(x => new IES.ExamServer.Models.SubjectExam { id=x.id, name=x.name, examId=id }).ToList(); evaluationSyncInfo.name = art.name; evaluationSyncInfo.pid= art.pId; evaluationSyncInfo.dataTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(); HashSet grouplist = new HashSet(); if (art.classes.IsNotEmpty()) { art.classes.ForEach(x => { grouplist.Add(x); }); } if (art.stuLists.IsNotEmpty()) { art.stuLists.ForEach(x => { grouplist.Add(x); }); } if (art.tchLists.IsNotEmpty()) { art.tchLists.ForEach(x => { grouplist.Add(x); }); } evaluationSyncInfo.grouplist=grouplist.ToList(); var quota_22 = art.settings.Find(x => x.id.Equals("quota_21")); foreach (var item in quota_22.task) { if (!string.IsNullOrWhiteSpace(item.acId)) { var subject = art.subjects.Find(x => x.id.Equals(item.subject)); var examResponse= await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Common).ReadItemStreamAsync(item.acId, new PartitionKey($"Exam-{owner}")); if (examResponse.IsSuccessStatusCode) { ExamInfo exam = JsonDocument.Parse(examResponse.Content).RootElement.Deserialize() ; var papers = exam.papers.FindAll(x => x.subjectId.Equals(item.subject)); evaluationSyncInfo.subjects.Add(new IES.ExamServer.Models.SubjectExam { id=item.subject, name=subject.name, examId=item.acId, papers=papers?.Select(x => new SubjectExamPaper { paperId=x.id,paperName=x.name, blob=x.blob, subjectId=x.subjectId }).ToList(), }); } } } } } break; default: break; } if (evaluationSyncInfo.subjects.IsNotEmpty() && evaluationSyncInfo.grouplist.IsNotEmpty()) { long blobTime =-1; long blobSize = 0; long blobCount = 0; if (!string.IsNullOrWhiteSpace(evaluationSyncInfo.shortCode)) { evaluationSyncInfo.shortCode = $"{MurmurHash3.Hash32(evaluationSyncInfo.id)}"; } var listInfo = await GroupListService.GetMemberByListids(_coreAPIHttpService, azureCosmos.GetCosmosClient(), _dingDing, evaluationSyncInfo.grouplist, schoolCode); evaluationSyncInfo.studentCount = listInfo.rmembers.Count(); evaluationSyncInfo.paperCount =evaluationSyncInfo.subjects.Select(x => x.papers).Count(); var client = azureStorage.GetBlobContainerClient(owner); foreach (var subject in evaluationSyncInfo.subjects) { foreach (var paper in subject.papers) { List blobs = new List(); try { await foreach (BlobItem blobItem in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, paper.blob)) { var lastModified = blobItem.Properties.LastModified; if (lastModified.HasValue) { lastModified.Value.ToUnixTimeMilliseconds(); } var hash = blobItem.Properties.ContentHash; var path = blobItem.Name; var size = blobItem.Properties.ContentLength; blobs.Add(new BlobHashInfo { hash = Md5Hash.GetbyteToString(hash), last = lastModified.HasValue ? lastModified.Value.ToUnixTimeMilliseconds() : 0, path = path, size = size.HasValue ? size.Value : 0 }); }; } catch { } long lastTime = blobs.Max(x => x.last); blobTime= lastTime>blobTime?lastTime:blobTime; if (blobs.IsNotEmpty()) { blobSize+=blobs.Sum(x => x.size); blobCount+=blobs.Count; var order = blobs.OrderBy(x => $"{x.path}-{x.hash}-{x.size}-{x.last}"); string blobStr = string.Join(",", order.Select(x=> $"{x.path}-{x.hash}-{x.size}-{x.last}")); // 计算hash,校准路径,文件hash,文件大小,最后修改时间 paper.paperHash= ShaHashHelper.GetSHA1(blobStr); } } } evaluationSyncInfo.blobTime = blobTime> evaluationSyncInfo.blobTime ? blobTime : evaluationSyncInfo.blobTime; evaluationSyncInfo.blobSize = blobSize; evaluationSyncInfo.blobCount = blobCount; evaluationSyncInfo.blobLastHash = evaluationSyncInfo.blobHash; evaluationSyncInfo.blobHash = ShaHashHelper.GetSHA1(string.Join("-", evaluationSyncInfo.subjects.SelectMany(x => x.papers).Select(x => x.paperHash))); } } } }