TriggerExamImport.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 DocumentFormat.OpenXml.Office2010.Excel;
  16. using HTEXLib.COMM.Helpers;
  17. namespace TEAMModelOS.FunctionV4.CosmosDB
  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)
  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. 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)).First();
  53. if (overallEducation != null)
  54. {
  55. var exam = overallEducation.intelligence.Find(f => f.examId.Equals(x.id));
  56. if (exam != null)
  57. {
  58. var item = exam.itemScore.Find(f => f.id.Equals(x.id));
  59. if (item != null)
  60. {
  61. item.name= x.name;
  62. item.score = y.score;
  63. item.totalScore = x.items.Sum(b => b.score);
  64. item.type = examImport.type;
  65. }
  66. else {
  67. exam.itemScore.Add(new ItemScore { name = x.name, score = y.score, totalScore = x.items.Sum(b => b.score), type = examImport.type, id = x.id });
  68. }
  69. exam.sumScore = examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score);
  70. exam.totalScore = examImport.subjects.SelectMany(t => t.items).Sum(n => n.score);
  71. 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);
  72. exam.examType = examImport.type;
  73. exam.examDate = examImport.time;
  74. exam.examName=examImport.name;
  75. }
  76. else {
  77. overallEducation.intelligence.Add(new EducationScore
  78. {
  79. examName = examImport.name,
  80. examId = examImport.id,
  81. examDate = examImport.time,
  82. examType = examImport.type,
  83. totalScore = examImport.subjects.SelectMany(t => t.items).Sum(n => n.score),
  84. sumScore = examImport.subjects.SelectMany(z => z.students).Where(b => b.id.Equals(y.id)).Sum(v => v.score),
  85. 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),
  86. itemScore = new List<ItemScore> { new ItemScore { name = x.name, score = y.score, totalScore = x.items.Sum(b => b.score), type = examImport.type ,id=x.id} }
  87. });
  88. }
  89. }
  90. else {
  91. overallEducation = new OverallEducation {
  92. id = id,
  93. code = $"OverallEducation-{examImport.school}",
  94. pk = "OverallEducation",
  95. periodId = examImport.periodId,
  96. year = examImport.year,
  97. semesterId = examImport.semesterId,
  98. schoolCode = examImport.school,
  99. studentId = y.id,
  100. name = y.name,
  101. classId = y.classId,
  102. stuYear = y.stuYear,
  103. intelligence = new List<EducationScore> { new EducationScore {
  104. examName=examImport.name,
  105. examId=examImport.id,
  106. examDate=examImport.time,
  107. examType=examImport.type,
  108. totalScore= examImport.subjects.SelectMany(t=>t.items).Sum(n=>n.score),
  109. sumScore= examImport.subjects.SelectMany(z=>z.students).Where(b=>b.id.Equals(y.id)).Sum(v=>v.score),
  110. 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),
  111. itemScore= new List<ItemScore>{ new ItemScore { name=x.name,score=y.score,totalScore=x.items.Sum(b=>b.score),type=examImport.type } }
  112. } }
  113. };
  114. overallEducations.Add(overallEducation);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }