SchoolController.cs 52 KB

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