using Microsoft.Extensions.Configuration; 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; using Azure.Cosmos; using System.Net.Http; using TEAMModelOS.SDK.Models.Cosmos.School; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Models; using HTEXLib.COMM.Helpers; namespace TEAMModelOS.FunctionV4.CosmosDB { public class TriggerExamImport { public static async Task Trigger(CoreAPIHttpService _coreAPIHttpService, AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing, CosmosClient client, JsonElement input, TriggerData data, IHttpClientFactory _httpClient, IConfiguration _configuration,AzureRedisFactory _azureRedis) { ExamImport examImport= input.ToObject(); if (examImport != null) { HashSet ids = new HashSet(); foreach (var x in examImport.subjects) { foreach (var y in x.students) { string id = $"{examImport.year}-{examImport.semesterId}-{y.id}"; ids.Add(id); } } HashSet overallEducations = new HashSet(); string sql = $"select value c from c where c.id in ({string.Join(",", ids.Select(z => $"'{z}'"))}) and c.periodId='{examImport.periodId}' "; var result = await client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetList(sql, $"OverallEducation-{examImport.school}"); IEnumerable notInDbIds = null; if (result.list.IsNotEmpty()) { notInDbIds= ids.Except(result.list.Select(x => x.id)); overallEducations = new HashSet(result.list); } else { notInDbIds = ids; } HashSet overallEducationChanged = new HashSet(); foreach (var x in examImport.subjects) { foreach (var y in x.students) { string id = $"{examImport.year}-{examImport.semesterId}-{y.id}"; var overallEducation = overallEducations.Where(z => z.id.Equals(id)).FirstOrDefault(); if (overallEducation != null) { EducationScore exam = null; if (x.id.Equals("subject_music") || x.id.Equals("subject_painting")) { exam = overallEducation.art.Find(f => f.examId.Equals(examImport.id)); } else { exam = overallEducation.intelligence.Find(f => f.examId.Equals(examImport.id)); } if (exam != null) { var item = exam.itemScore.Find(f => f.id.Equals(x.id)); if (item != null) { item.name= x.name; if (item.score != y.score) { overallEducationChanged.Add(overallEducation); item.score = y.score; } if (item.totalScore != x.items.Sum(b => b.score)) { overallEducationChanged.Add(overallEducation); item.totalScore = x.items.Sum(b => b.score); } item.type = x.name; } else { overallEducationChanged.Add(overallEducation); exam.itemScore.Add(new ItemScore { name = x.name, score = y.score, totalScore = x.items.Sum(b => b.score), type = x.name, id = x.id , time=examImport.time}); } if (exam.sumScore != exam.itemScore.Sum(x => x.score)) { overallEducationChanged.Add(overallEducation); //exam.sumScore = examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score); exam.sumScore = exam.itemScore.Sum(x => x.score); } if (exam.totalScore != exam.itemScore.Sum(x => x.totalScore)) { overallEducationChanged.Add(overallEducation); exam.totalScore = exam.itemScore.Sum(x => x.totalScore); // exam.totalScore = examImport.subjects.SelectMany(t => t.items).Sum(n => n.score); } if (exam.rate != examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score) * 1.0 / examImport.subjects.SelectMany(t => t.items).Sum(n => n.score)) { overallEducationChanged.Add(overallEducation); exam.rate = examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score) * 1.0 / examImport.subjects.SelectMany(t => t.items).Sum(n => n.score); } exam.examType = examImport.type; exam.examDate = examImport.time; exam.examName=examImport.name; } else { exam = new EducationScore { examName = examImport.name, examId = examImport.id, examDate = examImport.time, examType = examImport.type, //totalScore = examImport.subjects.SelectMany(t => t.items).Sum(n => n.score), //sumScore = examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score), rate = examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score) * 1.0 / examImport.subjects.SelectMany(t => t.items).Sum(n => n.score), itemScore = new List { new ItemScore { name = x.name, score = y.score, totalScore = x.items.Sum(b => b.score), type = x.name, time = examImport.time, id = x.id } } }; exam.totalScore = exam.itemScore.Sum(x => x.totalScore); exam.sumScore = exam.itemScore.Sum(x => x.score); if (x.id.Equals("subject_music") || x.id.Equals("subject_painting")) { overallEducation.art.Add(exam); } else { overallEducation.intelligence.Add(exam); } overallEducationChanged.Add(overallEducation); } } else { EducationScore exam = new EducationScore { examName = examImport.name, examId = examImport.id, examDate = examImport.time, examType = examImport.type, //totalScore = examImport.subjects.SelectMany(t => t.items).Sum(n => n.score), //sumScore = examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score), rate = examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score) * 1.0 / examImport.subjects.SelectMany(t => t.items).Sum(n => n.score), itemScore = new List { new ItemScore { id = x.id, time = examImport.time, name = x.name, score = y.score, totalScore = x.items.Sum(b => b.score), type = x.name } } }; exam.totalScore = exam.itemScore.Sum(x => x.totalScore); exam.sumScore = exam.itemScore.Sum(x => x.score); overallEducation = new OverallEducation { id = id, code = $"OverallEducation-{examImport.school}", pk = "OverallEducation", periodId = examImport.periodId, year = examImport.year, semesterId = examImport.semesterId, schoolCode = examImport.school, studentId = y.id, name = y.name, classId = y.classId, stuYear = y.stuYear, intelligence = x.id.Equals("subject_music") || x.id.Equals("subject_painting") ? new List { }:new List { exam }, art= x.id.Equals("subject_music") || x.id.Equals("subject_painting") ? new List { exam } : new List { }, }; overallEducationChanged.Add(overallEducation); overallEducations.Add(overallEducation); } } } foreach (var item in overallEducationChanged) { await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS,Constant.Student).UpsertItemAsync(item,new PartitionKey(item.code)); string key = $"OverallEducation:{item.schoolCode}:{item.periodId}:{item.year}:{item.semesterId}:{item.classId}"; await _azureRedis.GetRedisClient(8).HashSetAsync(key, item.studentId, item.ToJsonString()); await _azureRedis.GetRedisClient(8).KeyExpireAsync(key, new TimeSpan(180 * 24, 0, 0)); } } } } }