ActivityStatsWay.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using Microsoft.Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using TEAMModelOS.SDK.Models.Cosmos.BI;
  8. using TEAMModelOS.SDK.Models.Service.BI;
  9. using TEAMModelOS.SDK.DI;
  10. namespace TEAMModelOS.SDK.Models.Service.BIStatsWay
  11. {
  12. public static class ActivityStatsWay
  13. {
  14. /// <summary>
  15. /// 统计学校活动
  16. /// </summary>
  17. /// <param name="cosmosClient"></param>
  18. /// <param name="scId"></param>
  19. /// <returns></returns>
  20. public static async Task<ActivityStats> GetSchoolAll(CosmosClient cosmosClient, string scId,int year = 0)
  21. {
  22. ActivityStats actStats = new();
  23. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  24. if (year < dateTime.Year)
  25. dateTime = new(year, 12, 31, 23, 59, 59, TimeSpan.Zero);
  26. var (lastDayS, lastdayE) = TimeHelper.GetStartOrEnd(dateTime.AddDays(-1)); //昨天开始时间
  27. var (dayS, dayE) = TimeHelper.GetStartOrEnd(dateTime); //今天开始时间
  28. var (lastWeekS, lastWeekE) = TimeHelper.GetStartOrEnd(dateTime, "lastweek"); //计算上周开始/结束时间
  29. var (weekS, weekE) = TimeHelper.GetStartOrEnd(dateTime, "week"); //计算本周开始/结束时间
  30. var (lastTermS, lastTermE) = TimeHelper.GetStartOrEnd(dateTime, "lastterm"); //计算上学期开始/结束时间
  31. var (termS, termE) = TimeHelper.GetStartOrEnd(dateTime, "term"); //计算本学期开始/结束时间
  32. var (lastMonthS, lastMonthE) = TimeHelper.GetStartOrEnd(dateTime, "lastMonth"); //上月开始/结束时间
  33. var (monthS, monthE) = TimeHelper.GetStartOrEnd(dateTime, "month"); //本月开始/结束时间
  34. var (lastYearS, lastYearE) = TimeHelper.GetStartOrEnd(DateTimeOffset.Parse($"{dateTime.Year - 1}-1-1"), "year"); //计算去年开始/结束时间
  35. var (yearS, yearE) = TimeHelper.GetStartOrEnd(dateTime, "year"); //计算今年开始/结束时间
  36. string currSql = "select value(count(c.id)) from c ";
  37. string yearSql = $"select c.id,c.code,c.createTime from c where c.createTime >= {lastYearS} and c.createTime <= {yearE}";
  38. int exam = 0;
  39. int survey = 0;
  40. int vote = 0;
  41. int homework = 0;
  42. int lastDay = 0;
  43. int dayCnt = 0;
  44. int lastWeek = 0;
  45. int week = 0;
  46. int lastTerm = 0;
  47. int term = 0;
  48. int lastMonth = 0;
  49. int month = 0;
  50. DateTimeOffset lyearDay = new(year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, TimeSpan.Zero);
  51. List<StartEndTime> everyDay = TimeHelper.GetYearEveryDay(lyearDay);
  52. List<ArtRelStats> examSts = new();
  53. List<ArtRelStats> surveySts = new();
  54. List<ArtRelStats> voteSts = new();
  55. List<ArtRelStats> homeworkSts = new();
  56. foreach (var artType in StaticValue.activityTypes)
  57. {
  58. switch (artType)
  59. {
  60. case "Exam":
  61. exam = await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"Exam-{scId}", currSql);
  62. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryIteratorSql<ArtRelStats>(queryText: yearSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{scId}") }))
  63. {
  64. examSts.Add(item);
  65. }
  66. break;
  67. case "Survey":
  68. survey = await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"Survey-{scId}", currSql);
  69. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryIteratorSql<ArtRelStats>(queryText: yearSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Survey-{scId}") }))
  70. {
  71. surveySts.Add(item);
  72. }
  73. break;
  74. case "Vote":
  75. vote = await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"Vote-{scId}", currSql);
  76. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryIteratorSql<ArtRelStats>(queryText: yearSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Vote-{scId}") }))
  77. {
  78. voteSts.Add(item);
  79. }
  80. break;
  81. case "Homework":
  82. homework = await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"Homework-{scId}", currSql);
  83. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryIteratorSql<ArtRelStats>(queryText: yearSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Homework-{scId}") }))
  84. {
  85. homeworkSts.Add(item);
  86. }
  87. break;
  88. }
  89. }
  90. List<ArtRelStats> tempAll = new();
  91. if (examSts.Count > 0)
  92. {
  93. tempAll.AddRange(examSts);
  94. lastDay += examSts.FindAll(f => f.createTime >= lastDayS && f.createTime <= lastdayE).ToList().Count;
  95. dayCnt += examSts.FindAll(f => f.createTime >= dayS && f.createTime <= dayE).ToList().Count;
  96. lastWeek += examSts.FindAll(f => f.createTime >= lastWeekS && f.createTime <= lastWeekE).ToList().Count;
  97. week += examSts.FindAll(f => f.createTime >= weekS && f.createTime <= weekE).ToList().Count;
  98. lastTerm += examSts.FindAll(f => f.createTime >= lastTermS && f.createTime <= lastTermE).ToList().Count;
  99. term += examSts.FindAll(f => f.createTime >= termS && f.createTime <= termE).ToList().Count;
  100. lastMonth += examSts.FindAll(f => f.createTime >= lastMonthS && f.createTime <= lastMonthE).ToList().Count;
  101. month += examSts.FindAll(f => f.createTime >= monthS && f.createTime <= monthE).ToList().Count;
  102. }
  103. if (surveySts.Count > 0)
  104. {
  105. tempAll.AddRange(surveySts);
  106. lastDay += surveySts.FindAll(f => f.createTime >= lastDayS && f.createTime <= lastdayE).ToList().Count;
  107. dayCnt += surveySts.FindAll(f => f.createTime >= dayS && f.createTime <= dayE).ToList().Count;
  108. lastWeek += surveySts.FindAll(f => f.createTime >= lastWeekS && f.createTime <= lastWeekE).ToList().Count;
  109. week += surveySts.FindAll(f => f.createTime >= weekS && f.createTime <= weekE).ToList().Count;
  110. lastTerm += surveySts.FindAll(f => f.createTime >= lastTermS && f.createTime <= lastTermE).ToList().Count;
  111. term += surveySts.FindAll(f => f.createTime >= termS && f.createTime <= termE).ToList().Count;
  112. lastMonth += examSts.FindAll(f => f.createTime >= lastMonthS && f.createTime <= lastMonthE).ToList().Count;
  113. month += examSts.FindAll(f => f.createTime >= monthS && f.createTime <= monthE).ToList().Count;
  114. }
  115. if (voteSts.Count > 0)
  116. {
  117. tempAll.AddRange(voteSts);
  118. lastDay += voteSts.FindAll(f => f.createTime >= lastDayS && f.createTime <= lastdayE).ToList().Count;
  119. dayCnt += voteSts.FindAll(f => f.createTime >= dayS && f.createTime <= dayE).ToList().Count;
  120. lastWeek += voteSts.FindAll(f => f.createTime >= lastWeekS && f.createTime <= lastWeekE).ToList().Count;
  121. week += voteSts.FindAll(f => f.createTime >= weekS && f.createTime <= weekE).ToList().Count;
  122. lastTerm += voteSts.FindAll(f => f.createTime >= lastTermS && f.createTime <= lastTermE).ToList().Count;
  123. term += voteSts.FindAll(f => f.createTime >= termS && f.createTime <= termE).ToList().Count;
  124. lastMonth += examSts.FindAll(f => f.createTime >= lastMonthS && f.createTime <= lastMonthE).ToList().Count;
  125. month += examSts.FindAll(f => f.createTime >= monthS && f.createTime <= monthE).ToList().Count;
  126. }
  127. if (homeworkSts.Count > 0)
  128. {
  129. tempAll.AddRange(homeworkSts);
  130. lastDay += homeworkSts.FindAll(f => f.createTime >= lastDayS && f.createTime <= lastdayE).ToList().Count;
  131. dayCnt += homeworkSts.FindAll(f => f.createTime >= dayS && f.createTime <= dayE).ToList().Count;
  132. lastWeek += homeworkSts.FindAll(f => f.createTime >= lastWeekS && f.createTime <= lastWeekE).ToList().Count;
  133. week += homeworkSts.FindAll(f => f.createTime >= weekS && f.createTime <= weekE).ToList().Count;
  134. lastTerm += homeworkSts.FindAll(f => f.createTime >= lastTermS && f.createTime <= lastTermE).ToList().Count;
  135. term += homeworkSts.FindAll(f => f.createTime >= termS && f.createTime <= termE).ToList().Count;
  136. lastMonth += examSts.FindAll(f => f.createTime >= lastMonthS && f.createTime <= lastMonthE).ToList().Count;
  137. month += examSts.FindAll(f => f.createTime >= monthS && f.createTime <= monthE).ToList().Count;
  138. }
  139. List<double> actYear = new();
  140. if (tempAll.Count > 0)
  141. {
  142. for (int i = 0; i < everyDay.Count; i++)
  143. {
  144. actYear.Add(tempAll.FindAll(f => (f.createTime >= everyDay[i].start && f.createTime <= everyDay[i].end)).Count);
  145. }
  146. if (actYear.Count < 366)
  147. actYear.Add(0);
  148. }
  149. if (actYear.Count > 0)
  150. {
  151. actStats.year = actYear;
  152. }
  153. //string lastDaySql = $"c.createTime >= {lastDayS} and c.createTime <= {lastdayE}";
  154. //string daySql = $"c.createTime >= {dayS} and c.createTime <= {dayE}";
  155. //string lastWeekSql = $"c.createTime >= {lastWeekS} and c.createTime <= {lastWeekE}";
  156. //string weekSql = $"c.createTime >= {weekS} and c.createTime <= {weekE}";
  157. //string lastTermSql = $"c.createTime >= {lastTermS} and c.createTime <= {lastTermE}";
  158. //string termSql = $"c.createTime >= {termS} and c.createTime <= {termE}";
  159. //foreach (var type in StaticValue.activityTypes)
  160. //{
  161. // switch (type)
  162. // {
  163. // case "Exam":
  164. // exam += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam'", code: $"Exam-{scId}");
  165. // lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {lastDaySql}", code: $"Exam-{scId}");
  166. // dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {daySql}", code: $"Exam-{scId}");
  167. // lastWeek += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {lastWeekSql}", code: $"Exam-{scId}");
  168. // week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {weekSql}", code: $"Exam-{scId}");
  169. // lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {lastTermSql}", code: $"Exam-{scId}");
  170. // term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {termSql}", code: $"Exam-{scId}");
  171. // break;
  172. // case "Survey":
  173. // survey += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey'", code: $"Survey-{scId}");
  174. // lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {lastDaySql}", code: $"Survey-{scId}");
  175. // dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {daySql}", code: $"Survey-{scId}");
  176. // lastWeek += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {lastWeekSql}", code: $"Survey-{scId}");
  177. // week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {weekSql}", code: $"Survey-{scId}");
  178. // lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {lastTermSql}", code: $"Survey-{scId}");
  179. // term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {termSql}", code: $"Survey-{scId}");
  180. // break;
  181. // case "Vote":
  182. // vote += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote'", code: $"Vote-{scId}");
  183. // lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {lastDaySql}", code: $"Vote-{scId}");
  184. // dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {daySql}", code: $"Vote-{scId}");
  185. // lastWeek += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {lastWeekSql}", code: $"Vote-{scId}");
  186. // week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {weekSql}", code: $"Vote-{scId}");
  187. // lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {lastTermSql}", code: $"Vote-{scId}");
  188. // term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {termSql}", code: $"Vote-{scId}");
  189. // break;
  190. // case "Homework":
  191. // homework += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework'", code: $"Homework-{scId}");
  192. // lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {lastDaySql}", code: $"Homework-{scId}");
  193. // dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {daySql}", code: $"Homework-{scId}");
  194. // lastWeek += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {lastWeekSql}", code: $"Homework-{scId}");
  195. // week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {weekSql}", code: $"Homework-{scId}");
  196. // lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {lastTermSql}", code: $"Homework-{scId}");
  197. // term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {termSql}", code: $"Homework-{scId}");
  198. // break;
  199. // }
  200. //}
  201. actStats.all = (exam + survey + vote + homework);
  202. actStats.exam = exam;
  203. actStats.survey = survey;
  204. actStats.vote = vote;
  205. actStats.homework = homework;
  206. actStats.lastDay = lastDay;
  207. actStats.dayCnt = dayCnt;
  208. actStats.lastWeek = lastWeek;
  209. actStats.week = week;
  210. actStats.lastTerm = lastTerm;
  211. actStats.term = term;
  212. actStats.lastMonth = lastMonth;
  213. actStats.month = month;
  214. actStats.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  215. return actStats;
  216. }
  217. }
  218. }