123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- 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 Microsoft.Azure.Cosmos;
- using System.Net.Http;
- using TEAMModelOS.SDK.Models.Cosmos.School;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.Function;
- namespace TEAMModelOS.CosmosDBTriggers
- {
- 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<ExamImport>();
- if (examImport != null) {
- HashSet<string> ids = new HashSet<string>();
- foreach (var x in examImport.subjects)
- {
- foreach (var y in x.students)
- {
- string id = $"{examImport.year}-{examImport.semesterId}-{y.id}";
- ids.Add(id);
- }
- }
- HashSet<OverallEducation> overallEducations = new HashSet<OverallEducation>();
- 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<OverallEducation>(sql, $"OverallEducation-{examImport.school}");
- IEnumerable<string> notInDbIds = null;
- if (result.list.IsNotEmpty())
- {
- notInDbIds= ids.Except(result.list.Select(x => x.id));
- overallEducations = new HashSet<OverallEducation>(result.list);
- }
- else {
- notInDbIds = ids;
- }
- HashSet<OverallEducation> overallEducationChanged = new HashSet<OverallEducation>();
- 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<ItemScore> { 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<ItemScore> { 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<EducationScore> { }:new List<EducationScore> { exam },
- art= x.id.Equals("subject_music") || x.id.Equals("subject_painting") ? new List<EducationScore> { exam } : new List<EducationScore> { },
- };
- 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));
- }
- }
- }
- }
- }
|