BIHttpTrigger.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using Azure.Cosmos;
  9. using Microsoft.Azure.Functions.Worker;
  10. using Microsoft.Azure.Functions.Worker.Http;
  11. using StackExchange.Redis;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.SDK.Models.Cosmos.BI;
  15. using TEAMModelOS.SDK.Models.Service.BIStatsWay;
  16. namespace TEAMModelOS.FunctionV4.HttpTrigger
  17. {
  18. public class BIHttpTrigger
  19. {
  20. private readonly AzureCosmosFactory _azureCosmos;
  21. private readonly DingDing _dingDing;
  22. private readonly AzureStorageFactory _azureStorage;
  23. private readonly AzureRedisFactory _azureRedis;
  24. private readonly HttpClient _httpClient;
  25. public BIHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, HttpClient httpClient)
  26. {
  27. _azureCosmos = azureCosmos;
  28. _dingDing = dingDing;
  29. _azureStorage = azureStorage;
  30. _azureRedis = azureRedis;
  31. _httpClient = httpClient;
  32. }
  33. /// <summary>
  34. /// 统计学校的信息
  35. /// </summary>
  36. /// <param name="req"></param>
  37. /// <returns></returns>
  38. [Function("stats-sc-info")]
  39. public async Task<HttpResponseData> StatsSchoolInfo([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestData req)
  40. {
  41. var response = req.CreateResponse(HttpStatusCode.OK);
  42. try
  43. {
  44. string scId = null;
  45. var cosmosClient = _azureCosmos.GetCosmosClient();
  46. string data = await new StreamReader(req.Body).ReadToEndAsync();
  47. var json = JsonDocument.Parse(data).RootElement;
  48. if (json.TryGetProperty("schoolId", out JsonElement _schoolId))
  49. {
  50. scId = $"{_schoolId}";
  51. }
  52. if (string.IsNullOrEmpty(scId))
  53. {
  54. return response;
  55. }
  56. bool locKey = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Train:Statistics:Lock:{scId}");
  57. if (!locKey)
  58. {
  59. await _azureRedis.GetRedisClient(8).SetAddAsync($"Train:Statistics:Lock:{scId}", new RedisValue(scId));
  60. DateTime minutes = DateTime.UtcNow.AddMinutes(15); //15分钟
  61. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"Train:Statistics:Lock:{scId}", minutes);
  62. bool isExist = true;
  63. StatsInfo statsInfo = new();
  64. var scDataStats = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{scId}", new PartitionKey("Stats"));
  65. if (scDataStats.Status == 200)
  66. {
  67. using var fileJson = await JsonDocument.ParseAsync(scDataStats.ContentStream);
  68. statsInfo = fileJson.ToObject<StatsInfo>();
  69. }
  70. else
  71. isExist = false;
  72. statsInfo = await SchoolStatsWay.upSingleSc(cosmosClient, scId);
  73. statsInfo.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  74. if (isExist)
  75. statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<StatsInfo>(statsInfo, statsInfo.id, new PartitionKey("Stats"));
  76. else
  77. statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<StatsInfo>(statsInfo, new PartitionKey("Stats"));
  78. }
  79. return response;
  80. }
  81. catch (Exception)
  82. {
  83. throw;
  84. }
  85. }
  86. }
  87. }