123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.DI;
- using Microsoft.Extensions.Options;
- using Microsoft.Azure.Cosmos;
- using System.Text.Json;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- using TEAMModelOS.SDK.Models;
- using TEAMModelBI.Models;
- using TEAMModelOS.SDK.Extension;
- using System.Text;
- using TEAMModelBI.Tool;
- using MathNet.Numerics.LinearAlgebra.Double;
- using TEAMModelBI.Tool.CosmosBank;
- using TEAMModelOS.SDK.Context.BI;
- using TEAMModelOS.SDK.Models.Service.BI;
- using TEAMModelOS.SDK.Context.Constant;
- using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
- using TEAMModelOS.SDK.Models.Cosmos.BI.BITable;
- using StackExchange.Redis;
- using System.Threading;
- namespace TEAMModelBI.Controllers.Census
- {
- [Route("lesson")]
- [ApiController]
- public class LessonSticsController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly AzureStorageFactory _azureStorage;
- private readonly AzureRedisFactory _azureRedis;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- public LessonSticsController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureFactory, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureFactory;
- _azureRedis = azureRedis;
- _dingDing = dingDing;
- _option = option?.Value;
- }
- /// <summary>
- /// 依据区级ID分析该去所有学校课例 //已对接
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-schoolan")]
- public async Task<IActionResult> GetSchoolsAn(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("areaId", out JsonElement areaId);
- //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
- var (lWeekS, lWeekE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week");
- var (monthS, monthE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "month");
- var cosmosClient = _azureCosmos.GetCosmosClient();
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- List<AreaSchools> areaSchools = new();
- StringBuilder scSql = new("select c.id,c.name,c.picture from c ");
- if (!string.IsNullOrEmpty($"{areaId}"))
- scSql.Append($" where c.areaId='{areaId}'");
- else
- scSql.Append($" where c.areaId = null or c.areaId = ''");
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<AreaSchools>(queryText: scSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base")}))
- {
- areaSchools.Add(item);
- }
- if (areaSchools.Count > 0)
- {
- foreach (var item in areaSchools)
- {
- item.allCnt = await CommonFind.GetSqlValueCount(cosmosClient,"School",$"select value(count(c.id)) from c ",$"LessonRecord-{item.id}");
- item.weekCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(count(c.id)) from c where c.startTime >= {lWeekS} and c.startTime <= {lWeekE}", $"LessonRecord-{item.id}");
- item.monthCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(count(c.id)) from c where c.startTime >= {monthS} and c.startTime <= {monthE}", $"LessonRecord-{item.id}");
- }
- areaSchools = areaSchools.Where(w => (w.allCnt != 0 || w.weekCnt != 0 || w.monthCnt != 0)).ToList();
- }
- return Ok(new { state = RespondCode.Ok, areaSchools });
- }
- /// <summary>
- /// 统计区级一年每周的课例数据趋势 //已对接
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-weekcount")]
- public async Task<IActionResult> GetWeekCount(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("areaId", out JsonElement areaId);
- jsonElement.TryGetProperty("isJoin", out JsonElement isJoin);
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- //Dictionary<int, double> weeks = new();
- List<double> weeks = new();
- var cosmosClient = _azureCosmos.GetCosmosClient();
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- int year = DateTimeOffset.UtcNow.Year;
- int dayOfweek = (int)DateTimeOffset.Parse($"{year}-1-1").DayOfWeek;
- int currentTime = DateTimeOffset.UtcNow.DayOfYear / 7 + 1;
- int currentTime1 = DateTimeOffset.UtcNow.DayOfYear / 7;
- var sqlTxts = "select value(c) from c";
- List<LessonCount> scount = new();
- List<LessonCount> tcount = new();
- StringBuilder sqlTxt = new("select value(c.id) from c");
- if (!string.IsNullOrEmpty($"{areaId}"))
- sqlTxt.Append($" where c.areaId='{areaId}'");
- else
- {
- if ($"{isJoin}".Equals("noJoin"))
- sqlTxt.Append($" where c.areaId = null or c.areaId = ''");
- }
- List<string> schools = await CommonFind.GetValueSingle(cosmosClient, "School",sqlTxt.ToString(), "Base");
- foreach (var sId in schools)
- {
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<LessonCount>(queryText: sqlTxts, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{sId}-{year}") }))
- {
- scount.Add(item);
- }
- }
- List<string> teacIds = await CommonFind.FindRolesId(cosmosClient, schools);
- foreach (var tId in teacIds)
- {
- var sqlTxtt = $"select value(c) from c where c.id='{tId}'";
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIteratorSql<LessonCount>(queryText: sqlTxtt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{year}") }))
- {
- tcount.Add(item);
- }
- }
- int days = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? days = 366 : days = 365;
- List<List<double>> lessons = new();
- if (scount.Count > 0)
- {
- foreach (LessonCount item in scount)
- {
- lessons.Add(item.beginCount);
- }
- }
- if (tcount.Count > 0)
- {
- foreach (LessonCount item in tcount)
- {
- lessons.Add(item.beginCount);
- }
- }
- if (lessons.Count > 0)
- {
- var bmatrix = DenseMatrix.OfColumns(lessons);
- //开学第一周周内开课
- if (dayOfweek == 0)
- {
- dayOfweek = 7;
- }
- //第一周多少天
- var dd = 7 - dayOfweek + 1;
- //一年有几周
- int sweeks = days / 7;
- //查询天数
- int dayYear = 0;
- if (currentTime > 0)
- {
- for (int i = 0; i <= currentTime; i++)
- {
- if (i == 0)
- {
- var bsum = bmatrix.SubMatrix(dayYear, dd, 0, bmatrix.ColumnCount).ColumnSums().Sum();
- dayYear += dd;
- //weeks.Add(i, bsum);
- weeks.Add(bsum);
- }
- else
- {
- var bsum = bmatrix.SubMatrix(dayYear, 7, 0, bmatrix.ColumnCount).ColumnSums().Sum();
- dayYear += 7;
- //weeks.Add(i, bsum);
- weeks.Add(bsum);
- }
- }
- }
- //最后一周是否有余
- int stary = days - dayYear;
- if (stary > 0 && stary < 7)
- {
- var bsum = bmatrix.SubMatrix(dayYear, stary - 1, 0, bmatrix.ColumnCount).ColumnSums().Sum();
- //weeks.Add((sweeks + 1), bsum);
- weeks.Add(bsum);
- }
- }
- return Ok(new { state = 200, weeks });
- }
- /// <summary>
- /// 统计所有区级的课例和活动 //已对接
- /// </summary>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-allarea")]
- public async Task<IActionResult> GetAllArea(JsonElement jsonElement)
- {
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- List<AllAreaInfo> areaInfos = new();
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIteratorSql<AllAreaInfo>(queryText: $"select c.id,c.name,c.standard,c.standardName from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
- {
- areaInfos.Add(item);
- }
- foreach (var area in areaInfos)
- {
- string comSql = $"select value(c.id) from c where c.areaId='{area.id}'";
- List<string> scIds = await CommonFind.GetValueSingle(cosmosClient, "School", comSql, "Base");
- int tLessCnt = 0;
- int tActCnt = 0;
- string Sql = $"select value(c.id) from c where c.areaId='{area.id}'";
- if (scIds.Count > 0)
- {
- comSql = BICommonWay.ManyScSql("c.school", scIds);
- string lesSql = $"select value(count(c.id)) from c where c.pk='LessonRecord' and {comSql}";
- tLessCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, lesSql);
- comSql = BICommonWay.ManyScSql(" and c.school", scIds);
- tActCnt = await ActivityWay.GetCnt(cosmosClient, condSql: comSql);
- }
- area.lessCnt = tLessCnt;
- area.actCnt = tActCnt;
- area.allCnt = tLessCnt + tActCnt;
- }
- areaInfos = areaInfos.Where(w => w.lessCnt != 0 && w.actCnt != 0).ToList();
- return Ok(new { state = 200, areaInfos = areaInfos.OrderByDescending(ob => ob.allCnt).ToList() });
- }
- /// <summary>
- /// 查询课例数量
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-count")]
- public async Task<IActionResult> GetCount(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
- if (!jsonElement.TryGetProperty("term", out JsonElement term)) return BadRequest();
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- var (start, end) = TimeHelper.GetTermStartOrEnd(DateTime.Now);
- object totals = new();
- StringBuilder sqlTxt = new($"select value(COUNT(c.id)) from c where c.pk='LessonRecord'");
- if (!string.IsNullOrEmpty($"{tmdId}"))
- {
- List<SchoolLen> schoolLens = new();
- List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
- foreach (var itemId in schoolIds)
- {
- School school = new();
- var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.Content);
- school = json.ToObject<School>();
- }
- SchoolLen schoolLen = new()
- {
- id = itemId,
- name =
- school.name != null ? school.name : itemId
- };
- //string sqlTxt = $"SELECT COUNT(c.id) AS totals FROM c WHERE c.code='LessonRecord-{itemId}'";
- sqlTxt.Append($" WHERE c.code='LessonRecord-{itemId}'");
- if (bool.Parse($"{term}") == true)
- {
- sqlTxt.Append($" and c.startTime >= {start} and c.startTime <= {end}");
- }
- schoolLen.totals = await CommonFind.GetSqlValueCount(cosmosClient, "School", sqlTxt.ToString());
- schoolLens.Add(schoolLen);
- }
- totals = schoolLens;
- }
- else
- {
- sqlTxt.Append($" where c.pk='LessonRecord'");
- if (bool.Parse($"{term}") == true)
- {
- sqlTxt.Append($" and c.startTime >= {start} and c.startTime <= {end}");
- }
- totals = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, sqlTxt.ToString());
- }
- return Ok(new { state = 200, totals });
- }
- /// <summary>
- /// 管家所关联的课例数据
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-assiist")]
- public async Task<IActionResult> GetAssiist(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- List<SchoolLen> schoolLens = new();
- List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
- foreach (var itemId in schoolIds)
- {
- School school = new();
- var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.Content);
- school = json.ToObject<School>();
- }
- SchoolLen schoolLen = new() { id = itemId, name = school != null ? school.name : itemId };
- string sqlTxt = $"SELECT value(COUNT(c.id)) FROM c WHERE c.code='LessonRecord-{itemId}'";
- schoolLen.totals = await CommonFind.GetSqlValueCount(cosmosClient, "School", sqlTxt);
- schoolLens.Add(schoolLen);
- }
- return Ok(new { state = 200, schoolLens });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI, {_option.Location} /lesson/get-assiist {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 统计所有的课例数据
- /// </summary>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-diccount")]
- public async Task<IActionResult> GetDicCount(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- if (!string.IsNullOrEmpty($"{tmdId}"))
- {
- jsonElement.TryGetProperty("years", out JsonElement _years);
- int years = DateTime.UtcNow.Year;
- if (!string.IsNullOrEmpty($"{_years}"))
- {
- years = _years.GetInt32();
- }
- List<SchoolLen> schoolLens = new();
- List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
- foreach (string schoolId in schoolIds)
- {
- School school = new();
- var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(schoolId, new PartitionKey("Base"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.Content);
- school = json.ToObject<School>();
- }
- SchoolLen schoolLen = new() { id = schoolId, name = school != null ? school.name : schoolId };
- List<List<double>> begin = new();
- await foreach (var lcount in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<LessonCount>(queryText: "select value(c) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{schoolId}-{years}") }))
- {
- begin.Add(lcount.beginCount);
- }
- schoolLen.totals = (long)DenseMatrix.OfColumns(begin).ColumnSums().Sum();
- schoolLens.Add(schoolLen);
- }
- return Ok(new { state = 200, schoolLens });
- }
- else
- {
- List<List<double>> begin = new();
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<LessonCount>(queryText: "select value(c) from c where c.pk='LessonCount'", requestOptions: new QueryRequestOptions() { }))
- {
- begin.Add(item.beginCount);
- }
- var count = DenseMatrix.OfColumns(begin).ColumnSums().Sum();
- return Ok(new { state = 200, count });
- }
- }
- /// <summary>
- /// 顾问关联的学校统计本学期的课例
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [HttpPost("get-termcount")]
- public async Task<IActionResult> GetTermCount(JsonElement jsonElement)
- {
- if (jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) BadRequest();
- //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- List<SchoolLen> schoolLens = new();
- List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
- foreach (var scid in schoolIds)
- {
- School school = new();
- var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(scid, new PartitionKey("Base"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.Content);
- school = json.ToObject<School>();
- }
- SchoolLen schoolLen = new() { id = scid, name = !string.IsNullOrEmpty(school.name) ? school.name : scid };
- DateTimeOffset dateTime = DateTimeOffset.UtcNow;
- int year = (dateTime.Month <= 8 && dateTime.Month >= 3) ? dateTime.Year : dateTime.Year - 1;
- long stime = DateTimeOffset.Parse($"{year}-9-1").ToUnixTimeMilliseconds();
- //long etime = DateTimeOffset.Parse($"{year}-2-{((year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 29 : 28)}").ToUnixTimeMilliseconds();
- double totals = 0;
- var syear = DateTimeOffset.FromUnixTimeMilliseconds(stime).Year;
- var tyear = DateTimeOffset.UtcNow.Year;
- var tday = DateTimeOffset.UtcNow.DayOfYear;
- //今年多少天
- int tdays = (tyear % 4 == 0 && tyear % 100 != 0 || tyear % 400 == 0) ? 366 : 365;
- //去年多少天
- int pydays = (syear % 4 == 0 && syear % 100 != 0 || syear % 400 == 0) ? 366 : 365;
- List<LessonCount> scount = new();
- List<LessonCount> tcount = new();
- DenseMatrix dense = null;
- var queryClass = $"select value(c) from c ";
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<LessonCount>(queryText: queryClass, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{scid}-{syear}") }))
- {
- scount.Add(item);
- }
- if (tyear > syear)
- {
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<LessonCount>(queryText: queryClass, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{scid}-{tyear}") }))
- {
- tcount.Add(item);
- }
- if (tcount.Count > 0)
- {
- List<List<double>> be = new();
- foreach (var item in tcount)
- {
- be.Add(item.beginCount);
- }
- dense = DenseMatrix.OfColumns(be);
- }
- }
- if (scount.Count > 0)
- {
- List<List<double>> begin = new();
- foreach (LessonCount lesson in scount)
- {
- begin.Add(lesson.beginCount);
- }
- var matrix = DenseMatrix.OfColumns(begin);
- //求本学期
- var sdays = DateTimeOffset.FromUnixTimeMilliseconds(stime).DayOfYear;
- if (tday - sdays < 0)
- {
- //var tmatrix = DenseMatrix.OfColumns(scount.beginCount);
- //跨年后开始到本学期结束
- double endMonth = 0;
- if (null != dense)
- {
- endMonth = dense.SubMatrix(0, tday, 0, dense.ColumnCount).ColumnSums().Sum();
- }
- var startMonth = matrix.SubMatrix(sdays - 1, pydays - sdays, 0, matrix.ColumnCount).ColumnSums().Sum();
- totals = (endMonth + startMonth);
- }
- else
- {
- var allMonth = matrix.SubMatrix(sdays - 1, tday - sdays + 1, 0, matrix.ColumnCount).ColumnSums().Sum();
- totals = allMonth;
- }
- }
- schoolLen.totals = (long)totals;
- schoolLens.Add(schoolLen);
- }
- return Ok(new { state = 200, schoolLens });
- }
- /// <summary>
- /// 统计区级课例
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-areacount")]
- public async Task<IActionResult> GetAreaCount(JsonElement jsonElement)
- {
- if(!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- List<string> schools = new();
- var cosmosClient = _azureCosmos.GetCosmosClient();
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- StringBuilder scSqlTxt = new("select value(c.id) from c");
- if (!string.IsNullOrEmpty($"{areaId}"))
- {
- scSqlTxt.Append($" where c.areaId='{areaId}'");
- }
- schools = await CommonFind.FindScIds(cosmosClient, scSqlTxt.ToString(), "Base");
- //所有的课程记录
- List<LessonRecord> records = new();
- List<string> tecIds = new();
- foreach (var school in schools)
- {
- string sqlTxt = $"select value(c) from c where c.code='LessonRecord-{school}'";
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<LessonRecord>(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{school}") }))
- {
- records.Add(item);
- }
- }
- tecIds = await CommonFind.FindRolesId(cosmosClient, schools);
- foreach (var tecId in tecIds)
- {
- string sqlTxt = $"select value(c) from c where c.id='{tecId}'";
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIteratorSql<LessonRecord>(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord") }))
- {
- records.Add(item);
- }
- }
- int dayCount = 0;
- var (dayStart, dayEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow);
- records.ForEach(x => { if (x.startTime >= dayStart && x.startTime <= dayEnd) dayCount += 1; });
- int weekCount = 0;
- var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week");
- records.ForEach(x => { if (x.startTime >= weekStart && x.startTime <= weekEnd) weekCount += 1; });
- int monthCount = 0;
- var (monthStart, monthEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "month");
- records.ForEach(x => { if (x.startTime >= monthStart && x.startTime <= monthEnd) monthCount += 1; });
- int termCount = 0;
- var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term");
- records.ForEach(x => { if (x.startTime >= termStart && x.startTime <= termEnd) termCount += 1; });
- double teachCount = records.Where(r => r.tmdid != null).Where((x, i) => records.FindIndex(z => z.tmdid == x.tmdid) == i).ToList().Count;
- return Ok(new { state = 200, lessonCount = records.Count, teachCount, dayCount, weekCount, monthCount, termCount });
- }
- /// <summary>
- /// 依据课例Id获取课例详情 数据管理工具——查询工具
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-info")]
- public async Task<IActionResult> GetInfo(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("lessonId", out JsonElement lessonId)) return BadRequest();
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- List<object> lessons = new();
- string sqlTxt = $"select * from c where c.id='{lessonId}'";
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
- {
- using var json = await JsonDocument.ParseAsync(item.Content);
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- lessons.Add(obj.ToObject<object>());
- }
- }
- return Ok(new { state = 200, lessons });
- }
- /// <summary>
- /// 获取课例统计数据
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [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>
- [ProducesDefaultResponseType]
- [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 BILeesonService.SetTableStats(_azureStorage, _azureRedis, _dingDing, item, num.GetInt32(), type: type.GetInt32(), "school", 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.Content);
- 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.Content);
- 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.Content);
- 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.Content);
- 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>
- [ProducesDefaultResponseType]
- [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").GetItemQueryIteratorSql<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").GetItemQueryIteratorSql<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.Content);
- 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.Content);
- 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
- public record AreaSchools
- {
- public string id { get; set; }
- public string name { get; set; }
- public string picture { get; set; }
- public int allCnt { get; set; }
- public int weekCnt { get; set; }
- public int monthCnt { get; set; }
- }
- public record AllAreaInfo
- {
- public string id { get; set; }
- public string name { get; set; }
- public string standard { get; set; }
- public string standardName { get; set; }
- public int lessCnt { get; set; }
- public int actCnt { get; set; }
- public int allCnt { get; set; }
- }
- public record SchoolLen
- {
- public string id { get; set; }
- public string name { get; set;}
- public long totals { get; set; }
- }
- }
- }
|