SchoolController.cs 82 KB

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