SchoolController.cs 26 KB

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