BIHttpTrigger.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Dynamic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. using Azure.Cosmos;
  10. using Azure.Storage.Blobs;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.Azure.Functions.Worker;
  13. using Microsoft.Azure.Functions.Worker.Http;
  14. using StackExchange.Redis;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Models.Cosmos.BI;
  18. using TEAMModelOS.SDK.Models.Service.BIStatsWay;
  19. namespace TEAMModelOS.FunctionV4.HttpTrigger
  20. {
  21. public class BIHttpTrigger
  22. {
  23. private readonly AzureCosmosFactory _azureCosmos;
  24. private readonly DingDing _dingDing;
  25. private readonly AzureStorageFactory _azureStorage;
  26. private readonly AzureRedisFactory _azureRedis;
  27. private readonly HttpClient _httpClient;
  28. public BIHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, HttpClient httpClient)
  29. {
  30. _azureCosmos = azureCosmos;
  31. _dingDing = dingDing;
  32. _azureStorage = azureStorage;
  33. _azureRedis = azureRedis;
  34. _httpClient = httpClient;
  35. }
  36. /// <summary>
  37. /// 统计学校的信息
  38. /// </summary>
  39. /// <param name="req"></param>
  40. /// <returns></returns>
  41. [Function("stats-sc-info")]
  42. public async Task<HttpResponseData> StatsSchoolInfo([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestData req)
  43. {
  44. var response = req.CreateResponse(HttpStatusCode.OK);
  45. dynamic jsondata = new ExpandoObject();
  46. try
  47. {
  48. string scId = null;
  49. var cosmosClient = _azureCosmos.GetCosmosClient();
  50. var tableClient = _azureStorage.GetCloudTableClient();
  51. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  52. string data = await new StreamReader(req.Body).ReadToEndAsync();
  53. var json = JsonDocument.Parse(data).RootElement;
  54. jsondata = json;
  55. if (json.TryGetProperty("schoolId", out JsonElement _schoolId))
  56. {
  57. scId = $"{_schoolId}";
  58. }
  59. if (string.IsNullOrEmpty(scId))
  60. {
  61. return response;
  62. }
  63. bool locKey = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Train:Statistics:Lock:{scId}");
  64. if (!locKey)
  65. {
  66. await _azureRedis.GetRedisClient(8).SetAddAsync($"Train:Statistics:Lock:{scId}", new RedisValue(scId));
  67. DateTime minutes = DateTime.UtcNow.AddMinutes(15); //15分钟
  68. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"Train:Statistics:Lock:{scId}", minutes);
  69. bool isExist = true;
  70. StatsInfo statsInfo = new();
  71. var scDataStats = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{scId}", new PartitionKey("Stats"));
  72. if (scDataStats.Status == 200)
  73. {
  74. using var fileJson = await JsonDocument.ParseAsync(scDataStats.ContentStream);
  75. statsInfo = fileJson.ToObject<StatsInfo>();
  76. }
  77. else
  78. isExist = false;
  79. statsInfo = await SchoolStatsWay.GetSingleSc(cosmosClient, scId);
  80. statsInfo.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  81. if (isExist)
  82. statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<StatsInfo>(statsInfo, statsInfo.id, new PartitionKey("Stats"));
  83. else
  84. statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<StatsInfo>(statsInfo, new PartitionKey("Stats"));
  85. }
  86. //保存操作记录
  87. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "trigger-schoolStats", $"触发更新统计数据库", _dingDing);
  88. }
  89. catch (Exception ex)
  90. {
  91. await _dingDing.SendBotMsg($"stats-sc-info,{ex.Message}\n{ex.StackTrace}\n{jsondata.ToJsonString()}", GroupNames.成都开发測試群組);
  92. }
  93. return response;
  94. }
  95. /// <summary>
  96. /// 处理传过来的信息加入到统计信息中
  97. /// </summary>
  98. /// <param name="req"></param>
  99. /// <returns></returns>
  100. [Function("set-scstats-type")]
  101. public async Task<HttpResponseData> SetSchoolStatsType([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestData req)
  102. {
  103. var response = req.CreateResponse(HttpStatusCode.OK);
  104. dynamic jsondata = new ExpandoObject();
  105. try
  106. {
  107. string scId = null,type = null;
  108. var cosmosClient = _azureCosmos.GetCosmosClient();
  109. var tableClient = _azureStorage.GetCloudTableClient();
  110. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  111. string data = await new StreamReader(req.Body).ReadToEndAsync();
  112. var json = JsonDocument.Parse(data).RootElement;
  113. jsondata = json;
  114. if (json.TryGetProperty("schoolId", out JsonElement _schoolId))
  115. {
  116. scId = $"{_schoolId}";
  117. }
  118. if (json.TryGetProperty("type", out JsonElement _type))
  119. {
  120. type = $"{_type}";
  121. }
  122. if (string.IsNullOrEmpty(scId) && string.IsNullOrEmpty(scId))
  123. {
  124. await _dingDing.SendBotMsg($"set-scstats-type, {req.Body};转换后:{json}", GroupNames.成都开发測試群組);
  125. return response;
  126. }
  127. switch ($"{type}")
  128. {
  129. case "Exam":
  130. break;
  131. case "Survey":
  132. break;
  133. case "Vote":
  134. break;
  135. case "Homework":
  136. break;
  137. case "Less":
  138. break;
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. await _dingDing.SendBotMsg($"stats-sc-info,{ex.Message}\n{ex.StackTrace}\n{jsondata.ToJsonString()}", GroupNames.成都开发測試群組);
  144. }
  145. return response;
  146. }
  147. }
  148. }