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