123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- 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;
- }
- /// <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);
- 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<StatsInfo>();
- }
- 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, statsInfo.id, new PartitionKey("Stats"));
- else
- statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<StatsInfo>(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;
- }
- /// <summary>
- /// 处理传过来的信息加入到统计信息中
- /// </summary>
- /// <param name="req"></param>
- /// <returns></returns>
- [Function("set-scstats-type")]
- public async Task<HttpResponseData> 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;
- }
- }
- }
|