using Azure.Cosmos; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Threading.Tasks; using TEAMModelBI.Tool; using TEAMModelBI.Tool.Context; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Models; namespace TEAMModelBI.Controllers.BISchool { [Route("Lesson")] [ApiController] public class LessonController : ControllerBase { private readonly AzureCosmosFactory _azureCosmos; private readonly AzureStorageFactory _azureStorage; private readonly AzureRedisFactory _azureRedis; public LessonController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis) { _azureCosmos = azureCosmos; _azureStorage = azureStorage; _azureRedis = azureRedis; } /// /// 历史记录读取 /// /// /// [HttpPost("get-alllesson")] public async Task GetAllLessonRecords(JsonElement jsonElement) { jsonElement.TryGetProperty("site", out JsonElement site); var cosmosClient = _azureCosmos.GetCosmosClient(); if ($"{site}".Equals(BIConst.GlobalSite)) cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite); List scLesson = new(); List tchLesson = new(); await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS,"School").GetItemQueryIterator(queryText: $"select value(c) from c where c.pk='LessonRecord'",requestOptions:new QueryRequestOptions() { })) { scLesson.Add(item); } await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator(queryText: $"select value(c) from c where c.pk='LessonRecord'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("LessonRecord") })) { tchLesson.Add(item); } //scLesson.ForEach(item => { TimeHelper.GetDateTime(item.startTime).Hour }) List lessCnts = scLesson.Select(item => new LessCnt { hour = TimeHelper.GetDateTime(item.startTime).ToString("yyyyMMddHH"), cat = item.id, upload = item.upload, schoolId = item.school }).ToList(); List openCnt = lessCnts.FindAll(s => s.upload == 0).ToList(); List lessCnt = lessCnts.FindAll(s => s.upload == 1).ToList(); var openL = openCnt.GroupBy(g => g.hour).Select(s => new { key = s.Key, cnt = s.Count() }).ToList(); var lessL = lessCnt.GroupBy(g => g.hour).Select(s => new { key = s.Key, cnt = s.Count() }).ToList(); //openL.ForEach(f=> { }) return Ok(new { state = 200, openL, lessL, openCnt, lessCnt, lessCnts }); } public record LessCnt { public string hour { get; set; } public string schoolId { get; set; } public string cat { get; set; } public int upload { get; set; } } } }