SchoolController.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelBI.Models;
  12. using TEAMModelBI.Tool;
  13. using TEAMModelBI.Tool.CosmosBank;
  14. using TEAMModelOS.Models;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Models;
  18. namespace TEAMModelBI.Controllers.Census
  19. {
  20. [Route("school")]
  21. [ApiController]
  22. public class SchoolController : ControllerBase
  23. {
  24. private readonly AzureCosmosFactory _azureCosmos;
  25. private readonly AzureStorageFactory _azureStorage;
  26. private readonly DingDing _dingDing;
  27. private readonly Option _option;
  28. public SchoolController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  29. {
  30. _azureCosmos = azureCosmos;
  31. _azureStorage = azureStorage;
  32. _dingDing = dingDing;
  33. _option = option?.Value;
  34. }
  35. /// <summary>
  36. /// 统计所有分析:基础、课例、活动、资源
  37. /// </summary>
  38. /// <param name="jsonElement"></param>
  39. /// <returns></returns>
  40. [HttpPost("get-all")]
  41. public async Task<IActionResult> GetAll()
  42. {
  43. var cosmosClient = _azureCosmos.GetCosmosClient();
  44. long schoolCount = 0; //学校数量
  45. int tecCount = 0; //教师数量
  46. int stuCount = 0; //学生数量
  47. long roomCount = 0; //教室数量
  48. long wisdomRoomCount = 0; //智慧教室数量
  49. long allClassCount = 0; //所有班级
  50. long allLessCount = 0; //所有课例
  51. long lastYearLessCount = 0; //去年课例
  52. long yearLessCount = 0;//今年课例
  53. long lastWeekLessCount = 0; //上周课例
  54. long weekLessCount = 0; //本周课例
  55. long lastTermLessCount = 0; //上学期课例
  56. long termLessCount = 0; //本学期课例
  57. long allActivityCount = 0; //所有活动
  58. long lastActivityCount = 0; //去年活动
  59. long activityCount = 0; //今年活动
  60. long lastWeekActivitCount = 0; //上周活动
  61. long weekActivitCount = 0; //本周活动
  62. long lastTermActivitCount =0; //上学期活动
  63. long TermActivitCount = 0; //本学期学期活动
  64. long rercCount = 0; //所有资源数量
  65. long weekRercCount = 0; //本周资源数量
  66. long lastWeekRercCount = 0; //上周资源数量
  67. long lastTermRercCount = 0; //上学期资源
  68. long termRercCount = 0; //本学期资源
  69. long lastYearRercCount = 0; //去年资源
  70. long yearRercCount = 0; //去年资源
  71. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  72. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  73. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  74. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  75. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  76. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  77. schoolCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c", "School", "Base"); //所有学校数量
  78. tecCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c", "Teacher", "Base"); //所有教师数量
  79. stuCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c", "Student", "Base"); //所有学生数量
  80. allClassCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c where c.pk = 'Class'", new List<string>() { "School" }); //所有班级数量
  81. roomCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c where c.pk = 'Room'", new List<string>() { "School" }); //所有教室数量
  82. wisdomRoomCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c where c.pk = 'Room' and c.serial != null", new List<string>() { "School" }); //智慧教室数量
  83. List<string> containers = new() { "School", "Teacher" };
  84. string lessSqlTxt = "select count(c.id) as totals from c where c.pk='LessonRecord' and c.startTime>={0} and c.startTime<={1}";
  85. allLessCount = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='LessonRecord'", containers);//所有课例
  86. lastYearLessCount = await CommonFind.FindTotals(cosmosClient, string.Format(lessSqlTxt, lastYearStart, lastYearEnd), containers); //去年课例
  87. yearLessCount = await CommonFind.FindTotals(cosmosClient, string.Format(lessSqlTxt, yearStart, yearEnd), containers); //今年课例
  88. lastWeekLessCount = await CommonFind.FindTotals(cosmosClient, string.Format(lessSqlTxt, lastWeekStart, lastWeekEnd), containers); //上周课例
  89. weekLessCount = await CommonFind.FindTotals(cosmosClient, string.Format(lessSqlTxt, weekStart, weekEnd), containers); //本周课例
  90. lastTermLessCount = await CommonFind.FindTotals(cosmosClient, string.Format(lessSqlTxt, lastTermStart, lastTermEnd), containers); //上学期课例
  91. termLessCount = await CommonFind.FindTotals(cosmosClient, string.Format(lessSqlTxt, termStart, termEnd), containers); //上学期课例
  92. List<string> containerTypes = new() { "Common" };
  93. string typeSqlTxt = "select count(c.id) as totals from c where c.pk='{0}' and c.createTime >={1} and c.createTime<= {2}";
  94. foreach (var type in StaticValue.activityTypes)
  95. {
  96. allActivityCount += await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='{type}' ", containerTypes);//所有活动
  97. lastActivityCount += await CommonFind.FindTotals(cosmosClient, string.Format(typeSqlTxt, type, lastYearStart, lastYearEnd), containerTypes); //去年活动
  98. activityCount += await CommonFind.FindTotals(cosmosClient, string.Format(typeSqlTxt, type, yearStart, yearEnd), containerTypes); //今年活动
  99. lastWeekActivitCount += await CommonFind.FindTotals(cosmosClient, string.Format(typeSqlTxt, type, lastWeekStart, lastWeekEnd), containerTypes); //上周活动
  100. weekActivitCount += await CommonFind.FindTotals(cosmosClient, string.Format(typeSqlTxt, type, weekStart, weekEnd), containerTypes); //本周活动
  101. lastTermActivitCount += await CommonFind.FindTotals(cosmosClient, string.Format(typeSqlTxt, type, lastTermStart, lastTermEnd), containerTypes); //上学期活动
  102. TermActivitCount += await CommonFind.FindTotals(cosmosClient, string.Format(typeSqlTxt, type, termStart, termEnd), containerTypes); //本学期学期活动
  103. }
  104. string bloblSqlTxt = "select count(c.id) as totals from c where c.pk='Bloblog' and c.time>={0} and c.time<={1}";
  105. rercCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) as totals from c where c.pk='Bloblog'", containers); //所有资源
  106. lastWeekRercCount = await CommonFind.FindTotals(cosmosClient, string.Format(bloblSqlTxt, lastWeekStart, lastWeekEnd), containers); //上周资源
  107. weekRercCount = await CommonFind.FindTotals(cosmosClient, string.Format(bloblSqlTxt, weekStart, weekEnd), containers); //本周资源
  108. lastTermRercCount = await CommonFind.FindTotals(cosmosClient, string.Format(bloblSqlTxt, lastTermStart, lastTermEnd), containers); //上学期资源
  109. termRercCount = await CommonFind.FindTotals(cosmosClient, string.Format(bloblSqlTxt, termStart, termEnd), containers); //这学期资源
  110. lastYearRercCount = await CommonFind.FindTotals(cosmosClient, string.Format(bloblSqlTxt, lastYearStart, lastYearEnd), containers); //去年资源
  111. yearRercCount = await CommonFind.FindTotals(cosmosClient, string.Format(bloblSqlTxt, yearStart, yearEnd), containers); //今年资源
  112. return Ok(new { state = 200, schoolCount, tecCount, stuCount, allClassCount, roomCount, wisdomRoomCount, allLessCount, lastYearLessCount, yearLessCount, lastWeekLessCount, weekLessCount, lastTermLessCount, termLessCount, allActivityCount, lastActivityCount, activityCount, lastWeekActivitCount, weekActivitCount, lastTermActivitCount, TermActivitCount, rercCount, lastWeekRercCount, weekRercCount, lastTermRercCount, termRercCount, lastYearRercCount, yearRercCount });
  113. }
  114. /// <summary>
  115. /// 查询顾问相关的学校统计数据
  116. /// </summary>
  117. /// <param name="jsonElement"></param>
  118. /// <returns></returns>
  119. [HttpPost("get-assist")]
  120. public async Task<IActionResult> GetAssistStatis(JsonElement jsonElement)
  121. {
  122. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  123. int tecCount = 0; //教师数量
  124. int stuCount = 0; //学校数量
  125. int classCount = 0; //班级数量
  126. int roomCount = 0; //智慧教师数量
  127. int allLessonCount = 0; //课例数量
  128. int lastWeekLessCount = 0;// 上周课例数
  129. int weekLessCount = 0; //本周课例
  130. int lastTermLessCount = 0;// 上学期课例数
  131. int termLessCount = 0; //本学期课例
  132. int lastYearLessCount = 0; //去年课例
  133. int yearLessCount = 0; //今年课例
  134. int allBloblog = 0; //学校资源
  135. int lastYearBloblog = 0; //去年学校资源
  136. int yearBloblog = 0; //今年学校资源
  137. long allActivity = 0; //学校所有活动
  138. long lastYearActivity = 0; //去年学校所有活动
  139. long yearActivity = 0; //今年学校所有活动
  140. long lastWeekActivity = 0; //上周学校所有活动
  141. long weekActivity = 0; //本周学校所有活动
  142. var cosmosClient = _azureCosmos.GetCosmosClient();
  143. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  144. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  145. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  146. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  147. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  148. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  149. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  150. string unifySqlTxt = "select count(c.id) as totals from c";
  151. string unifyTimeSql = "select count(c.id) as totals from c where c.startTime>={0} and c.startTime<={1}";
  152. string blobTimeSql = "select count(c.id) as totals from c where c.time>={0} and c.time<={1}";
  153. List<RecSchoolDate> recSchoolDates = new();
  154. foreach (var itemId in schoolIds)
  155. {
  156. School school = new();
  157. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
  158. if (response.Status == 200)
  159. {
  160. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  161. school = json.ToObject<School>();
  162. }
  163. tecCount += await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where ARRAY_CONTAINS(c.roles,'teacher',true) and c.status = 'join'", "School", $"Teacher-{itemId}");
  164. stuCount += await CommonFind.FindTotals(cosmosClient, unifySqlTxt, "Student", $"Base-{itemId}");
  165. classCount += await CommonFind.FindTotals(cosmosClient, unifySqlTxt, "School", $"Class-{itemId}");
  166. roomCount += await CommonFind.FindTotals(cosmosClient, unifySqlTxt, "School", $"Room-{itemId}");
  167. //学校所有课例
  168. int tempLessCount = await CommonFind.FindTotals(cosmosClient, unifySqlTxt, "School", $"LessonRecord-{itemId}");
  169. lastWeekLessCount += await CommonFind.FindTotals(cosmosClient, string.Format(unifyTimeSql,lastWeekStart,lastWeekEnd), "School", $"LessonRecord-{itemId}");
  170. weekLessCount += await CommonFind.FindTotals(cosmosClient, string.Format(unifyTimeSql, weekStart, weekEnd), "School", $"LessonRecord-{itemId}");
  171. lastTermLessCount += await CommonFind.FindTotals(cosmosClient, string.Format(unifyTimeSql, lastTermStart, lastTermEnd), "School", $"LessonRecord-{itemId}");
  172. termLessCount += await CommonFind.FindTotals(cosmosClient, string.Format(unifyTimeSql, termStart, termEnd), "School", $"LessonRecord-{itemId}");
  173. lastYearLessCount += await CommonFind.FindTotals(cosmosClient, string.Format(unifyTimeSql, lastYearStart, lastYearEnd), "School", $"LessonRecord-{itemId}");
  174. yearLessCount += await CommonFind.FindTotals(cosmosClient, string.Format(unifyTimeSql, yearStart, yearEnd), "School", $"LessonRecord-{itemId}");
  175. //学校资源
  176. int tempBloblog = await CommonFind.FindTotals(cosmosClient, unifySqlTxt, "School", $"Bloblog-{itemId}");
  177. lastYearBloblog += await CommonFind.FindTotals(cosmosClient, string.Format(blobTimeSql,lastYearStart,lastYearEnd), "School", $"Bloblog-{itemId}");
  178. yearBloblog += await CommonFind.FindTotals(cosmosClient, string.Format(blobTimeSql, yearStart, yearEnd), "School", $"Bloblog-{itemId}");
  179. //学校活动
  180. long tempallActivity = 0;
  181. //统计活动
  182. foreach (var type in StaticValue.activityTypes)
  183. {
  184. string sqlTime = "SELECT count(c.id) as totals FROM c where c.pk = '{0}' and c.school = '{1}' and c.createTime>={2} and c.createTime<={3}";
  185. tempallActivity += await CommonFind.FindTotals(cosmosClient, $"SELECT count(c.id) as totals FROM c where c.pk = '{type}' and c.school = '{itemId}'", new List<string> { "Common" });
  186. lastYearActivity += await CommonFind.FindTotals(cosmosClient, string.Format(sqlTime,type,itemId, lastYearStart, lastYearEnd), new List<string> { "Common" });
  187. yearActivity += await CommonFind.FindTotals(cosmosClient, string.Format(sqlTime, type, itemId, yearStart, yearEnd), new List<string> { "Common" });
  188. lastWeekActivity += await CommonFind.FindTotals(cosmosClient, string.Format(sqlTime, type, itemId, lastWeekStart, lastWeekEnd), new List<string> { "Common" });
  189. weekActivity += await CommonFind.FindTotals(cosmosClient, string.Format(sqlTime, type, itemId, weekStart, weekEnd), new List<string> { "Common" });
  190. }
  191. allLessonCount += tempLessCount;
  192. allActivity += tempallActivity;
  193. allBloblog += tempBloblog;
  194. recSchoolDates.Add(new RecSchoolDate() { id = school.id, name = school.name, dataCount = (tempLessCount + tempallActivity + tempBloblog) });
  195. }
  196. return Ok(new { state = 200, schoolCount = schoolIds.Count, tecCount, stuCount, classCount, roomCount, allLessonCount, lastWeekLessCount, weekLessCount, lastTermLessCount, termLessCount, lastYearLessCount, yearLessCount,allBloblog , lastYearBloblog, yearBloblog , allActivity , lastYearActivity ,yearActivity , lastWeekActivity, weekActivity, recSchoolDates });
  197. }
  198. /// <summary>
  199. /// 依据学校Id统计学校分析
  200. /// </summary>
  201. /// <param name="jsonElement"></param>
  202. /// <returns></returns>
  203. [HttpPost("get-idstatis")]
  204. public async Task<IActionResult> GetIdStatis(JsonElement jsonElement)
  205. {
  206. if (!jsonElement.TryGetProperty("schoolId", out JsonElement schoolId)) return BadRequest();
  207. int tecCount = 0; //学校教师数量
  208. int stuCount = 0; //学校学生数量
  209. int classCount = 0; //班级数量
  210. int roomCount = 0; //教室教师数量
  211. long allLessCount = 0; //课例总数
  212. int lastWeekLess = 0; //上周的总数
  213. int weekLess = 0; //本周的总数
  214. int lastTermLess = 0; //上学期的总数
  215. int termLess = 0; //本学期的总数
  216. int lessYearLess = 0; //去年的总数
  217. int yearLess = 0; //去年的总数
  218. int allActivity = 0; //学校所有活动
  219. int lastYearActivity = 0; //去年学校所有活动
  220. int yearActivity = 0; //今年学校所有活动
  221. int lastWeekActivity = 0; //上周学校所有活动
  222. int weekActivity = 0; //本周学校所有活动
  223. int allBlob = 0; //所有资源
  224. int lastYearBlob = 0; //去年的资源
  225. int yearBlob = 0; //去年的资源
  226. var cosmosClient = _azureCosmos.GetCosmosClient();
  227. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  228. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  229. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  230. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  231. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  232. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  233. tecCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", "SELECT value(count(c.id)) FROM c WHERE ARRAY_CONTAINS(c.roles, 'teacher', true) AND c.status = 'join'", code:$"Teacher-{schoolId}");
  234. stuCount = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", code: $"Base-{schoolId}");
  235. classCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Class-{schoolId}");
  236. roomCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Room-{schoolId}");
  237. string unifyTimeSql = "select value(count(c.id)) from c where c.startTime>={0} and c.startTime<={1}";
  238. allLessCount = await LessonStatisWay.GetSchoolIdLessonCount(cosmosClient, $"{schoolId}");
  239. lastWeekLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastWeekStart, lastWeekEnd), $"LessonRecord-{schoolId}");
  240. weekLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, weekStart, lastWeekEnd), $"LessonRecord-{schoolId}");
  241. lastTermLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastTermStart, lastTermEnd), $"LessonRecord-{schoolId}") ;
  242. termLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, termStart, termEnd), $"LessonRecord-{schoolId}");
  243. lessYearLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastYearStart, lastYearEnd), $"LessonRecord-{schoolId}");
  244. yearLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, yearStart, yearEnd), $"LessonRecord-{schoolId}");
  245. //统计活动
  246. foreach (var type in StaticValue.activityTypes)
  247. {
  248. string sqlTime = "select value(count(c.id)) from c where c.pk = '{0}' and c.school = '{1}' and c.createTime>={2} and c.createTime<={3}";
  249. allActivity += await JointlySingleQuery.GetValueInt(cosmosClient,"Common", $"select value(count(c.id)) from c where c.pk = '{type}' and c.school = '{schoolId}'");
  250. lastYearActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, lastYearStart, lastYearEnd));
  251. yearActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, yearStart, yearEnd));
  252. lastWeekActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, lastWeekStart, lastWeekEnd));
  253. weekActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, weekStart, weekEnd));
  254. }
  255. //学校资源
  256. string blobTimeSql = "select value(count(c.id)) from c where c.time>={0} and c.time<={1}";
  257. allBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Bloblog-{schoolId}");
  258. lastYearBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(blobTimeSql, lastYearStart, lastYearEnd), $"Bloblog-{schoolId}");
  259. yearBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(blobTimeSql, yearStart, yearEnd), $"Bloblog-{schoolId}");
  260. //获取所有的课程记录
  261. List<LessonRecord> records = new();
  262. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonRecord>(queryText: "select value(c) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{schoolId}") }))
  263. {
  264. records.Add(item);
  265. }
  266. List<(string name, int count)> gradeCount = new();
  267. if (records.Count > 0)
  268. {
  269. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{schoolId}", new PartitionKey($"Base"));
  270. School sc = new();
  271. if (response.Status == 200)
  272. {
  273. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  274. sc = json.ToObject<School>();
  275. }
  276. List<string> grades = new();
  277. foreach (var item in records)
  278. {
  279. foreach (string gId in item.grade)
  280. {
  281. if (!grades.Contains(gId))
  282. {
  283. grades.Add(gId);
  284. }
  285. }
  286. }
  287. foreach (var gId in grades)
  288. {
  289. var c = records.Where(r => r.grade.Contains(gId)).Count();
  290. gradeCount.Add((gId, c));
  291. }
  292. }
  293. return Ok(new { state = 200, tecCount, stuCount, classCount, roomCount, allLessCount, lastWeekLess, weekLess, lastTermLess, termLess, lessYearLess, yearLess, allActivity, lastYearActivity, yearActivity, lastWeekActivity, weekActivity, allBlob, lastYearBlob, yearBlob });
  294. }
  295. /// <summary>
  296. /// 依据Id查询School容器 数据管理工具——查询工具
  297. /// </summary>
  298. /// <param name="jsonElement"></param>
  299. /// <returns></returns>
  300. [HttpPost("get-info")]
  301. public async Task<IActionResult> GetSchool(JsonElement jsonElement)
  302. {
  303. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  304. var cosmosClient = _azureCosmos.GetCosmosClient();
  305. List<object> infos = new List<object>();
  306. string sqlTxt = $"select value(c) from c where c.id='{id}'";
  307. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  308. {
  309. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  310. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  311. {
  312. infos.Add(obj.ToObject<object>());
  313. }
  314. }
  315. return Ok(new { state = 200, infos });
  316. }
  317. public record RecSchoolDate
  318. {
  319. public string id { get; set; }
  320. public string name { get; set; }
  321. public long dataCount { get; set; }
  322. }
  323. }
  324. }