using Microsoft.Azure.Cosmos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Models.Cosmos.Common;
using TEAMModelOS.SDK.Models.Cosmos.Normal;
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();
if (!string.IsNullOrWhiteSpace(evaluationSyncInfo.shortCode))
{
evaluationSyncInfo.shortCode = $"{MurmurHash3.Hash32(exam.id)}";
}
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,
paperCount= group.list.Count,
paperIds= group.list.Select(x => x.id).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.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_22"));
foreach (var item in quota_22.task)
{
}
}
}
break;
default:
break;
}
var listInfo= await GroupListService.GetMemberByListids(_coreAPIHttpService, azureCosmos.GetCosmosClient(), _dingDing, evaluationSyncInfo.grouplist, schoolCode);
evaluationSyncInfo.studentCount = listInfo.rmembers.Count();
}
}
}