LessonStatisWay.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using Microsoft.Azure.Cosmos;
  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. namespace TEAMModelOS.SDK.Models.Service.BI
  10. {
  11. public class LessonStatisWay
  12. {
  13. /// <summary>
  14. /// 依据学校Id、教师Id统计课例总数
  15. /// </summary>
  16. /// <param name="cosmosClient"></param>
  17. /// <param name="scIds"></param>
  18. /// <param name="tecIds"></param>
  19. /// <returns></returns>
  20. public async static Task<int> GetAll(CosmosClient cosmosClient, List<string> scIds = null, List<string> tecIds = null)
  21. {
  22. int totals = 0;
  23. string sqlTxt = "select count(c.id) as totals from c";
  24. if (scIds.Count > 0)
  25. {
  26. foreach (string sc in scIds)
  27. {
  28. await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{sc}") }))
  29. {
  30. using var json = await JsonDocument.ParseAsync(itemTeac.Content);
  31. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
  32. {
  33. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  34. {
  35. totals += obj.GetProperty("totals").GetInt32();
  36. }
  37. }
  38. }
  39. }
  40. }
  41. if (tecIds.Count > 0)
  42. {
  43. foreach (string sc in scIds)
  44. {
  45. string sqlTecTxt = $"select count(c.id) from c where c.id='{sc}'";
  46. await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord") }))
  47. {
  48. using var json = await JsonDocument.ParseAsync(itemTeac.Content);
  49. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
  50. {
  51. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  52. {
  53. totals += obj.GetProperty("totals").GetInt32();
  54. }
  55. }
  56. }
  57. }
  58. }
  59. return totals;
  60. }
  61. /// <summary>
  62. /// 依据学校Id查询学校和学校里面教师的课例统计
  63. /// </summary>
  64. /// <param name="cosmosClient"></param>
  65. /// <param name="schoolId"></param>
  66. /// <returns></returns>
  67. public async static Task<int> GetSchoolIdLessonCount(CosmosClient cosmosClient, string schoolId)
  68. {
  69. int totals = 0;
  70. string LessonSqlTxt = "select value(count(c.id)) from c";
  71. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<int>(queryText: LessonSqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{schoolId}") }))
  72. {
  73. totals += item;
  74. }
  75. List<string> tecIdS = new();
  76. string managerSql = $"SELECT value(c.id) FROM c WHERE ARRAY_CONTAINS(c.roles, 'teacher', true) AND c.pk = 'Teacher' AND c.status = 'join'";
  77. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<string>(queryText: managerSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{schoolId}") }))
  78. {
  79. tecIdS.Add(item);
  80. }
  81. foreach (var itemId in tecIdS)
  82. {
  83. string tecLessSQL = $"select value(count(c.id)) from c where c.tmdid='{itemId}'";
  84. await foreach (var itemCount in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIteratorSql<int>(queryText: tecLessSQL, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("LessonRecord") }))
  85. {
  86. totals += itemCount;
  87. }
  88. }
  89. return totals;
  90. }
  91. /// <summary>
  92. /// 学校按年级统计课例
  93. /// </summary>
  94. /// <param name="records"></param>
  95. /// <returns></returns>
  96. public static List<(string name, string periodId, int count)> GetGradeCount(List<LessAnalyse> records)
  97. {
  98. List<(string gradeIn, string periodId)> grades = new();
  99. List<(string name, string periodId, int count)> gCount = new();
  100. foreach (var record in records)
  101. {
  102. foreach (string gId in record.grade)
  103. {
  104. var temp = grades.Find(x => x.gradeIn.Equals(gId) && x.periodId.Equals(record.periodId));
  105. if (temp.gradeIn == null && temp.periodId == null)
  106. {
  107. grades.Add((gId, record.periodId));
  108. }
  109. }
  110. }
  111. foreach (var gId in grades)
  112. {
  113. var c = records.Where(r => r.grade.Contains(gId.gradeIn) && r.periodId.Equals(gId.periodId)).Count();
  114. gCount.Add((gId.gradeIn, gId.periodId, c));
  115. }
  116. return gCount;
  117. }
  118. /// <summary>
  119. /// 学校按年级统计课例
  120. /// </summary>
  121. /// <param name="records"></param>
  122. /// <returns></returns>
  123. public static List<(string name, string periodId, int count)> GetGradeCount(List<LessonRecord> records)
  124. {
  125. List<(string gradeIn, string periodId)> grades = new();
  126. List<(string name, string periodId, int count)> gCount = new();
  127. foreach (var record in records)
  128. {
  129. foreach (string gId in record.grade)
  130. {
  131. var temp = grades.Find(x => x.gradeIn.Equals(gId) && x.periodId.Equals(record.periodId));
  132. if (temp.gradeIn == null && temp.periodId == null)
  133. {
  134. grades.Add((gId, record.periodId));
  135. }
  136. }
  137. }
  138. foreach (var gId in grades)
  139. {
  140. var c = records.Where(r => r.grade.Contains(gId.gradeIn) && r.periodId.Equals(gId.periodId)).Count();
  141. gCount.Add((gId.gradeIn, gId.periodId, c));
  142. }
  143. return gCount;
  144. }
  145. }
  146. }