SchoolController.cs 26 KB

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