SchoolController.cs 27 KB

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