SchoolController.cs 26 KB

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