123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Text.Json;
- using System.Threading.Tasks;
- using Azure.Cosmos;
- using Microsoft.Azure.Functions.Worker;
- using Microsoft.Azure.Functions.Worker.Http;
- using StackExchange.Redis;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models.Cosmos.BI;
- using TEAMModelOS.SDK.Models.Service.BIStatsWay;
- namespace TEAMModelOS.FunctionV4.HttpTrigger
- {
- public class BIHttpTrigger
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly DingDing _dingDing;
- private readonly AzureStorageFactory _azureStorage;
- private readonly AzureRedisFactory _azureRedis;
- private readonly HttpClient _httpClient;
- public BIHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, HttpClient httpClient)
- {
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _azureStorage = azureStorage;
- _azureRedis = azureRedis;
- _httpClient = httpClient;
- }
- /// <summary>
- /// 统计学校的信息
- /// </summary>
- /// <param name="req"></param>
- /// <returns></returns>
- [Function("stats-sc-info")]
- public async Task<HttpResponseData> StatsSchoolInfo([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestData req)
- {
- var response = req.CreateResponse(HttpStatusCode.OK);
- try
- {
- string scId = null;
- var cosmosClient = _azureCosmos.GetCosmosClient();
- string data = await new StreamReader(req.Body).ReadToEndAsync();
- var json = JsonDocument.Parse(data).RootElement;
- if (json.TryGetProperty("schoolId", out JsonElement _schoolId))
- {
- scId = $"{_schoolId}";
- }
- if (string.IsNullOrEmpty(scId))
- {
- return response;
- }
- bool locKey = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Train:Statistics:Lock:{scId}");
- if (!locKey)
- {
- await _azureRedis.GetRedisClient(8).SetAddAsync($"Train:Statistics:Lock:{scId}", new RedisValue(scId));
- DateTime minutes = DateTime.UtcNow.AddMinutes(15); //15分钟
- await _azureRedis.GetRedisClient(8).KeyExpireAsync($"Train:Statistics:Lock:{scId}", minutes);
- bool isExist = true;
- StatsInfo statsInfo = new();
- var scDataStats = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{scId}", new PartitionKey("Stats"));
- if (scDataStats.Status == 200)
- {
- using var fileJson = await JsonDocument.ParseAsync(scDataStats.ContentStream);
- statsInfo = fileJson.ToObject<StatsInfo>();
- }
- else
- isExist = false;
- statsInfo = await SchoolStatsWay.upSingleSc(cosmosClient, scId);
- statsInfo.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- if (isExist)
- statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<StatsInfo>(statsInfo, statsInfo.id, new PartitionKey("Stats"));
- else
- statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<StatsInfo>(statsInfo, new PartitionKey("Stats"));
- }
- return response;
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
- }
|