TriggerExamImport.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Microsoft.Extensions.Configuration;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK;
  10. using Azure.Cosmos;
  11. using System.Net.Http;
  12. using TEAMModelOS.SDK.Models.Cosmos.School;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.SDK.Models;
  15. using HTEXLib.COMM.Helpers;
  16. using TEAMModelOS.Function;
  17. namespace TEAMModelOS.CosmosDBTriggers
  18. {
  19. public class TriggerExamImport
  20. {
  21. public static async Task Trigger(CoreAPIHttpService _coreAPIHttpService, AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  22. CosmosClient client, JsonElement input, TriggerData data, IHttpClientFactory _httpClient, IConfiguration _configuration,AzureRedisFactory _azureRedis)
  23. {
  24. ExamImport examImport= input.ToObject<ExamImport>();
  25. if (examImport != null) {
  26. HashSet<string> ids = new HashSet<string>();
  27. foreach (var x in examImport.subjects)
  28. {
  29. foreach (var y in x.students)
  30. {
  31. string id = $"{examImport.year}-{examImport.semesterId}-{y.id}";
  32. ids.Add(id);
  33. }
  34. }
  35. HashSet<OverallEducation> overallEducations = new HashSet<OverallEducation>();
  36. string sql = $"select value c from c where c.id in ({string.Join(",", ids.Select(z => $"'{z}'"))}) and c.periodId='{examImport.periodId}' ";
  37. var result = await client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetList<OverallEducation>(sql, $"OverallEducation-{examImport.school}");
  38. IEnumerable<string> notInDbIds = null;
  39. if (result.list.IsNotEmpty())
  40. {
  41. notInDbIds= ids.Except(result.list.Select(x => x.id));
  42. overallEducations = new HashSet<OverallEducation>(result.list);
  43. }
  44. else {
  45. notInDbIds = ids;
  46. }
  47. HashSet<OverallEducation> overallEducationChanged = new HashSet<OverallEducation>();
  48. foreach (var x in examImport.subjects)
  49. {
  50. foreach (var y in x.students)
  51. {
  52. string id = $"{examImport.year}-{examImport.semesterId}-{y.id}";
  53. var overallEducation = overallEducations.Where(z => z.id.Equals(id)).FirstOrDefault();
  54. if (overallEducation != null)
  55. {
  56. EducationScore exam = null;
  57. if (x.id.Equals("subject_music") || x.id.Equals("subject_painting"))
  58. {
  59. exam = overallEducation.art.Find(f => f.examId.Equals(examImport.id));
  60. }
  61. else {
  62. exam = overallEducation.intelligence.Find(f => f.examId.Equals(examImport.id));
  63. }
  64. if (exam != null)
  65. {
  66. var item = exam.itemScore.Find(f => f.id.Equals(x.id));
  67. if (item != null)
  68. {
  69. item.name= x.name;
  70. if (item.score != y.score) {
  71. overallEducationChanged.Add(overallEducation);
  72. item.score = y.score;
  73. }
  74. if (item.totalScore != x.items.Sum(b => b.score)) {
  75. overallEducationChanged.Add(overallEducation);
  76. item.totalScore = x.items.Sum(b => b.score);
  77. }
  78. item.type = x.name;
  79. }
  80. else {
  81. overallEducationChanged.Add(overallEducation);
  82. 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});
  83. }
  84. if (exam.sumScore != exam.itemScore.Sum(x => x.score)) {
  85. overallEducationChanged.Add(overallEducation);
  86. //exam.sumScore = examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score);
  87. exam.sumScore = exam.itemScore.Sum(x => x.score);
  88. }
  89. if (exam.totalScore != exam.itemScore.Sum(x => x.totalScore))
  90. {
  91. overallEducationChanged.Add(overallEducation);
  92. exam.totalScore = exam.itemScore.Sum(x => x.totalScore);
  93. // exam.totalScore = examImport.subjects.SelectMany(t => t.items).Sum(n => n.score);
  94. }
  95. 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))
  96. {
  97. overallEducationChanged.Add(overallEducation);
  98. 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);
  99. }
  100. exam.examType = examImport.type;
  101. exam.examDate = examImport.time;
  102. exam.examName=examImport.name;
  103. }
  104. else {
  105. exam = new EducationScore
  106. {
  107. examName = examImport.name,
  108. examId = examImport.id,
  109. examDate = examImport.time,
  110. examType = examImport.type,
  111. //totalScore = examImport.subjects.SelectMany(t => t.items).Sum(n => n.score),
  112. //sumScore = examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score),
  113. 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),
  114. 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 } }
  115. };
  116. exam.totalScore = exam.itemScore.Sum(x => x.totalScore);
  117. exam.sumScore = exam.itemScore.Sum(x => x.score);
  118. if (x.id.Equals("subject_music") || x.id.Equals("subject_painting"))
  119. {
  120. overallEducation.art.Add(exam);
  121. }
  122. else
  123. {
  124. overallEducation.intelligence.Add(exam);
  125. }
  126. overallEducationChanged.Add(overallEducation);
  127. }
  128. }
  129. else {
  130. EducationScore exam = new EducationScore
  131. {
  132. examName = examImport.name,
  133. examId = examImport.id,
  134. examDate = examImport.time,
  135. examType = examImport.type,
  136. //totalScore = examImport.subjects.SelectMany(t => t.items).Sum(n => n.score),
  137. //sumScore = examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score),
  138. 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),
  139. 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 } }
  140. };
  141. exam.totalScore = exam.itemScore.Sum(x => x.totalScore);
  142. exam.sumScore = exam.itemScore.Sum(x => x.score);
  143. overallEducation = new OverallEducation {
  144. id = id,
  145. code = $"OverallEducation-{examImport.school}",
  146. pk = "OverallEducation",
  147. periodId = examImport.periodId,
  148. year = examImport.year,
  149. semesterId = examImport.semesterId,
  150. schoolCode = examImport.school,
  151. studentId = y.id,
  152. name = y.name,
  153. classId = y.classId,
  154. stuYear = y.stuYear,
  155. intelligence = x.id.Equals("subject_music") || x.id.Equals("subject_painting") ? new List<EducationScore> { }:new List<EducationScore> { exam },
  156. art= x.id.Equals("subject_music") || x.id.Equals("subject_painting") ? new List<EducationScore> { exam } : new List<EducationScore> { },
  157. };
  158. overallEducationChanged.Add(overallEducation);
  159. overallEducations.Add(overallEducation);
  160. }
  161. }
  162. }
  163. foreach (var item in overallEducationChanged) {
  164. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS,Constant.Student).UpsertItemAsync(item,new PartitionKey(item.code));
  165. string key = $"OverallEducation:{item.schoolCode}:{item.periodId}:{item.year}:{item.semesterId}:{item.classId}";
  166. await _azureRedis.GetRedisClient(8).HashSetAsync(key, item.studentId, item.ToJsonString());
  167. await _azureRedis.GetRedisClient(8).KeyExpireAsync(key, new TimeSpan(180 * 24, 0, 0));
  168. }
  169. }
  170. }
  171. }
  172. }