ActivityStatsWay.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using 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. namespace TEAMModelOS.SDK.Models.Service.BIStatsWay
  10. {
  11. public static class ActivityStatsWay
  12. {
  13. /// <summary>
  14. /// 统计学校活动
  15. /// </summary>
  16. /// <param name="cosmosClient"></param>
  17. /// <param name="scId"></param>
  18. /// <returns></returns>
  19. public static async Task<ActivityStats> GetSchoolAll(CosmosClient cosmosClient, string scId)
  20. {
  21. ActivityStats actStats = new();
  22. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  23. var (lastDayS, lastdayE) = TimeHelper.GetStartOrEnd(dateTime.AddDays(-1)); //昨天开始时间
  24. var (dayS, dayE) = TimeHelper.GetStartOrEnd(dateTime); //今天开始时间
  25. var (lastWeekS, lastWeekE) = TimeHelper.GetStartOrEnd(dateTime, "lastweek"); //计算上周开始/结束时间
  26. var (weekS, weekE) = TimeHelper.GetStartOrEnd(dateTime, "week"); //计算本周开始/结束时间
  27. var (lastTermS, lastTermE) = TimeHelper.GetStartOrEnd(dateTime, "lastterm"); //计算上学期开始/结束时间
  28. var (termS, termE) = TimeHelper.GetStartOrEnd(dateTime, "term"); //计算本学期开始/结束时间
  29. string currSql = "select value(count(c.id)) from c ";
  30. int exam = 0;
  31. int survey = 0;
  32. int vote = 0;
  33. int homework = 0;
  34. int lastDay = 0;
  35. int dayCnt = 0;
  36. int lastWeek = 0;
  37. int week = 0;
  38. int lastTerm = 0;
  39. int term = 0;
  40. string lastDaySql = $"c.createTime >= {lastDayS} and c.createTime <= {lastdayE}";
  41. string daySql = $"c.createTime >= {dayS} and c.createTime <= {dayE}";
  42. string lastWeekSql = $"c.createTime >= {lastWeekS} and c.createTime <= {lastWeekE}";
  43. string weekSql = $"c.createTime >= {weekS} and c.createTime <= {weekE}";
  44. string lastTermSql = $"c.createTime >= {lastTermS} and c.createTime <= {lastTermE}";
  45. string termSql = $"c.createTime >= {termS} and c.createTime <= {termE}";
  46. foreach (var type in StaticValue.activityTypes)
  47. {
  48. switch (type)
  49. {
  50. case "Exam":
  51. exam += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam'", code: $"Exam-{scId}");
  52. lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {lastDaySql}", code: $"Exam-{scId}");
  53. dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {daySql}", code: $"Exam-{scId}");
  54. lastWeek += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {lastWeekSql}", code: $"Exam-{scId}");
  55. week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {weekSql}", code: $"Exam-{scId}");
  56. lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {lastTermSql}", code: $"Exam-{scId}");
  57. term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Exam' and {termSql}", code: $"Exam-{scId}");
  58. break;
  59. case "Survey":
  60. survey += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey'", code: $"Survey-{scId}");
  61. lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {lastDaySql}", code: $"Survey-{scId}");
  62. dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {daySql}", code: $"Survey-{scId}");
  63. lastWeek += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {lastWeekSql}", code: $"Survey-{scId}");
  64. week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {weekSql}", code: $"Survey-{scId}");
  65. lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {lastTermSql}", code: $"Survey-{scId}");
  66. term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Survey' and {termSql}", code: $"Survey-{scId}");
  67. break;
  68. case "Vote":
  69. vote += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote'", code: $"Vote-{scId}");
  70. lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {lastDaySql}", code: $"Vote-{scId}");
  71. dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {daySql}", code: $"Vote-{scId}");
  72. lastWeek += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {lastWeekSql}", code: $"Vote-{scId}");
  73. week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {weekSql}", code: $"Vote-{scId}");
  74. lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {lastTermSql}", code: $"Vote-{scId}");
  75. term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Vote' and {termSql}", code: $"Vote-{scId}");
  76. break;
  77. case "Homework":
  78. homework += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework'", code: $"Homework-{scId}");
  79. lastDay += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {lastDaySql}", code: $"Homework-{scId}");
  80. dayCnt += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {daySql}", code: $"Homework-{scId}");
  81. lastWeek += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {lastWeekSql}", code: $"Homework-{scId}");
  82. week += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {weekSql}", code: $"Homework-{scId}");
  83. lastTerm += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {lastTermSql}", code: $"Homework-{scId}");
  84. term += await JointlySingleQuery.GetValueInt(cosmosClient, "Common", $"{currSql} where c.pk='Homework' and {termSql}", code: $"Homework-{scId}");
  85. break;
  86. }
  87. }
  88. actStats.cnt = (exam + survey + vote + homework);
  89. actStats.exam = exam;
  90. actStats.survey = survey;
  91. actStats.vote = vote;
  92. actStats.homework = homework;
  93. actStats.lastDay = lastDay;
  94. actStats.dayCnt = dayCnt;
  95. actStats.lastWeek = lastWeek;
  96. actStats.week = week;
  97. actStats.lastTerm = lastTerm;
  98. actStats.term = term;
  99. return actStats;
  100. }
  101. }
  102. }