SchoolController.cs 33 KB

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