BIHttpTrigger.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }
  96. }