SchoolController.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using Pipelines.Sockets.Unofficial.Arenas;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelBI.Models;
  13. using TEAMModelBI.Tool;
  14. using TEAMModelBI.Tool.CosmosBank;
  15. using TEAMModelOS.Models;
  16. using TEAMModelOS.SDK.Context.BI;
  17. using TEAMModelOS.SDK.DI;
  18. using TEAMModelOS.SDK.Extension;
  19. using TEAMModelOS.SDK.Models;
  20. using TEAMModelOS.SDK.Models.Cosmos.BI;
  21. using TEAMModelOS.SDK.Models.Service.BI;
  22. using static TEAMModelBI.Controllers.BINormal.AreaRelevantController;
  23. using static TEAMModelBI.Controllers.Census.LessonSticsController;
  24. namespace TEAMModelBI.Controllers.Census
  25. {
  26. [Route("school")]
  27. [ApiController]
  28. public class SchoolController : ControllerBase
  29. {
  30. private readonly AzureCosmosFactory _azureCosmos;
  31. private readonly AzureStorageFactory _azureStorage;
  32. private readonly DingDing _dingDing;
  33. private readonly Option _option;
  34. public SchoolController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  35. {
  36. _azureCosmos = azureCosmos;
  37. _azureStorage = azureStorage;
  38. _dingDing = dingDing;
  39. _option = option?.Value;
  40. }
  41. /// <summary>
  42. /// 统计所有分析:基础、课例、活动、资源
  43. /// </summary>
  44. /// <param name="jsonElement"></param>
  45. /// <returns></returns>
  46. [ProducesDefaultResponseType]
  47. [HttpPost("get-all")]
  48. public async Task<IActionResult> GetAll(JsonElement jsonElement)
  49. {
  50. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  51. var cosmosClient = _azureCosmos.GetCosmosClient();
  52. //////分开部署,就不需要,一站多用时,取消注释
  53. //if ($"{site}".Equals(BIConst.Global))
  54. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  55. long schoolCount = 0; //学校数量
  56. int tecCount = 0; //教师数量
  57. int stuCount = 0; //学生数量
  58. long roomCount = 0; //教室数量
  59. long wisdomRoomCount = 0; //智慧教室数量
  60. long allClassCount = 0; //所有班级
  61. long allLessCount = 0; //所有课例
  62. long lastYearLessCount = 0; //去年课例
  63. long yearLessCount = 0;//今年课例
  64. long lastWeekLessCount = 0; //上周课例
  65. long weekLessCount = 0; //本周课例
  66. long lastTermLessCount = 0; //上学期课例
  67. long termLessCount = 0; //本学期课例
  68. long allActivityCount = 0; //所有活动
  69. long lastActivityCount = 0; //去年活动
  70. long activityCount = 0; //今年活动
  71. long lastWeekActivitCount = 0; //上周活动
  72. long weekActivitCount = 0; //本周活动
  73. long lastTermActivitCount = 0; //上学期活动
  74. long TermActivitCount = 0; //本学期学期活动
  75. long rercCount = 0; //所有资源数量
  76. long weekRercCount = 0; //本周资源数量
  77. long lastWeekRercCount = 0; //上周资源数量
  78. long lastTermRercCount = 0; //上学期资源
  79. long termRercCount = 0; //本学期资源
  80. long lastYearRercCount = 0; //去年资源
  81. long yearRercCount = 0; //去年资源
  82. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  83. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  84. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  85. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  86. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  87. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  88. schoolCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c", "Base"); //所有学校数量
  89. tecCount = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", "select value(count(c.id)) from c", "Base"); //所有教师数量
  90. stuCount = await CommonFind.GetSqlValueCount(cosmosClient, "Student", "select value(count(c.id)) from c", "Base"); //所有学生数量
  91. allClassCount = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School" }, "select count(c.id) totals from c where c.pk = 'Class'"); //所有班级数量
  92. roomCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c where c.pk = 'Room'"); //所有教室数量
  93. wisdomRoomCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c where c.pk = 'Room' and c.serial != null"); //智慧教室数量
  94. List<string> containers = new() { "School", "Teacher" };
  95. string lessSqlTxt = "select value(count(c.id)) from c where c.pk='LessonRecord' and c.startTime>={0} and c.startTime<={1}";
  96. allLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, $"select count(c.id) as totals from c where c.pk='LessonRecord'");//所有课例
  97. lastYearLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastYearStart, lastYearEnd)); //去年课例
  98. yearLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, yearStart, yearEnd)); //今年课例
  99. lastWeekLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastWeekStart, lastWeekEnd)); //上周课例
  100. weekLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, weekStart, weekEnd)); //本周课例
  101. lastTermLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastTermStart, lastTermEnd)); //上学期课例
  102. termLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, termStart, termEnd)); //上学期课例
  103. string typeSqlTxt = "select value(count(c.id)) from c where c.pk='{0}' and c.createTime >={1} and c.createTime<= {2}";
  104. foreach (var type in StaticValue.activityTypes)
  105. {
  106. allActivityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", $"select value(count(c.id)) from c where c.pk='{type}' ");//所有活动
  107. lastActivityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastYearStart, lastYearEnd)); //去年活动
  108. activityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, yearStart, yearEnd)); //今年活动
  109. lastWeekActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastWeekStart, lastWeekEnd)); //上周活动
  110. weekActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, weekStart, weekEnd)); //本周活动
  111. lastTermActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastTermStart, lastTermEnd)); //上学期活动
  112. TermActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, termStart, termEnd)); //本学期学期活动
  113. }
  114. string bloblSqlTxt = "select value(count(c.id)) from c where c.pk='Bloblog' and c.time>={0} and c.time<={1}";
  115. rercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, "select value(count(c.id)) from c where c.pk='Bloblog'"); //所有资源
  116. lastWeekRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastWeekStart, lastWeekEnd)); //上周资源
  117. weekRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, weekStart, weekEnd)); //本周资源
  118. lastTermRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastTermStart, lastTermEnd)); //上学期资源
  119. termRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, termStart, termEnd)); //这学期资源
  120. lastYearRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastYearStart, lastYearEnd)); //去年资源
  121. yearRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, yearStart, yearEnd)); //今年资源
  122. 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 });
  123. }
  124. /// <summary>
  125. /// 查询顾问相关的学校统计数据
  126. /// </summary>
  127. /// <param name="jsonElement"></param>
  128. /// <returns></returns>
  129. [ProducesDefaultResponseType]
  130. [HttpPost("get-assist")]
  131. public async Task<IActionResult> GetAssistStatis(JsonElement jsonElement)
  132. {
  133. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  134. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  135. int tecCount = 0; //教师数量
  136. int stuCount = 0; //学校数量
  137. int classCount = 0; //班级数量
  138. int roomCount = 0; //智慧教师数量
  139. int allLessonCount = 0; //课例数量
  140. int lastWeekLessCount = 0;// 上周课例数
  141. int weekLessCount = 0; //本周课例
  142. int lastTermLessCount = 0;// 上学期课例数
  143. int termLessCount = 0; //本学期课例
  144. int lastYearLessCount = 0; //去年课例
  145. int yearLessCount = 0; //今年课例
  146. int allBloblog = 0; //学校资源
  147. int lastYearBloblog = 0; //去年学校资源
  148. int yearBloblog = 0; //今年学校资源
  149. long allActivity = 0; //学校所有活动
  150. long lastYearActivity = 0; //去年学校所有活动
  151. long yearActivity = 0; //今年学校所有活动
  152. long lastWeekActivity = 0; //上周学校所有活动
  153. long weekActivity = 0; //本周学校所有活动
  154. var cosmosClient = _azureCosmos.GetCosmosClient();
  155. ////分开部署,就不需要,一站多用时,取消注释
  156. //if ($"{site}".Equals(BIConst.Global))
  157. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  158. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  159. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  160. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  161. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  162. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  163. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  164. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  165. string unifySqlTxt = "select value(count(c.id)) from c";
  166. string unifyTimeSql = "select value(count(c.id)) from c where c.startTime>={0} and c.startTime<={1}";
  167. string blobTimeSql = "select value(count(c.id)) from c where c.time>={0} and c.time<={1}";
  168. List<RecSchoolDate> recSchoolDates = new();
  169. foreach (var itemId in schoolIds)
  170. {
  171. School school = new();
  172. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
  173. if (response.Status == 200)
  174. {
  175. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  176. school = json.ToObject<School>();
  177. }
  178. 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}");
  179. stuCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", unifySqlTxt, $"Base-{itemId}");
  180. classCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Class-{itemId}");
  181. roomCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Room-{itemId}");
  182. //学校所有课例
  183. int tempLessCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"LessonRecord-{itemId}");
  184. lastWeekLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastWeekStart, lastWeekEnd), $"LessonRecord-{itemId}");
  185. weekLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, weekStart, weekEnd), $"LessonRecord-{itemId}");
  186. lastTermLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastTermStart, lastTermEnd), $"LessonRecord-{itemId}");
  187. termLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, termStart, termEnd), $"LessonRecord-{itemId}");
  188. lastYearLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastYearStart, lastYearEnd), $"LessonRecord-{itemId}");
  189. yearLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, yearStart, yearEnd), $"LessonRecord-{itemId}");
  190. //学校资源
  191. int tempBloblog = await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Bloblog-{itemId}");
  192. lastYearBloblog += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(blobTimeSql, lastYearStart, lastYearEnd), $"Bloblog-{itemId}");
  193. yearBloblog += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(blobTimeSql, yearStart, yearEnd), $"Bloblog-{itemId}");
  194. //学校活动
  195. long tempallActivity = 0;
  196. //统计活动
  197. foreach (var type in StaticValue.activityTypes)
  198. {
  199. 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}";
  200. tempallActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", $"SELECT value(count(c.id)) FROM c where c.pk = '{type}' and c.school = '{itemId}'");
  201. lastYearActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, lastYearStart, lastYearEnd));
  202. yearActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, yearStart, yearEnd));
  203. lastWeekActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, lastWeekStart, lastWeekEnd));
  204. weekActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, weekStart, weekEnd));
  205. }
  206. allLessonCount += tempLessCount;
  207. allActivity += tempallActivity;
  208. allBloblog += tempBloblog;
  209. recSchoolDates.Add(new RecSchoolDate() { id = school.id, name = school.name, dataCount = (tempLessCount + tempallActivity + tempBloblog) });
  210. }
  211. 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 });
  212. }
  213. /// <summary>
  214. /// 依据学校Id统计学校分析
  215. /// </summary>
  216. /// <param name="jsonElement"></param>
  217. /// <returns></returns>
  218. [ProducesDefaultResponseType]
  219. [HttpPost("get-idstatis")]
  220. public async Task<IActionResult> GetIdStatis(JsonElement jsonElement)
  221. {
  222. if (!jsonElement.TryGetProperty("schoolId", out JsonElement schoolId)) return BadRequest();
  223. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  224. int tecCount = 0; //学校教师数量
  225. int stuCount = 0; //学校学生数量
  226. int classCount = 0; //班级数量
  227. int roomCount = 0; //教室教师数量
  228. long allLessCount = 0; //课例总数
  229. int lastWeekLess = 0; //上周的总数
  230. int weekLess = 0; //本周的总数
  231. int lastTermLess = 0; //上学期的总数
  232. int termLess = 0; //本学期的总数
  233. int lessYearLess = 0; //去年的总数
  234. int yearLess = 0; //去年的总数
  235. int allActivity = 0; //学校所有活动
  236. int lastYearActivity = 0; //去年学校所有活动
  237. int yearActivity = 0; //今年学校所有活动
  238. int lastWeekActivity = 0; //上周学校所有活动
  239. int weekActivity = 0; //本周学校所有活动
  240. int allBlob = 0; //所有资源
  241. int lastYearBlob = 0; //去年的资源
  242. int yearBlob = 0; //去年的资源
  243. var cosmosClient = _azureCosmos.GetCosmosClient();
  244. ////分开部署,就不需要,一站多用时,取消注释
  245. //if ($"{site}".Equals(BIConst.Global))
  246. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  247. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  248. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  249. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  250. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  251. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  252. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  253. 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}");
  254. stuCount = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", code: $"Base-{schoolId}");
  255. classCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Class-{schoolId}");
  256. roomCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Room-{schoolId}");
  257. string unifyTimeSql = "select value(count(c.id)) from c where c.startTime>={0} and c.startTime<={1}";
  258. allLessCount = await LessonStatisWay.GetSchoolIdLessonCount(cosmosClient, $"{schoolId}");
  259. lastWeekLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastWeekStart, lastWeekEnd), $"LessonRecord-{schoolId}");
  260. weekLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, weekStart, lastWeekEnd), $"LessonRecord-{schoolId}");
  261. lastTermLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastTermStart, lastTermEnd), $"LessonRecord-{schoolId}");
  262. termLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, termStart, termEnd), $"LessonRecord-{schoolId}");
  263. lessYearLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastYearStart, lastYearEnd), $"LessonRecord-{schoolId}");
  264. yearLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, yearStart, yearEnd), $"LessonRecord-{schoolId}");
  265. //统计活动
  266. foreach (var type in StaticValue.activityTypes)
  267. {
  268. 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}";
  269. allActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"select value(count(c.id)) from c where c.pk = '{type}' and c.school = '{schoolId}'");
  270. lastYearActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, lastYearStart, lastYearEnd));
  271. yearActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, yearStart, yearEnd));
  272. lastWeekActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, lastWeekStart, lastWeekEnd));
  273. weekActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, weekStart, weekEnd));
  274. }
  275. //学校资源
  276. string blobTimeSql = "select value(count(c.id)) from c where c.time>={0} and c.time<={1}";
  277. allBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Bloblog-{schoolId}");
  278. lastYearBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(blobTimeSql, lastYearStart, lastYearEnd), $"Bloblog-{schoolId}");
  279. yearBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(blobTimeSql, yearStart, yearEnd), $"Bloblog-{schoolId}");
  280. //获取所有的课程记录
  281. List<LessonRecord> records = new();
  282. 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}") }))
  283. {
  284. records.Add(item);
  285. }
  286. List<(string name, int count)> gradeCount = new();
  287. if (records.Count > 0)
  288. {
  289. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{schoolId}", new PartitionKey($"Base"));
  290. School sc = new();
  291. if (response.Status == 200)
  292. {
  293. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  294. sc = json.ToObject<School>();
  295. }
  296. List<string> grades = new();
  297. foreach (var item in records)
  298. {
  299. foreach (string gId in item.grade)
  300. {
  301. if (!grades.Contains(gId))
  302. {
  303. grades.Add(gId);
  304. }
  305. }
  306. }
  307. foreach (var gId in grades)
  308. {
  309. var c = records.Where(r => r.grade.Contains(gId)).Count();
  310. gradeCount.Add((gId, c));
  311. }
  312. }
  313. return Ok(new { state = 200, tecCount, stuCount, classCount, roomCount, allLessCount, lastWeekLess, weekLess, lastTermLess, termLess, lessYearLess, yearLess, allActivity, lastYearActivity, yearActivity, lastWeekActivity, weekActivity, allBlob, lastYearBlob, yearBlob });
  314. }
  315. /// <summary>
  316. /// 未区域学校统计 未写完
  317. /// </summary>
  318. /// <param name="jsonElement"></param>
  319. /// <returns></returns>
  320. [ProducesDefaultResponseType]
  321. [HttpPost("get-noarea")]
  322. public async Task<IActionResult> GetNoAreaCnt(JsonElement jsonElement)
  323. {
  324. var cosmosClient = _azureCosmos.GetCosmosClient();
  325. int tLessCnt = 0; //课例
  326. int tActCnt = 0; //活动
  327. List<string> scIds = new(); //学校id集合
  328. string scIdSql = "select value(c.id) from c where c.areaId=null or c.areaId=''";
  329. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: scIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  330. {
  331. scIds.Add(item);
  332. }
  333. string comSql = BICommonWay.ManyScSql("c.school", scIds);
  334. string lesSql = $"select value(count(c.id)) from c where c.pk='LessonRecord' and {comSql}";
  335. tLessCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, lesSql);
  336. comSql = BICommonWay.ManyScSql(" and c.school", scIds);
  337. tActCnt = await ActivityWay.GetCnt(cosmosClient, condSql: comSql);
  338. return Ok(new { state = 200, scCnt=scIds.Count(), tLessCnt, tActCnt });
  339. }
  340. /// <summary>
  341. /// 未加区的学校统计详细
  342. /// </summary>
  343. /// <param name="jsonElement"></param>
  344. /// <returns></returns>
  345. [HttpPost("get-noareasc")]
  346. public async Task<IActionResult> GetNoAareaScCnt(JsonElement jsonElement)
  347. {
  348. var cosmosClient = _azureCosmos.GetCosmosClient();
  349. var tableClient = _azureStorage.GetCloudTableClient();
  350. var table = tableClient.GetTableReference("BIDDUserInfo");
  351. var (dayS, dayE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow);
  352. var (weekS, weekE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week");
  353. var (monthS, monthE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "month");
  354. var (termS, termE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term");
  355. List<AreaAssist> areaAssists = new(); //顾问信息
  356. int scCnt = 0; //学校人数
  357. int weekScCnt = 0; //本周学校人数
  358. int motnhScCnt = 0; //本月学校人数
  359. int tchCnt = 0; //所有区级老师
  360. int weekTchCnt = 0; //所有区级老师
  361. int monthTchCnt = 0; //所有区级老师
  362. int stuCnt = 0; //所有区级学生
  363. int weekStuCnt = 0; //所有区级学生
  364. int monthStuCnt = 0; //所有区级学生
  365. int areaSize = 0; //区级空间
  366. int weekSize = 0; //本周空间
  367. int monthSize = 0; //本月空间
  368. int allLess = 0; //所有课例
  369. int weekLess = 0; //周课例
  370. int monthLess = 0; //月课例
  371. int termLess = 0; //学期课例
  372. int totalTime = 0; //累计学时
  373. int allActCnt = 0; //所有活动
  374. int weekActivity = 0; //周活动数量
  375. int monthActCnt = 0; //月活动数量
  376. int termActivity = 0; //学期活动
  377. int onLineCount = 0; //线上研修人数
  378. int offlineCount = 0; //线下研修人数
  379. int classRoomCount = 0; //课堂实录人数
  380. int submitCount = 0; //认证材料人数
  381. int fulfilCount = 0; //已完成
  382. int carryOnCount = 0; //进行中
  383. int noCount = 0; //未开始
  384. HashSet<string> tchIds = new();
  385. List<string> scIds = new(); //学校id集合
  386. string scIdSql = "select value(c.id) from c where c.areaId=null or c.areaId=''";
  387. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: scIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  388. {
  389. scIds.Add(item);
  390. }
  391. string scsSql = BICommonWay.ManyScSql("c.school", scIds);
  392. string cntSql = $"select value(count(c.id)) from c where {scsSql}";
  393. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<int>(queryText: cntSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  394. {
  395. tchCnt += item;
  396. }
  397. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<int>(queryText: cntSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  398. {
  399. stuCnt += item;
  400. }
  401. foreach (var scId in scIds)
  402. {
  403. string tchIdSql = $"select value(c.id) from c where array_contains(c.roles,'assist',true) and c.pk='Teacher'";
  404. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: tchIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{scId}") }))
  405. {
  406. tchIds.Add(item);
  407. }
  408. }
  409. List<string> noIds = new();
  410. foreach (var item in tchIds)
  411. {
  412. List<DingDingUserInfo> ddUsers = await table.QueryWhereString<DingDingUserInfo>($"PartitionKey eq '{_option.Location}' and tmdId eq '{item}'");
  413. if (ddUsers.Count > 0)
  414. {
  415. foreach (var dduser in ddUsers)
  416. {
  417. AreaAssist areaAssist = new()
  418. {
  419. userId = dduser.userId,
  420. unionId = dduser.unionId,
  421. name = dduser.name,
  422. mobile = dduser.mobile,
  423. avatar = dduser.avatar,
  424. tmdId = dduser.tmdId,
  425. tmdName = dduser.tmdName,
  426. tmdMobile = dduser.tmdMobile,
  427. picture = dduser.picture,
  428. roles = dduser.roles,
  429. schoolIds = dduser.schoolIds
  430. };
  431. areaAssists.Add(areaAssist);
  432. }
  433. }
  434. else { noIds.Add(item); }
  435. }
  436. AreaSetting setting = new()
  437. {
  438. allTime = 50,
  439. classTime = 5,
  440. submitTime = 15,
  441. onlineTime = 20,
  442. offlineTime = 10,
  443. lessonMinutes = 45,
  444. };
  445. string oftenSql = "select value(count(c.id)) from c";
  446. return Ok(new { state = 200 });
  447. }
  448. /// <summary>
  449. /// 依据Id查询School容器 数据管理工具——查询工具
  450. /// </summary>
  451. /// <param name="jsonElement"></param>
  452. /// <returns></returns>
  453. [ProducesDefaultResponseType]
  454. [HttpPost("get-info")]
  455. public async Task<IActionResult> GetSchool(JsonElement jsonElement)
  456. {
  457. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  458. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  459. var cosmosClient = _azureCosmos.GetCosmosClient();
  460. //if ($"{site}".Equals(BIConst.Global))
  461. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  462. List<object> infos = new List<object>();
  463. string sqlTxt = $"select value(c) from c where c.id='{id}'";
  464. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  465. {
  466. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  467. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  468. {
  469. infos.Add(obj.ToObject<object>());
  470. }
  471. }
  472. return Ok(new { state = 200, infos });
  473. }
  474. public record RecSchoolDate
  475. {
  476. public string id { get; set; }
  477. public string name { get; set; }
  478. public long dataCount { get; set; }
  479. }
  480. }
  481. }