12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364 |
- 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 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)
- {
- if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
- //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();
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<AreaSchools>(queryText: $"select c.id,c.name,c.picture from c where c.areaId='{areaId}'", 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("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 c.id from c");
- if (!string.IsNullOrEmpty($"{areaId}"))
- {
- sqlTxt.Append($" where c.areaId='{areaId}'");
- }
- List<string> schools = await CommonFind.FindSchoolIds(cosmosClient, sqlTxt.ToString(), "Base");
- foreach (var sId in schools)
- {
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<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").GetItemQueryIterator<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").GetItemQueryIterator<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 });
- }
- /// <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.ContentStream);
- 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.ContentStream);
- 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.ContentStream);
- 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").GetItemQueryIterator<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").GetItemQueryIterator<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.ContentStream);
- 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").GetItemQueryIterator<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").GetItemQueryIterator<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 c.id from c");
- if (!string.IsNullOrEmpty($"{areaId}"))
- {
- scSqlTxt.Append($" where c.areaId='{areaId}'");
- }
- schools = await CommonFind.FindSchoolIds(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").GetItemQueryIterator<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").GetItemQueryIterator<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").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- 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.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>
- [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").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
- 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; }
- }
- }
- }
|