SchoolController.cs 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. StringBuilder scSql = new("select value(c.id) from c");
  593. StringBuilder lastScSql = new("select value(c.id) from c");
  594. //if (!string.IsNullOrEmpty($"{areaId}"))
  595. //{
  596. // scSql.Append($" where c.areaId='{areaId}'");
  597. // lastScSql.Append($" where c.areaId='{areaId}'");
  598. // var respSc = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{areaId}", new PartitionKey($"Base-Area"));
  599. // if (respSc.Status == 200)
  600. // {
  601. // using var fileJson = await JsonDocument.ParseAsync(respSc.ContentStream);
  602. // area = fileJson.ToObject<Area>();
  603. // }
  604. // else
  605. // return Ok(new { state = RespondCode.NotFound, msg = $"未找到id:{areaId};相关的学区,请检查!" });
  606. //}
  607. if (!string.IsNullOrEmpty($"{tmdId}"))
  608. {
  609. switch ($"{role}")
  610. {
  611. case "assists":
  612. scSql.Append($" join a in c.assists where a.id='{tmdId}'");
  613. lastScSql.Append($" join a in c.assists where a.id='{tmdId}'");
  614. break;
  615. case "sales":
  616. scSql.Append($" join a in c.sales where a.id='{tmdId}'");
  617. lastScSql.Append($" join a in c.sales where a.id='{tmdId}'");
  618. break;
  619. default:
  620. return Ok(new { state = RespondCode.ParamsError, msg = "role参数错误" });
  621. }
  622. }
  623. scIds = await CommonFind.FindScIds(cosmosClient, scSql.ToString(), "BIRel");
  624. StringBuilder statsSql = new("select value(c) from c");
  625. if (scIds.Count > 0)
  626. {
  627. statsSql.Append($" where {BICommonWay.ManyScSql("c.id", scIds, $"{DateTimeOffset.UtcNow.Year}-")}");
  628. }
  629. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  630. List<ScStats> scStatss = new();
  631. List<StatsInfo> statsInfos = new();
  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");
  637. if (scIds.Count > 0)
  638. {
  639. lastStsSql.Append($" where {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");
  743. if (scIds.Count > 0)
  744. statsSql.Append($" where {BICommonWay.ManyScSql("c.id", scIds, $"{DateTimeOffset.UtcNow.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");
  750. if (scIds.Count > 0)
  751. {
  752. lastStsSql.Append($" where {BICommonWay.ManyScSql("c.id", scIds, $"{DateTimeOffset.UtcNow.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. int areaCnt = await JointlySingleQuery.GetValueInt(cosmosClient, "Normal", "Base-Area", sql);
  936. int scCnt = await JointlySingleQuery.GetValueInt(cosmosClient, "Normal", "Base", sql);
  937. int scWeekCnt = await JointlySingleQuery.GetValueInt(cosmosClient, "Normal", "Base", $"{sql} where c.createTime >= {weekS} and c.createTime <= {weekE}");
  938. int scMonthCnt = await JointlySingleQuery.GetValueInt(cosmosClient, "Normal", "Base", $"{sql} where c.createTime >= {mthS} and c.createTime <= {mthE}");
  939. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<StatsInfo>(queryText: $"select value(c) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Statistics") }))
  940. {
  941. statsInfos.Add(item);
  942. }
  943. var areaGrup = statsInfos.GroupBy(p => p.areaId).Select(s => new { areaId = s.Key, 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();
  944. StatsInfo statsInfo = null;
  945. statsInfo = SchoolStatsWay.GetAreaStats(cosmosClient, _httpTrigger, _option, statsInfos);
  946. if (statsInfo != null)
  947. {
  948. allScStats.sc = statsInfos.Count;
  949. allScStats.weekSc = statsInfos.FindAll(f=>f.scCreateTime >= weekS && f.scCreateTime <= weekE).Count;
  950. allScStats.monthSc = statsInfos.FindAll(f => f.scCreateTime >= mthS && f.scCreateTime <= mthE).Count;
  951. allScStats.tch = statsInfo.tch;
  952. allScStats.dayTch = statsInfo.dayTch;
  953. allScStats.weekTch = statsInfo.weekTch;
  954. allScStats.monthTch = statsInfo.monthTch;
  955. allScStats.stu = statsInfo.stu;
  956. allScStats.dayStu = statsInfo.dayStu;
  957. allScStats.weekStu = statsInfo.weekStu;
  958. allScStats.monthStu = statsInfo.monthStu;
  959. allScStats.room = statsInfo.room;
  960. allScStats.witRoom = statsInfo.witRoom;
  961. allScStats.size = statsInfo.size;
  962. allScStats.scCreateTime = statsInfo.scCreateTime;
  963. allScStats.upTime = statsInfo.upTime;
  964. allScStats.lessStats.open = statsInfo.lesson.all;
  965. allScStats.lessStats.open = statsInfo.lesson.open;
  966. allScStats.lessStats.less = statsInfo.lesson.less;
  967. allScStats.lessStats.lastDay = statsInfo.lesson.lastDay;
  968. allScStats.lessStats.day = statsInfo.lesson.day;
  969. allScStats.lessStats.lastWeek = statsInfo.lesson.lastWeek;
  970. allScStats.lessStats.week = statsInfo.lesson.week;
  971. allScStats.lessStats.lastTerm = statsInfo.lesson.lastTerm;
  972. allScStats.lessStats.term = statsInfo.lesson.term;
  973. allScStats.lessStats.lastDayInter = statsInfo.lesson.lastDayInter;
  974. allScStats.lessStats.dayInter = statsInfo.lesson.dayInter;
  975. allScStats.lessStats.lastMonthInter = statsInfo.lesson.lastMonthInter;
  976. allScStats.lessStats.monthInter = statsInfo.lesson.monthInter;
  977. allScStats.lessStats.lastYearInter = statsInfo.lesson.lastYearInter;
  978. allScStats.lessStats.yearInter = statsInfo.lesson.yearInter;
  979. allScStats.actStats.all = statsInfo.activity.all;
  980. allScStats.actStats.exam = statsInfo.activity.exam;
  981. allScStats.actStats.survey = statsInfo.activity.survey;
  982. allScStats.actStats.vote = statsInfo.activity.vote;
  983. allScStats.actStats.homework = statsInfo.activity.homework;
  984. allScStats.actStats.lastDay = statsInfo.activity.lastDay;
  985. allScStats.actStats.dayCnt = statsInfo.activity.dayCnt;
  986. allScStats.actStats.lastWeek = statsInfo.activity.lastWeek;
  987. allScStats.actStats.week = statsInfo.activity.week;
  988. allScStats.actStats.lastTerm = statsInfo.activity.lastTerm;
  989. allScStats.actStats.term = statsInfo.activity.term;
  990. allScStats.actStats.lastMonth = statsInfo.activity.lastMonth;
  991. allScStats.actStats.month = statsInfo.activity.month;
  992. if (statsInfo.study != null)
  993. {
  994. allScStats.srStats.learnTime = statsInfo.study.learnTime;
  995. allScStats.srStats.online = statsInfo.study.online;
  996. allScStats.srStats.offline = statsInfo.study.offline;
  997. allScStats.srStats.classRoom = statsInfo.study.classRoom;
  998. allScStats.srStats.submit = statsInfo.study.submit;
  999. allScStats.srStats.notStarted = statsInfo.study.notStarted;
  1000. allScStats.srStats.ongoing = statsInfo.study.ongoing;
  1001. allScStats.srStats.finish = statsInfo.study.finish;
  1002. }
  1003. }
  1004. return Ok(new { state = RespondCode.Ok, allScStats, areaGrup });
  1005. }
  1006. #endregion 新的统计接口
  1007. /// <summary>
  1008. /// 学校信息中间件查询接口
  1009. /// </summary>
  1010. /// <param name="jsonElement"></param>
  1011. /// <returns></returns>
  1012. [ProducesDefaultResponseType]
  1013. [HttpPost("get-scinfos")]
  1014. public async Task<IActionResult> GetScInfos(JsonElement jsonElement)
  1015. {
  1016. var cosmosClient = _azureCosmos.GetCosmosClient();
  1017. jsonElement.TryGetProperty("role", out JsonElement role);
  1018. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  1019. jsonElement.TryGetProperty("areaId", out JsonElement areaId);
  1020. StringBuilder sql = new($"select value(c) from c");
  1021. if (!string.IsNullOrEmpty($"{role}") && !string.IsNullOrEmpty($"{tmdId}") && string.IsNullOrEmpty($"{areaId}"))
  1022. {
  1023. switch ($"{role}")
  1024. {
  1025. case "assist":
  1026. sql.Append($" join a in c.assist where a.id='{tmdId}'");
  1027. break;
  1028. case "sales":
  1029. sql.Append($" join a in c.sales where a.id='{tmdId}'");
  1030. break;
  1031. }
  1032. }
  1033. if (!string.IsNullOrEmpty($"{areaId}") && !string.IsNullOrEmpty($"{role}"))
  1034. {
  1035. sql.Append($" where c.areaId ='{areaId}'");
  1036. }
  1037. //List<string> scId = await CommonFind.FindScIds(cosmosClient, "select value(c.id) from c ", "Base");
  1038. List<BIRelation> scInfos = new();
  1039. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<BIRelation>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BIRel") }))
  1040. {
  1041. scInfos.Add(item);
  1042. }
  1043. return Ok(new { state = RespondCode.Ok, allCnt = scInfos.Count, scInfos });
  1044. }
  1045. /// <summary>
  1046. /// 依据Id查询School容器 数据管理工具——查询工具
  1047. /// </summary>
  1048. /// <param name="jsonElement"></param>
  1049. /// <returns></returns>
  1050. [ProducesDefaultResponseType]
  1051. [HttpPost("get-info")]
  1052. public async Task<IActionResult> GetSchool(JsonElement jsonElement)
  1053. {
  1054. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  1055. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  1056. var cosmosClient = _azureCosmos.GetCosmosClient();
  1057. //if ($"{site}".Equals(BIConst.Global))
  1058. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  1059. List<object> infos = new List<object>();
  1060. string sqlTxt = $"select value(c) from c where c.id='{id}'";
  1061. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  1062. {
  1063. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1064. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1065. {
  1066. infos.Add(obj.ToObject<object>());
  1067. }
  1068. }
  1069. return Ok(new { state = 200, infos });
  1070. }
  1071. public record RecSchoolDate
  1072. {
  1073. public string id { get; set; }
  1074. public string name { get; set; }
  1075. public long dataCount { get; set; }
  1076. }
  1077. /// <summary>
  1078. /// 学校列表信息
  1079. /// </summary>
  1080. public record SchoolInfo
  1081. {
  1082. public string id { get; set; }
  1083. public string name { get; set; }
  1084. public string picture { get; set; }
  1085. public string areaId { get; set; }
  1086. public string areaName { get; set; }
  1087. public int size { get; set; }
  1088. public int scale { get; set; }
  1089. public long createDate { get; set; }
  1090. public List<IdInfo> assists { get; set; } = new List<IdInfo>();
  1091. public List<IdInfo> sales { get; set; } = new List<IdInfo>();
  1092. public List<string> serial { get; set; } = new List<string>();
  1093. public List<string> service { get; set; } = new List<string>();
  1094. public List<string> hard { get; set; } = new List<string>();
  1095. public int less { get; set; }
  1096. public int lastWeekLess { get; set; }
  1097. public int monthLess { get; set; }
  1098. }
  1099. /// <summary>
  1100. /// 区级信息
  1101. /// </summary>
  1102. public record AreaStats
  1103. {
  1104. public string id { get; set; }
  1105. public string name { get; set; }
  1106. public int weekSc { get; set; }
  1107. public int monthSc{ get; set; }
  1108. public int tch { get; set; }
  1109. public int dayTch { get; set; }
  1110. public int weekTch { get; set; }
  1111. public int monthTch { get; set; }
  1112. public int room { get; set; }
  1113. public int witRoom { get; set; }
  1114. public int size { get; set; }
  1115. public int stu { get; set; }
  1116. public int dayStu { get; set; }
  1117. public int weekStu { get; set; }
  1118. public int monthStu { get; set; }
  1119. public ManyLessStats lessStats { get; set; } = new ManyLessStats();
  1120. public ManyScActStats actStats { get; set; } = new ManyScActStats();
  1121. public ScSRStats srStats { get; set; } = new ScSRStats();
  1122. }
  1123. /// <summary>
  1124. /// 前端显示学校统计数据
  1125. /// </summary>
  1126. public record ScStats
  1127. {
  1128. public string id { get; set; }
  1129. public string schoolId { get; set; }
  1130. public string name { get; set; }
  1131. public string picture { get; set; }
  1132. public string areaId { get; set; }
  1133. public int tch { get; set; }
  1134. public int dayTch { get; set; }
  1135. public int weekTch { get; set; }
  1136. public int monthTch { get; set; }
  1137. public int stu { get; set; }
  1138. public int dayStu { get; set; }
  1139. public int weekStu { get; set; }
  1140. public int monthStu { get; set; }
  1141. public int room { get; set; }
  1142. public int witRoom { get; set; }
  1143. public int size { get; set; }
  1144. public long scCreateTime { get; set; }
  1145. public long upTime { get; set; }
  1146. public ManyLessStats lessStats { get; set; } = new ManyLessStats();
  1147. public ManyScActStats actStats { get; set; } = new ManyScActStats();
  1148. public ScSRStats srStats { get; set; } = new ScSRStats();
  1149. }
  1150. /// <summary>
  1151. /// 所有
  1152. /// </summary>
  1153. public record AllScStats
  1154. {
  1155. public int sc { get; set; }
  1156. public int weekSc { get; set; }
  1157. public int monthSc { get; set; }
  1158. public int tch { get; set; }
  1159. public int dayTch { get; set; }
  1160. public int weekTch { get; set; }
  1161. public int monthTch { get; set; }
  1162. public int stu { get; set; }
  1163. public int dayStu { get; set; }
  1164. public int weekStu { get; set; }
  1165. public int monthStu { get; set; }
  1166. public int room { get; set; }
  1167. public int witRoom { get; set; }
  1168. public int size { get; set; }
  1169. public long scCreateTime { get; set; }
  1170. public long upTime { get; set; }
  1171. public ScLessStats lessStats { get; set; } = new ScLessStats();
  1172. public ScActStats actStats { get; set; } = new ScActStats();
  1173. public ScSRStats srStats { get; set; } = new ScSRStats();
  1174. }
  1175. /// <summary>
  1176. /// 课例活动数据
  1177. /// </summary>
  1178. public record ScLessStats
  1179. {
  1180. public int all { get; set; }
  1181. public int open { get; set; }
  1182. public int less { get; set; }
  1183. public int lastDay { get; set; }
  1184. public int day { get; set; }
  1185. public int lastWeek { get; set; }
  1186. public int week { get; set; }
  1187. public int lastTerm { get; set; }
  1188. public int term { get; set; }
  1189. public int month { get; set; }
  1190. public int lastMonth { get; set; }
  1191. public int lastDayInter { get; set; }
  1192. public int dayInter { get; set; }
  1193. public int lastMonthInter { get; set; }
  1194. public int monthInter { get; set; }
  1195. public int lastYearInter { get; set; }
  1196. public int yearInter { get; set; }
  1197. }
  1198. /// <summary>
  1199. /// 课例活动数据
  1200. /// </summary>
  1201. public record ManyLessStats : ScLessStats
  1202. {
  1203. public int lastYear { get; set; }
  1204. public List<YearMonth> yearInters { get; set; } = new List<YearMonth>();//12个月
  1205. public List<YearMonth> year { get; set; } = new List<YearMonth>(); //12个月
  1206. }
  1207. public record ScActStats
  1208. {
  1209. public int all { get; set; }
  1210. public int exam { get; set; }
  1211. public int survey { get; set; }
  1212. public int vote { get; set; }
  1213. public int homework { get; set; }
  1214. public int lastDay { get; set; }
  1215. public int dayCnt { get; set; }
  1216. public int lastWeek { get; set; }
  1217. public int week { get; set; }
  1218. public int lastTerm { get; set; }
  1219. public int term { get; set; }
  1220. public int lastMonth { get; set; }
  1221. public int month { get; set; }
  1222. }
  1223. /// <summary>
  1224. /// 学校活动数据
  1225. /// </summary>
  1226. public record ManyScActStats : ScActStats
  1227. {
  1228. public int lastYear { get; set; }
  1229. public List<YearMonth> year { get; set; } = new List<YearMonth>(); //12个月
  1230. }
  1231. /// <summary>
  1232. /// 研修统计数据
  1233. /// </summary>
  1234. public record ScSRStats
  1235. {
  1236. public int learnTime { get; set; }
  1237. public int online { get; set; }
  1238. public int offline { get; set; }
  1239. public int classRoom { get; set; }
  1240. public int submit { get; set; }
  1241. public int notStarted { get; set; }
  1242. public int ongoing { get; set; }
  1243. public int finish { get; set; }
  1244. }
  1245. /// <summary>
  1246. /// 查询去年的统计
  1247. /// </summary>
  1248. public record LastYearLessAndAct
  1249. {
  1250. /// <summary>
  1251. /// 课例活动
  1252. /// </summary>
  1253. public LessonStats lesson { get; set; } = new LessonStats();
  1254. /// <summary>
  1255. /// 活动
  1256. /// </summary>
  1257. public ActivityStats activity { get; set; } = new ActivityStats();
  1258. }
  1259. }
  1260. }