BIStatsNotice.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using Microsoft.Azure.Cosmos;
  2. using Microsoft.Extensions.Configuration;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. using TEAMModelOS.SDK.Models.Cosmos.BI.BICommon;
  12. using static TEAMModelOS.SDK.CoreAPIHttpService;
  13. namespace TEAMModelOS.SDK.Models.Service.BI
  14. {
  15. public static class BIStatsNotice
  16. {
  17. /// <summary>
  18. /// 汇总统计通知
  19. /// </summary>
  20. /// <param name="cosmosClient">连接</param>
  21. /// <param name="_dingDing">错误消息发送</param>
  22. /// <param name="type">统计类型</param>
  23. /// <param name="idType">账号类型 private:个人 school 学校</param>
  24. /// <param name="id">账号id</param>
  25. /// <param name="count">数量</param>
  26. /// <param name="careDate">创建时间</param>
  27. /// <returns></returns>
  28. public static async Task SetStatsNotice(CosmosClient cosmosClient, DingDing _dingDing, string type, string idType, string id, int count, long careDate = 0)
  29. {
  30. try
  31. {
  32. DateTimeOffset dateOff = DateTimeOffset.UtcNow;
  33. if (careDate != 0)
  34. {
  35. if (careDate > 1000000000000)
  36. dateOff = DateTimeOffset.FromUnixTimeMilliseconds(careDate);
  37. else
  38. dateOff = DateTimeOffset.FromUnixTimeSeconds(careDate);
  39. }
  40. string dt = dateOff.ToString("yyyyMMdd");
  41. StatsNotice statsNotice = new();
  42. var resStsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync($"{dt}-{id}", new PartitionKey("StatsNotice"));
  43. if (resStsInfo.Status == 200)
  44. {
  45. using var fileJson = await JsonDocument.ParseAsync(resStsInfo.Content);
  46. statsNotice = fileJson.ToObject<StatsNotice>();
  47. }
  48. else
  49. {
  50. statsNotice.id = $"{dt}-{id}";
  51. statsNotice.type = idType;
  52. statsNotice.rangeId = id;
  53. }
  54. switch (type)
  55. {
  56. case "Student":
  57. statsNotice.stuDayCnt += count;
  58. break;
  59. case "Classroom":
  60. statsNotice.classroomDayCnt += count;
  61. break;
  62. case "Weigh":
  63. statsNotice.weighDayCnt += count;
  64. break;
  65. case "Homework":
  66. statsNotice.homeworkDayCnt = count;
  67. break;
  68. }
  69. statsNotice.day = dt;
  70. statsNotice.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  71. statsNotice.ttl = 86400;
  72. if (resStsInfo.Status == 200)
  73. {
  74. statsNotice = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<StatsNotice>(statsNotice, statsNotice.id, new PartitionKey("StatsNotice"));
  75. }
  76. else
  77. {
  78. statsNotice = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync<StatsNotice>(statsNotice, new PartitionKey("StatsNotice"));
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. await _dingDing.SendBotMsg($"BI,{Environment.GetEnvironmentVariable("Option:Location")},SetStatsNotice(),参数: 统计类型:{type},类型私人/学校:{idType},id:{id}, 增/减数:{count},时间戳:{careDate}\n {ex.Message}\n {ex.StackTrace}", GroupNames.成都开发測試群組);
  84. }
  85. }
  86. /// <summary>
  87. /// 发送每日课程信息通知
  88. /// </summary>
  89. /// <param name="_coreAPIHttpService"></param>
  90. /// <param name="cosmosClient"></param>
  91. /// <param name="_dingDing"></param>
  92. /// <param name="_configuration"></param>
  93. /// <param name="jsonElement"></param>
  94. /// <returns></returns>
  95. public static async Task MonitorStatsNotice(CoreAPIHttpService _coreAPIHttpService, CosmosClient cosmosClient, DingDing _dingDing, IConfiguration _configuration,JsonElement jsonElement)
  96. {
  97. try
  98. {
  99. StatsNotice statsNotice = jsonElement.ToObject<StatsNotice>();
  100. DateTimeOffset dateOffset = DateTimeOffset.UtcNow;
  101. jsonElement.TryGetProperty("dateTime", out JsonElement dateTime);
  102. jsonElement.TryGetProperty("id", out JsonElement id);
  103. string dt = "";
  104. if (string.IsNullOrEmpty($"{dateTime}"))
  105. {
  106. dt = DateTimeOffset.UtcNow.ToString("yyyyMMdd");
  107. }
  108. else
  109. {
  110. dt = $"{dateTime}";
  111. }
  112. StringBuilder sqlTxt = new($"select value(c) from c where c.day='{dt}'");
  113. if (!string.IsNullOrEmpty($"{id}"))
  114. {
  115. sqlTxt.Append($" and c.rangeId = '{id}'");
  116. }
  117. List<StatsNotice> statsNotices = new();
  118. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<StatsNotice>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("StatsNotice") }))
  119. {
  120. statsNotices.Add(item);
  121. }
  122. foreach (var item in statsNotices)
  123. {
  124. List<string> tacherIds = new();
  125. if (item.type == "school")
  126. {
  127. await foreach (var tchId in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<string>(queryText: "SELECT c.id FROM c WHERE ARRAY_CONTAINS(c.roles, 'admin', true) AND c.pk = 'Teacher' AND c.status = 'join'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{item.rangeId}") }))
  128. {
  129. tacherIds.Add(tchId);
  130. }
  131. }
  132. else if (item.type == "private")
  133. {
  134. tacherIds.Add(item.rangeId);
  135. }
  136. NotifyData notifyData = new() { hubName = "hita", sender = "IES", tags = tacherIds.Select(s => $"{s}-hata").ToList(), title = "每日课程信息", body = $"每日课程信息如下:\n\r学生名单数:{item.stuDayCnt} \n\r 课堂记录数:{item.classroomDayCnt} \n\r 评量活动数:{item.weighDayCnt} \n\r 作业活动数:{item.homeworkDayCnt}", eventId = $"IES_send", data = item.ToJsonString() };
  137. _coreAPIHttpService.CoursePushNotify(notifyData, new Dictionary<string, object>(), $"{Environment.GetEnvironmentVariable("Option:Location")}", _configuration, _dingDing);
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. await _dingDing.SendBotMsg($"BI,{Environment.GetEnvironmentVariable("Option:Location")},MonitorStatsNotice(),参数:{jsonElement}\n {ex.StackTrace}", GroupNames.成都开发測試群組);
  143. }
  144. }
  145. }
  146. }