SchoolController.cs 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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.ActivitySticsController;
  29. using static TEAMModelBI.Controllers.Census.SchoolController;
  30. using LessonStats = TEAMModelOS.SDK.Models.Cosmos.BI.LessonStats;
  31. namespace TEAMModelBI.Controllers.Census
  32. {
  33. [Route("school")]
  34. [ApiController]
  35. public class SchoolController : ControllerBase
  36. {
  37. private readonly AzureCosmosFactory _azureCosmos;
  38. private readonly AzureStorageFactory _azureStorage;
  39. private readonly DingDing _dingDing;
  40. private readonly Option _option;
  41. private readonly HttpTrigger _httpTrigger;
  42. public SchoolController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, HttpTrigger httpTrigger)
  43. {
  44. _azureCosmos = azureCosmos;
  45. _azureStorage = azureStorage;
  46. _dingDing = dingDing;
  47. _option = option?.Value;
  48. _httpTrigger = httpTrigger;
  49. }
  50. /// <summary>
  51. /// 统计所有分析:基础、课例、活动、资源
  52. /// </summary>
  53. /// <param name="jsonElement"></param>
  54. /// <returns></returns>
  55. [ProducesDefaultResponseType]
  56. [HttpPost("get-all")]
  57. public async Task<IActionResult> GetAll(JsonElement jsonElement)
  58. {
  59. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  60. var cosmosClient = _azureCosmos.GetCosmosClient();
  61. //////分开部署,就不需要,一站多用时,取消注释
  62. //if ($"{site}".Equals(BIConst.Global))
  63. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  64. long schoolCount = 0; //学校数量
  65. int tecCount = 0; //教师数量
  66. int stuCount = 0; //学生数量
  67. long roomCount = 0; //教室数量
  68. long wisdomRoomCount = 0; //智慧教室数量
  69. long allClassCount = 0; //所有班级
  70. long allLessCount = 0; //所有课例
  71. long lastYearLessCount = 0; //去年课例
  72. long yearLessCount = 0;//今年课例
  73. long lastWeekLessCount = 0; //上周课例
  74. long weekLessCount = 0; //本周课例
  75. long lastTermLessCount = 0; //上学期课例
  76. long termLessCount = 0; //本学期课例
  77. long allActivityCount = 0; //所有活动
  78. long lastActivityCount = 0; //去年活动
  79. long activityCount = 0; //今年活动
  80. long lastWeekActivitCount = 0; //上周活动
  81. long weekActivitCount = 0; //本周活动
  82. long lastTermActivitCount = 0; //上学期活动
  83. long TermActivitCount = 0; //本学期学期活动
  84. long rercCount = 0; //所有资源数量
  85. long weekRercCount = 0; //本周资源数量
  86. long lastWeekRercCount = 0; //上周资源数量
  87. long lastTermRercCount = 0; //上学期资源
  88. long termRercCount = 0; //本学期资源
  89. long lastYearRercCount = 0; //去年资源
  90. long yearRercCount = 0; //去年资源
  91. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  92. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  93. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  94. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  95. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  96. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  97. schoolCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c", "Base"); //所有学校数量
  98. tecCount = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", "select value(count(c.id)) from c", "Base"); //所有教师数量
  99. stuCount = await CommonFind.GetSqlValueCount(cosmosClient, "Student", "select value(count(c.id)) from c", "Base"); //所有学生数量
  100. allClassCount = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School" }, "select count(c.id) totals from c where c.pk = 'Class'"); //所有班级数量
  101. roomCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c where c.pk = 'Room'"); //所有教室数量
  102. wisdomRoomCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(count(c.id)) from c where c.pk = 'Room' and c.serial != null"); //智慧教室数量
  103. List<string> containers = new() { "School", "Teacher" };
  104. string lessSqlTxt = "select value(count(c.id)) from c where c.pk='LessonRecord' and c.startTime>={0} and c.startTime<={1}";
  105. allLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, $"select count(c.id) as totals from c where c.pk='LessonRecord'");//所有课例
  106. lastYearLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastYearStart, lastYearEnd)); //去年课例
  107. yearLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, yearStart, yearEnd)); //今年课例
  108. lastWeekLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastWeekStart, lastWeekEnd)); //上周课例
  109. weekLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, weekStart, weekEnd)); //本周课例
  110. lastTermLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, lastTermStart, lastTermEnd)); //上学期课例
  111. termLessCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(lessSqlTxt, termStart, termEnd)); //上学期课例
  112. string typeSqlTxt = "select value(count(c.id)) from c where c.pk='{0}' and c.createTime >={1} and c.createTime<= {2}";
  113. foreach (var type in StaticValue.activityTypes)
  114. {
  115. allActivityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", $"select value(count(c.id)) from c where c.pk='{type}' ");//所有活动
  116. lastActivityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastYearStart, lastYearEnd)); //去年活动
  117. activityCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, yearStart, yearEnd)); //今年活动
  118. lastWeekActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastWeekStart, lastWeekEnd)); //上周活动
  119. weekActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, weekStart, weekEnd)); //本周活动
  120. lastTermActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, lastTermStart, lastTermEnd)); //上学期活动
  121. TermActivitCount += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(typeSqlTxt, type, termStart, termEnd)); //本学期学期活动
  122. }
  123. string bloblSqlTxt = "select value(count(c.id)) from c where c.pk='Bloblog' and c.time>={0} and c.time<={1}";
  124. rercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, "select value(count(c.id)) from c where c.pk='Bloblog'"); //所有资源
  125. lastWeekRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastWeekStart, lastWeekEnd)); //上周资源
  126. weekRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, weekStart, weekEnd)); //本周资源
  127. lastTermRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastTermStart, lastTermEnd)); //上学期资源
  128. termRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, termStart, termEnd)); //这学期资源
  129. lastYearRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, lastYearStart, lastYearEnd)); //去年资源
  130. yearRercCount = await CommonFind.GetSqlValueCount(cosmosClient, containers, string.Format(bloblSqlTxt, yearStart, yearEnd)); //今年资源
  131. 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 });
  132. }
  133. /// <summary>
  134. /// 查询顾问相关的学校统计数据
  135. /// </summary>
  136. /// <param name="jsonElement"></param>
  137. /// <returns></returns>
  138. [ProducesDefaultResponseType]
  139. [HttpPost("get-assist")]
  140. public async Task<IActionResult> GetAssistStatis(JsonElement jsonElement)
  141. {
  142. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  143. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  144. int tecCount = 0; //教师数量
  145. int stuCount = 0; //学校数量
  146. int classCount = 0; //班级数量
  147. int roomCount = 0; //智慧教师数量
  148. int allLessonCount = 0; //课例数量
  149. int lastWeekLessCount = 0;// 上周课例数
  150. int weekLessCount = 0; //本周课例
  151. int lastTermLessCount = 0;// 上学期课例数
  152. int termLessCount = 0; //本学期课例
  153. int lastYearLessCount = 0; //去年课例
  154. int yearLessCount = 0; //今年课例
  155. int allBloblog = 0; //学校资源
  156. int lastYearBloblog = 0; //去年学校资源
  157. int yearBloblog = 0; //今年学校资源
  158. long allActivity = 0; //学校所有活动
  159. long lastYearActivity = 0; //去年学校所有活动
  160. long yearActivity = 0; //今年学校所有活动
  161. long lastWeekActivity = 0; //上周学校所有活动
  162. long weekActivity = 0; //本周学校所有活动
  163. var cosmosClient = _azureCosmos.GetCosmosClient();
  164. ////分开部署,就不需要,一站多用时,取消注释
  165. //if ($"{site}".Equals(BIConst.Global))
  166. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  167. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  168. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  169. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  170. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  171. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  172. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  173. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  174. string unifySqlTxt = "select value(count(c.id)) from c";
  175. string unifyTimeSql = "select value(count(c.id)) from c where c.startTime>={0} and c.startTime<={1}";
  176. string blobTimeSql = "select value(count(c.id)) from c where c.time>={0} and c.time<={1}";
  177. List<RecSchoolDate> recSchoolDates = new();
  178. foreach (var itemId in schoolIds)
  179. {
  180. School school = new();
  181. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
  182. if (response.Status == 200)
  183. {
  184. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  185. school = json.ToObject<School>();
  186. }
  187. 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}");
  188. stuCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", unifySqlTxt, $"Base-{itemId}");
  189. classCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Class-{itemId}");
  190. roomCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Room-{itemId}");
  191. //学校所有课例
  192. int tempLessCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"LessonRecord-{itemId}");
  193. lastWeekLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastWeekStart, lastWeekEnd), $"LessonRecord-{itemId}");
  194. weekLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, weekStart, weekEnd), $"LessonRecord-{itemId}");
  195. lastTermLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastTermStart, lastTermEnd), $"LessonRecord-{itemId}");
  196. termLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, termStart, termEnd), $"LessonRecord-{itemId}");
  197. lastYearLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, lastYearStart, lastYearEnd), $"LessonRecord-{itemId}");
  198. yearLessCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(unifyTimeSql, yearStart, yearEnd), $"LessonRecord-{itemId}");
  199. //学校资源
  200. int tempBloblog = await CommonFind.GetSqlValueCount(cosmosClient, "School", unifySqlTxt, $"Bloblog-{itemId}");
  201. lastYearBloblog += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(blobTimeSql, lastYearStart, lastYearEnd), $"Bloblog-{itemId}");
  202. yearBloblog += await CommonFind.GetSqlValueCount(cosmosClient, "School", string.Format(blobTimeSql, yearStart, yearEnd), $"Bloblog-{itemId}");
  203. //学校活动
  204. long tempallActivity = 0;
  205. //统计活动
  206. foreach (var type in StaticValue.activityTypes)
  207. {
  208. 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}";
  209. tempallActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", $"SELECT value(count(c.id)) FROM c where c.pk = '{type}' and c.school = '{itemId}'");
  210. lastYearActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, lastYearStart, lastYearEnd));
  211. yearActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, yearStart, yearEnd));
  212. lastWeekActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, lastWeekStart, lastWeekEnd));
  213. weekActivity += await CommonFind.GetSqlValueCount(cosmosClient, "Common", string.Format(sqlTime, type, itemId, weekStart, weekEnd));
  214. }
  215. allLessonCount += tempLessCount;
  216. allActivity += tempallActivity;
  217. allBloblog += tempBloblog;
  218. recSchoolDates.Add(new RecSchoolDate() { id = school.id, name = school.name, dataCount = (tempLessCount + tempallActivity + tempBloblog) });
  219. }
  220. 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 });
  221. }
  222. /// <summary>
  223. /// 依据学校Id统计学校分析
  224. /// </summary>
  225. /// <param name="jsonElement"></param>
  226. /// <returns></returns>
  227. [ProducesDefaultResponseType]
  228. [HttpPost("get-idstatis")]
  229. public async Task<IActionResult> GetIdStatis(JsonElement jsonElement)
  230. {
  231. if (!jsonElement.TryGetProperty("schoolId", out JsonElement schoolId)) return BadRequest();
  232. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  233. int tecCount = 0; //学校教师数量
  234. int stuCount = 0; //学校学生数量
  235. int classCount = 0; //班级数量
  236. int roomCount = 0; //教室教师数量
  237. long allLessCount = 0; //课例总数
  238. int lastWeekLess = 0; //上周的总数
  239. int weekLess = 0; //本周的总数
  240. int lastTermLess = 0; //上学期的总数
  241. int termLess = 0; //本学期的总数
  242. int lessYearLess = 0; //去年的总数
  243. int yearLess = 0; //去年的总数
  244. int allActivity = 0; //学校所有活动
  245. int lastYearActivity = 0; //去年学校所有活动
  246. int yearActivity = 0; //今年学校所有活动
  247. int lastWeekActivity = 0; //上周学校所有活动
  248. int weekActivity = 0; //本周学校所有活动
  249. int allBlob = 0; //所有资源
  250. int lastYearBlob = 0; //去年的资源
  251. int yearBlob = 0; //去年的资源
  252. var cosmosClient = _azureCosmos.GetCosmosClient();
  253. ////分开部署,就不需要,一站多用时,取消注释
  254. //if ($"{site}".Equals(BIConst.Global))
  255. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  256. var (lastWeekStart, lastWeekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastweek"); //计算上周开始/结束时间
  257. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week"); //计算本周开始/结束时间
  258. var (lastTermStart, lastTermEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "lastterm"); //计算上学期开始/结束时间
  259. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term"); //计算本学期开始/结束时间
  260. var (lastYearStart, lastYearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{DateTimeOffset.UtcNow.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  261. var (yearStart, yearEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "year"); //计算今年开始/结束时间
  262. tecCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Teacher-{schoolId}", "SELECT value(count(c.id)) FROM c WHERE ARRAY_CONTAINS(c.roles, 'teacher', true) AND c.status = 'join'");
  263. stuCount = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", $"Base-{schoolId}");
  264. classCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Class-{schoolId}");
  265. roomCount = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Room-{schoolId}");
  266. string unifyTimeSql = "select value(count(c.id)) from c where c.startTime>={0} and c.startTime<={1}";
  267. allLessCount = await LessonStatisWay.GetSchoolIdLessonCount(cosmosClient, $"{schoolId}");
  268. lastWeekLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"LessonRecord-{schoolId}", string.Format(unifyTimeSql, lastWeekStart, lastWeekEnd));
  269. weekLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"LessonRecord-{schoolId}", string.Format(unifyTimeSql, weekStart, lastWeekEnd));
  270. lastTermLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"LessonRecord-{schoolId}", string.Format(unifyTimeSql, lastTermStart, lastTermEnd));
  271. termLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"LessonRecord-{schoolId}", string.Format(unifyTimeSql, termStart, termEnd));
  272. lessYearLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"LessonRecord-{schoolId}", string.Format(unifyTimeSql, lastYearStart, lastYearEnd));
  273. yearLess = await JointlySingleQuery.GetValueInt(cosmosClient, "School", string.Format(unifyTimeSql, yearStart, yearEnd), $"LessonRecord-{schoolId}");
  274. //统计活动
  275. foreach (var type in StaticValue.activityTypes)
  276. {
  277. 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}";
  278. allActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{type}-{schoolId}", $"select value(count(c.id)) from c where c.pk = '{type}' and c.school = '{schoolId}'");
  279. lastYearActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{type}-{schoolId}", string.Format(sqlTime, type, schoolId, lastYearStart, lastYearEnd));
  280. yearActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{type}-{schoolId}", string.Format(sqlTime, type, schoolId, yearStart, yearEnd));
  281. lastWeekActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{type}-{schoolId}", string.Format(sqlTime, type, schoolId, lastWeekStart, lastWeekEnd));
  282. weekActivity += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{type}-{schoolId}", string.Format(sqlTime, type, schoolId, weekStart, weekEnd));
  283. }
  284. //学校资源
  285. string blobTimeSql = "select value(count(c.id)) from c where c.time>={0} and c.time<={1}";
  286. allBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Bloblog-{schoolId}");
  287. lastYearBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Bloblog-{schoolId}", string.Format(blobTimeSql, lastYearStart, lastYearEnd));
  288. yearBlob = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"Bloblog-{schoolId}", string.Format(blobTimeSql, yearStart, yearEnd));
  289. //获取所有的课程记录
  290. List<LessonRecord> records = new();
  291. 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}") }))
  292. {
  293. records.Add(item);
  294. }
  295. List<(string name, int count)> gradeCount = new();
  296. if (records.Count > 0)
  297. {
  298. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{schoolId}", new PartitionKey($"Base"));
  299. School sc = new();
  300. if (response.Status == 200)
  301. {
  302. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  303. sc = json.ToObject<School>();
  304. }
  305. List<string> grades = new();
  306. foreach (var item in records)
  307. {
  308. foreach (string gId in item.grade)
  309. {
  310. if (!grades.Contains(gId))
  311. {
  312. grades.Add(gId);
  313. }
  314. }
  315. }
  316. foreach (var gId in grades)
  317. {
  318. var c = records.Where(r => r.grade.Contains(gId)).Count();
  319. gradeCount.Add((gId, c));
  320. }
  321. }
  322. return Ok(new { state = 200, tecCount, stuCount, classCount, roomCount, allLessCount, lastWeekLess, weekLess, lastTermLess, termLess, lessYearLess, yearLess, allActivity, lastYearActivity, yearActivity, lastWeekActivity, weekActivity, allBlob, lastYearBlob, yearBlob });
  323. }
  324. /// <summary>
  325. /// 未区域学校统计
  326. /// </summary>
  327. /// <param name="jsonElement"></param>
  328. /// <returns></returns>
  329. [ProducesDefaultResponseType]
  330. [HttpPost("get-noarea")]
  331. public async Task<IActionResult> GetNoAreaCnt(JsonElement jsonElement)
  332. {
  333. var cosmosClient = _azureCosmos.GetCosmosClient();
  334. int tLessCnt = 0; //课例
  335. int tActCnt = 0; //活动
  336. List<string> scIds = new(); //学校id集合
  337. string scIdSql = "select value(c.id) from c where c.areaId=null or c.areaId=''";
  338. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: scIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  339. {
  340. scIds.Add(item);
  341. }
  342. string comSql = BICommonWay.ManyScSql("c.school", scIds);
  343. string lesSql = $"select value(count(c.id)) from c where c.pk='LessonRecord' and {comSql}";
  344. tLessCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, lesSql);
  345. comSql = BICommonWay.ManyScSql(" and c.school", scIds);
  346. tActCnt = await ActivityWay.GetCnt(cosmosClient, condSql: comSql);
  347. return Ok(new { state = 200, scCnt = scIds.Count(), tLessCnt, tActCnt });
  348. }
  349. /// <summary>
  350. /// 未加区的学校统计
  351. /// </summary>
  352. /// <param name="jsonElement"></param>
  353. /// <returns></returns>
  354. [ProducesDefaultResponseType]
  355. [HttpPost("get-noareasc")]
  356. public async Task<IActionResult> GetNoAareaScCnt(JsonElement jsonElement)
  357. {
  358. var cosmosClient = _azureCosmos.GetCosmosClient();
  359. var tableClient = _azureStorage.GetCloudTableClient();
  360. var table = tableClient.GetTableReference("BIDDUserInfo");
  361. var (dayS, dayE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow);
  362. var (weekS, weekE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week");
  363. var (monthS, monthE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "month");
  364. var (termS, termE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term");
  365. List<AreaAssist> areaAssists = new(); //顾问信息
  366. int tchCnt = 0; //所有区级老师
  367. int stuCnt = 0; //所有区级学生
  368. int tLessCnt = 0; //课例数量
  369. int tActCnt = 0; //活动数量
  370. HashSet<string> tchIds = new();
  371. List<string> scIds = new(); //学校id集合
  372. string scIdSql = "select value(c.id) from c where c.areaId=null or c.areaId=''";
  373. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: scIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  374. {
  375. scIds.Add(item);
  376. }
  377. string scsSql = BICommonWay.ManyScSql("c.school", scIds);
  378. string cntSql = $"select value(count(c.id)) from c where {scsSql}";
  379. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<int>(queryText: cntSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  380. {
  381. tchCnt += item;
  382. }
  383. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<int>(queryText: cntSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  384. {
  385. stuCnt += item;
  386. }
  387. foreach (var scId in scIds)
  388. {
  389. string tchIdSql = $"select value(c.id) from c where array_contains(c.roles,'assist',true) and c.pk='Teacher'";
  390. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: tchIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{scId}") }))
  391. {
  392. tchIds.Add(item);
  393. }
  394. }
  395. List<string> noIds = new();
  396. foreach (var item in tchIds)
  397. {
  398. List<DingDingUserInfo> ddUsers = await table.QueryWhereString<DingDingUserInfo>($"PartitionKey eq '{_option.Location}' and tmdId eq '{item}'");
  399. if (ddUsers.Count > 0)
  400. {
  401. foreach (var dduser in ddUsers)
  402. {
  403. AreaAssist areaAssist = new()
  404. {
  405. userId = dduser.userId,
  406. unionId = dduser.unionId,
  407. name = dduser.name,
  408. mobile = dduser.mobile,
  409. avatar = dduser.avatar,
  410. tmdId = dduser.tmdId,
  411. tmdName = dduser.tmdName,
  412. tmdMobile = dduser.tmdMobile,
  413. picture = dduser.picture,
  414. roles = dduser.roles,
  415. schoolIds = dduser.schoolIds
  416. };
  417. areaAssists.Add(areaAssist);
  418. }
  419. }
  420. else { noIds.Add(item); }
  421. }
  422. if (scIds.Count > 0)
  423. {
  424. string lesSql = $"select value(count(c.id)) from c where c.pk='LessonRecord' and {scsSql}";
  425. tLessCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, lesSql);
  426. string actComSql = BICommonWay.ManyScSql(" and c.school", scIds);
  427. tActCnt = await ActivityWay.GetCnt(cosmosClient, condSql: actComSql);
  428. }
  429. return Ok(new { state = RespondCode.Ok, scCnt = scIds.Count, tchCnt, stuCnt, allCnt = tLessCnt + tActCnt, tLessCnt, tActCnt });
  430. }
  431. #region 新的统计接口
  432. /// <summary>
  433. /// 学校统计
  434. /// 新的数据结构
  435. /// </summary>
  436. /// <param name="jsonElement"></param>
  437. /// <returns></returns>
  438. [ProducesDefaultResponseType]
  439. [HttpPost("get-scstats")]
  440. public async Task<IActionResult> GetSchoolStats(JsonElement jsonElement)
  441. {
  442. if (!jsonElement.TryGetProperty("scId", out JsonElement scId)) return BadRequest();
  443. var cosmosClient = _azureCosmos.GetCosmosClient();
  444. School school = new();
  445. ScStats scStats = new();
  446. var respSc = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{scId}", new PartitionKey($"Base"));
  447. if (respSc.Status == 200)
  448. {
  449. using var fileJson = await JsonDocument.ParseAsync(respSc.ContentStream);
  450. school = fileJson.ToObject<School>();
  451. }
  452. else
  453. return Ok(new { state = RespondCode.NotFound, msg = $"未找到id:{scId};相关的学校,请检查" });
  454. try
  455. {
  456. bool isExist = true;
  457. StatsInfo statsInfo = new();
  458. var scDataStats = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{DateTimeOffset.UtcNow.Year}-{scId}", new PartitionKey("Statistics"));
  459. if (scDataStats.Status == 200)
  460. {
  461. using var fileJson = await JsonDocument.ParseAsync(scDataStats.ContentStream);
  462. statsInfo = fileJson.ToObject<StatsInfo>();
  463. }
  464. else
  465. {
  466. scStats.id = school.id;
  467. isExist = false;
  468. }
  469. List<LastYearLessAndAct> lastYear = new();
  470. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LastYearLessAndAct>(queryText: $"select c.lesson,c.activity from c where c.id='{DateTimeOffset.UtcNow.Year - 1}-{scId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Statistics") }))
  471. {
  472. lastYear.Add(item);
  473. }
  474. bool isnew = false;
  475. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  476. long now = dateTime.ToUnixTimeMilliseconds();
  477. if (!string.IsNullOrEmpty(statsInfo.id))
  478. {
  479. scStats.id = statsInfo.id;
  480. scStats.schoolId = statsInfo.schoolId;
  481. scStats.name = statsInfo.name;
  482. scStats.picture = statsInfo.picture;
  483. scStats.areaId = statsInfo.areaId;
  484. scStats.tch = statsInfo.tch;
  485. scStats.dayTch = statsInfo.dayTch;
  486. scStats.weekTch = statsInfo.weekTch;
  487. scStats.monthTch = statsInfo.monthTch;
  488. scStats.stu = statsInfo.stu;
  489. scStats.dayStu = statsInfo.dayStu;
  490. scStats.weekStu = statsInfo.weekStu;
  491. scStats.monthStu = statsInfo.monthStu;
  492. scStats.room = statsInfo.room;
  493. scStats.witRoom = statsInfo.witRoom;
  494. scStats.size = statsInfo.size;
  495. scStats.scCreateTime = statsInfo.scCreateTime;
  496. scStats.upTime = statsInfo.upTime;
  497. scStats.lessStats.all = statsInfo.lesson.all;
  498. scStats.lessStats.open = statsInfo.lesson.open;
  499. scStats.lessStats.less = statsInfo.lesson.less;
  500. scStats.lessStats.lastDay = statsInfo.lesson.lastDay;
  501. scStats.lessStats.day = statsInfo.lesson.day;
  502. scStats.lessStats.lastWeek = statsInfo.lesson.lastWeek;
  503. scStats.lessStats.week = statsInfo.lesson.week;
  504. scStats.lessStats.lastTerm = statsInfo.lesson.lastTerm;
  505. scStats.lessStats.term = statsInfo.lesson.term;
  506. scStats.lessStats.month = statsInfo.lesson.month;
  507. scStats.lessStats.lastMonth = statsInfo.lesson.lastMonth;
  508. scStats.lessStats.lastDayInter = statsInfo.lesson.lastDayInter;
  509. scStats.lessStats.dayInter = statsInfo.lesson.dayInter;
  510. scStats.lessStats.lastMonthInter = statsInfo.lesson.lastMonthInter;
  511. scStats.lessStats.monthInter = statsInfo.lesson.monthInter;
  512. scStats.lessStats.lastYear = ((int)BICommonWay.ManyDoubleMerge(lastYear.Select(s => s.lesson.year).Where(w => w.Count > 0).ToList()).Sum());
  513. scStats.lessStats.lastYearInter = statsInfo.lesson.lastYearInter;
  514. scStats.lessStats.yearInter = statsInfo.lesson.yearInter;
  515. scStats.lessStats.yearInters = TimeHelper.GetYearMonth(statsInfo.lesson.yearInters, dateTime.Year, dateTime.Month);
  516. scStats.lessStats.year = TimeHelper.GetYearMonth(statsInfo.lesson.year, dateTime.Year, dateTime.Month);
  517. scStats.actStats.all = statsInfo.activity.all;
  518. scStats.actStats.exam = statsInfo.activity.exam;
  519. scStats.actStats.survey = statsInfo.activity.survey;
  520. scStats.actStats.vote = statsInfo.activity.vote;
  521. scStats.actStats.homework = statsInfo.activity.homework;
  522. scStats.actStats.lastDay = statsInfo.activity.lastDay;
  523. scStats.actStats.dayCnt = statsInfo.activity.dayCnt;
  524. scStats.actStats.lastWeek = statsInfo.activity.lastWeek;
  525. scStats.actStats.week = statsInfo.activity.week;
  526. scStats.actStats.lastTerm = statsInfo.activity.lastTerm;
  527. scStats.actStats.term = statsInfo.activity.term;
  528. scStats.actStats.lastMonth = statsInfo.activity.lastMonth;
  529. scStats.actStats.month = statsInfo.activity.month;
  530. scStats.actStats.lastYear = ((int)BICommonWay.ManyDoubleMerge(lastYear.Select(s => s.activity.year).Where(w => w.Count > 0).ToList()).Sum());
  531. scStats.actStats.year = TimeHelper.GetYearMonth(statsInfo.activity.year, dateTime.Year, dateTime.Month);
  532. if (statsInfo.study != null)
  533. {
  534. scStats.srStats.learnTime = statsInfo.study.learnTime;
  535. scStats.srStats.online = statsInfo.study.online;
  536. scStats.srStats.offline = statsInfo.study.offline;
  537. scStats.srStats.classRoom = statsInfo.study.classRoom;
  538. scStats.srStats.submit = statsInfo.study.submit;
  539. scStats.srStats.notStarted = statsInfo.study.notStarted;
  540. scStats.srStats.ongoing = statsInfo.study.ongoing;
  541. scStats.srStats.finish = statsInfo.study.finish;
  542. }
  543. }
  544. //if(statsInfo.upTime > 0 && !isnew) {
  545. // long timedis = 20 * 60 * 1000;//20分钟
  546. // if (now - statsInfo.upTime < timedis)
  547. // isnew = true;
  548. //}
  549. //if (statsInfo.upTime == 0 && isnew)
  550. // statsInfo.upTime = now;
  551. //if (statsInfo.upTime == 0 && isnew)
  552. // statsInfo.upTime = DateTimeOffset.UtcNow.Add(new TimeSpan(-1, 0, 0, 0)).ToUnixTimeMilliseconds();
  553. //if (!isnew)
  554. //{
  555. // //Function 处理Function 更新或者新增
  556. // //_ = _httpTrigger.RequestHttpTrigger(new { schoolId = $"{school.id}"}, _option.Location, "stats-sc-info");
  557. // //异步方法处理 同步更新
  558. // statsInfo = await SchoolStatsWay.GetSingleSc(cosmosClient, school.id);
  559. // statsInfo.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  560. // if (isExist)
  561. // statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<StatsInfo>(statsInfo, statsInfo.id, new PartitionKey("Stats"));
  562. // else
  563. // statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<StatsInfo>(statsInfo, new PartitionKey("Stats"));
  564. //}
  565. return Ok(new { state = RespondCode.Ok, scStats });
  566. }
  567. catch (Exception ex)
  568. {
  569. //await _dingDing.SendBotMsg($"BI,{_option.Location} /school/set-scstats \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  570. return BadRequest();
  571. }
  572. }
  573. /// <summary>
  574. /// 依据身份统计学校
  575. /// 新的数据结构
  576. /// </summary>
  577. /// <param name="jsonElement"></param>
  578. /// <returns></returns>
  579. [ProducesDefaultResponseType]
  580. [HttpPost("get-rolestats")]
  581. public async Task<IActionResult> GetRoleStats(JsonElement jsonElement)
  582. {
  583. //jsonElement.TryGetProperty("areaId", out JsonElement areaId);
  584. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  585. jsonElement.TryGetProperty("role", out JsonElement role);
  586. //if (string.IsNullOrEmpty($"{areaId}") && string.IsNullOrEmpty($"{tmdId}"))
  587. // return Ok(new { stats = RespondCode.ParamsError, msg = "areaId/tmdId两者参数不能都为空!" });
  588. var cosmosClient = _azureCosmos.GetCosmosClient();
  589. Area area = null;
  590. ScStats areaScStats = new();
  591. List<string> scIds = new();
  592. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  593. List<ScStats> scStatss = new();
  594. List<StatsInfo> statsInfos = new();
  595. StringBuilder scSql = new("select value(c.id) from c");
  596. StringBuilder lastScSql = new("select value(c.id) from c");
  597. //if (!string.IsNullOrEmpty($"{areaId}"))
  598. //{
  599. // scSql.Append($" where c.areaId='{areaId}'");
  600. // lastScSql.Append($" where c.areaId='{areaId}'");
  601. // var respSc = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{areaId}", new PartitionKey($"Base-Area"));
  602. // if (respSc.Status == 200)
  603. // {
  604. // using var fileJson = await JsonDocument.ParseAsync(respSc.ContentStream);
  605. // area = fileJson.ToObject<Area>();
  606. // }
  607. // else
  608. // return Ok(new { state = RespondCode.NotFound, msg = $"未找到id:{areaId};相关的学区,请检查!" });
  609. //}
  610. if (!string.IsNullOrEmpty($"{tmdId}"))
  611. {
  612. switch ($"{role}")
  613. {
  614. case "assists":
  615. scSql.Append($" join a in c.assists where a.id='{tmdId}'");
  616. lastScSql.Append($" join a in c.assists where a.id='{tmdId}'");
  617. break;
  618. case "sales":
  619. scSql.Append($" join a in c.sales where a.id='{tmdId}'");
  620. lastScSql.Append($" join a in c.sales where a.id='{tmdId}'");
  621. break;
  622. default:
  623. return Ok(new { state = RespondCode.ParamsError, msg = "role参数错误" });
  624. }
  625. }
  626. scIds = await CommonFind.FindScIds(cosmosClient, scSql.ToString(), "BIRel");
  627. StringBuilder statsSql = new($"select value(c) from c where c.year={dateTime.Year}");
  628. if (scIds.Count > 0)
  629. {
  630. statsSql.Append($" and {BICommonWay.ManyScSql("c.id", scIds, $"{dateTime.Year}-")}");
  631. }
  632. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<StatsInfo>(queryText: statsSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Statistics") }))
  633. {
  634. statsInfos.Add(item);
  635. }
  636. StringBuilder lastStsSql = new($"select c.lesson,c.activity from c where c.year={dateTime.Year}");
  637. if (scIds.Count > 0)
  638. {
  639. lastStsSql.Append($" and {BICommonWay.ManyScSql("c.id", scIds, $"{DateTimeOffset.UtcNow.Year - 1}-")}");
  640. }
  641. List<LastYearLessAndAct> lastYear = new();
  642. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LastYearLessAndAct>(queryText: lastStsSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Statistics") }))
  643. {
  644. lastYear.Add(item);
  645. }
  646. StatsInfo statsInfo = null;
  647. statsInfo = SchoolStatsWay.GetAreaStats(cosmosClient, _httpTrigger, _option, statsInfos, scIds, area);
  648. if (statsInfo != null)
  649. {
  650. areaScStats.id = statsInfo.id;
  651. areaScStats.schoolId = statsInfo.schoolId;
  652. areaScStats.name = statsInfo.name;
  653. areaScStats.picture = statsInfo.picture;
  654. areaScStats.areaId = statsInfo.areaId;
  655. areaScStats.tch = statsInfo.tch;
  656. areaScStats.dayTch = statsInfo.dayTch;
  657. areaScStats.weekTch = statsInfo.weekTch;
  658. areaScStats.monthTch = statsInfo.monthTch;
  659. areaScStats.stu = statsInfo.stu;
  660. areaScStats.dayStu = statsInfo.dayStu;
  661. areaScStats.weekStu = statsInfo.weekStu;
  662. areaScStats.monthStu = statsInfo.monthStu;
  663. areaScStats.room = statsInfo.room;
  664. areaScStats.witRoom = statsInfo.witRoom;
  665. areaScStats.size = statsInfo.size;
  666. areaScStats.scCreateTime = statsInfo.scCreateTime;
  667. areaScStats.upTime = statsInfo.upTime;
  668. areaScStats.lessStats.open = statsInfo.lesson.all;
  669. areaScStats.lessStats.open = statsInfo.lesson.open;
  670. areaScStats.lessStats.less = statsInfo.lesson.less;
  671. areaScStats.lessStats.lastDay = statsInfo.lesson.lastDay;
  672. areaScStats.lessStats.day = statsInfo.lesson.day;
  673. areaScStats.lessStats.lastWeek = statsInfo.lesson.lastWeek;
  674. areaScStats.lessStats.week = statsInfo.lesson.week;
  675. areaScStats.lessStats.lastTerm = statsInfo.lesson.lastTerm;
  676. areaScStats.lessStats.term = statsInfo.lesson.term;
  677. areaScStats.lessStats.lastDayInter = statsInfo.lesson.lastDayInter;
  678. areaScStats.lessStats.dayInter = statsInfo.lesson.dayInter;
  679. areaScStats.lessStats.lastMonthInter = statsInfo.lesson.lastMonthInter;
  680. areaScStats.lessStats.monthInter = statsInfo.lesson.monthInter;
  681. areaScStats.lessStats.lastYearInter = statsInfo.lesson.lastYearInter;
  682. areaScStats.lessStats.yearInter = statsInfo.lesson.yearInter;
  683. areaScStats.lessStats.yearInters = TimeHelper.GetYearMonth(statsInfo.lesson.yearInters, dateTime.Year, dateTime.Month);
  684. areaScStats.lessStats.lastYear = ((int)BICommonWay.ManyDoubleMerge(lastYear.Select(s => s.lesson.year).Where(w => w.Count > 0).ToList()).Sum());
  685. areaScStats.lessStats.year = TimeHelper.GetYearMonth(statsInfo.lesson.year, dateTime.Year, dateTime.Month);
  686. areaScStats.actStats.all = statsInfo.activity.all;
  687. areaScStats.actStats.exam = statsInfo.activity.exam;
  688. areaScStats.actStats.survey = statsInfo.activity.survey;
  689. areaScStats.actStats.vote = statsInfo.activity.vote;
  690. areaScStats.actStats.homework = statsInfo.activity.homework;
  691. areaScStats.actStats.lastDay = statsInfo.activity.lastDay;
  692. areaScStats.actStats.dayCnt = statsInfo.activity.dayCnt;
  693. areaScStats.actStats.lastWeek = statsInfo.activity.lastWeek;
  694. areaScStats.actStats.week = statsInfo.activity.week;
  695. areaScStats.actStats.lastTerm = statsInfo.activity.lastTerm;
  696. areaScStats.actStats.term = statsInfo.activity.term;
  697. areaScStats.actStats.lastMonth = statsInfo.activity.lastMonth;
  698. areaScStats.actStats.month = statsInfo.activity.month;
  699. areaScStats.actStats.lastYear = ((int)BICommonWay.ManyDoubleMerge(lastYear.Select(s => s.activity.year).Where(w => w.Count > 0).ToList()).Sum());
  700. areaScStats.actStats.year = TimeHelper.GetYearMonth(statsInfo.activity.year, dateTime.Year, dateTime.Month);
  701. if (statsInfo.study != null)
  702. {
  703. areaScStats.srStats.learnTime = statsInfo.study.learnTime;
  704. areaScStats.srStats.online = statsInfo.study.online;
  705. areaScStats.srStats.offline = statsInfo.study.offline;
  706. areaScStats.srStats.classRoom = statsInfo.study.classRoom;
  707. areaScStats.srStats.submit = statsInfo.study.submit;
  708. areaScStats.srStats.notStarted = statsInfo.study.notStarted;
  709. areaScStats.srStats.ongoing = statsInfo.study.ongoing;
  710. areaScStats.srStats.finish = statsInfo.study.finish;
  711. }
  712. }
  713. 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();
  714. return Ok(new { state = RespondCode.Ok, areaScStats, scInfos });
  715. }
  716. /// <summary>
  717. /// 依据区级Id统计 新数据结构
  718. /// </summary>
  719. /// <param name="jsonElement"></param>
  720. /// <returns></returns>
  721. [ProducesDefaultResponseType]
  722. [HttpPost("get-areastats")]
  723. public async Task<IActionResult> GetAreaStats(JsonElement jsonElement)
  724. {
  725. if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  726. var cosmosClient = _azureCosmos.GetCosmosClient();
  727. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  728. Area area = null;
  729. AreaStats areaScStats = new();
  730. List<string> scIds = new();
  731. List<SchoolInfo> schoolInfos = new();
  732. List<ScStats> scStatss = new();
  733. List<StatsInfo> statsInfos = new();
  734. var (weekS, weekE) = TimeHelper.GetStartOrEnd(dateTime, "week"); //计算本周开始/结束时间
  735. var (mthS, mthE) = TimeHelper.GetStartOrEnd(dateTime, "month"); //本月开始/结束时间
  736. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<SchoolInfo>(queryText: $"select c.id,c.name,c.picture,c.areaId,c.areaName,c.size,c.scale,c.assists,c.sales,c.createDate,c.serial,c.service,c.hard from c where c.areaId='{areaId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BIRel") }))
  737. {
  738. schoolInfos.Add(item);
  739. }
  740. if (schoolInfos.Count > 0)
  741. scIds = schoolInfos.Select(s => s.id).ToList();
  742. StringBuilder statsSql = new($"select value(c) from c where c.year={dateTime.Year}");
  743. if (scIds.Count > 0)
  744. statsSql.Append($" {BICommonWay.ManyScSql("c.id", scIds, $"{dateTime.Year}-")}");
  745. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<StatsInfo>(queryText: statsSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Statistics") }))
  746. {
  747. statsInfos.Add(item);
  748. }
  749. StringBuilder lastStsSql = new($"select c.lesson,c.activity from c where c.year={dateTime.Year}");
  750. if (scIds.Count > 0)
  751. {
  752. lastStsSql.Append($" where {BICommonWay.ManyScSql("c.id", scIds, $"{dateTime.Year - 1}-")}");
  753. }
  754. List<LastYearLessAndAct> lastYear = new();
  755. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LastYearLessAndAct>(queryText: lastStsSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Statistics") }))
  756. {
  757. lastYear.Add(item);
  758. }
  759. StatsInfo statsInfo = null;
  760. statsInfo = SchoolStatsWay.GetAreaStats(cosmosClient, _httpTrigger, _option, statsInfos, scIds, area);
  761. if (statsInfo != null)
  762. {
  763. //areaScStats = new
  764. //{
  765. // statsInfo.id,
  766. // statsInfo.schoolId,
  767. // statsInfo.name,
  768. // statsInfo.picture,
  769. // statsInfo.areaId,
  770. // weekSc = schoolInfos.Where(w => w.createDate >= weekS && w.createDate <= weekE).Count(),
  771. // monthSc = schoolInfos.Where(w => w.createDate >= mthS && w.createDate <= mthE).Count(),
  772. // statsInfo.tch,
  773. // statsInfo.dayTch,
  774. // statsInfo.weekTch,
  775. // statsInfo.monthTch,
  776. // statsInfo.stu,
  777. // statsInfo.dayStu,
  778. // statsInfo.weekStu,
  779. // statsInfo.monthStu,
  780. // statsInfo.room,
  781. // statsInfo.witRoom,
  782. // statsInfo.size,
  783. // statsInfo.scCreateTime,
  784. // statsInfo.upTime,
  785. // lessStats = new
  786. // {
  787. // statsInfo.lesson.all,
  788. // statsInfo.lesson.open,
  789. // statsInfo.lesson.less,
  790. // statsInfo.lesson.lastDay,
  791. // statsInfo.lesson.day,
  792. // statsInfo.lesson.lastWeek,
  793. // statsInfo.lesson.week,
  794. // statsInfo.lesson.lastTerm,
  795. // statsInfo.lesson.term,
  796. // statsInfo.lesson.lastDayInter,
  797. // statsInfo.lesson.dayInter,
  798. // statsInfo.lesson.lastMonthInter,
  799. // statsInfo.lesson.monthInter,
  800. // statsInfo.lesson.lastYearInter,
  801. // statsInfo.lesson.yearInter,
  802. // yearInters = TimeHelper.GetYearMonth(statsInfo.lesson.yearInters, dateTime.Year, dateTime.Month),
  803. // lastYear = ((int)BICommonWay.ManyDoubleMerge(lastYear.Select(s => s.lesson.year).Where(w => w.Count > 0).ToList()).Sum()),
  804. // year = TimeHelper.GetYearMonth(statsInfo.lesson.year, dateTime.Year, dateTime.Month)
  805. // },
  806. // actStats = new
  807. // {
  808. // statsInfo.activity.all,
  809. // statsInfo.activity.exam,
  810. // statsInfo.activity.survey,
  811. // statsInfo.activity.vote,
  812. // statsInfo.activity.homework,
  813. // statsInfo.activity.lastDay,
  814. // statsInfo.activity.dayCnt,
  815. // statsInfo.activity.lastWeek,
  816. // statsInfo.activity.week,
  817. // statsInfo.activity.lastTerm,
  818. // statsInfo.activity.term,
  819. // lastYear = ((int)BICommonWay.ManyDoubleMerge(lastYear.Select(s => s.activity.year).Where(w => w.Count > 0).ToList()).Sum()),
  820. // year = TimeHelper.GetYearMonth(statsInfo.activity.year, dateTime.Year, dateTime.Month)
  821. // },
  822. // srStats = new
  823. // {
  824. // statsInfo.study.learnTime,
  825. // statsInfo.study.online,
  826. // statsInfo.study.offline,
  827. // statsInfo.study.classRoom,
  828. // statsInfo.study.submit,
  829. // statsInfo.study.notStarted,
  830. // statsInfo.study.ongoing,
  831. // statsInfo.study.finish
  832. // }
  833. //};
  834. areaScStats.id = statsInfo.id;
  835. areaScStats.name = statsInfo.name;
  836. areaScStats.weekSc = schoolInfos.Where(w => w.createDate >= weekS && w.createDate <= weekE).Count();
  837. areaScStats.monthSc = schoolInfos.Where(w => w.createDate >= mthS && w.createDate <= mthE).Count();
  838. areaScStats.tch = statsInfo.tch;
  839. areaScStats.dayTch = statsInfo.dayTch;
  840. areaScStats.weekTch = statsInfo.weekTch;
  841. areaScStats.monthTch = statsInfo.monthTch;
  842. areaScStats.stu = statsInfo.stu;
  843. areaScStats.dayStu = statsInfo.dayStu;
  844. areaScStats.weekStu = statsInfo.weekStu;
  845. areaScStats.monthStu = statsInfo.monthStu;
  846. areaScStats.room = statsInfo.room;
  847. areaScStats.witRoom = statsInfo.witRoom;
  848. areaScStats.size = statsInfo.size;
  849. areaScStats.lessStats.open = statsInfo.lesson.all;
  850. areaScStats.lessStats.open = statsInfo.lesson.open;
  851. areaScStats.lessStats.less = statsInfo.lesson.less;
  852. areaScStats.lessStats.lastDay = statsInfo.lesson.lastDay;
  853. areaScStats.lessStats.day = statsInfo.lesson.day;
  854. areaScStats.lessStats.lastWeek = statsInfo.lesson.lastWeek;
  855. areaScStats.lessStats.week = statsInfo.lesson.week;
  856. areaScStats.lessStats.lastTerm = statsInfo.lesson.lastTerm;
  857. areaScStats.lessStats.term = statsInfo.lesson.term;
  858. areaScStats.lessStats.lastDayInter = statsInfo.lesson.lastDayInter;
  859. areaScStats.lessStats.dayInter = statsInfo.lesson.dayInter;
  860. areaScStats.lessStats.lastMonthInter = statsInfo.lesson.lastMonthInter;
  861. areaScStats.lessStats.monthInter = statsInfo.lesson.monthInter;
  862. areaScStats.lessStats.lastYearInter = statsInfo.lesson.lastYearInter;
  863. areaScStats.lessStats.yearInter = statsInfo.lesson.yearInter;
  864. areaScStats.lessStats.yearInters = TimeHelper.GetYearMonth(statsInfo.lesson.yearInters, dateTime.Year, dateTime.Month);
  865. areaScStats.lessStats.lastYear = ((int)BICommonWay.ManyDoubleMerge(lastYear.Select(s => s.lesson.year).Where(w => w.Count > 0).ToList()).Sum());
  866. areaScStats.lessStats.year = TimeHelper.GetYearMonth(statsInfo.lesson.year, dateTime.Year, dateTime.Month);
  867. areaScStats.actStats.all = statsInfo.activity.all;
  868. areaScStats.actStats.exam = statsInfo.activity.exam;
  869. areaScStats.actStats.survey = statsInfo.activity.survey;
  870. areaScStats.actStats.vote = statsInfo.activity.vote;
  871. areaScStats.actStats.homework = statsInfo.activity.homework;
  872. areaScStats.actStats.lastDay = statsInfo.activity.lastDay;
  873. areaScStats.actStats.dayCnt = statsInfo.activity.dayCnt;
  874. areaScStats.actStats.lastWeek = statsInfo.activity.lastWeek;
  875. areaScStats.actStats.week = statsInfo.activity.week;
  876. areaScStats.actStats.lastTerm = statsInfo.activity.lastTerm;
  877. areaScStats.actStats.term = statsInfo.activity.term;
  878. areaScStats.actStats.lastMonth = statsInfo.activity.lastMonth;
  879. areaScStats.actStats.month = statsInfo.activity.month;
  880. areaScStats.actStats.lastYear = ((int)BICommonWay.ManyDoubleMerge(lastYear.Select(s => s.activity.year).Where(w => w.Count > 0).ToList()).Sum());
  881. areaScStats.actStats.year = TimeHelper.GetYearMonth(statsInfo.activity.year, dateTime.Year, dateTime.Month);
  882. if (statsInfo.study != null)
  883. {
  884. areaScStats.srStats.learnTime = statsInfo.study.learnTime;
  885. areaScStats.srStats.online = statsInfo.study.online;
  886. areaScStats.srStats.offline = statsInfo.study.offline;
  887. areaScStats.srStats.classRoom = statsInfo.study.classRoom;
  888. areaScStats.srStats.submit = statsInfo.study.submit;
  889. areaScStats.srStats.notStarted = statsInfo.study.notStarted;
  890. areaScStats.srStats.ongoing = statsInfo.study.ongoing;
  891. areaScStats.srStats.finish = statsInfo.study.finish;
  892. }
  893. }
  894. List<double> weekLess = BICommonWay.weekDoubleMerge(statsInfos.Select(s => s.lesson.year).Where(w => w.Count > 0).ToList(), dateTime);
  895. schoolInfos.ForEach(fe =>
  896. {
  897. var tempSts = statsInfos.Find(f => f.schoolId.Equals(fe.id));
  898. if (tempSts != null)
  899. {
  900. fe.less = tempSts.lesson.all;
  901. fe.lastWeekLess = tempSts.lesson.lastWeek;
  902. fe.monthLess = tempSts.lesson.month;
  903. }
  904. });
  905. List<IdInfo> assits = new();
  906. schoolInfos.ForEach(fe => assits.AddRange(fe.assists));
  907. List<IdInfo> saless = new();
  908. schoolInfos.ForEach(fe => saless.AddRange(fe.sales));
  909. return Ok(new { state = RespondCode.Ok, areaScStats, schoolInfos, weekLess, assists = assits.Where((w, i) => assits.FindIndex(s => s.id.Equals(w.id)) == i).ToList(), saless = saless.Where((w, i) => saless.FindIndex(f => f.id.Equals(w.id)) == i).ToList() });
  910. }
  911. /// <summary>
  912. /// 所有区级的统计
  913. /// </summary>
  914. /// <param name="jsonElement"></param>
  915. /// <returns></returns>
  916. [ProducesDefaultResponseType]
  917. [HttpPost("get-allscstats")]
  918. public async Task<IActionResult> GetAllAreaStats(JsonElement jsonElement)
  919. {
  920. var cosmosClient = _azureCosmos.GetCosmosClient();
  921. AllScStats allScStats = new();
  922. List<StatsInfo> statsInfos = new();
  923. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  924. var (lastDayS, lastdayE) = TimeHelper.GetStartOrEnd(dateTime.AddDays(-1)); //昨天开始时间
  925. var (dayS, dayE) = TimeHelper.GetStartOrEnd(dateTime); //今天开始时间
  926. var (lastWeekS, lastWeekE) = TimeHelper.GetStartOrEnd(dateTime, "lastweek"); //计算上周开始/结束时间
  927. var (weekS, weekE) = TimeHelper.GetStartOrEnd(dateTime, "week"); //计算本周开始/结束时间
  928. var (lastTermS, lastTermE) = TimeHelper.GetStartOrEnd(dateTime, "lastterm"); //计算上学期开始/结束时间
  929. var (termS, termE) = TimeHelper.GetStartOrEnd(dateTime, "term"); //计算本学期开始/结束时间
  930. var (lastMthS, LastmthE) = TimeHelper.GetStartOrEnd(dateTime, "lastMonth"); //上月开始/结束时间
  931. var (mthS, mthE) = TimeHelper.GetStartOrEnd(dateTime, "month"); //本月开始/结束时间
  932. var (lastYearS, lastYearE) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{dateTime.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  933. var (yearS, yearE) = TimeHelper.GetStartOrEnd(dateTime, "year"); //计算今年开始/结束时间
  934. string sql = "select value(count(c.id)) from c ";
  935. List<EasyInfo> easyInfos = new();
  936. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<EasyInfo>(queryText: $"select c.id,c.code,c.name from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
  937. {
  938. easyInfos.Add(item);
  939. }
  940. int scCnt = await JointlySingleQuery.GetValueInt(cosmosClient, "Normal", "Base", sql);
  941. int scWeekCnt = await JointlySingleQuery.GetValueInt(cosmosClient, "Normal", "Base", $"{sql} where c.createTime >= {weekS} and c.createTime <= {weekE}");
  942. int scMonthCnt = await JointlySingleQuery.GetValueInt(cosmosClient, "Normal", "Base", $"{sql} where c.createTime >= {mthS} and c.createTime <= {mthE}");
  943. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<StatsInfo>(queryText: $"select value(c) from c where c.year={dateTime.Year}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Statistics") }))
  944. {
  945. statsInfos.Add(item);
  946. }
  947. List<AreaInfoStats> areaGroup = statsInfos.GroupBy(p => p.areaId).Select(s => new AreaInfoStats() { areaId = s.Key, name = easyInfos.Where(f => f.id.Equals(s.Key)).Select(sn=>sn.name).FirstOrDefault(), lessAct = (s.Select(sl => sl.lesson.all).Sum() + s.Select(sa => sa.activity.all).Sum()), lessCnt = s.Select(sl => sl.lesson.all).Sum(), actCnt = s.Select(sa => sa.activity.all).Sum() }).ToList();
  948. StatsInfo statsInfo = null;
  949. statsInfo = SchoolStatsWay.GetAreaStats(cosmosClient, _httpTrigger, _option, statsInfos);
  950. if (statsInfo != null)
  951. {
  952. allScStats.areaCnt = easyInfos.Count;
  953. allScStats.sc = statsInfos.Count;
  954. allScStats.weekSc = scWeekCnt;
  955. allScStats.monthSc = scMonthCnt;
  956. allScStats.tch = statsInfo.tch;
  957. allScStats.dayTch = statsInfo.dayTch;
  958. allScStats.weekTch = statsInfo.weekTch;
  959. allScStats.monthTch = statsInfo.monthTch;
  960. allScStats.stu = statsInfo.stu;
  961. allScStats.dayStu = statsInfo.dayStu;
  962. allScStats.weekStu = statsInfo.weekStu;
  963. allScStats.monthStu = statsInfo.monthStu;
  964. allScStats.room = statsInfo.room;
  965. allScStats.witRoom = statsInfo.witRoom;
  966. allScStats.size = statsInfo.size;
  967. allScStats.scCreateTime = statsInfo.scCreateTime;
  968. allScStats.upTime = statsInfo.upTime;
  969. allScStats.lessStats.open = statsInfo.lesson.all;
  970. allScStats.lessStats.open = statsInfo.lesson.open;
  971. allScStats.lessStats.less = statsInfo.lesson.less;
  972. allScStats.lessStats.lastDay = statsInfo.lesson.lastDay;
  973. allScStats.lessStats.day = statsInfo.lesson.day;
  974. allScStats.lessStats.lastWeek = statsInfo.lesson.lastWeek;
  975. allScStats.lessStats.week = statsInfo.lesson.week;
  976. allScStats.lessStats.lastTerm = statsInfo.lesson.lastTerm;
  977. allScStats.lessStats.term = statsInfo.lesson.term;
  978. allScStats.lessStats.lastDayInter = statsInfo.lesson.lastDayInter;
  979. allScStats.lessStats.dayInter = statsInfo.lesson.dayInter;
  980. allScStats.lessStats.lastMonthInter = statsInfo.lesson.lastMonthInter;
  981. allScStats.lessStats.monthInter = statsInfo.lesson.monthInter;
  982. allScStats.lessStats.lastYearInter = statsInfo.lesson.lastYearInter;
  983. allScStats.lessStats.yearInter = statsInfo.lesson.yearInter;
  984. allScStats.actStats.all = statsInfo.activity.all;
  985. allScStats.actStats.exam = statsInfo.activity.exam;
  986. allScStats.actStats.survey = statsInfo.activity.survey;
  987. allScStats.actStats.vote = statsInfo.activity.vote;
  988. allScStats.actStats.homework = statsInfo.activity.homework;
  989. allScStats.actStats.lastDay = statsInfo.activity.lastDay;
  990. allScStats.actStats.dayCnt = statsInfo.activity.dayCnt;
  991. allScStats.actStats.lastWeek = statsInfo.activity.lastWeek;
  992. allScStats.actStats.week = statsInfo.activity.week;
  993. allScStats.actStats.lastTerm = statsInfo.activity.lastTerm;
  994. allScStats.actStats.term = statsInfo.activity.term;
  995. allScStats.actStats.lastMonth = statsInfo.activity.lastMonth;
  996. allScStats.actStats.month = statsInfo.activity.month;
  997. if (statsInfo.study != null)
  998. {
  999. allScStats.srStats.learnTime = statsInfo.study.learnTime;
  1000. allScStats.srStats.online = statsInfo.study.online;
  1001. allScStats.srStats.offline = statsInfo.study.offline;
  1002. allScStats.srStats.classRoom = statsInfo.study.classRoom;
  1003. allScStats.srStats.submit = statsInfo.study.submit;
  1004. allScStats.srStats.notStarted = statsInfo.study.notStarted;
  1005. allScStats.srStats.ongoing = statsInfo.study.ongoing;
  1006. allScStats.srStats.finish = statsInfo.study.finish;
  1007. }
  1008. }
  1009. return Ok(new { state = RespondCode.Ok, allScStats, areaGroup });
  1010. }
  1011. #endregion 新的统计接口
  1012. /// <summary>
  1013. /// 学校信息中间件查询接口
  1014. /// </summary>
  1015. /// <param name="jsonElement"></param>
  1016. /// <returns></returns>
  1017. [ProducesDefaultResponseType]
  1018. [HttpPost("get-scinfos")]
  1019. public async Task<IActionResult> GetScInfos(JsonElement jsonElement)
  1020. {
  1021. var cosmosClient = _azureCosmos.GetCosmosClient();
  1022. jsonElement.TryGetProperty("role", out JsonElement role);
  1023. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  1024. jsonElement.TryGetProperty("areaId", out JsonElement areaId);
  1025. StringBuilder sql = new($"select value(c) from c");
  1026. if (!string.IsNullOrEmpty($"{role}") && !string.IsNullOrEmpty($"{tmdId}") && string.IsNullOrEmpty($"{areaId}"))
  1027. {
  1028. switch ($"{role}")
  1029. {
  1030. case "assist":
  1031. sql.Append($" join a in c.assist where a.id='{tmdId}'");
  1032. break;
  1033. case "sales":
  1034. sql.Append($" join a in c.sales where a.id='{tmdId}'");
  1035. break;
  1036. }
  1037. }
  1038. if (!string.IsNullOrEmpty($"{areaId}") && !string.IsNullOrEmpty($"{role}"))
  1039. {
  1040. sql.Append($" where c.areaId ='{areaId}'");
  1041. }
  1042. //List<string> scId = await CommonFind.FindScIds(cosmosClient, "select value(c.id) from c ", "Base");
  1043. List<BIRelation> scInfos = new();
  1044. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<BIRelation>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BIRel") }))
  1045. {
  1046. scInfos.Add(item);
  1047. }
  1048. return Ok(new { state = RespondCode.Ok, allCnt = scInfos.Count, scInfos });
  1049. }
  1050. /// <summary>
  1051. /// 依据Id查询School容器 数据管理工具——查询工具
  1052. /// </summary>
  1053. /// <param name="jsonElement"></param>
  1054. /// <returns></returns>
  1055. [ProducesDefaultResponseType]
  1056. [HttpPost("get-info")]
  1057. public async Task<IActionResult> GetSchool(JsonElement jsonElement)
  1058. {
  1059. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  1060. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  1061. var cosmosClient = _azureCosmos.GetCosmosClient();
  1062. //if ($"{site}".Equals(BIConst.Global))
  1063. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  1064. List<object> infos = new List<object>();
  1065. string sqlTxt = $"select value(c) from c where c.id='{id}'";
  1066. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  1067. {
  1068. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1069. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1070. {
  1071. infos.Add(obj.ToObject<object>());
  1072. }
  1073. }
  1074. return Ok(new { state = 200, infos });
  1075. }
  1076. public record RecSchoolDate
  1077. {
  1078. public string id { get; set; }
  1079. public string name { get; set; }
  1080. public long dataCount { get; set; }
  1081. }
  1082. /// <summary>
  1083. /// 学校列表信息
  1084. /// </summary>
  1085. public record SchoolInfo
  1086. {
  1087. public string id { get; set; }
  1088. public string name { get; set; }
  1089. public string picture { get; set; }
  1090. public string areaId { get; set; }
  1091. public string areaName { get; set; }
  1092. public int size { get; set; }
  1093. public int scale { get; set; }
  1094. public long createDate { get; set; }
  1095. public List<IdInfo> assists { get; set; } = new List<IdInfo>();
  1096. public List<IdInfo> sales { get; set; } = new List<IdInfo>();
  1097. public List<string> serial { get; set; } = new List<string>();
  1098. public List<string> service { get; set; } = new List<string>();
  1099. public List<string> hard { get; set; } = new List<string>();
  1100. public int less { get; set; }
  1101. public int lastWeekLess { get; set; }
  1102. public int monthLess { get; set; }
  1103. }
  1104. /// <summary>
  1105. /// 区级信息
  1106. /// </summary>
  1107. public record AreaStats
  1108. {
  1109. public string id { get; set; }
  1110. public string name { get; set; }
  1111. public int weekSc { get; set; }
  1112. public int monthSc { get; set; }
  1113. public int tch { get; set; }
  1114. public int dayTch { get; set; }
  1115. public int weekTch { get; set; }
  1116. public int monthTch { get; set; }
  1117. public int room { get; set; }
  1118. public int witRoom { get; set; }
  1119. public int size { get; set; }
  1120. public int stu { get; set; }
  1121. public int dayStu { get; set; }
  1122. public int weekStu { get; set; }
  1123. public int monthStu { get; set; }
  1124. public ManyLessStats lessStats { get; set; } = new ManyLessStats();
  1125. public ManyScActStats actStats { get; set; } = new ManyScActStats();
  1126. public ScSRStats srStats { get; set; } = new ScSRStats();
  1127. }
  1128. /// <summary>
  1129. /// 前端显示学校统计数据
  1130. /// </summary>
  1131. public record ScStats
  1132. {
  1133. public string id { get; set; }
  1134. public string schoolId { get; set; }
  1135. public string name { get; set; }
  1136. public string picture { get; set; }
  1137. public string areaId { get; set; }
  1138. public int tch { get; set; }
  1139. public int dayTch { get; set; }
  1140. public int weekTch { get; set; }
  1141. public int monthTch { get; set; }
  1142. public int stu { get; set; }
  1143. public int dayStu { get; set; }
  1144. public int weekStu { get; set; }
  1145. public int monthStu { get; set; }
  1146. public int room { get; set; }
  1147. public int witRoom { get; set; }
  1148. public int size { get; set; }
  1149. public long scCreateTime { get; set; }
  1150. public long upTime { get; set; }
  1151. public ManyLessStats lessStats { get; set; } = new ManyLessStats();
  1152. public ManyScActStats actStats { get; set; } = new ManyScActStats();
  1153. public ScSRStats srStats { get; set; } = new ScSRStats();
  1154. }
  1155. /// <summary>
  1156. /// 所有
  1157. /// </summary>
  1158. public record AllScStats
  1159. {
  1160. public int areaCnt { get; set; }
  1161. public int sc { get; set; }
  1162. public int weekSc { get; set; }
  1163. public int monthSc { get; set; }
  1164. public int tch { get; set; }
  1165. public int dayTch { get; set; }
  1166. public int weekTch { get; set; }
  1167. public int monthTch { get; set; }
  1168. public int stu { get; set; }
  1169. public int dayStu { get; set; }
  1170. public int weekStu { get; set; }
  1171. public int monthStu { get; set; }
  1172. public int room { get; set; }
  1173. public int witRoom { get; set; }
  1174. public int size { get; set; }
  1175. public long scCreateTime { get; set; }
  1176. public long upTime { get; set; }
  1177. public ScLessStats lessStats { get; set; } = new ScLessStats();
  1178. public ScActStats actStats { get; set; } = new ScActStats();
  1179. public ScSRStats srStats { get; set; } = new ScSRStats();
  1180. }
  1181. public record AreaInfoStats
  1182. {
  1183. public string areaId { get; set; }
  1184. public string name { get; set; }
  1185. public int lessAct { get; set; }
  1186. public int lessCnt { get; set; }
  1187. public int actCnt { get; set; }
  1188. }
  1189. /// <summary>
  1190. /// 课例活动数据
  1191. /// </summary>
  1192. public record ScLessStats
  1193. {
  1194. public int all { get; set; }
  1195. public int open { get; set; }
  1196. public int less { get; set; }
  1197. public int lastDay { get; set; }
  1198. public int day { get; set; }
  1199. public int lastWeek { get; set; }
  1200. public int week { get; set; }
  1201. public int lastTerm { get; set; }
  1202. public int term { get; set; }
  1203. public int month { get; set; }
  1204. public int lastMonth { get; set; }
  1205. public int lastDayInter { get; set; }
  1206. public int dayInter { get; set; }
  1207. public int lastMonthInter { get; set; }
  1208. public int monthInter { get; set; }
  1209. public int lastYearInter { get; set; }
  1210. public int yearInter { get; set; }
  1211. }
  1212. /// <summary>
  1213. /// 课例活动数据
  1214. /// </summary>
  1215. public record ManyLessStats : ScLessStats
  1216. {
  1217. public int lastYear { get; set; }
  1218. public List<YearMonth> yearInters { get; set; } = new List<YearMonth>();//12个月
  1219. public List<YearMonth> year { get; set; } = new List<YearMonth>(); //12个月
  1220. }
  1221. public record ScActStats
  1222. {
  1223. public int all { get; set; }
  1224. public int exam { get; set; }
  1225. public int survey { get; set; }
  1226. public int vote { get; set; }
  1227. public int homework { get; set; }
  1228. public int lastDay { get; set; }
  1229. public int dayCnt { get; set; }
  1230. public int lastWeek { get; set; }
  1231. public int week { get; set; }
  1232. public int lastTerm { get; set; }
  1233. public int term { get; set; }
  1234. public int lastMonth { get; set; }
  1235. public int month { get; set; }
  1236. }
  1237. /// <summary>
  1238. /// 学校活动数据
  1239. /// </summary>
  1240. public record ManyScActStats : ScActStats
  1241. {
  1242. public int lastYear { get; set; }
  1243. public List<YearMonth> year { get; set; } = new List<YearMonth>(); //12个月
  1244. }
  1245. /// <summary>
  1246. /// 研修统计数据
  1247. /// </summary>
  1248. public record ScSRStats
  1249. {
  1250. public int learnTime { get; set; }
  1251. public int online { get; set; }
  1252. public int offline { get; set; }
  1253. public int classRoom { get; set; }
  1254. public int submit { get; set; }
  1255. public int notStarted { get; set; }
  1256. public int ongoing { get; set; }
  1257. public int finish { get; set; }
  1258. }
  1259. /// <summary>
  1260. /// 查询去年的统计
  1261. /// </summary>
  1262. public record LastYearLessAndAct
  1263. {
  1264. /// <summary>
  1265. /// 课例活动
  1266. /// </summary>
  1267. public LessonStats lesson { get; set; } = new LessonStats();
  1268. /// <summary>
  1269. /// 活动
  1270. /// </summary>
  1271. public ActivityStats activity { get; set; } = new ActivityStats();
  1272. }
  1273. }
  1274. }