SchoolController.cs 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. using Azure.Cosmos;
  2. using DocumentFormat.OpenXml.Bibliography;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Options;
  6. using NUnit.Framework.Constraints;
  7. using Pipelines.Sockets.Unofficial.Arenas;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Text.Json;
  14. using System.Threading.Tasks;
  15. using TEAMModelBI.Models;
  16. using TEAMModelBI.Tool;
  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.Cosmos.BI.BISchool;
  26. using TEAMModelOS.SDK.Models.Service.BI;
  27. using TEAMModelOS.SDK.Models.Service.BIStatsWay;
  28. using static TEAMModelBI.Controllers.Census.SchoolController;
  29. using LessonStats = TEAMModelOS.SDK.Models.Cosmos.BI.LessonStats;
  30. namespace TEAMModelBI.Controllers.Census
  31. {
  32. [Route("school")]
  33. [ApiController]
  34. public class SchoolController : ControllerBase
  35. {
  36. private readonly AzureCosmosFactory _azureCosmos;
  37. private readonly AzureStorageFactory _azureStorage;
  38. private readonly DingDing _dingDing;
  39. private readonly Option _option;
  40. private readonly HttpTrigger _httpTrigger;
  41. public SchoolController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, HttpTrigger httpTrigger)
  42. {
  43. _azureCosmos = azureCosmos;
  44. _azureStorage = azureStorage;
  45. _dingDing = dingDing;
  46. _option = option?.Value;
  47. _httpTrigger = httpTrigger;
  48. }
  49. /// <summary>
  50. /// 统计所有分析:基础、课例、活动、资源
  51. /// </summary>
  52. /// <param name="jsonElement"></param>
  53. /// <returns></returns>
  54. [ProducesDefaultResponseType]
  55. [HttpPost("get-all")]
  56. public async Task<IActionResult> GetAll(JsonElement jsonElement)
  57. {
  58. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  59. var cosmosClient = _azureCosmos.GetCosmosClient();
  60. //////分开部署,就不需要,一站多用时,取消注释
  61. //if ($"{site}".Equals(BIConst.Global))
  62. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  63. long schoolCount = 0; //学校数量
  64. int tecCount = 0; //教师数量
  65. int stuCount = 0; //学生数量
  66. long roomCount = 0; //教室数量
  67. long wisdomRoomCount = 0; //智慧教室数量
  68. long allClassCount = 0; //所有班级
  69. long allLessCount = 0; //所有课例
  70. long lastYearLessCount = 0; //去年课例
  71. long yearLessCount = 0;//今年课例
  72. long lastWeekLessCount = 0; //上周课例
  73. long weekLessCount = 0; //本周课例
  74. long lastTermLessCount = 0; //上学期课例
  75. long termLessCount = 0; //本学期课例
  76. long allActivityCount = 0; //所有活动
  77. long lastActivityCount = 0; //去年活动
  78. long activityCount = 0; //今年活动
  79. long lastWeekActivitCount = 0; //上周活动
  80. long weekActivitCount = 0; //本周活动
  81. long lastTermActivitCount = 0; //上学期活动
  82. long TermActivitCount = 0; //本学期学期活动
  83. long rercCount = 0; //所有资源数量
  84. long weekRercCount = 0; //本周资源数量
  85. long lastWeekRercCount = 0; //上周资源数量
  86. long lastTermRercCount = 0; //上学期资源
  87. long termRercCount = 0; //本学期资源
  88. long lastYearRercCount = 0; //去年资源
  89. long yearRercCount = 0; //去年资源
  90. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  91. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  92. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  93. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  94. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  95. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  96. schoolCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c", "Base"); //所有学校数量
  97. tecCount = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", "select value(count(c.id)) from c", "Base"); //所有教师数量
  98. stuCount = await CommonFind.GetSqlValueCount(cosmosClient, "Student", "select value(count(c.id)) from c", "Base"); //所有学生数量
  99. allClassCount = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School" }, "select count(c.id) totals from c where c.pk = 'Class'"); //所有班级数量
  100. roomCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c where c.pk = 'Room'"); //所有教室数量
  101. wisdomRoomCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c where c.pk = 'Room' and c.serial != null"); //智慧教室数量
  102. List<string> containers = new() { "School", "Teacher" };
  103. string lessSqlTxt = "select value(count(c.id)) from c where c.pk='LessonRecord' and c.startTime>={0} and c.startTime<={1}";
  104. allLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, $"select count(c.id) as totals from c where c.pk='LessonRecord'");//所有课例
  105. lastYearLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastYearStart, lastYearEnd)); //去年课例
  106. yearLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, yearStart, yearEnd)); //今年课例
  107. lastWeekLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastWeekStart, lastWeekEnd)); //上周课例
  108. weekLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, weekStart, weekEnd)); //本周课例
  109. lastTermLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastTermStart, lastTermEnd)); //上学期课例
  110. termLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, termStart, termEnd)); //上学期课例
  111. string typeSqlTxt = "select value(count(c.id)) from c where c.pk='{0}' and c.createTime >={1} and c.createTime<= {2}";
  112. foreach (var type in StaticValue.activityTypes)
  113. {
  114. allActivityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", $"select value(count(c.id)) from c where c.pk='{type}' ");//所有活动
  115. lastActivityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastYearStart, lastYearEnd)); //去年活动
  116. activityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, yearStart, yearEnd)); //今年活动
  117. lastWeekActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastWeekStart, lastWeekEnd)); //上周活动
  118. weekActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, weekStart, weekEnd)); //本周活动
  119. lastTermActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastTermStart, lastTermEnd)); //上学期活动
  120. TermActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, termStart, termEnd)); //本学期学期活动
  121. }
  122. string bloblSqlTxt = "select value(count(c.id)) from c where c.pk='Bloblog' and c.time>={0} and c.time<={1}";
  123. rercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, "select value(count(c.id)) from c where c.pk='Bloblog'"); //所有资源
  124. lastWeekRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastWeekStart, lastWeekEnd)); //上周资源
  125. weekRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, weekStart, weekEnd)); //本周资源
  126. lastTermRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastTermStart, lastTermEnd)); //上学期资源
  127. termRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, termStart, termEnd)); //这学期资源
  128. lastYearRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastYearStart, lastYearEnd)); //去年资源
  129. yearRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, yearStart, yearEnd)); //今年资源
  130. 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 });
  131. }
  132. /// <summary>
  133. /// 查询顾问相关的学校统计数据
  134. /// </summary>
  135. /// <param name="jsonElement"></param>
  136. /// <returns></returns>
  137. [ProducesDefaultResponseType]
  138. [HttpPost("get-assist")]
  139. public async Task<IActionResult> GetAssistStatis(JsonElement jsonElement)
  140. {
  141. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  142. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  143. int tecCount = 0; //教师数量
  144. int stuCount = 0; //学校数量
  145. int classCount = 0; //班级数量
  146. int roomCount = 0; //智慧教师数量
  147. int allLessonCount = 0; //课例数量
  148. int lastWeekLessCount = 0;// 上周课例数
  149. int weekLessCount = 0; //本周课例
  150. int lastTermLessCount = 0;// 上学期课例数
  151. int termLessCount = 0; //本学期课例
  152. int lastYearLessCount = 0; //去年课例
  153. int yearLessCount = 0; //今年课例
  154. int allBloblog = 0; //学校资源
  155. int lastYearBloblog = 0; //去年学校资源
  156. int yearBloblog = 0; //今年学校资源
  157. long allActivity = 0; //学校所有活动
  158. long lastYearActivity = 0; //去年学校所有活动
  159. long yearActivity = 0; //今年学校所有活动
  160. long lastWeekActivity = 0; //上周学校所有活动
  161. long weekActivity = 0; //本周学校所有活动
  162. var cosmosClient = _azureCosmos.GetCosmosClient();
  163. ////分开部署,就不需要,一站多用时,取消注释
  164. //if ($"{site}".Equals(BIConst.Global))
  165. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  166. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  167. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  168. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  169. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  170. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  171. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  172. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  173. string unifySqlTxt = "select value(count(c.id)) from c";
  174. string unifyTimeSql = "select value(count(c.id)) from c where c.startTime>={0} and c.startTime<={1}";
  175. string blobTimeSql = "select value(count(c.id)) from c where c.time>={0} and c.time<={1}";
  176. List<RecSchoolDate> recSchoolDates = new();
  177. foreach (var itemId in schoolIds)
  178. {
  179. School school = new();
  180. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
  181. if (response.Status == 200)
  182. {
  183. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  184. school = json.ToObject<School>();
  185. }
  186. 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}");
  187. stuCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", unifySqlTxt, $"Base-{itemId}");
  188. classCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Class-{itemId}");
  189. roomCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Room-{itemId}");
  190. //学校所有课例
  191. int tempLessCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"LessonRecord-{itemId}");
  192. lastWeekLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastWeekStart, lastWeekEnd), $"LessonRecord-{itemId}");
  193. weekLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, weekStart, weekEnd), $"LessonRecord-{itemId}");
  194. lastTermLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastTermStart, lastTermEnd), $"LessonRecord-{itemId}");
  195. termLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, termStart, termEnd), $"LessonRecord-{itemId}");
  196. lastYearLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastYearStart, lastYearEnd), $"LessonRecord-{itemId}");
  197. yearLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, yearStart, yearEnd), $"LessonRecord-{itemId}");
  198. //学校资源
  199. int tempBloblog = await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Bloblog-{itemId}");
  200. lastYearBloblog += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(blobTimeSql, lastYearStart, lastYearEnd), $"Bloblog-{itemId}");
  201. yearBloblog += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(blobTimeSql, yearStart, yearEnd), $"Bloblog-{itemId}");
  202. //学校活动
  203. long tempallActivity = 0;
  204. //统计活动
  205. foreach (var type in StaticValue.activityTypes)
  206. {
  207. 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}";
  208. tempallActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", $"SELECT value(count(c.id)) FROM c where c.pk = '{type}' and c.school = '{itemId}'");
  209. lastYearActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, lastYearStart, lastYearEnd));
  210. yearActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, yearStart, yearEnd));
  211. lastWeekActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, lastWeekStart, lastWeekEnd));
  212. weekActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, weekStart, weekEnd));
  213. }
  214. allLessonCount += tempLessCount;
  215. allActivity += tempallActivity;
  216. allBloblog += tempBloblog;
  217. recSchoolDates.Add(new RecSchoolDate() { id = school.id, name = school.name, dataCount = (tempLessCount + tempallActivity + tempBloblog) });
  218. }
  219. 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 });
  220. }
  221. /// <summary>
  222. /// 依据学校Id统计学校分析
  223. /// </summary>
  224. /// <param name="jsonElement"></param>
  225. /// <returns></returns>
  226. [ProducesDefaultResponseType]
  227. [HttpPost("get-idstatis")]
  228. public async Task<IActionResult> GetIdStatis(JsonElement jsonElement)
  229. {
  230. if (!jsonElement.TryGetProperty("schoolId", out JsonElement schoolId)) return BadRequest();
  231. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  232. int tecCount = 0; //学校教师数量
  233. int stuCount = 0; //学校学生数量
  234. int classCount = 0; //班级数量
  235. int roomCount = 0; //教室教师数量
  236. long allLessCount = 0; //课例总数
  237. int lastWeekLess = 0; //上周的总数
  238. int weekLess = 0; //本周的总数
  239. int lastTermLess = 0; //上学期的总数
  240. int termLess = 0; //本学期的总数
  241. int lessYearLess = 0; //去年的总数
  242. int yearLess = 0; //去年的总数
  243. int allActivity = 0; //学校所有活动
  244. int lastYearActivity = 0; //去年学校所有活动
  245. int yearActivity = 0; //今年学校所有活动
  246. int lastWeekActivity = 0; //上周学校所有活动
  247. int weekActivity = 0; //本周学校所有活动
  248. int allBlob = 0; //所有资源
  249. int lastYearBlob = 0; //去年的资源
  250. int yearBlob = 0; //去年的资源
  251. var cosmosClient = _azureCosmos.GetCosmosClient();
  252. ////分开部署,就不需要,一站多用时,取消注释
  253. //if ($"{site}".Equals(BIConst.Global))
  254. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  255. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  256. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  257. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  258. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  259. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  260. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  261. 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}");
  262. stuCount = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", code: $"Base-{schoolId}");
  263. classCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Class-{schoolId}");
  264. roomCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Room-{schoolId}");
  265. string unifyTimeSql = "select value(count(c.id)) from c where c.startTime>={0} and c.startTime<={1}";
  266. allLessCount = await LessonStatisWay.GetSchoolIdLessonCount(cosmosClient, $"{schoolId}");
  267. lastWeekLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastWeekStart, lastWeekEnd), $"LessonRecord-{schoolId}");
  268. weekLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, weekStart, lastWeekEnd), $"LessonRecord-{schoolId}");
  269. lastTermLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastTermStart, lastTermEnd), $"LessonRecord-{schoolId}");
  270. termLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, termStart, termEnd), $"LessonRecord-{schoolId}");
  271. lessYearLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, lastYearStart, lastYearEnd), $"LessonRecord-{schoolId}");
  272. yearLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, yearStart, yearEnd), $"LessonRecord-{schoolId}");
  273. //统计活动
  274. foreach (var type in StaticValue.activityTypes)
  275. {
  276. 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}";
  277. allActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"select value(count(c.id)) from c where c.pk = '{type}' and c.school = '{schoolId}'");
  278. lastYearActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, lastYearStart, lastYearEnd));
  279. yearActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, yearStart, yearEnd));
  280. lastWeekActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, lastWeekStart, lastWeekEnd));
  281. weekActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", string.Format(sqlTime, type, schoolId, weekStart, weekEnd));
  282. }
  283. //学校资源
  284. string blobTimeSql = "select value(count(c.id)) from c where c.time>={0} and c.time<={1}";
  285. allBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"Bloblog-{schoolId}");
  286. lastYearBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(blobTimeSql, lastYearStart, lastYearEnd), $"Bloblog-{schoolId}");
  287. yearBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(blobTimeSql, yearStart, yearEnd), $"Bloblog-{schoolId}");
  288. //获取所有的课程记录
  289. List<LessonRecord> records = new();
  290. 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}") }))
  291. {
  292. records.Add(item);
  293. }
  294. List<(string name, int count)> gradeCount = new();
  295. if (records.Count > 0)
  296. {
  297. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{schoolId}", new PartitionKey($"Base"));
  298. School sc = new();
  299. if (response.Status == 200)
  300. {
  301. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  302. sc = json.ToObject<School>();
  303. }
  304. List<string> grades = new();
  305. foreach (var item in records)
  306. {
  307. foreach (string gId in item.grade)
  308. {
  309. if (!grades.Contains(gId))
  310. {
  311. grades.Add(gId);
  312. }
  313. }
  314. }
  315. foreach (var gId in grades)
  316. {
  317. var c = records.Where(r => r.grade.Contains(gId)).Count();
  318. gradeCount.Add((gId, c));
  319. }
  320. }
  321. return Ok(new { state = 200, tecCount, stuCount, classCount, roomCount, allLessCount, lastWeekLess, weekLess, lastTermLess, termLess, lessYearLess, yearLess, allActivity, lastYearActivity, yearActivity, lastWeekActivity, weekActivity, allBlob, lastYearBlob, yearBlob });
  322. }
  323. /// <summary>
  324. /// 未区域学校统计
  325. /// </summary>
  326. /// <param name="jsonElement"></param>
  327. /// <returns></returns>
  328. [ProducesDefaultResponseType]
  329. [HttpPost("get-noarea")]
  330. public async Task<IActionResult> GetNoAreaCnt(JsonElement jsonElement)
  331. {
  332. var cosmosClient = _azureCosmos.GetCosmosClient();
  333. int tLessCnt = 0; //课例
  334. int tActCnt = 0; //活动
  335. List<string> scIds = new(); //学校id集合
  336. string scIdSql = "select value(c.id) from c where c.areaId=null or c.areaId=''";
  337. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: scIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  338. {
  339. scIds.Add(item);
  340. }
  341. string comSql = BICommonWay.ManyScSql("c.school", scIds);
  342. string lesSql = $"select value(count(c.id)) from c where c.pk='LessonRecord' and {comSql}";
  343. tLessCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, lesSql);
  344. comSql = BICommonWay.ManyScSql(" and c.school", scIds);
  345. tActCnt = await ActivityWay.GetCnt(cosmosClient, condSql: comSql);
  346. return Ok(new { state = 200, scCnt = scIds.Count(), tLessCnt, tActCnt });
  347. }
  348. /// <summary>
  349. /// 未加区的学校统计
  350. /// </summary>
  351. /// <param name="jsonElement"></param>
  352. /// <returns></returns>
  353. [ProducesDefaultResponseType]
  354. [HttpPost("get-noareasc")]
  355. public async Task<IActionResult> GetNoAareaScCnt(JsonElement jsonElement)
  356. {
  357. var cosmosClient = _azureCosmos.GetCosmosClient();
  358. var tableClient = _azureStorage.GetCloudTableClient();
  359. var table = tableClient.GetTableReference("BIDDUserInfo");
  360. var (dayS, dayE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow);
  361. var (weekS, weekE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week");
  362. var (monthS, monthE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "month");
  363. var (termS, termE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term");
  364. List<AreaAssist> areaAssists = new(); //顾问信息
  365. int tchCnt = 0; //所有区级老师
  366. int stuCnt = 0; //所有区级学生
  367. int tLessCnt = 0; //课例数量
  368. int tActCnt = 0; //活动数量
  369. HashSet<string> tchIds = new();
  370. List<string> scIds = new(); //学校id集合
  371. string scIdSql = "select value(c.id) from c where c.areaId=null or c.areaId=''";
  372. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: scIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  373. {
  374. scIds.Add(item);
  375. }
  376. string scsSql = BICommonWay.ManyScSql("c.school", scIds);
  377. string cntSql = $"select value(count(c.id)) from c where {scsSql}";
  378. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<int>(queryText: cntSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  379. {
  380. tchCnt += item;
  381. }
  382. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<int>(queryText: cntSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  383. {
  384. stuCnt += item;
  385. }
  386. foreach (var scId in scIds)
  387. {
  388. string tchIdSql = $"select value(c.id) from c where array_contains(c.roles,'assist',true) and c.pk='Teacher'";
  389. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: tchIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{scId}") }))
  390. {
  391. tchIds.Add(item);
  392. }
  393. }
  394. List<string> noIds = new();
  395. foreach (var item in tchIds)
  396. {
  397. List<DingDingUserInfo> ddUsers = await table.QueryWhereString<DingDingUserInfo>($"PartitionKey eq '{_option.Location}' and tmdId eq '{item}'");
  398. if (ddUsers.Count > 0)
  399. {
  400. foreach (var dduser in ddUsers)
  401. {
  402. AreaAssist areaAssist = new()
  403. {
  404. userId = dduser.userId,
  405. unionId = dduser.unionId,
  406. name = dduser.name,
  407. mobile = dduser.mobile,
  408. avatar = dduser.avatar,
  409. tmdId = dduser.tmdId,
  410. tmdName = dduser.tmdName,
  411. tmdMobile = dduser.tmdMobile,
  412. picture = dduser.picture,
  413. roles = dduser.roles,
  414. schoolIds = dduser.schoolIds
  415. };
  416. areaAssists.Add(areaAssist);
  417. }
  418. }
  419. else { noIds.Add(item); }
  420. }
  421. if (scIds.Count > 0)
  422. {
  423. string lesSql = $"select value(count(c.id)) from c where c.pk='LessonRecord' and {scsSql}";
  424. tLessCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, lesSql);
  425. string actComSql = BICommonWay.ManyScSql(" and c.school", scIds);
  426. tActCnt = await ActivityWay.GetCnt(cosmosClient, condSql: actComSql);
  427. }
  428. return Ok(new { state = RespondCode.Ok, scCnt = scIds.Count, tchCnt, stuCnt, allCnt = tLessCnt + tActCnt, tLessCnt, tActCnt });
  429. }
  430. /// <summary>
  431. /// 学校统计
  432. /// 新的数据结构
  433. /// </summary>
  434. /// <param name="jsonElement"></param>
  435. /// <returns></returns>
  436. [ProducesDefaultResponseType]
  437. [HttpPost("get-scstats")]
  438. public async Task<IActionResult> GetSchoolStats(JsonElement jsonElement)
  439. {
  440. if (!jsonElement.TryGetProperty("scId", out JsonElement scId)) return BadRequest();
  441. var cosmosClient = _azureCosmos.GetCosmosClient();
  442. School school = new();
  443. ScStats scStats = new();
  444. var respSc = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{scId}", new PartitionKey($"Base"));
  445. if (respSc.Status == 200)
  446. {
  447. using var fileJson = await JsonDocument.ParseAsync(respSc.ContentStream);
  448. school = fileJson.ToObject<School>();
  449. }
  450. else
  451. return Ok(new { state = RespondCode.NotFound, msg = $"未找到id:{scId};相关的学校,请检查" });
  452. try
  453. {
  454. bool isExist = true;
  455. StatsInfo statsInfo = new();
  456. var scDataStats = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{DateTimeOffset.UtcNow.Year}-{scId}", new PartitionKey("Statistics"));
  457. if (scDataStats.Status == 200)
  458. {
  459. using var fileJson = await JsonDocument.ParseAsync(scDataStats.ContentStream);
  460. statsInfo = fileJson.ToObject<StatsInfo>();
  461. }
  462. else
  463. {
  464. scStats.id = school.id;
  465. isExist = false;
  466. }
  467. bool isnew = false;
  468. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  469. long now = dateTime.ToUnixTimeMilliseconds();
  470. if (!string.IsNullOrEmpty(statsInfo.id))
  471. {
  472. scStats.id = statsInfo.id;
  473. scStats.schoolId = statsInfo.schoolId;
  474. scStats.name = statsInfo.name;
  475. scStats.picture = statsInfo.picture;
  476. scStats.areaId = statsInfo.areaId;
  477. scStats.tch = statsInfo.tch;
  478. scStats.dayTch = statsInfo.dayTch;
  479. scStats.weekTch = statsInfo.weekTch;
  480. scStats.monthTch = statsInfo.monthTch;
  481. scStats.stu = statsInfo.stu;
  482. scStats.dayStu = statsInfo.dayStu;
  483. scStats.weekStu = statsInfo.weekStu;
  484. scStats.monthStu = statsInfo.monthStu;
  485. scStats.room = statsInfo.room;
  486. scStats.witRoom = statsInfo.witRoom;
  487. scStats.size = statsInfo.size;
  488. scStats.scCreateTime = statsInfo.scCreateTime;
  489. scStats.upTime = statsInfo.upTime;
  490. scStats.lessStats.open = statsInfo.lesson.all;
  491. scStats.lessStats.open = statsInfo.lesson.open;
  492. scStats.lessStats.less = statsInfo.lesson.less;
  493. scStats.lessStats.lastDay = statsInfo.lesson.lastDay;
  494. scStats.lessStats.day = statsInfo.lesson.day;
  495. scStats.lessStats.lastWeek = statsInfo.lesson.lastWeek;
  496. scStats.lessStats.week = statsInfo.lesson.week;
  497. scStats.lessStats.lastTerm = statsInfo.lesson.lastTerm;
  498. scStats.lessStats.term = statsInfo.lesson.term;
  499. scStats.lessStats.lastDayInter = statsInfo.lesson.lastDayInter;
  500. scStats.lessStats.dayInter = statsInfo.lesson.dayInter;
  501. scStats.lessStats.lastMonthInter = statsInfo.lesson.lastMonthInter;
  502. scStats.lessStats.monthInter = statsInfo.lesson.monthInter;
  503. scStats.lessStats.lastYearInter = statsInfo.lesson.lastYearInter;
  504. scStats.lessStats.yearInter = statsInfo.lesson.yearInter;
  505. scStats.lessStats.yearInters = TimeHelper.GetYearMonth(statsInfo.lesson.yearInters, dateTime.Year, dateTime.Month);
  506. scStats.lessStats.year = TimeHelper.GetYearMonth(statsInfo.lesson.year, dateTime.Year, dateTime.Month);
  507. scStats.actStats.all = statsInfo.activity.all;
  508. scStats.actStats.exam = statsInfo.activity.exam;
  509. scStats.actStats.survey = statsInfo.activity.survey;
  510. scStats.actStats.vote = statsInfo.activity.vote;
  511. scStats.actStats.homework = statsInfo.activity.homework;
  512. scStats.actStats.lastDay = statsInfo.activity.lastDay;
  513. scStats.actStats.dayCnt = statsInfo.activity.dayCnt;
  514. scStats.actStats.lastWeek = statsInfo.activity.lastWeek;
  515. scStats.actStats.week = statsInfo.activity.week;
  516. scStats.actStats.lastTerm = statsInfo.activity.lastTerm;
  517. scStats.actStats.term = statsInfo.activity.term;
  518. //scStats.actStats.LastYear = TimeHelper.GetYearMonth(statsInfo.activity.LastYear, dateTime.Year, dateTime.Month);
  519. scStats.actStats.year = TimeHelper.GetYearMonth(statsInfo.activity.year, dateTime.Year, dateTime.Month);
  520. if (statsInfo.study != null)
  521. {
  522. scStats.srStats.learnTime = statsInfo.study.learnTime;
  523. scStats.srStats.online = statsInfo.study.online;
  524. scStats.srStats.offline = statsInfo.study.offline;
  525. scStats.srStats.classRoom = statsInfo.study.classRoom;
  526. scStats.srStats.submit = statsInfo.study.submit;
  527. scStats.srStats.notStarted = statsInfo.study.notStarted;
  528. scStats.srStats.ongoing = statsInfo.study.ongoing;
  529. scStats.srStats.finish = statsInfo.study.finish;
  530. }
  531. }
  532. //if(statsInfo.upTime > 0 && !isnew) {
  533. // long timedis = 20 * 60 * 1000;//20分钟
  534. // if (now - statsInfo.upTime < timedis)
  535. // isnew = true;
  536. //}
  537. //if (statsInfo.upTime == 0 && isnew)
  538. // statsInfo.upTime = now;
  539. //if (statsInfo.upTime == 0 && isnew)
  540. // statsInfo.upTime = DateTimeOffset.UtcNow.Add(new TimeSpan(-1, 0, 0, 0)).ToUnixTimeMilliseconds();
  541. //if (!isnew)
  542. //{
  543. // //Function 处理Function 更新或者新增
  544. // //_ = _httpTrigger.RequestHttpTrigger(new { schoolId = $"{school.id}"}, _option.Location, "stats-sc-info");
  545. // //异步方法处理 同步更新
  546. // statsInfo = await SchoolStatsWay.GetSingleSc(cosmosClient, school.id);
  547. // statsInfo.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  548. // if (isExist)
  549. // statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<StatsInfo>(statsInfo, statsInfo.id, new PartitionKey("Stats"));
  550. // else
  551. // statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<StatsInfo>(statsInfo, new PartitionKey("Stats"));
  552. //}
  553. return Ok(new { state = RespondCode.Ok, scStats });
  554. }
  555. catch (Exception ex)
  556. {
  557. //await _dingDing.SendBotMsg($"BI,{_option.Location} /school/set-scstats \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  558. return BadRequest();
  559. }
  560. }
  561. /// <summary>
  562. /// 区域统计
  563. /// 新的数据结构
  564. /// </summary>
  565. /// <param name="jsonElement"></param>
  566. /// <returns></returns>
  567. [ProducesDefaultResponseType]
  568. [HttpPost("get-areastats")]
  569. public async Task<IActionResult> GetAreaStats(JsonElement jsonElement)
  570. {
  571. jsonElement.TryGetProperty("areaId", out JsonElement areaId);
  572. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  573. jsonElement.TryGetProperty("role", out JsonElement role);
  574. if (string.IsNullOrEmpty($"{areaId}") && string.IsNullOrEmpty($"{tmdId}"))
  575. return Ok(new { stats = RespondCode.ParamsError, msg = "areaId/tmdId两者参数不能都为空!" });
  576. var cosmosClient = _azureCosmos.GetCosmosClient();
  577. Area area = null;
  578. ScStats areaScStats = new();
  579. List<string> scIds = new();
  580. StringBuilder scSql = new("select value(c.id) from c");
  581. StringBuilder lastScSql = new("select value(c.id) from c");
  582. if (!string.IsNullOrEmpty($"{areaId}"))
  583. {
  584. scSql.Append($" where c.areaId='{areaId}'");
  585. lastScSql.Append($" where c.areaId='{areaId}'");
  586. var respSc = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{areaId}", new PartitionKey($"Base-Area"));
  587. if (respSc.Status == 200)
  588. {
  589. using var fileJson = await JsonDocument.ParseAsync(respSc.ContentStream);
  590. area = fileJson.ToObject<Area>();
  591. }
  592. else
  593. return Ok(new { state = RespondCode.NotFound, msg = $"未找到id:{areaId};相关的学区,请检查!" });
  594. }
  595. if (!string.IsNullOrEmpty($"{tmdId}"))
  596. {
  597. switch ($"{role}")
  598. {
  599. case "assists":
  600. scSql.Append($" join a in c.assists where a.id='{tmdId}'");
  601. lastScSql.Append($" join a in c.assists where a.id='{tmdId}'");
  602. break;
  603. case "sales":
  604. scSql.Append($" join a in c.sales where a.id='{tmdId}'");
  605. lastScSql.Append($" join a in c.sales where a.id='{tmdId}'");
  606. break;
  607. default:
  608. return Ok(new { state = RespondCode.ParamsError, msg = "role参数错误" });
  609. }
  610. }
  611. scIds = await CommonFind.FindScIds(cosmosClient, scSql.ToString(), "BIRel");
  612. StringBuilder statsSql = new("select value(c) from c");
  613. if (scIds.Count > 0)
  614. {
  615. statsSql.Append($" where {BICommonWay.ManyScSql("c.id", scIds, $"{DateTimeOffset.UtcNow.Year}-")}");
  616. }
  617. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  618. List<ScStats> scStatss = new();
  619. List<StatsInfo> statsInfos = new();
  620. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<StatsInfo>(queryText: statsSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Statistics") }))
  621. {
  622. statsInfos.Add(item);
  623. }
  624. StringBuilder lastStsSql = new("select c.lesson,c.activity from c");
  625. if (scIds.Count > 0)
  626. {
  627. lastStsSql.Append($" where {BICommonWay.ManyScSql("c.id", scIds, $"{DateTimeOffset.UtcNow.Year - 1}-")}");
  628. }
  629. List<LastYearLessAndAct> lastYear = new();
  630. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LastYearLessAndAct>(queryText: lastStsSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Statistics") }))
  631. {
  632. lastYear.Add(item);
  633. }
  634. StatsInfo statsInfo = null;
  635. statsInfo = SchoolStatsWay.GetAreaStats(cosmosClient, _httpTrigger, _option, statsInfos, scIds, area);
  636. if (statsInfo != null)
  637. {
  638. areaScStats.id = statsInfo.id;
  639. areaScStats.schoolId = statsInfo.schoolId;
  640. areaScStats.name = statsInfo.name;
  641. areaScStats.picture = statsInfo.picture;
  642. areaScStats.areaId = statsInfo.areaId;
  643. areaScStats.tch = statsInfo.tch;
  644. areaScStats.dayTch = statsInfo.dayTch;
  645. areaScStats.weekTch = statsInfo.weekTch;
  646. areaScStats.monthTch = statsInfo.monthTch;
  647. areaScStats.stu = statsInfo.stu;
  648. areaScStats.dayStu = statsInfo.dayStu;
  649. areaScStats.weekStu = statsInfo.weekStu;
  650. areaScStats.monthStu = statsInfo.monthStu;
  651. areaScStats.room = statsInfo.room;
  652. areaScStats.witRoom = statsInfo.witRoom;
  653. areaScStats.size = statsInfo.size;
  654. areaScStats.scCreateTime = statsInfo.scCreateTime;
  655. areaScStats.upTime = statsInfo.upTime;
  656. areaScStats.lessStats.open = statsInfo.lesson.all;
  657. areaScStats.lessStats.open = statsInfo.lesson.open;
  658. areaScStats.lessStats.less = statsInfo.lesson.less;
  659. areaScStats.lessStats.lastDay = statsInfo.lesson.lastDay;
  660. areaScStats.lessStats.day = statsInfo.lesson.day;
  661. areaScStats.lessStats.lastWeek = statsInfo.lesson.lastWeek;
  662. areaScStats.lessStats.week = statsInfo.lesson.week;
  663. areaScStats.lessStats.lastTerm = statsInfo.lesson.lastTerm;
  664. areaScStats.lessStats.term = statsInfo.lesson.term;
  665. areaScStats.lessStats.lastDayInter = statsInfo.lesson.lastDayInter;
  666. areaScStats.lessStats.dayInter = statsInfo.lesson.dayInter;
  667. areaScStats.lessStats.lastMonthInter = statsInfo.lesson.lastMonthInter;
  668. areaScStats.lessStats.monthInter = statsInfo.lesson.monthInter;
  669. areaScStats.lessStats.lastYearInter = statsInfo.lesson.lastYearInter;
  670. areaScStats.lessStats.yearInter = statsInfo.lesson.yearInter;
  671. areaScStats.lessStats.yearInters = TimeHelper.GetYearMonth(statsInfo.lesson.yearInters, dateTime.Year, dateTime.Month);
  672. areaScStats.lessStats.LastYear = TimeHelper.GetYearMonth(BICommonWay.ManyDoubleMerge(lastYear.Select(s => s.lesson.year).Where(w => w.Count > 0).ToList()), dateTime.Year, dateTime.Month);
  673. areaScStats.lessStats.year = TimeHelper.GetYearMonth(statsInfo.lesson.year, dateTime.Year, dateTime.Month);
  674. areaScStats.actStats.all = statsInfo.activity.all;
  675. areaScStats.actStats.exam = statsInfo.activity.exam;
  676. areaScStats.actStats.survey = statsInfo.activity.survey;
  677. areaScStats.actStats.vote = statsInfo.activity.vote;
  678. areaScStats.actStats.homework = statsInfo.activity.homework;
  679. areaScStats.actStats.lastDay = statsInfo.activity.lastDay;
  680. areaScStats.actStats.dayCnt = statsInfo.activity.dayCnt;
  681. areaScStats.actStats.lastWeek = statsInfo.activity.lastWeek;
  682. areaScStats.actStats.week = statsInfo.activity.week;
  683. areaScStats.actStats.lastTerm = statsInfo.activity.lastTerm;
  684. areaScStats.actStats.term = statsInfo.activity.term;
  685. areaScStats.actStats.LastYear = TimeHelper.GetYearMonth(BICommonWay.ManyDoubleMerge(lastYear.Select(s => s.lesson.year).Where(w => w.Count > 0).ToList()), dateTime.Year, dateTime.Month);
  686. areaScStats.actStats.year = TimeHelper.GetYearMonth(statsInfo.activity.year, dateTime.Year, dateTime.Month);
  687. if (statsInfo.study != null)
  688. {
  689. areaScStats.srStats.learnTime = statsInfo.study.learnTime;
  690. areaScStats.srStats.online = statsInfo.study.online;
  691. areaScStats.srStats.offline = statsInfo.study.offline;
  692. areaScStats.srStats.classRoom = statsInfo.study.classRoom;
  693. areaScStats.srStats.submit = statsInfo.study.submit;
  694. areaScStats.srStats.notStarted = statsInfo.study.notStarted;
  695. areaScStats.srStats.ongoing = statsInfo.study.ongoing;
  696. areaScStats.srStats.finish = statsInfo.study.finish;
  697. }
  698. }
  699. var scInfos = statsInfos.Select(s => new { s.id, s.schoolId, s.name, s.picture, actLess = (s.lesson.all + s.activity.all), actCnt = s.activity.all, lessCnt = s.lesson.all, lessOpen = s.lesson.open, lessUp = s.lesson.less }).ToList();
  700. return Ok(new { state = RespondCode.Ok, areaScStats, scInfos });
  701. }
  702. /// <summary>
  703. /// 学校信息中间件查询接口
  704. /// </summary>
  705. /// <param name="jsonElement"></param>
  706. /// <returns></returns>
  707. [ProducesDefaultResponseType]
  708. [HttpPost("get-scinfos")]
  709. public async Task<IActionResult> GetScInfos(JsonElement jsonElement)
  710. {
  711. var cosmosClient = _azureCosmos.GetCosmosClient();
  712. jsonElement.TryGetProperty("role", out JsonElement role);
  713. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  714. jsonElement.TryGetProperty("areaId", out JsonElement areaId);
  715. StringBuilder sql = new($"select value(c) from c");
  716. if (!string.IsNullOrEmpty($"{role}") && !string.IsNullOrEmpty($"{tmdId}") && string.IsNullOrEmpty($"{areaId}"))
  717. {
  718. switch ($"{role}")
  719. {
  720. case "assist":
  721. sql.Append($" join a in c.assist where a.id='{tmdId}'");
  722. break;
  723. case "sales":
  724. sql.Append($" join a in c.sales where a.id='{tmdId}'");
  725. break;
  726. }
  727. }
  728. if (!string.IsNullOrEmpty($"{areaId}") && !string.IsNullOrEmpty($"{role}"))
  729. {
  730. sql.Append($" where c.areaId ='{areaId}'");
  731. }
  732. //List<string> scId = await CommonFind.FindScIds(cosmosClient, "select value(c.id) from c ", "Base");
  733. List<BIRelation> scInfos = new();
  734. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<BIRelation>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BIRel") }))
  735. {
  736. scInfos.Add(item);
  737. }
  738. return Ok(new { state = RespondCode.Ok, allCnt = scInfos.Count, scInfos });
  739. }
  740. /// <summary>
  741. /// 依据Id查询School容器 数据管理工具——查询工具
  742. /// </summary>
  743. /// <param name="jsonElement"></param>
  744. /// <returns></returns>
  745. [ProducesDefaultResponseType]
  746. [HttpPost("get-info")]
  747. public async Task<IActionResult> GetSchool(JsonElement jsonElement)
  748. {
  749. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  750. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  751. var cosmosClient = _azureCosmos.GetCosmosClient();
  752. //if ($"{site}".Equals(BIConst.Global))
  753. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  754. List<object> infos = new List<object>();
  755. string sqlTxt = $"select value(c) from c where c.id='{id}'";
  756. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  757. {
  758. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  759. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  760. {
  761. infos.Add(obj.ToObject<object>());
  762. }
  763. }
  764. return Ok(new { state = 200, infos });
  765. }
  766. public record RecSchoolDate
  767. {
  768. public string id { get; set; }
  769. public string name { get; set; }
  770. public long dataCount { get; set; }
  771. }
  772. /// <summary>
  773. /// 前端显示学校统计数据
  774. /// </summary>
  775. public record ScStats
  776. {
  777. public string id { get; set; }
  778. public string schoolId { get; set; }
  779. public string name { get; set; }
  780. public string picture { get; set; }
  781. public string areaId { get; set; }
  782. public int tch { get; set; }
  783. public int dayTch { get; set; }
  784. public int weekTch { get; set; }
  785. public int monthTch { get; set; }
  786. public int stu { get; set; }
  787. public int dayStu { get; set; }
  788. public int weekStu { get; set; }
  789. public int monthStu { get; set; }
  790. public int room { get; set; }
  791. public int witRoom { get; set; }
  792. public int size { get; set; }
  793. public long scCreateTime { get; set; }
  794. public long upTime { get; set; }
  795. public ScLessStats lessStats { get; set; } = new ScLessStats();
  796. public ScActStats actStats { get; set; } = new ScActStats();
  797. public ScSRStats srStats { get; set; } = new ScSRStats();
  798. }
  799. /// <summary>
  800. /// 课例活动数据
  801. /// </summary>
  802. public record ScLessStats
  803. {
  804. public int all { get; set; }
  805. public int open { get; set; }
  806. public int less { get; set; }
  807. public int lastDay { get; set; }
  808. public int day { get; set; }
  809. public int lastWeek { get; set; }
  810. public int week { get; set; }
  811. public int lastTerm { get; set; }
  812. public int term { get; set; }
  813. public int lastDayInter { get; set; }
  814. public int dayInter { get; set; }
  815. public int lastMonthInter { get; set; }
  816. public int monthInter { get; set; }
  817. public int lastYearInter { get; set; }
  818. public int yearInter { get; set; }
  819. public List<YearMonth> yearInters { get; set; } = new List<YearMonth>();//12个月
  820. public List<YearMonth> LastYear { get; set; } = new List<YearMonth>();//12个月
  821. public List<YearMonth> year { get; set; } = new List<YearMonth>(); //12个月
  822. }
  823. /// <summary>
  824. /// 学校活动数据
  825. /// </summary>
  826. public record ScActStats
  827. {
  828. public int all { get; set; }
  829. public int exam { get; set; }
  830. public int survey { get; set; }
  831. public int vote { get; set; }
  832. public int homework { get; set; }
  833. public int lastDay { get; set; }
  834. public int dayCnt { get; set; }
  835. public int lastWeek { get; set; }
  836. public int week { get; set; }
  837. public int lastTerm { get; set; }
  838. public int term { get; set; }
  839. public List<YearMonth> LastYear { get; set; } = new List<YearMonth>(); //12个月
  840. public List<YearMonth> year { get; set; } = new List<YearMonth>(); //12个月
  841. }
  842. /// <summary>
  843. /// 研修统计数据
  844. /// </summary>
  845. public record ScSRStats
  846. {
  847. public int learnTime { get; set; }
  848. public int online { get; set; }
  849. public int offline { get; set; }
  850. public int classRoom { get; set; }
  851. public int submit { get; set; }
  852. public int notStarted { get; set; }
  853. public int ongoing { get; set; }
  854. public int finish { get; set; }
  855. }
  856. /// <summary>
  857. /// 查询去年的统计
  858. /// </summary>
  859. public record LastYearLessAndAct
  860. {
  861. /// <summary>
  862. /// 课例活动
  863. /// </summary>
  864. public LessonStats lesson { get; set; } = new LessonStats();
  865. /// <summary>
  866. /// 活动
  867. /// </summary>
  868. public ActivityStats activity { get; set; } = new ActivityStats();
  869. }
  870. }
  871. }