TriggerExamImport.cs 11 KB

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