123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- 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
- {
- /// <summary>
- /// 活动数据打包
- /// </summary>
- /// <param name="id"></param>
- /// <param name="scope"></param>
- /// <param name="owner"></param>
- /// <param name="type"></param>
- /// <param name="azureCosmos"></param>
- /// <param name="azureStorage"></param>
- 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<EvaluationSyncInfo>();
- }
- 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<ExamInfo>();
-
- 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<string> grouplist = new HashSet<string>();
- 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<ArtEvaluation>();
-
- // 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.dataTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
- HashSet<string> grouplist = new HashSet<string>();
- 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<ExamInfo>() ;
- 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<BlobHashInfo> blobs = new List<BlobHashInfo>();
- 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)));
- }
- }
- }
- }
|