123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728 |
- using Azure.Cosmos;
- using MathNet.Numerics.LinearAlgebra.Double;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using StackExchange.Redis;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.Json;
- using System.Threading;
- using System.Threading.Tasks;
- using TEAMModelBI.Tool;
- using TEAMModelBI.Tool.Context;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
- using TEAMModelOS.SDK.Models.Cosmos.BI.BITable;
- using TEAMModelOS.SDK.Models.Service.BI;
- namespace TEAMModelBI.Controllers.BISchool
- {
- [Route("Lesson")]
- [ApiController]
- public class LessonController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly AzureStorageFactory _azureStorage;
- private readonly AzureRedisFactory _azureRedis;
- private readonly DingDing _dingDing;
- public LessonController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- _azureRedis = azureRedis;
- _dingDing = dingDing;
- }
- [HttpPost("get-less")]
- public async Task<IActionResult> GetLess(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("school", out JsonElement school)) return BadRequest();
- var table = _azureStorage.GetCloudTableClient().GetTableReference("BIStats");
- var tableSql = $" PartitionKey eq 'LessonHour' and school eq '{school}'";
- //lambda 表达式排序
- var lessStats = await table.QueryWhereString<LessStats>(tableSql.ToString());
- DenseMatrix openDense = null;
- DenseMatrix lessDense = null;
- List<List<double>> tempOpens = new();
- List<List<double>> tempLesss = new();
- foreach (var item in lessStats)
- {
- if (item.open != null)
- {
- List<double> opens = item.open.Split(',').ToList().ConvertAll(c => Convert.ToDouble(c));
- tempOpens.Add(opens);
- }
- if(item.lesson != null)
- {
- List<double> lesss = item.lesson.Split(',').ToList().ConvertAll(c => Convert.ToDouble(c));
- tempLesss.Add(lesss);
- }
- }
- openDense = DenseMatrix.OfColumns(tempOpens);
- lessDense = DenseMatrix.OfColumns(tempLesss);
- return Ok(new { state = 200 , openDense ,lessDense});
- }
- /// <summary>
- /// 测试 通过时间戳保存统计类型接口
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [HttpPost("set-bilessonstats")]
- public async Task<IActionResult> SetBILeesonStats(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("unix", out JsonElement unix)) return BadRequest();
- if (!jsonElement.TryGetProperty("num", out JsonElement num)) return BadRequest();
- jsonElement.TryGetProperty("schoolId", out JsonElement schoolId);
- jsonElement.TryGetProperty("type", out JsonElement type);
- jsonElement.TryGetProperty("unixs", out JsonElement unixs);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- DateTime expireYear = DateTime.UtcNow.AddYears(1); //一个月到期
- var UUID = Guid.NewGuid().ToString();
- List<long> unixsT = unixs.ToObject<List<long>>();
- RedisValue token = Environment.MachineName;
- foreach (var item in unixsT)
- {
- //await SetBILeesonStats(cosmosClient, item, num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
- //await BILeeson.SetCosmosDBStats(cosmosClient, _azureRedis, _dingDing, item, num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
- await BILeeson.SetTableStats(_azureStorage, _azureRedis, _dingDing, item, num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
- }
- //await BILeeson.SetBILeesonStats(cosmosClient, _azureRedis, unix.GetInt64(), num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
- //await SetBILeesonStats(cosmosClient, unix.GetInt64(), num.GetInt32(), type: -1, schoolId: schoolId.GetString());
- return Ok(new { state = 200 });
- }
- /// <summary>
- /// 通过时间戳保存统计类型
- /// </summary>
- /// <param name="cosmosClient">连接字符串</param>
- /// <param name="unix">时间戳</param>
- /// <param name="num">加减数量,可为负数</param>
- /// <param name="type">课例类型,0开课 1 课例</param>
- /// <param name="schoolId">学校ID,可不传</param>
- /// <returns></returns>
- public static async Task SetBILeesonStats(CosmosClient cosmosClient, long unix, int num, int type = 0, string schoolId = null)
- {
- //Monitor.TryEnter(unix); //锁对象
- SemaphoreSlim slimlock = new(1, 1); //对可同时访问资源或资源池的线程数加以限制
- await slimlock.WaitAsync();
- DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeMilliseconds(unix);
- int year, month, day, hour, days;
- year = dateTime.Year;
- month = dateTime.Month;
- day = dateTime.Day;
- hour = dateTime.Hour;
- string hourId = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
- var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- days = dateTime.DayOfYear;
- double[] daDays = new double[yearDays];
- daDays[days] = num;
- List<double> yDays = new(daDays);
- double[] daHours = new double[23];
- daHours[hour] = num;
- List<double> yHours = new(daHours);
- var openRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{year}", new PartitionKey("LessonYear"));
- if (openRes.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(openRes.ContentStream);
- LessonStats lessonYear = json.ToObject<LessonStats>();
- if (type == 0)
- {
- if (lessonYear.open.Count == 0)
- lessonYear.open = yDays;
- else lessonYear.open[days] = lessonYear.open[days] + num;
- }
- else if (type == 1)
- {
- if (lessonYear.lesson.Count == 0)
- lessonYear.lesson = yDays;
- else lessonYear.lesson[days] = lessonYear.lesson[days] + num;
- }
- await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYear, lessonYear.id, new PartitionKey($"{lessonYear.code}"));
- }
- else
- {
- LessonStats lessonYear = new();
- lessonYear.id = $"{year}";
- lessonYear.code = "LessonYear";
- lessonYear.pk = "LessonYear";
- if (type == 0)
- lessonYear.open = yDays;
- else if (type == 1)
- lessonYear.lesson = yDays;
- await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYear, new PartitionKey($"{lessonYear.code}"));
- }
- var lessRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{hourId}", new PartitionKey("LessonHour"));
- if (lessRes.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(lessRes.ContentStream);
- LessonStats lessonHour = json.ToObject<LessonStats>();
- if (type == 0)
- {
- if (lessonHour.open.Count == 0)
- lessonHour.open = yHours;
- else lessonHour.open[hour] = lessonHour.open[hour] + num;
- }
- else if (type == 1)
- {
- if (lessonHour.lesson.Count == 0)
- lessonHour.lesson = yHours;
- else lessonHour.lesson[hour] = lessonHour.lesson[hour] + num;
- }
- await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonHour, lessonHour.id, new PartitionKey($"LessonHour"));
- }
- else
- {
- LessonStats lessonHour = new();
- lessonHour.id = $"{hourId}";
- lessonHour.code = "LessonHour";
- lessonHour.pk = "LessonHour";
- if (type == 0)
- lessonHour.open = yDays;
- else if (type == 1)
- lessonHour.lesson = yDays;
- await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonHour, new PartitionKey("LessonHour"));
- }
- //学校统计
- if (!string.IsNullOrEmpty(schoolId))
- {
- var openResSc = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{year}", new PartitionKey($"LessonYear-{schoolId}"));
- if (openResSc.Status == 200)
- {
- using var jsonSc = await JsonDocument.ParseAsync(openResSc.ContentStream);
- LessonStats lessonYearSc = jsonSc.ToObject<LessonStats>();
- if (type == 0)
- if (lessonYearSc.open.Count == 0)
- lessonYearSc.open = yDays;
- else lessonYearSc.open[days] = lessonYearSc.open[days] + num;
- else if (type == 1)
- {
- if (lessonYearSc.lesson.Count == 0)
- lessonYearSc.lesson = yDays;
- else lessonYearSc.lesson[days] = lessonYearSc.lesson[days] + num;
- }
- await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYearSc, lessonYearSc.id, new PartitionKey($"LessonYear-{schoolId}"));
- }
- else
- {
- LessonStats lessonYearSc = new();
- lessonYearSc.id = $"{year}";
- lessonYearSc.code = $"LessonYear-{schoolId}";
- lessonYearSc.pk = "LessonYear";
- if (type == 0)
- lessonYearSc.open = yDays;
- else if (type == 1)
- lessonYearSc.lesson = yDays;
- await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYearSc, new PartitionKey($"LessonYear-{schoolId}"));
- }
- var lessResSc = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{hourId}", new PartitionKey($"LessonHour-{schoolId}"));
- if (lessResSc.Status == 200)
- {
- using var jsonSc = await JsonDocument.ParseAsync(lessResSc.ContentStream);
- LessonStats lessonHourSc = jsonSc.ToObject<LessonStats>();
- if (type == 0)
- if (lessonHourSc.open.Count == 0)
- lessonHourSc.open = yDays;
- else lessonHourSc.open[days] = lessonHourSc.open[days] + num;
- else if (type == 1)
- {
- if (lessonHourSc.lesson.Count == 0)
- lessonHourSc.lesson = yHours;
- else lessonHourSc.lesson[hour] = lessonHourSc.lesson[hour] + num;
- }
- await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonHourSc, lessonHourSc.id, new PartitionKey($"LessonHour-{schoolId}"));
- }
- else
- {
- LessonStats lessonYearSc = new();
- lessonYearSc.id = $"{hourId}";
- lessonYearSc.code = $"LessonHour-{schoolId}";
- lessonYearSc.pk = "LessonHour";
- if (type == 0)
- lessonYearSc.open = yHours;
- else if (type == 1)
- lessonYearSc.lesson = yHours;
- await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYearSc, new PartitionKey($"LessonHour-{schoolId}"));
- }
- }
- slimlock.Release();
- //Monitor.Enter(unix);
- }
- /// <summary>
- /// 历史记录读取,并统计存储
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [HttpPost("get-alllesson")]
- public async Task<IActionResult> GetAllLessonRecords(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("site", out JsonElement site);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- if ($"{site}".Equals(BIConst.Global))
- cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- List<LessonRecord> allLesson = new();
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonRecord>(queryText: $"select value(c) from c where c.pk='LessonRecord'", requestOptions: new QueryRequestOptions() { }))
- {
- allLesson.Add(item);
- }
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<LessonRecord>(queryText: $"select value(c) from c where c.pk='LessonRecord'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("LessonRecord") }))
- {
- allLesson.Add(item);
- }
- (List<LessonStats> lessYears, List<LessonStats> lessHours) = await AllLessonStats(allLesson, _azureRedis);
- foreach (var itemY in lessYears)
- {
- var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(itemY.id, new PartitionKey($"{itemY.code}"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- LessonStats lessonYear = json.ToObject<LessonStats>();
- lessonYear.open = itemY.open;
- lessonYear.lesson = itemY.lesson;
- await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYear, lessonYear.id, new PartitionKey($"{lessonYear.code}"));
- }
- else
- await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(itemY, new PartitionKey($"{itemY.code}"));
- }
- foreach (var itemH in lessHours)
- {
- var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(itemH.id, new PartitionKey($"{itemH.code}"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- LessonStats lessonHour = json.ToObject<LessonStats>();
- lessonHour.open = itemH.open;
- lessonHour.lesson = itemH.lesson;
- await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonHour, lessonHour.id, new PartitionKey($"{lessonHour.code}"));
- }
- else
- await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(itemH, new PartitionKey($"{itemH.code}"));
- }
- return Ok(new { state = 200, ycnt = lessYears.Count, hcnt = lessHours.Count, lessYears, lessHours });
- }
- #region 课例历史记录统计
- /// <summary>
- /// 统计所有课例
- /// </summary>
- /// <param name="lessRecs"></param>
- /// <returns></returns>
- public static async Task<(List<LessonStats> lessonYears, List<LessonStats> lessonHours)> AllLessonStats(List<LessonRecord> lessRecs, AzureRedisFactory azureRedis)
- {
- //统计所有
- List<LessCnt> allLess = lessRecs.Select(item => new LessCnt { hour = TimeHelper.GetDateTime(item.startTime).ToString("yyyyMMddHH"), cat = item.id, upload = item.upload, schoolId = item.school }).ToList();
- var openL = allLess.FindAll(f => f.upload == 0).GroupBy(g => g.hour).Select(s => new AllLess { key = s.Key, cnt = s.Count() }).ToList();
- var lessL = allLess.FindAll(s => s.upload == 1).GroupBy(g => g.hour).Select(s => new AllLess { key = s.Key, cnt = s.Count() }).ToList();
- List<LessonStats> lessYears = new();
- List<LessonStats> lessHours = new();
- //统计开课记录
- openL.ForEach(x =>
- {
- var (year, month, day, days, hour) = TimeHelper.GetDateTime(x.key);
- string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
- LessonStats finHour = lessHours.Find(f => f.id.Equals(id));
- if (finHour != null)
- {
- if (finHour.open.Count == 0)
- {
- double[] da = new double[23];
- da[hour] = x.cnt;
- List<double> tempDays = new(da);
- finHour.open = tempDays;
- }
- else finHour.open[hour] = x.cnt;
- azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"BILesson:All:Open:{finHour.id}", $"{hour}", x.cnt);//一天24小时课例数 有上传 base.josn 小时为单位
- }
- else
- {
- //小时统计
- LessonStats lessonHour = new();
- lessonHour.id = id;
- lessonHour.code = "LessonHour";
- lessonHour.pk = "LessonHour";
- double[] daHours = new double[23];
- daHours[hour] = x.cnt;
- List<double> hourDays = new(daHours);
- lessonHour.open = hourDays;
- //lessonHour.ttl = 24 * 60 * 60 * 8; //设置过期时间 8天后自动删除
- lessHours.Add(lessonHour);
- }
- LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}"));
- if (findLess != null)
- {
- if (findLess.open.Count == 0)
- {
- var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- double[] da = new double[yearDays];
- da[days] = x.cnt;
- List<double> tempDays = new(da);
- findLess.open = tempDays;
- }
- else findLess.open[days] = x.cnt;
- }
- else
- {
- LessonStats lessonYear = new();
- lessonYear.id = $"{year}";
- lessonYear.code = "LessonYear";
- lessonYear.pk = "LessonYear";
- var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- double[] daDays = new double[yearDays];
- daDays[days] = x.cnt;
- List<double> yDays = new(daDays);
- lessonYear.open = yDays;
- lessYears.Add(lessonYear);
- }
- });
- //统计课例
- lessL.ForEach(x =>
- {
- var (year, month, day, days, hour) = TimeHelper.GetDateTime(x.key);
- string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
- LessonStats finHour = lessHours.Find(f => f.id.Equals(id));
- if (finHour != null)
- {
- if (finHour.lesson.Count == 0)
- {
- double[] da = new double[23];
- da[hour] = x.cnt;
- List<double> tempDays = new(da);
- finHour.lesson = tempDays;
- }
- else finHour.lesson[hour] = x.cnt;
- }
- else
- {
- //小时统计
- LessonStats lessonHour = new();
- lessonHour.id = id;
- lessonHour.code = "LessonHour";
- lessonHour.pk = "LessonHour";
- double[] daHours = new double[23];
- daHours[hour] = x.cnt;
- List<double> hourDays = new(daHours);
- lessonHour.lesson = hourDays;
- lessHours.Add(lessonHour);
- }
- LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}"));
- if (findLess != null)
- {
- if (findLess.lesson.Count == 0)
- {
- var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- double[] da = new double[yearDays];
- da[days] = x.cnt;
- List<double> tempDays = new(da);
- findLess.lesson = tempDays;
- }
- else findLess.lesson[days] = x.cnt;
- }
- else
- {
- LessonStats lessonYear = new();
- lessonYear.id = $"{year}";
- lessonYear.code = "LessonYear";
- lessonYear.pk = "LessonYear";
- var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- double[] da = new double[yearDays];
- da[days] = x.cnt;
- List<double> tempDays = new(da);
- lessonYear.lesson = tempDays;
- lessYears.Add(lessonYear);
- }
- });
- //统计学校课例
- var scLess = lessRecs.Select(item => new LessCnt { hour = TimeHelper.GetDateTime(item.startTime).ToString("yyyyMMddHH"), cat = item.id, upload = item.upload, schoolId = item.school }).GroupBy(g => g.schoolId).Select(s => new ScLess { key = s.Key, lessCnt = s.ToList() }).ToList();
- foreach (var itemLess in scLess)
- {
- var openSCL = itemLess.lessCnt.FindAll(f => f.upload == 0).GroupBy(g => g.hour).Select(s => new ScLesson { key = s.Key, schoolId = itemLess.key, cnt = s.Count() }).ToList();
- var lessSCL = itemLess.lessCnt.FindAll(f => f.upload == 1).GroupBy(g => g.hour).Select(s => new ScLesson { key = s.Key, schoolId = itemLess.key, cnt = s.Count() }).ToList();
- openSCL.ForEach(f =>
- {
- if (!string.IsNullOrEmpty(f.schoolId))
- {
- var (year, month, day, days, hour) = TimeHelper.GetDateTime(f.key);
- string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
- string codeHour = $"LessonHour-{f.schoolId}";
- LessonStats finHour = lessHours.Find(f => f.id.Equals(id) && f.code.Equals(codeHour));
- if (finHour != null)
- {
- if (finHour.open.Count == 0)
- {
- double[] da = new double[23];
- da[hour] = f.cnt;
- List<double> tempDays = new(da);
- finHour.open = tempDays;
- }
- else finHour.open[hour] = f.cnt;
- }
- else
- {
- //小时统计
- LessonStats lessonHour = new();
- lessonHour.id = id;
- lessonHour.code = codeHour;
- lessonHour.pk = "LessonHour";
- double[] daHours = new double[23];
- daHours[hour] = f.cnt;
- List<double> hourDays = new(daHours);
- lessonHour.open = hourDays;
- //lessonHour.ttl = 24 * 60 * 60 * 8; //设置过期时间
- lessHours.Add(lessonHour);
- }
- string codeYear = $"LessonYear-{f.schoolId}";
- LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}") && f.code.Equals(codeYear));
- if (findLess != null)
- {
- if (findLess.open.Count == 0)
- {
- var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- double[] da = new double[yearDays];
- da[days] = f.cnt;
- List<double> tempDays = new(da);
- findLess.open = tempDays;
- }
- else findLess.open[days] = f.cnt;
- }
- else
- {
- LessonStats lessonYear = new();
- lessonYear.id = $"{year}";
- lessonYear.code = codeYear;
- lessonYear.code = "LessonYear";
- var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- double[] daDays = new double[yearDays];
- daDays[days] = f.cnt;
- List<double> yDays = new(daDays);
- lessonYear.open = yDays;
- lessYears.Add(lessonYear);
- }
- }
- });
- lessSCL.ForEach(x =>
- {
- if (!string.IsNullOrEmpty(x.schoolId))
- {
- if (!string.IsNullOrEmpty(x.schoolId))
- {
- var (year, month, day, days, hour) = TimeHelper.GetDateTime(x.key);
- string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
- string codeHour = $"LessonHour-{x.schoolId}";
- LessonStats finHour = lessHours.Find(f => f.id.Equals(id) && f.code.Equals(codeHour));
- if (finHour != null)
- {
- if (finHour.lesson.Count == 0)
- {
- double[] da = new double[23];
- da[hour] = x.cnt;
- List<double> tempDays = new(da);
- finHour.lesson = tempDays;
- }
- else finHour.lesson[hour] = x.cnt;
- }
- else
- {
- //小时统计
- LessonStats lessonHour = new();
- lessonHour.id = id;
- lessonHour.code = codeHour;
- lessonHour.pk = "LessonHour";
- double[] daHours = new double[23];
- daHours[hour] = x.cnt;
- List<double> hourDays = new(daHours);
- lessonHour.lesson = hourDays;
- lessHours.Add(lessonHour);
- }
- string codeYear = $"LessonYear-{x.schoolId}";
- LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}") && f.code.Equals(codeYear));
- if (findLess != null)
- {
- if (findLess.lesson.Count == 0)
- {
- var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- double[] da = new double[yearDays];
- da[days] = x.cnt;
- List<double> tempDays = new(da);
- findLess.lesson = tempDays;
- }
- else findLess.lesson[days] = x.cnt;
- }
- else
- {
- LessonStats lessonYear = new();
- lessonYear.id = $"{year}";
- lessonYear.code = codeYear;
- lessonYear.pk = "LessonYear";
- var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- double[] da = new double[yearDays];
- da[days] = x.cnt;
- List<double> tempDays = new(da);
- lessonYear.lesson = tempDays;
- lessYears.Add(lessonYear);
- }
- }
- }
- });
- };
- //openL.ForEach(x => {
- // var (year, days, hour) = TimeHelper.GetDateTime(x.key);
- // LessonYear findLess = lessYears.Find(f => f.id.Equals($"{year}"));
- // if (findLess != null)
- // {
- // if (findLess.openYear.Count == 0)
- // {
- // var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- // double[] da = new double[yearDays];
- // da[days] = x.cnt;
- // List<double> tempDays = new(da);
- // findLess.openYear = tempDays;
- // }
- // else findLess.openYear[days] = x.cnt;
- // }
- // else
- // {
- // LessonYear lessonYear = new();
- // lessonYear.id = $"{year}";
- // lessonYear.code = "LessonYear";
- // var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- // double[] da = new double[yearDays];
- // da[days] = x.cnt;
- // List<double> tempDays = new(da);
- // lessonYear.openYear = tempDays;
- // lessYears.Add(lessonYear);
- // }
- //});
- //lessL.ForEach(x => {
- // var (year, days, hour) = TimeHelper.GetDateTime(x.key);
- // LessonYear findLess = lessYears.Find(f => f.id.Equals($"{year}"));
- // if (findLess != null)
- // {
- // if (findLess.lessonYear.Count == 0)
- // {
- // var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- // double[] da = new double[yearDays];
- // da[days] = x.cnt;
- // List<double> tempDays = new(da);
- // findLess.lessonYear = tempDays;
- // }
- // else findLess.lessonYear[days] = x.cnt;
- // }
- // else
- // {
- // LessonYear lessonYear = new();
- // lessonYear.id = $"{year}";
- // lessonYear.code = "LessonYear";
- // var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
- // double[] da = new double[yearDays];
- // da[days] = x.cnt;
- // List<double> tempDays = new(da);
- // lessonYear.lessonYear = tempDays;
- // lessYears.Add(lessonYear);
- // }
- //});
- return (lessYears, lessHours);
- }
- /// <summary>
- /// 统计学校课例
- /// </summary>
- public record ScLesson
- {
- public string key { get; set; }
- public string schoolId { get; set; }
- public int cnt { get; set; }
- }
- /// <summary>
- /// 统计学校课例详细信息
- /// </summary>
- public record ScLess
- {
- public string key { get; set; }
- public List<LessCnt> lessCnt { get; set; }
- }
- /// <summary>
- /// 统计所有课例
- /// </summary>
- public record AllLess
- {
- public string key { get; set; }
- public int cnt { get; set; }
- }
- /// <summary>
- /// 统计所有课例详细
- /// </summary>
- public record LessCnt
- {
- public string hour { get; set; }
- public string schoolId { get; set; }
- public string cat { get; set; }
- public int upload { get; set; }
- }
- #endregion
- }
- }
|