StatsWay.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. using Microsoft.Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using TEAMModelOS.SDK.DI;
  8. using TEAMModelOS.SDK.Extension;
  9. using TEAMModelOS.SDK.Models;
  10. using TEAMModelOS.SDK.Models.Cosmos.BI;
  11. using TEAMModelOS.SDK.Models.Service.BI;
  12. namespace TEAMModelBI.Tool.CosmosBank
  13. {
  14. public static class StatsWay
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. /// <param name="cosmosClient"></param>
  20. /// <param name="scId"></param>
  21. /// <returns></returns>
  22. public static async Task<StatsInfo> upSingleSc(CosmosClient cosmosClient, string scId)
  23. {
  24. StatsInfo statsInfo = new() { id = $"{scId}" };
  25. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  26. var (dayS, dayE) = TimeHelper.GetStartOrEnd(dateTime); //今天开始时间 13位
  27. var (lastWeekS, lastWeekE) = TimeHelper.GetStartOrEnd(dateTime, "lastweek"); //计算上周开始/结束时间
  28. var (weekS, weekE) = TimeHelper.GetStartOrEnd(dateTime, "week"); //计算本周开始/结束时间
  29. var (lastTermS, lastTermE) = TimeHelper.GetStartOrEnd(dateTime, "lastterm"); //计算上学期开始/结束时间
  30. var (termS, termE) = TimeHelper.GetStartOrEnd(dateTime, "term"); //计算本学期开始/结束时间
  31. var (lastMthS, LastmthE) = TimeHelper.GetStartOrEnd(dateTime, "lastMonth"); //上月开始/结束时间
  32. var (mthS, mthE) = TimeHelper.GetStartOrEnd(dateTime, "month"); //本月开始/结束时间
  33. var (lastYearS, lastYearE) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{dateTime.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  34. var (yearS, yearE) = TimeHelper.GetStartOrEnd(dateTime, "year"); //计算今年开始/结束时间
  35. string currSql = "select value(count(c.id)) from c";
  36. string dayWhereSql = $"c.createTime >= {dayS} and c.createTime <= {dayE}";
  37. string weekWhereSql = $"c.createTime >= {weekS} and c.createTime <= {weekE}";
  38. string monthWhereSql = $"c.createTime >= {mthS} and c.createTime <= {mthE}";
  39. ScBase scBase = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemAsync<ScBase>(statsInfo.id, new PartitionKey("Base"));
  40. statsInfo.name = scBase.name;
  41. statsInfo.picture = scBase.picture;
  42. statsInfo.size = scBase.size;
  43. statsInfo.scCreateTime = scBase.createTime;
  44. statsInfo.areaId = scBase.areaId;
  45. string tchSql = $"{currSql} where ARRAY_CONTAINS(c.roles, 'teacher', true) AND c.status = 'join'";
  46. statsInfo.tch = await JointlySingleQuery.GetValueInt(cosmosClient, "School", tchSql, $"Teacher-{scBase.id}");
  47. statsInfo.dayTch = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"{tchSql} and {dayWhereSql} ", $"Teacher-{scBase.id}");
  48. statsInfo.weekTch = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"{tchSql} and {weekWhereSql}", $"Teacher-{scBase.id}");
  49. statsInfo.monthTch = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"{tchSql} and {monthWhereSql}", $"Teacher-{scBase.id}");
  50. statsInfo.stu = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", currSql, $"Base-{scBase.id}");
  51. statsInfo.dayStu = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", $"{currSql} where {dayWhereSql}", $"Base-{scBase.id}");
  52. statsInfo.weekStu = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", $"{currSql} where {weekWhereSql}", $"Base-{scBase.id}");
  53. statsInfo.monthStu = await JointlySingleQuery.GetValueInt(cosmosClient, "Student", $"{currSql} where {monthWhereSql}", $"Base-{scBase.id}");
  54. statsInfo.room = await JointlySingleQuery.GetValueInt(cosmosClient, "School", currSql, $"Room-{scBase.id}");
  55. statsInfo.witRoom = await JointlySingleQuery.GetValueInt(cosmosClient, "School", $"{currSql} where (c.serial != null or c.serial != '' or IS_DEFINED(c.serial) = true)", $"Room-{scBase.id}");
  56. statsInfo.resourceCnt = await JointlySingleQuery.GetValueInt(cosmosClient, "School", currSql, $"Bloblog-{scBase.id}");
  57. statsInfo.lesson = await GetLessStats(cosmosClient, statsInfo.id);
  58. statsInfo.activity = await GetActStats(cosmosClient, statsInfo.id);
  59. statsInfo.study = await GetStudyStats(cosmosClient, statsInfo.id);
  60. return statsInfo;
  61. }
  62. /// <summary>
  63. /// 课例
  64. /// </summary>
  65. /// <param name="cosmosClient"></param>
  66. /// <param name="id"></param>
  67. /// <returns></returns>
  68. public static async Task<LessonStats> GetLessStats(CosmosClient cosmosClient, string id)
  69. {
  70. LessonStats lessStats = new();
  71. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  72. var (lastDayS, lastdayE) = TimeHelper.GetStartOrEnd(dateTime.AddDays(-1)); //昨天开始时间
  73. var (dayS, dayE) = TimeHelper.GetStartOrEnd(dateTime); //今天开始时间
  74. var (lastWeekS, lastWeekE) = TimeHelper.GetStartOrEnd(dateTime, "lastweek"); //计算上周开始/结束时间
  75. var (weekS, weekE) = TimeHelper.GetStartOrEnd(dateTime, "week"); //计算本周开始/结束时间
  76. var (lastTermS, lastTermE) = TimeHelper.GetStartOrEnd(dateTime, "lastterm"); //计算上学期开始/结束时间
  77. var (termS, termE) = TimeHelper.GetStartOrEnd(dateTime, "term"); //计算本学期开始/结束时间
  78. var (lastMthS, LastmthE) = TimeHelper.GetStartOrEnd(dateTime, "lastMonth"); //上月开始/结束时间
  79. var (mthS, mthE) = TimeHelper.GetStartOrEnd(dateTime, "month"); //本月开始/结束时间
  80. var (lastYearS, lastYearE) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{dateTime.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  81. var (yearS, yearE) = TimeHelper.GetStartOrEnd(dateTime, "year"); //计算今年开始/结束时间
  82. string currSql = "select value(count(c.id)) from c where c.pk='LessonRecord'";
  83. lessStats.all = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"LessonRecord-{id}", $"{currSql}");
  84. lessStats.open = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"LessonRecord-{id}", $"{currSql} and c.upload = 0");
  85. lessStats.less = await JointlySingleQuery.GetValueInt(cosmosClient, "School", code: $"LessonRecord-{id}", $"{currSql} and c.upload = 1");
  86. List<LessRelStats> lessRelStats = new();
  87. string sql = $"select c.id,c.code,c.startTime,c.clientInteractionCount from c where c.pk='LessonRecord' and c.startTime >= {lastYearS} and c.startTime <= {yearE}";
  88. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<LessRelStats>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{id}") }))
  89. {
  90. lessRelStats.Add(item);
  91. }
  92. lessStats.lastDay = lessRelStats.FindAll(f => f.startTime >= lastDayS && f.startTime <= lastdayE).ToList().Count;
  93. lessStats.day = lessRelStats.FindAll(f => f.startTime >= dayS && f.startTime <= dayE).ToList().Count;
  94. lessStats.lastWeek = lessRelStats.FindAll(f => f.startTime >= lastWeekS && f.startTime <= lastWeekE).ToList().Count;
  95. lessStats.week = lessRelStats.FindAll(f => f.startTime >= weekS && f.startTime <= weekE).ToList().Count;
  96. lessStats.lastTerm = lessRelStats.FindAll(f => f.startTime >= lastTermS && f.startTime <= lastTermE).ToList().Count;
  97. lessStats.term = lessRelStats.FindAll(f => f.startTime >= termS && f.startTime <= termE).ToList().Count;
  98. lessStats.lastDayInter = lessRelStats.FindAll(f => f.startTime >= lastDayS && f.startTime <= lastdayE).ToList().Select(s=>s.clientInteractionCount).Sum();
  99. lessStats.dayInter = lessRelStats.FindAll(f => f.startTime >= dayS && f.startTime <= dayS).ToList().Select(s => s.clientInteractionCount).Sum();
  100. lessStats.lastMonthInter = lessRelStats.FindAll(f => f.startTime >= lastWeekS && f.startTime <= lastWeekE).ToList().Select(s => s.clientInteractionCount).Sum();
  101. lessStats.monthInter = lessRelStats.FindAll(f => f.startTime >= weekS && f.startTime <= weekS).ToList().Select(s => s.clientInteractionCount).Sum();
  102. lessStats.lastYearInter = lessRelStats.FindAll(f => f.startTime >= lastTermS && f.startTime <= lastTermE).ToList().Select(s => s.clientInteractionCount).Sum();
  103. lessStats.yearInter = lessRelStats.FindAll(f => f.startTime >= termS && f.startTime <= termE).ToList().Select(s => s.clientInteractionCount).Sum();
  104. DateTimeOffset lyearDay = new(DateTimeOffset.UtcNow.Year - 1, DateTimeOffset.UtcNow.Month, DateTimeOffset.UtcNow.Day, DateTimeOffset.UtcNow.Hour, DateTimeOffset.UtcNow.Minute, DateTimeOffset.UtcNow.Second, TimeSpan.Zero);
  105. List<StartEndTime> leveryDay = TimeHelper.GetYearEveryDay(lyearDay);
  106. List<StartEndTime> everyDay = TimeHelper.GetYearEveryDay(DateTimeOffset.UtcNow);
  107. //for (int i = 0; i < leveryDay.Count; i++)
  108. //{
  109. // if (lessStats.LastYear.Count == 366)
  110. // lessStats.LastYear[i] = (double)lessRelStats.FindAll(f => (f.startTime >= leveryDay[i].start && f.startTime <= leveryDay[i].end)).Count;
  111. // else
  112. // lessStats.LastYear.Add((double)lessRelStats.FindAll(f => (f.startTime >= leveryDay[i].start && f.startTime <= leveryDay[i].end)).Count);
  113. //}
  114. for (int i = 0; i < everyDay.Count; i++)
  115. {
  116. if (lessStats.year.Count == 366)
  117. lessStats.year[i] = lessRelStats.FindAll(f => (f.startTime >= everyDay[i].start && f.startTime <= everyDay[i].end)).Count;
  118. else
  119. lessStats.year.Add(lessRelStats.FindAll(f => (f.startTime >= everyDay[i].start && f.startTime <= everyDay[i].end)).Count);
  120. }
  121. //string lastDaySql = $"{currSql} and c.startTime >= {lastDayS} and c.startTime <= {lastDayS}";
  122. //lessStats.lastDay = await JointlySingleQuery.GetValueInt(cosmosClient, "School", lastDaySql, code: $"LessonRecord-{id}");
  123. //string daySql = $"{currSql} and c.startTime >= {dayS} and c.startTime <= {dayE}";
  124. //lessStats.day = await JointlySingleQuery.GetValueInt(cosmosClient, "School", daySql, code: $"LessonRecord-{id}");
  125. //string lastWeekSql = $"{currSql} and c.startTime >= {lastWeekS} and c.startTime <= {lastWeekE}";
  126. //lessStats.lastWeek = await JointlySingleQuery.GetValueInt(cosmosClient, "School", lastWeekSql, code: $"LessonRecord-{id}");
  127. //string weekSql = $"{currSql} and c.startTime >= {weekS} and c.startTime <= {weekE}";
  128. //lessStats.week = await JointlySingleQuery.GetValueInt(cosmosClient, "School", weekSql, code: $"LessonRecord-{id}");
  129. //string lastTermSql = $"{currSql} and c.startTime >= {lastTermS} and c.startTime <= {lastTermE}";
  130. //lessStats.lastTerm = await JointlySingleQuery.GetValueInt(cosmosClient, "School", lastTermSql, code: $"LessonRecord-{id}");
  131. //string termSql = $"{currSql} and c.startTime >= {termS} and c.startTime <= {termE}";
  132. //lessStats.term = await JointlySingleQuery.GetValueInt(cosmosClient, "School", termSql, code: $"LessonRecord-{id}");
  133. //string InterSql = "select value(sum(c.clientInteractionCount)) from c where c.pk='LessonRecord'";
  134. //string lastDayInterSql = $"{InterSql} and c.startTime >= {lastDayS} and c.startTime <= {lastdayE}";
  135. //lessStats.lastDayInter = await JointlySingleQuery.GetValueInt(cosmosClient, "School", lastDayInterSql, code: $"LessonRecord-{id}");
  136. //string dayInterSql = $"{InterSql} and c.startTime >= {dayS} and c.startTime <= {dayE}";
  137. //lessStats.dayInter = await JointlySingleQuery.GetValueInt(cosmosClient, "School", dayInterSql, code: $"LessonRecord-{id}");
  138. //string lastMthInterSql = $"{InterSql} and c.startTime >= {lastMthS} and c.startTime <= {LastmthE}";
  139. //lessStats.lastMonthInter = await JointlySingleQuery.GetValueInt(cosmosClient, "School", lastMthInterSql, code: $"LessonRecord-{id}");
  140. //string mthInterSql = $"{InterSql} and c.startTime >= {mthS} and c.startTime <= {mthE}";
  141. //lessStats.monthInter = await JointlySingleQuery.GetValueInt(cosmosClient, "School", mthInterSql, code: $"LessonRecord-{id}");
  142. //string lastYearInterSql = $"{InterSql} and c.startTime >= {lastYearS} and c.startTime <= {lastYearE}";
  143. //lessStats.lastYearInter = await JointlySingleQuery.GetValueInt(cosmosClient, "School", lastYearInterSql, code: $"LessonRecord-{id}");
  144. //string yearInterSql = $"{InterSql} and c.startTime >= {yearS} and c.startTime <= {yearE}";
  145. //lessStats.yearInter = await JointlySingleQuery.GetValueInt(cosmosClient, "School", yearInterSql, code: $"LessonRecord-{id}");
  146. return lessStats;
  147. }
  148. /// <summary>
  149. /// 统计学校活动
  150. /// </summary>
  151. /// <param name="cosmosClient"></param>
  152. /// <param name="scId"></param>
  153. /// <returns></returns>
  154. public static async Task<ActivityStats> GetActStats(CosmosClient cosmosClient, string scId)
  155. {
  156. ActivityStats actStats = new();
  157. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  158. var (lastDayS, lastdayE) = TimeHelper.GetStartOrEnd(dateTime.AddDays(-1)); //昨天开始时间
  159. var (dayS, dayE) = TimeHelper.GetStartOrEnd(dateTime); //今天开始时间
  160. var (lastWeekS, lastWeekE) = TimeHelper.GetStartOrEnd(dateTime, "lastweek"); //计算上周开始/结束时间
  161. var (weekS, weekE) = TimeHelper.GetStartOrEnd(dateTime, "week"); //计算本周开始/结束时间
  162. var (lastTermS, lastTermE) = TimeHelper.GetStartOrEnd(dateTime, "lastterm"); //计算上学期开始/结束时间
  163. var (termS, termE) = TimeHelper.GetStartOrEnd(dateTime, "term"); //计算本学期开始/结束时间
  164. var (lastMonthS, lastMonthE) = TimeHelper.GetStartOrEnd(dateTime, "lastMonth"); //上月开始/结束时间
  165. var (monthS, monthE) = TimeHelper.GetStartOrEnd(dateTime, "month"); //本月开始/结束时间
  166. var (lastYearS, lastYearE) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{dateTime.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  167. var (yearS, yearE) = TimeHelper.GetStartOrEnd(dateTime, "year"); //计算今年开始/结束时间
  168. string currSql = "select value(count(c.id)) from c ";
  169. string yearSql = $"select c.id,c.code,c.createTime from c where c.createTime >= {lastYearS} and c.createTime <= {yearE}";
  170. int exam = 0;
  171. int survey = 0;
  172. int vote = 0;
  173. int homework = 0;
  174. int lastDay = 0;
  175. int dayCnt = 0;
  176. int lastWeek = 0;
  177. int week = 0;
  178. int lastTerm = 0;
  179. int term = 0;
  180. int lastMonth = 0;
  181. int month = 0;
  182. DateTimeOffset lyearDay = new(DateTimeOffset.UtcNow.Year - 1, DateTimeOffset.UtcNow.Month, DateTimeOffset.UtcNow.Day, DateTimeOffset.UtcNow.Hour, DateTimeOffset.UtcNow.Minute, DateTimeOffset.UtcNow.Second, TimeSpan.Zero);
  183. List<StartEndTime> leveryDay = TimeHelper.GetYearEveryDay(lyearDay);
  184. List<StartEndTime> everyDay = TimeHelper.GetYearEveryDay(DateTimeOffset.UtcNow);
  185. List<ArtRelStats> examSts = new();
  186. List<ArtRelStats> surveySts = new();
  187. List<ArtRelStats> voteSts = new();
  188. List<ArtRelStats> homeworkSts = new();
  189. foreach (var artType in StaticValue.activityTypes)
  190. {
  191. switch (artType)
  192. {
  193. case "Exam":
  194. exam = await JointlySingleQuery.GetValueInt(cosmosClient, "Common", currSql, $"Exam-{scId}");
  195. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryIteratorSql<ArtRelStats>(queryText: yearSql, requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{scId}")}))
  196. {
  197. examSts.Add(item);
  198. }
  199. break;
  200. case "Survey":
  201. survey = await JointlySingleQuery.GetValueInt(cosmosClient, "Common", currSql, $"Survey-{scId}");
  202. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryIteratorSql<ArtRelStats>(queryText: yearSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Survey-{scId}") }))
  203. {
  204. surveySts.Add(item);
  205. }
  206. break;
  207. case "Vote":
  208. vote = await JointlySingleQuery.GetValueInt(cosmosClient, "Common", currSql, $"Vote-{scId}");
  209. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryIteratorSql<ArtRelStats>(queryText: yearSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Vote-{scId}") }))
  210. {
  211. voteSts.Add(item);
  212. }
  213. break;
  214. case "Homework":
  215. homework = await JointlySingleQuery.GetValueInt(cosmosClient, "Common", currSql, $"Homework-{scId}");
  216. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryIteratorSql<ArtRelStats>(queryText: yearSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Homework-{scId}") }))
  217. {
  218. homeworkSts.Add(item);
  219. }
  220. break;
  221. }
  222. }
  223. List<ArtRelStats> tempAll = new();
  224. if (examSts.Count > 0)
  225. {
  226. tempAll.AddRange(examSts);
  227. lastDay += examSts.FindAll(f => f.createTime >= lastDayS && f.createTime <= lastdayE).ToList().Count;
  228. dayCnt += examSts.FindAll(f => f.createTime >= dayS && f.createTime <= dayE).ToList().Count;
  229. lastWeek += examSts.FindAll(f => f.createTime >= lastWeekS && f.createTime <= lastWeekE).ToList().Count;
  230. week += examSts.FindAll(f => f.createTime >= weekS && f.createTime <= weekE).ToList().Count;
  231. lastTerm += examSts.FindAll(f => f.createTime >= lastTermS && f.createTime <= lastTermE).ToList().Count;
  232. term += examSts.FindAll(f => f.createTime >= termS && f.createTime <= termE).ToList().Count;
  233. lastMonth += examSts.FindAll(f => f.createTime >= lastMonthS && f.createTime <= lastMonthE).ToList().Count;
  234. month += examSts.FindAll(f => f.createTime >= monthS && f.createTime <= monthE).ToList().Count;
  235. }
  236. if (surveySts.Count > 0)
  237. {
  238. tempAll.AddRange(surveySts);
  239. lastDay += surveySts.FindAll(f => f.createTime >= lastDayS && f.createTime <= lastdayE).ToList().Count;
  240. dayCnt += surveySts.FindAll(f => f.createTime >= dayS && f.createTime <= dayE).ToList().Count;
  241. lastWeek += surveySts.FindAll(f => f.createTime >= lastWeekS && f.createTime <= lastWeekE).ToList().Count;
  242. week += surveySts.FindAll(f => f.createTime >= weekS && f.createTime <= weekE).ToList().Count;
  243. lastTerm += surveySts.FindAll(f => f.createTime >= lastTermS && f.createTime <= lastTermE).ToList().Count;
  244. term += surveySts.FindAll(f => f.createTime >= termS && f.createTime <= termE).ToList().Count;
  245. lastMonth += examSts.FindAll(f => f.createTime >= lastMonthS && f.createTime <= lastMonthE).ToList().Count;
  246. month += examSts.FindAll(f => f.createTime >= monthS && f.createTime <= monthE).ToList().Count;
  247. }
  248. if (voteSts.Count > 0)
  249. {
  250. tempAll.AddRange(voteSts);
  251. lastDay += voteSts.FindAll(f => f.createTime >= lastDayS && f.createTime <= lastdayE).ToList().Count;
  252. dayCnt += voteSts.FindAll(f => f.createTime >= dayS && f.createTime <= dayE).ToList().Count;
  253. lastWeek += voteSts.FindAll(f => f.createTime >= lastWeekS && f.createTime <= lastWeekE).ToList().Count;
  254. week += voteSts.FindAll(f => f.createTime >= weekS && f.createTime <= weekE).ToList().Count;
  255. lastTerm += voteSts.FindAll(f => f.createTime >= lastTermS && f.createTime <= lastTermE).ToList().Count;
  256. term += voteSts.FindAll(f => f.createTime >= termS && f.createTime <= termE).ToList().Count;
  257. lastMonth += examSts.FindAll(f => f.createTime >= lastMonthS && f.createTime <= lastMonthE).ToList().Count;
  258. month += examSts.FindAll(f => f.createTime >= monthS && f.createTime <= monthE).ToList().Count;
  259. }
  260. if (homeworkSts.Count > 0)
  261. {
  262. tempAll.AddRange(homeworkSts);
  263. lastDay += homeworkSts.FindAll(f => f.createTime >= lastDayS && f.createTime <= lastdayE).ToList().Count;
  264. dayCnt += homeworkSts.FindAll(f => f.createTime >= dayS && f.createTime <= dayE).ToList().Count;
  265. lastWeek += homeworkSts.FindAll(f => f.createTime >= lastWeekS && f.createTime <= lastWeekE).ToList().Count;
  266. week += homeworkSts.FindAll(f => f.createTime >= weekS && f.createTime <= weekE).ToList().Count;
  267. lastTerm += homeworkSts.FindAll(f => f.createTime >= lastTermS && f.createTime <= lastTermE).ToList().Count;
  268. term += homeworkSts.FindAll(f => f.createTime >= termS && f.createTime <= termE).ToList().Count;
  269. lastMonth += examSts.FindAll(f => f.createTime >= lastMonthS && f.createTime <= lastMonthE).ToList().Count;
  270. month += examSts.FindAll(f => f.createTime >= monthS && f.createTime <= monthE).ToList().Count;
  271. }
  272. if (tempAll.Count > 0)
  273. {
  274. //for (int i = 0; i < leveryDay.Count; i++)
  275. //{
  276. // if (actStats.LastYear.Count == 366)
  277. // actStats.LastYear[i] = (double)tempAll.FindAll(f => (f.createTime >= leveryDay[i].start && f.createTime <= leveryDay[i].end)).Count;
  278. // else
  279. // actStats.LastYear.Add((double)tempAll.FindAll(f => (f.createTime >= leveryDay[i].start && f.createTime <= leveryDay[i].end)).Count);
  280. //}
  281. for (int i = 0; i < everyDay.Count; i++)
  282. {
  283. if (actStats.year.Count == 366)
  284. actStats.year[i] = tempAll.FindAll(f => (f.createTime >= everyDay[i].start && f.createTime <= everyDay[i].end)).Count;
  285. else
  286. actStats.year.Add(tempAll.FindAll(f => (f.createTime >= everyDay[i].start && f.createTime <= everyDay[i].end)).Count);
  287. }
  288. }
  289. //string lastDaySql = $"c.createTime >= {lastDayS} and c.createTime <= {lastdayE}";
  290. //string daySql = $"c.createTime >= {dayS} and c.createTime <= {dayE}";
  291. //string lastWeekSql = $"c.createTime >= {lastWeekS} and c.createTime <= {lastWeekE}";
  292. //string weekSql = $"c.createTime >= {weekS} and c.createTime <= {weekE}";
  293. //string lastTermSql = $"c.createTime >= {lastTermS} and c.createTime <= {lastTermE}";
  294. //string termSql = $"c.createTime >= {termS} and c.createTime <= {termE}";
  295. //foreach (var type in StaticValue.activityTypes)
  296. //{
  297. // switch (type)
  298. // {
  299. // case "Exam":
  300. // exam += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam'", code: $"Exam-{scId}");
  301. // lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {lastDaySql}", code: $"Exam-{scId}");
  302. // dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {daySql}", code: $"Exam-{scId}");
  303. // lastWeek+= await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {lastWeekSql}", code: $"Exam-{scId}");
  304. // week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {weekSql}", code: $"Exam-{scId}");
  305. // lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {lastTermSql}", code: $"Exam-{scId}");
  306. // term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {termSql}", code: $"Exam-{scId}");
  307. // break;
  308. // case "Survey":
  309. // survey += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey'", code: $"Survey-{scId}");
  310. // lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {lastDaySql}", code: $"Survey-{scId}");
  311. // dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {daySql}", code: $"Survey-{scId}");
  312. // lastWeek += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {lastWeekSql}", code: $"Survey-{scId}");
  313. // week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {weekSql}", code: $"Survey-{scId}");
  314. // lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {lastTermSql}", code: $"Survey-{scId}");
  315. // term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {termSql}", code: $"Survey-{scId}");
  316. // break;
  317. // case "Vote":
  318. // vote += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote'", code: $"Vote-{scId}");
  319. // lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {lastDaySql}", code: $"Vote-{scId}");
  320. // dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {daySql}", code: $"Vote-{scId}");
  321. // lastWeek += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {lastWeekSql}", code: $"Vote-{scId}");
  322. // week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {weekSql}", code: $"Vote-{scId}");
  323. // lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {lastTermSql}", code: $"Vote-{scId}");
  324. // term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {termSql}", code: $"Vote-{scId}");
  325. // break;
  326. // case "Homework":
  327. // homework += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework'", code: $"Homework-{scId}");
  328. // lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {lastDaySql}", code: $"Homework-{scId}");
  329. // dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {daySql}", code: $"Homework-{scId}");
  330. // lastWeek += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {lastWeekSql}", code: $"Homework-{scId}");
  331. // week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {weekSql}", code: $"Homework-{scId}");
  332. // lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {lastTermSql}", code: $"Homework-{scId}");
  333. // term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {termSql}", code: $"Homework-{scId}");
  334. // break;
  335. // }
  336. //}
  337. actStats.all = (exam + survey + vote + homework);
  338. actStats.exam = exam;
  339. actStats.survey = survey;
  340. actStats.vote = vote;
  341. actStats.homework = homework;
  342. actStats.lastDay = lastDay;
  343. actStats.dayCnt = dayCnt;
  344. actStats.lastWeek = lastWeek;
  345. actStats.week = week;
  346. actStats.lastTerm = lastTerm;
  347. actStats.term = term;
  348. actStats.lastMonth = lastMonth;
  349. actStats.month = month;
  350. actStats.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  351. return actStats;
  352. }
  353. /// <summary>
  354. /// 研修
  355. /// </summary>
  356. /// <param name="cosmosClient"></param>
  357. /// <param name="scId"></param>
  358. /// <returns></returns>
  359. public static async Task<StudyStats> GetStudyStats(CosmosClient cosmosClient, string scId)
  360. {
  361. StudyStats studyStats = new();
  362. List<AreaStudy> trains = new();
  363. School school = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemAsync<School>($"{scId}", new PartitionKey("Base"));
  364. //cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<string>(queryText:$"select value(c.areaId) from c where c.id='{scId}'", requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Base")});
  365. AreaSetting setting = null;
  366. var responSett = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{school.areaId}", new PartitionKey("AreaSetting"));
  367. if (responSett.StatusCode == System.Net.HttpStatusCode.OK)
  368. {
  369. using var fileJson = await JsonDocument.ParseAsync(responSett.Content);
  370. setting = fileJson.ToObject<AreaSetting>();
  371. }
  372. if (setting == null)
  373. {
  374. setting = new AreaSetting
  375. {
  376. allTime = 50,
  377. classTime = 5,
  378. submitTime = 15,
  379. onlineTime = 20,
  380. offlineTime = 10,
  381. lessonMinutes = 45,
  382. };
  383. }
  384. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIteratorSql<AreaStudy>(queryText: $"select c.id,c.code,c.totalTime,c.onlineTime,c.offlineTime,c.classTime,c.currency from c where c.pk='TeacherTrain' and c.school='{scId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"TeacherTrain-{scId}") }))
  385. {
  386. trains.Add(item);
  387. }
  388. studyStats.learnTime = trains.Select(s => s.totalTime).Sum();
  389. trains.ForEach(x =>
  390. {
  391. //线上研修
  392. if (x.onlineTime == 0) studyStats.online++; else if (x.onlineTime >= setting.onlineTime) studyStats.online++; else studyStats.online++;
  393. //线下研修
  394. if (x.offlineTime == 0) studyStats.offline++; else if (x.onlineTime >= setting.offlineTime) studyStats.offline++; else studyStats.offline++;
  395. //课堂实录
  396. if (x.classTime == 0) studyStats.classRoom++; else if (x.classTime >= setting.classTime) studyStats.classRoom++; else studyStats.classRoom++;
  397. //认证材料
  398. if (x.currency.submitTime == 0) studyStats.submit++; else if (x.currency.submitTime >= setting.submitTime) studyStats.submit++; else studyStats.submit++;
  399. if (x.currency.submitTime == 0 && x.classTime == 0 && x.offlineTime == 0 && x.onlineTime == 0) studyStats.notStarted++;
  400. else if (x.currency.submitTime >= setting.submitTime && x.classTime >= setting.classTime && x.onlineTime >= setting.offlineTime && x.onlineTime >= setting.onlineTime) studyStats.finish++;
  401. else studyStats.ongoing++;
  402. });
  403. return studyStats;
  404. }
  405. }
  406. }