using System; using System.Collections.Generic; using System.Dynamic; using System.IO; using System.Net; using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; using Azure.Cosmos; using Azure.Storage.Blobs; using Microsoft.AspNetCore.Http; 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; } /// /// 统计学校的信息 /// /// /// [Function("stats-sc-info")] public async Task StatsSchoolInfo([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestData req) { var response = req.CreateResponse(HttpStatusCode.OK); dynamic jsondata = new ExpandoObject(); try { string scId = null; var cosmosClient = _azureCosmos.GetCosmosClient(); var tableClient = _azureStorage.GetCloudTableClient(); var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public"); string data = await new StreamReader(req.Body).ReadToEndAsync(); var json = JsonDocument.Parse(data).RootElement; jsondata = json; 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(); } else isExist = false; statsInfo = await SchoolStatsWay.GetSingleSc(cosmosClient, scId); statsInfo.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); if (isExist) statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(statsInfo, statsInfo.id, new PartitionKey("Stats")); else statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(statsInfo, new PartitionKey("Stats")); } //保存操作记录 await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "trigger-schoolStats", $"触发更新统计数据库", _dingDing); } catch (Exception ex) { await _dingDing.SendBotMsg($"stats-sc-info,{ex.Message}\n{ex.StackTrace}\n{jsondata.ToJsonString()}", GroupNames.成都开发測試群組); } return response; } /// /// 处理传过来的信息加入到统计信息中 /// /// /// [Function("set-scstats-type")] public async Task SetSchoolStatsType([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestData req) { var response = req.CreateResponse(HttpStatusCode.OK); dynamic jsondata = new ExpandoObject(); try { string scId = null,type = null; var cosmosClient = _azureCosmos.GetCosmosClient(); var tableClient = _azureStorage.GetCloudTableClient(); var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public"); string data = await new StreamReader(req.Body).ReadToEndAsync(); var json = JsonDocument.Parse(data).RootElement; jsondata = json; if (json.TryGetProperty("schoolId", out JsonElement _schoolId)) { scId = $"{_schoolId}"; } if (json.TryGetProperty("type", out JsonElement _type)) { type = $"{_type}"; } if (string.IsNullOrEmpty(scId) && string.IsNullOrEmpty(scId)) { await _dingDing.SendBotMsg($"set-scstats-type, {req.Body};转换后:{json}", GroupNames.成都开发測試群組); return response; } switch ($"{type}") { case "Exam": break; case "Survey": break; case "Vote": break; case "Homework": break; case "Less": break; } } catch (Exception ex) { await _dingDing.SendBotMsg($"stats-sc-info,{ex.Message}\n{ex.StackTrace}\n{jsondata.ToJsonString()}", GroupNames.成都开发測試群組); } return response; } } }