SchoolController.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using NUnit.Framework.Constraints;
  6. using Pipelines.Sockets.Unofficial.Arenas;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Text.Json;
  13. using System.Threading.Tasks;
  14. using TEAMModelBI.Models;
  15. using TEAMModelBI.Tool;
  16. using TEAMModelBI.Tool.BIStatsWay;
  17. using TEAMModelBI.Tool.CosmosBank;
  18. using TEAMModelOS.Models;
  19. using TEAMModelOS.SDK.Context.BI;
  20. using TEAMModelOS.SDK.Context.Constant;
  21. using TEAMModelOS.SDK.DI;
  22. using TEAMModelOS.SDK.Extension;
  23. using TEAMModelOS.SDK.Models;
  24. using TEAMModelOS.SDK.Models.Cosmos.BI;
  25. using TEAMModelOS.SDK.Models.Service.BI;
  26. using static TEAMModelBI.Controllers.BINormal.AreaRelevantController;
  27. using static TEAMModelBI.Controllers.Census.LessonSticsController;
  28. namespace TEAMModelBI.Controllers.Census
  29. {
  30. [Route("school")]
  31. [ApiController]
  32. public class SchoolController : ControllerBase
  33. {
  34. private readonly AzureCosmosFactory _azureCosmos;
  35. private readonly AzureStorageFactory _azureStorage;
  36. private readonly DingDing _dingDing;
  37. private readonly Option _option;
  38. public SchoolController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  39. {
  40. _azureCosmos = azureCosmos;
  41. _azureStorage = azureStorage;
  42. _dingDing = dingDing;
  43. _option = option?.Value;
  44. }
  45. /// <summary>
  46. /// 统计所有分析:基础、课例、活动、资源
  47. /// </summary>
  48. /// <param name="jsonElement"></param>
  49. /// <returns></returns>
  50. [ProducesDefaultResponseType]
  51. [HttpPost("get-all")]
  52. public async Task<IActionResult> GetAll(JsonElement jsonElement)
  53. {
  54. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  55. var cosmosClient = _azureCosmos.GetCosmosClient();
  56. //////分开部署,就不需要,一站多用时,取消注释
  57. //if ($"{site}".Equals(BIConst.Global))
  58. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  59. long schoolCount = 0; //学校数量
  60. int tecCount = 0; //教师数量
  61. int stuCount = 0; //学生数量
  62. long roomCount = 0; //教室数量
  63. long wisdomRoomCount = 0; //智慧教室数量
  64. long allClassCount = 0; //所有班级
  65. long allLessCount = 0; //所有课例
  66. long lastYearLessCount = 0; //去年课例
  67. long yearLessCount = 0;//今年课例
  68. long lastWeekLessCount = 0; //上周课例
  69. long weekLessCount = 0; //本周课例
  70. long lastTermLessCount = 0; //上学期课例
  71. long termLessCount = 0; //本学期课例
  72. long allActivityCount = 0; //所有活动
  73. long lastActivityCount = 0; //去年活动
  74. long activityCount = 0; //今年活动
  75. long lastWeekActivitCount = 0; //上周活动
  76. long weekActivitCount = 0; //本周活动
  77. long lastTermActivitCount = 0; //上学期活动
  78. long TermActivitCount = 0; //本学期学期活动
  79. long rercCount = 0; //所有资源数量
  80. long weekRercCount = 0; //本周资源数量
  81. long lastWeekRercCount = 0; //上周资源数量
  82. long lastTermRercCount = 0; //上学期资源
  83. long termRercCount = 0; //本学期资源
  84. long lastYearRercCount = 0; //去年资源
  85. long yearRercCount = 0; //去年资源
  86. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  87. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  88. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  89. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  90. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  91. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  92. schoolCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c", "Base"); //所有学校数量
  93. tecCount = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", "select value(count(c.id)) from c", "Base"); //所有教师数量
  94. stuCount = await CommonFind.GetSqlValueCount(cosmosClient, "Student", "select value(count(c.id)) from c", "Base"); //所有学生数量
  95. allClassCount = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School" }, "select count(c.id) totals from c where c.pk = 'Class'"); //所有班级数量
  96. roomCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c where c.pk = 'Room'"); //所有教室数量
  97. wisdomRoomCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c where c.pk = 'Room' and c.serial != null"); //智慧教室数量
  98. List<string> containers = new() { "School", "Teacher" };
  99. string lessSqlTxt = "select value(count(c.id)) from c where c.pk='LessonRecord' and c.startTime>={0} and c.startTime<={1}";
  100. allLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, $"select count(c.id) as totals from c where c.pk='LessonRecord'");//所有课例
  101. lastYearLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastYearStart, lastYearEnd)); //去年课例
  102. yearLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, yearStart, yearEnd)); //今年课例
  103. lastWeekLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastWeekStart, lastWeekEnd)); //上周课例
  104. weekLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, weekStart, weekEnd)); //本周课例
  105. lastTermLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastTermStart, lastTermEnd)); //上学期课例
  106. termLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, termStart, termEnd)); //上学期课例
  107. string typeSqlTxt = "select value(count(c.id)) from c where c.pk='{0}' and c.createTime >={1} and c.createTime<= {2}";
  108. foreach (var type in StaticValue.activityTypes)
  109. {
  110. allActivityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", $"select value(count(c.id)) from c where c.pk='{type}' ");//所有活动
  111. lastActivityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastYearStart, lastYearEnd)); //去年活动
  112. activityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, yearStart, yearEnd)); //今年活动
  113. lastWeekActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastWeekStart, lastWeekEnd)); //上周活动
  114. weekActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, weekStart, weekEnd)); //本周活动
  115. lastTermActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastTermStart, lastTermEnd)); //上学期活动
  116. TermActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, termStart, termEnd)); //本学期学期活动
  117. }
  118. string bloblSqlTxt = "select value(count(c.id)) from c where c.pk='Bloblog' and c.time>={0} and c.time<={1}";
  119. rercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, "select value(count(c.id)) from c where c.pk='Bloblog'"); //所有资源
  120. lastWeekRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastWeekStart, lastWeekEnd)); //上周资源
  121. weekRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, weekStart, weekEnd)); //本周资源
  122. lastTermRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastTermStart, lastTermEnd)); //上学期资源
  123. termRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, termStart, termEnd)); //这学期资源
  124. lastYearRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastYearStart, lastYearEnd)); //去年资源
  125. yearRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, yearStart, yearEnd)); //今年资源
  126. 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 });
  127. }
  128. /// <summary>
  129. /// 查询顾问相关的学校统计数据
  130. /// </summary>
  131. /// <param name="jsonElement"></param>
  132. /// <returns></returns>
  133. [ProducesDefaultResponseType]
  134. [HttpPost("get-assist")]
  135. public async Task<IActionResult> GetAssistStatis(JsonElement jsonElement)
  136. {
  137. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  138. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  139. int tecCount = 0; //教师数量
  140. int stuCount = 0; //学校数量
  141. int classCount = 0; //班级数量
  142. int roomCount = 0; //智慧教师数量
  143. int allLessonCount = 0; //课例数量
  144. int lastWeekLessCount = 0;// 上周课例数
  145. int weekLessCount = 0; //本周课例
  146. int lastTermLessCount = 0;// 上学期课例数
  147. int termLessCount = 0; //本学期课例
  148. int lastYearLessCount = 0; //去年课例
  149. int yearLessCount = 0; //今年课例
  150. int allBloblog = 0; //学校资源
  151. int lastYearBloblog = 0; //去年学校资源
  152. int yearBloblog = 0; //今年学校资源
  153. long allActivity = 0; //学校所有活动
  154. long lastYearActivity = 0; //去年学校所有活动
  155. long yearActivity = 0; //今年学校所有活动
  156. long lastWeekActivity = 0; //上周学校所有活动
  157. long weekActivity = 0; //本周学校所有活动
  158. var cosmosClient = _azureCosmos.GetCosmosClient();
  159. ////分开部署,就不需要,一站多用时,取消注释
  160. //if ($"{site}".Equals(BIConst.Global))
  161. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  162. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  163. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  164. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  165. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  166. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  167. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  168. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  169. string unifySqlTxt = "select value(count(c.id)) from c";
  170. string unifyTimeSql = "select value(count(c.id)) from c where c.startTime>={0} and c.startTime<={1}";
  171. string blobTimeSql = "select value(count(c.id)) from c where c.time>={0} and c.time<={1}";
  172. List<RecSchoolDate> recSchoolDates = new();
  173. foreach (var itemId in schoolIds)
  174. {
  175. School school = new();
  176. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
  177. if (response.Status == 200)
  178. {
  179. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  180. school = json.ToObject<School>();
  181. }
  182. 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}");
  183. stuCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", unifySqlTxt, $"Base-{itemId}");
  184. classCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Class-{itemId}");
  185. roomCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Room-{itemId}");
  186. //学校所有课例
  187. int tempLessCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"LessonRecord-{itemId}");
  188. lastWeekLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastWeekStart, lastWeekEnd), $"LessonRecord-{itemId}");
  189. weekLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, weekStart, weekEnd), $"LessonRecord-{itemId}");
  190. lastTermLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastTermStart, lastTermEnd), $"LessonRecord-{itemId}");
  191. termLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, termStart, termEnd), $"LessonRecord-{itemId}");
  192. lastYearLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastYearStart, lastYearEnd), $"LessonRecord-{itemId}");
  193. yearLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, yearStart, yearEnd), $"LessonRecord-{itemId}");
  194. //学校资源
  195. int tempBloblog = await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Bloblog-{itemId}");
  196. lastYearBloblog += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(blobTimeSql, lastYearStart, lastYearEnd), $"Bloblog-{itemId}");
  197. yearBloblog += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(blobTimeSql, yearStart, yearEnd), $"Bloblog-{itemId}");
  198. //学校活动
  199. long tempallActivity = 0;
  200. //统计活动
  201. foreach (var type in StaticValue.activityTypes)
  202. {
  203. 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}";
  204. tempallActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", $"SELECT value(count(c.id)) FROM c where c.pk = '{type}' and c.school = '{itemId}'");
  205. lastYearActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, lastYearStart, lastYearEnd));
  206. yearActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, yearStart, yearEnd));
  207. lastWeekActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, lastWeekStart, lastWeekEnd));
  208. weekActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, weekStart, weekEnd));
  209. }
  210. allLessonCount += tempLessCount;
  211. allActivity += tempallActivity;
  212. allBloblog += tempBloblog;
  213. recSchoolDates.Add(new RecSchoolDate() { id = school.id, name = school.name, dataCount = (tempLessCount + tempallActivity + tempBloblog) });
  214. }
  215. 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 });
  216. }
  217. /// <summary>
  218. /// 依据学校Id统计学校分析
  219. /// </summary>
  220. /// <param name="jsonElement"></param>
  221. /// <returns></returns>
  222. [ProducesDefaultResponseType]
  223. [HttpPost("get-idstatis")]
  224. public async Task<IActionResult> GetIdStatis(JsonElement jsonElement)
  225. {
  226. if (!jsonElement.TryGetProperty("schoolId", out JsonElement schoolId)) return BadRequest();
  227. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  228. int tecCount = 0; //学校教师数量
  229. int stuCount = 0; //学校学生数量
  230. int classCount = 0; //班级数量
  231. int roomCount = 0; //教室教师数量
  232. long allLessCount = 0; //课例总数
  233. int lastWeekLess = 0; //上周的总数
  234. int weekLess = 0; //本周的总数
  235. int lastTermLess = 0; //上学期的总数
  236. int termLess = 0; //本学期的总数
  237. int lessYearLess = 0; //去年的总数
  238. int yearLess = 0; //去年的总数
  239. int allActivity = 0; //学校所有活动
  240. int lastYearActivity = 0; //去年学校所有活动
  241. int yearActivity = 0; //今年学校所有活动
  242. int lastWeekActivity = 0; //上周学校所有活动
  243. int weekActivity = 0; //本周学校所有活动
  244. int allBlob = 0; //所有资源
  245. int lastYearBlob = 0; //去年的资源
  246. int yearBlob = 0; //去年的资源
  247. var cosmosClient = _azureCosmos.GetCosmosClient();
  248. ////分开部署,就不需要,一站多用时,取消注释
  249. //if ($"{site}".Equals(BIConst.Global))
  250. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  251. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  252. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  253. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  254. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  255. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  256. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  257. 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}");
  258. stuCount = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", code: $"Base-{schoolId}");
  259. classCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Class-{schoolId}");
  260. roomCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Room-{schoolId}");
  261. string unifyTimeSql = "select value(count(c.id)) from c where c.startTime>={0} and c.startTime<={1}";
  262. allLessCount = await LessonStatisWay.GetSchoolIdLessonCount(cosmosClient, $"{schoolId}");
  263. lastWeekLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastWeekStart, lastWeekEnd), $"LessonRecord-{schoolId}");
  264. weekLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, weekStart, lastWeekEnd), $"LessonRecord-{schoolId}");
  265. lastTermLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastTermStart, lastTermEnd), $"LessonRecord-{schoolId}");
  266. termLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, termStart, termEnd), $"LessonRecord-{schoolId}");
  267. lessYearLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastYearStart, lastYearEnd), $"LessonRecord-{schoolId}");
  268. yearLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, yearStart, yearEnd), $"LessonRecord-{schoolId}");
  269. //统计活动
  270. foreach (var type in StaticValue.activityTypes)
  271. {
  272. 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}";
  273. allActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"select value(count(c.id)) from c where c.pk = '{type}' and c.school = '{schoolId}'");
  274. lastYearActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, lastYearStart, lastYearEnd));
  275. yearActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, yearStart, yearEnd));
  276. lastWeekActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, lastWeekStart, lastWeekEnd));
  277. weekActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, weekStart, weekEnd));
  278. }
  279. //学校资源
  280. string blobTimeSql = "select value(count(c.id)) from c where c.time>={0} and c.time<={1}";
  281. allBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Bloblog-{schoolId}");
  282. lastYearBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(blobTimeSql, lastYearStart, lastYearEnd), $"Bloblog-{schoolId}");
  283. yearBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(blobTimeSql, yearStart, yearEnd), $"Bloblog-{schoolId}");
  284. //获取所有的课程记录
  285. List<LessonRecord> records = new();
  286. 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}") }))
  287. {
  288. records.Add(item);
  289. }
  290. List<(string name, int count)> gradeCount = new();
  291. if (records.Count > 0)
  292. {
  293. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{schoolId}", new PartitionKey($"Base"));
  294. School sc = new();
  295. if (response.Status == 200)
  296. {
  297. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  298. sc = json.ToObject<School>();
  299. }
  300. List<string> grades = new();
  301. foreach (var item in records)
  302. {
  303. foreach (string gId in item.grade)
  304. {
  305. if (!grades.Contains(gId))
  306. {
  307. grades.Add(gId);
  308. }
  309. }
  310. }
  311. foreach (var gId in grades)
  312. {
  313. var c = records.Where(r => r.grade.Contains(gId)).Count();
  314. gradeCount.Add((gId, c));
  315. }
  316. }
  317. return Ok(new { state = 200, tecCount, stuCount, classCount, roomCount, allLessCount, lastWeekLess, weekLess, lastTermLess, termLess, lessYearLess, yearLess, allActivity, lastYearActivity, yearActivity, lastWeekActivity, weekActivity, allBlob, lastYearBlob, yearBlob });
  318. }
  319. /// <summary>
  320. /// 未区域学校统计
  321. /// </summary>
  322. /// <param name="jsonElement"></param>
  323. /// <returns></returns>
  324. [ProducesDefaultResponseType]
  325. [HttpPost("get-noarea")]
  326. public async Task<IActionResult> GetNoAreaCnt(JsonElement jsonElement)
  327. {
  328. var cosmosClient = _azureCosmos.GetCosmosClient();
  329. int tLessCnt = 0; //课例
  330. int tActCnt = 0; //活动
  331. List<string> scIds = new(); //学校id集合
  332. string scIdSql = "select value(c.id) from c where c.areaId=null or c.areaId=''";
  333. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: scIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  334. {
  335. scIds.Add(item);
  336. }
  337. string comSql = BICommonWay.ManyScSql("c.school", scIds);
  338. string lesSql = $"select value(count(c.id)) from c where c.pk='LessonRecord' and {comSql}";
  339. tLessCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, lesSql);
  340. comSql = BICommonWay.ManyScSql(" and c.school", scIds);
  341. tActCnt = await ActivityWay.GetCnt(cosmosClient, condSql: comSql);
  342. return Ok(new { state = 200, scCnt=scIds.Count(), tLessCnt, tActCnt });
  343. }
  344. /// <summary>
  345. /// 未加区的学校统计
  346. /// </summary>
  347. /// <param name="jsonElement"></param>
  348. /// <returns></returns>
  349. [ProducesDefaultResponseType]
  350. [HttpPost("get-noareasc")]
  351. public async Task<IActionResult> GetNoAareaScCnt(JsonElement jsonElement)
  352. {
  353. var cosmosClient = _azureCosmos.GetCosmosClient();
  354. var tableClient = _azureStorage.GetCloudTableClient();
  355. var table = tableClient.GetTableReference("BIDDUserInfo");
  356. var (dayS, dayE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow);
  357. var (weekS, weekE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week");
  358. var (monthS, monthE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "month");
  359. var (termS, termE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term");
  360. List<AreaAssist> areaAssists = new(); //顾问信息
  361. int tchCnt = 0; //所有区级老师
  362. int stuCnt = 0; //所有区级学生
  363. int tLessCnt = 0; //课例数量
  364. int tActCnt = 0; //活动数量
  365. HashSet<string> tchIds = new();
  366. List<string> scIds = new(); //学校id集合
  367. string scIdSql = "select value(c.id) from c where c.areaId=null or c.areaId=''";
  368. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: scIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  369. {
  370. scIds.Add(item);
  371. }
  372. string scsSql = BICommonWay.ManyScSql("c.school", scIds);
  373. string cntSql = $"select value(count(c.id)) from c where {scsSql}";
  374. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<int>(queryText: cntSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  375. {
  376. tchCnt += item;
  377. }
  378. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<int>(queryText: cntSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  379. {
  380. stuCnt += item;
  381. }
  382. foreach (var scId in scIds)
  383. {
  384. string tchIdSql = $"select value(c.id) from c where array_contains(c.roles,'assist',true) and c.pk='Teacher'";
  385. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: tchIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{scId}") }))
  386. {
  387. tchIds.Add(item);
  388. }
  389. }
  390. List<string> noIds = new();
  391. foreach (var item in tchIds)
  392. {
  393. List<DingDingUserInfo> ddUsers = await table.QueryWhereString<DingDingUserInfo>($"PartitionKey eq '{_option.Location}' and tmdId eq '{item}'");
  394. if (ddUsers.Count > 0)
  395. {
  396. foreach (var dduser in ddUsers)
  397. {
  398. AreaAssist areaAssist = new()
  399. {
  400. userId = dduser.userId,
  401. unionId = dduser.unionId,
  402. name = dduser.name,
  403. mobile = dduser.mobile,
  404. avatar = dduser.avatar,
  405. tmdId = dduser.tmdId,
  406. tmdName = dduser.tmdName,
  407. tmdMobile = dduser.tmdMobile,
  408. picture = dduser.picture,
  409. roles = dduser.roles,
  410. schoolIds = dduser.schoolIds
  411. };
  412. areaAssists.Add(areaAssist);
  413. }
  414. }
  415. else { noIds.Add(item); }
  416. }
  417. if (scIds.Count > 0)
  418. {
  419. string lesSql = $"select value(count(c.id)) from c where c.pk='LessonRecord' and {scsSql}";
  420. tLessCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, lesSql);
  421. string actComSql = BICommonWay.ManyScSql(" and c.school", scIds);
  422. tActCnt = await ActivityWay.GetCnt(cosmosClient, condSql: actComSql);
  423. }
  424. return Ok(new { state = RespondCode.Ok, scCnt = scIds.Count, tchCnt, stuCnt, allCnt = tLessCnt + tActCnt, tLessCnt, tActCnt});
  425. }
  426. /// <summary>
  427. /// 学校统计
  428. /// </summary>
  429. /// <param name="jsonElement"></param>
  430. /// <returns></returns>
  431. [ProducesDefaultResponseType]
  432. [HttpPost("get-scstats")]
  433. public async Task<IActionResult> GetSchoolStats(JsonElement jsonElement)
  434. {
  435. if (!jsonElement.TryGetProperty("scId", out JsonElement scId)) return BadRequest();
  436. var cosmosClinet = _azureCosmos.GetCosmosClient();
  437. School school = new();
  438. ScStats scStats = new();
  439. var respSc = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{scId}", new PartitionKey($"Base"));
  440. if (respSc.Status == 200)
  441. {
  442. using var fileJson = await JsonDocument.ParseAsync(respSc.ContentStream);
  443. school = fileJson.ToObject<School>();
  444. }
  445. else
  446. return Ok(new { state = RespondCode.NotFound, msg = $"未找到id:{scId};相关的学校,请检查" });
  447. try
  448. {
  449. StatsInfo statsInfo = new();
  450. var scDataStats = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{scId}", new PartitionKey("Stats"));
  451. if (scDataStats.Status == 200)
  452. {
  453. using var fileJson = await JsonDocument.ParseAsync(scDataStats.ContentStream);
  454. statsInfo = fileJson.ToObject<StatsInfo>();
  455. }
  456. else
  457. scStats.id = school.id;
  458. bool isnew = false;
  459. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  460. long now = dateTime.ToUnixTimeMilliseconds();
  461. if (!string.IsNullOrEmpty(statsInfo.id))
  462. {
  463. scStats.id= statsInfo.id;
  464. scStats.name = statsInfo.name;
  465. scStats.picture = statsInfo.picture;
  466. scStats.tch = statsInfo.tch;
  467. scStats.dayTch = statsInfo.dayTch;
  468. scStats.weekTch = statsInfo.weekTch;
  469. scStats.monthTch = statsInfo.monthTch;
  470. scStats.stu = statsInfo.stu;
  471. scStats.dayStu = statsInfo.dayStu;
  472. scStats.weekStu = statsInfo.weekStu;
  473. scStats.monthStu = statsInfo.monthStu;
  474. scStats.room = statsInfo.room;
  475. scStats.witRoom = statsInfo.witRoom;
  476. scStats.size = statsInfo.size;
  477. scStats.scCreateTime = statsInfo.scCreateTime;
  478. scStats.upTime = statsInfo.upTime;
  479. scStats.lessStats.open = statsInfo.lesson.open;
  480. scStats.lessStats.less = statsInfo.lesson.less;
  481. scStats.lessStats.lastDay = statsInfo.lesson.lastDay;
  482. scStats.lessStats.day = statsInfo.lesson.day;
  483. scStats.lessStats.lastWeek = statsInfo.lesson.lastWeek;
  484. scStats.lessStats.week = statsInfo.lesson.week;
  485. scStats.lessStats.lastTerm = statsInfo.lesson.lastTerm;
  486. scStats.lessStats.term = statsInfo.lesson.term;
  487. scStats.lessStats.lastDayInter = statsInfo.lesson.lastDayInter;
  488. scStats.lessStats.dayInter = statsInfo.lesson.dayInter;
  489. scStats.lessStats.lastMonthInter = statsInfo.lesson.lastMonthInter;
  490. scStats.lessStats.monthInter = statsInfo.lesson.monthInter;
  491. scStats.lessStats.lastYearInter = statsInfo.lesson.lastYearInter;
  492. scStats.lessStats.yearInter=statsInfo.lesson.yearInter;
  493. scStats.lessStats.LastYear = TimeHelper.GetYearMonth(statsInfo.lesson.LastYear, dateTime.Year, dateTime.Month);
  494. scStats.lessStats.year = TimeHelper.GetYearMonth(statsInfo.lesson.year, dateTime.Year, dateTime.Month);
  495. scStats.actStats.cnt = statsInfo.activity.cnt;
  496. scStats.actStats.exam = statsInfo.activity.exam;
  497. scStats.actStats.survey = statsInfo.activity.survey;
  498. scStats.actStats.vote = statsInfo.activity.vote;
  499. scStats.actStats.homework = statsInfo.activity.homework;
  500. scStats.actStats.lastDay = statsInfo.activity.lastDay;
  501. scStats.actStats.dayCnt = statsInfo.activity.dayCnt;
  502. scStats.actStats.lastWeek = statsInfo.activity.lastWeek;
  503. scStats.actStats.week = statsInfo.activity.week;
  504. scStats.actStats.lastTerm = statsInfo.activity.lastTerm;
  505. scStats.actStats.term = statsInfo.activity.term;
  506. scStats.actStats.LastYear = TimeHelper.GetYearMonth(statsInfo.activity.LastYear ,dateTime.Year,dateTime.Month);
  507. scStats.actStats.year = TimeHelper.GetYearMonth(statsInfo.activity.year, dateTime.Year, dateTime.Month);
  508. scStats.srStats.learnTime = statsInfo.study.learnTime;
  509. scStats.srStats.onlineTime = statsInfo.study.onlineTime;
  510. scStats.srStats.offlineTime = statsInfo.study.offlineTime;
  511. scStats.srStats.classTime = statsInfo.study.classTime;
  512. scStats.srStats.currency = statsInfo.study.currency;
  513. scStats.srStats.notStarted = statsInfo.study.notStarted;
  514. scStats.srStats.ongoing = statsInfo.study.ongoing;
  515. scStats.srStats.finish = statsInfo.study.finish;
  516. }
  517. if(statsInfo.upTime > 0 && !isnew) {
  518. long timedis = 15 * 60 * 1000;//15分钟
  519. if (now - statsInfo.upTime < timedis)
  520. isnew = true;
  521. }
  522. if (statsInfo.upTime == 0 && isnew)
  523. statsInfo.upTime = now;
  524. if (statsInfo.upTime == 0 && isnew)
  525. statsInfo.upTime = DateTimeOffset.UtcNow.Add(new TimeSpan(-1, 0, 0, 0)).ToUnixTimeMilliseconds();
  526. statsInfo = await SchoolStatsWay.upSingleSc(cosmosClinet, school.id);
  527. return Ok(new { state = RespondCode.Ok, statsInfo });
  528. }
  529. catch (Exception ex)
  530. {
  531. //await _dingDing.SendBotMsg($"BI,{_option.Location} /school/set-scstats \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  532. return BadRequest();
  533. }
  534. }
  535. /// <summary>
  536. /// 依据Id查询School容器 数据管理工具——查询工具
  537. /// </summary>
  538. /// <param name="jsonElement"></param>
  539. /// <returns></returns>
  540. [ProducesDefaultResponseType]
  541. [HttpPost("get-info")]
  542. public async Task<IActionResult> GetSchool(JsonElement jsonElement)
  543. {
  544. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  545. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  546. var cosmosClient = _azureCosmos.GetCosmosClient();
  547. //if ($"{site}".Equals(BIConst.Global))
  548. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  549. List<object> infos = new List<object>();
  550. string sqlTxt = $"select value(c) from c where c.id='{id}'";
  551. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  552. {
  553. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  554. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  555. {
  556. infos.Add(obj.ToObject<object>());
  557. }
  558. }
  559. return Ok(new { state = 200, infos });
  560. }
  561. public record RecSchoolDate
  562. {
  563. public string id { get; set; }
  564. public string name { get; set; }
  565. public long dataCount { get; set; }
  566. }
  567. /// <summary>
  568. /// 前端显示学校统计数据
  569. /// </summary>
  570. public record ScStats
  571. {
  572. public string id { get; set; }
  573. public string name { get; set; }
  574. public string picture { get; set; }
  575. public int tch { get; set; }
  576. public int dayTch { get; set; }
  577. public int weekTch { get; set; }
  578. public int monthTch { get; set; }
  579. public int stu { get; set; }
  580. public int dayStu { get; set; }
  581. public int weekStu { get; set; }
  582. public int monthStu { get; set; }
  583. public int room { get; set; }
  584. public int witRoom { get; set; }
  585. public int size { get; set; }
  586. public long scCreateTime { get; set; }
  587. public long upTime { get; set; }
  588. public ScLessStats lessStats { get; set; }
  589. public ScActStats actStats { get; set; }
  590. public ScSRStats srStats { get; set; }
  591. }
  592. /// <summary>
  593. /// 课例活动数据
  594. /// </summary>
  595. public record ScLessStats
  596. {
  597. public int open { get; set; }
  598. public int less { get; set; }
  599. public int lastDay { get; set; }
  600. public int day { get; set; }
  601. public int lastWeek { get; set; }
  602. public int week { get; set; }
  603. public int lastTerm { get; set; }
  604. public int term { get; set; }
  605. public int lastDayInter { get; set; }
  606. public int dayInter { get; set; }
  607. public int lastMonthInter { get; set; }
  608. public int monthInter { get; set; }
  609. public int lastYearInter { get; set; }
  610. public int yearInter { get; set; }
  611. //public List<YearMonth> yearInter { get; set; } = new List<YearMonth>();//12个月
  612. public List<YearMonth> LastYear { get; set; } = new List<YearMonth>();//12个月
  613. public List<YearMonth> year { get; set; } = new List<YearMonth>(); //12个月
  614. }
  615. /// <summary>
  616. /// 学校活动数据
  617. /// </summary>
  618. public record ScActStats
  619. {
  620. public int cnt { get; set; }
  621. public int exam { get; set; }
  622. public int survey { get; set; }
  623. public int vote { get; set; }
  624. public int homework { get; set; }
  625. public int lastDay { get; set; }
  626. public int dayCnt { get; set; }
  627. public int lastWeek { get; set; }
  628. public int week { get; set; }
  629. public int lastTerm { get; set; }
  630. public int term { get; set; }
  631. public List<YearMonth> LastYear { get; set; } = new List<YearMonth>(); //12个月
  632. public List<YearMonth> year { get; set; } = new List<YearMonth>(); //12个月
  633. }
  634. /// <summary>
  635. /// 研修统计数据
  636. /// </summary>
  637. public record ScSRStats
  638. {
  639. public int learnTime { get; set; }
  640. public int onlineTime { get; set; }
  641. public int offlineTime { get; set; }
  642. public int classTime { get; set; }
  643. public int currency { get; set; }
  644. public int notStarted { get; set; }
  645. public int ongoing { get; set; }
  646. public int finish { get; set; }
  647. }
  648. }
  649. }