123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702 |
- using Azure.Cosmos;
- using MathNet.Numerics.LinearAlgebra.Double;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.Filter;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Helper.Common.DateTimeHelper;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- namespace TEAMModelOS.Controllers.Analysis
- {
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
- [Route("class/analysis")]
- [ApiController]
- public class ClassAnalysisController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly SnowflakeId _snowflakeId;
- private readonly AzureServiceBusFactory _serviceBus;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- private readonly AzureStorageFactory _azureStorage;
- private readonly CoreAPIHttpService _coreAPIHttpService;
- public ClassAnalysisController(CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
- IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage)
- {
- _azureCosmos = azureCosmos;
- _serviceBus = serviceBus;
- _snowflakeId = snowflakeId;
- _dingDing = dingDing;
- _option = option?.Value;
- _azureStorage = azureStorage;
- _coreAPIHttpService = coreAPIHttpService;
- }
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin")]
- [HttpPost("analysis-recod")]
- public async Task<IActionResult> analysisRecod(JsonElement requert)
- {
- try
- {
- //区级Id
- //if (!requert.TryGetProperty("time", out JsonElement time)) return BadRequest();
- if (!requert.TryGetProperty("stime", out JsonElement stime)) return BadRequest();
- if (!requert.TryGetProperty("etime", out JsonElement etime)) return BadRequest();
- if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
- if (!requert.TryGetProperty("periodId", out JsonElement pId)) return BadRequest();
- long st = stime.GetInt64();
- long et = etime.GetInt64();
- //获取当前学期所有的课程记录
- List<LessonRecord> records = new List<LessonRecord>();
- var client = _azureCosmos.GetCosmosClient();
- var queryClass = $"select value(c) from c where c.periodId = '{pId}'";
- string tId = string.Empty;
- if (requert.TryGetProperty("tmdId", out JsonElement tmdId))
- {
- queryClass = $"select value(c) from c where c.tmdid = '{tmdId}' and c.periodId = '{pId}'";
- tId = tmdId.GetString();
- }
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonRecord>(queryText: queryClass, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{code}") }))
- {
- records.Add(item);
- }
- if (records.Count > 0)
- {
- var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(code.GetString(), new PartitionKey($"Base"));
- School sc = new School();
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- sc = json.ToObject<School>();
- }
- List<(string name, double count)> rc = await getRecordCount(client, records, st, et, code.GetString(), tId);
- List<(int week, List<double> count)> tpc = await getTPCount(client, st, et, code.GetString(), tId);
- List<(string name, int count)> groups = getGroupCount(records);
- List<(string name, int count)> grades = getGradeCount(records);
- List<(string name, int count)> types = getTypeCount(records);
- List<double> exams = await getExamTypeCount(client, st, et, code.GetString());
- var subs = records.GroupBy(x => x.subjectId).Select(y => new { key = y.Key, count = y.ToList().Count }).ToList();
- List<string> groupIds = new List<string>();
- foreach (var item in groups)
- {
- groupIds.Add(item.name);
- }
- //var gs = sc.period.Where(s => s.id == pId.GetString()).Select(x => x.grades);
- (List<RMember> tchList, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, groupIds, code.GetString(), null);
- var total = rc.Select(x => new { x.name, value = x.count });
- var trend = tpc.Select(x => new { name = x.week, value = x.count });
- var classify_group = groups.Select(x => new { name = classLists.Where(c => c.id == x.name).Select(y => y.name), value = x.count });
- var classify_grade = grades.Select(x => new { x.name, value = x.count });
- var classify_type = types.Select(x => new { x.name, value = x.count });
- var classify_sub = subs.Select(x => new { name = x.key, value = x.count });
- return Ok(new { total, trend, classify_group, classify_grade, classify_type, classify_sub, exams });
- }
- else
- {
- return Ok(new { code = 404, msg = "暂无课程记录" });
- }
- // List<string> baseIds = await getId(client, id.GetString());
- /*List<object> studies = new();
- int days = DateTimeOffset.MaxValue.DayOfYear;
- int sday = (int)DateTimeOffset.UtcNow.DayOfWeek;
- var tts = DateTimeOffset.FromUnixTimeMilliseconds(st).DayOfYear;
- //double[,] bCount = new double[4,5];
- List<List<double>> bc = new List<List<double>>();
- double[] tc = new double[4];
- double[] tc2 = new double[4];
- List<double> list = new(tc);
- List<double> list2 = new(tc2);
- List<double> list3 = new(tc2);
- List<double> list4 = new(tc2);
- List<double> list5 = new(tc2);
- List<double> list6 = new(tc2);
- list[2] = 12;
- list[1] = 3;
- list[3] = 4;
- list2[2] = 7;
- list2[1] = 8;
- list2[3] = 6;
- bc.Add(list);
- bc.Add(list2);
- bc.Add(list3);
- bc.Add(list4);
- bc.Add(list5);
- bc.Add(list6);
- //double[,] d_matrix = new double[2, 3];
- var matrix5 = DenseMatrix.OfColumns(bc);
- //var matrix6 = DenseMatrix.OfArray(d_matrix);
- var cc = matrix5.ColumnSums().Sum();
- var dd = matrix5.RowSums();
- double[] ff = matrix5.Values;
- var total = matrix5.Values.Sum();
- var tday = DateTimeOffset.UtcNow.DayOfYear;
- int day1 = DateTimeOffset.FromUnixTimeMilliseconds(1640830249734).DayOfYear;
- int year1 = DateTimeOffset.FromUnixTimeMilliseconds(1640830249734).Year;
- int days1 = DateTimeHelper.getDays(year1);
- int ds = DateTimeHelper.getDays(2021);
- var dds = DateTimeOffset.FromUnixTimeMilliseconds(1640830249734).DayOfYear;
- int ssday = (int)DateTimeOffset.UtcNow.DayOfWeek;
- var d = (int)DateTimeOffset.FromUnixTimeMilliseconds(1639990956000).DayOfWeek;
- var asd = tday - d;
- var kk = matrix5.ColumnAbsoluteSums();
- var row = matrix5.Row(1, 2, 2);
- var submatrix = matrix5.SubMatrix(1, 2, 0, matrix5.ColumnCount);
- var hh = matrix5.EnumerateColumns();
- var aa = matrix5.EnumerateRows();
- LessonCount count = new LessonCount();
- count.id = Guid.NewGuid().ToString();
- count.code = "hbcn";*/
- //count.beginCount = bc;
- //await client.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(count, new PartitionKey($"{count.code}"));
- //Console.WriteLine(bCount);
- /* var query = $"select c.id,c.img,c.name,c.type,c.startTime,c.endTime,c.presenter,c.topic,c.address,c.owner,c.school from c ";
- await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Study-{id}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- studies.Add(obj.ToObject<object>());
- }
- }
- }*/
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},analysis/analysis-recod()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin")]
- [HttpPost("analysis-recod-teacher")]
- public async Task<IActionResult> analysisRecordTeacher(JsonElement requert)
- {
- try
- {
- //区级Id
- //if (!requert.TryGetProperty("time", out JsonElement time)) return BadRequest();
- if (!requert.TryGetProperty("stime", out JsonElement stime)) return BadRequest();
- if (!requert.TryGetProperty("etime", out JsonElement etime)) return BadRequest();
- if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
- //if (!requert.TryGetProperty("periodId", out JsonElement pId)) return BadRequest();
- long st = 0;
- long et = 0;
- try {
- stime.TryGetInt64(out st);
- etime.TryGetInt64(out et);
- }
- catch (Exception e) {
- //await _dingDing.SendBotMsg($"OS,{_option.Location},analysis/analysisRecordTeacher()\n{e.Message}{e.StackTrace}", GroupNames.成都开发測試群組);
- }
-
- /*if (null != stime.GetString() && null != etime.GetString()) {
- st = stime.GetInt64();
- et = etime.GetInt64();
- }*/
-
- //获取当前学期所有的课程记录
- List<LessonRecord> records = new List<LessonRecord>();
- var client = _azureCosmos.GetCosmosClient();
- var queryClass = $"select value(c) from c ";
- string tId = string.Empty;
- if (requert.TryGetProperty("tmdId", out JsonElement tmdId))
- {
- queryClass = $"select value(c) from c where c.tmdid = '{tmdId}'";
- tId = tmdId.GetString();
- }
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonRecord>(queryText: queryClass, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{code}") }))
- {
- records.Add(item);
- }
- if (records.Count > 0)
- {
- List<(string name, double count)> rc = await getRecordCount(client, records, st, et, code.GetString(), tId);
- List<(int week, List<double> count)> tpc = await getTPCount(client, st, et, code.GetString(), tId);
- var total = rc.Select(x => new { x.name, value = x.count });
- var trend = tpc.Select(x => new { name = x.week, value = x.count });
- var max = records.Select(x => x.tScore).Max();
- return Ok(new { total, trend, max });
- }
- else
- {
- return Ok(new { code = 404, msg = "暂无课程记录" });
- }
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},analysis/analysisRecordTeacher()\n{e.Message}{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- private List<(string name, int count)> getGroupCount(List<LessonRecord> records)
- {
- List<string> groupIds = new();
- List<(string name, int count)> grCount = new();
- foreach (var record in records)
- {
- foreach (string gId in record.groupIds)
- {
- if (!groupIds.Contains(gId))
- {
- groupIds.Add(gId);
- }
- }
- }
- foreach (var groupId in groupIds)
- {
- var c = records.Where(r => r.groupIds.Contains(groupId)).Count();
- grCount.Add((groupId, c));
- }
- return grCount;
- }
- private List<(string name, int count)> getGradeCount(List<LessonRecord> records)
- {
- List<string> grades = new();
- List<(string name, int count)> gCount = new();
- foreach (var record in records)
- {
- foreach (string gId in record.grade)
- {
- if (!grades.Contains(gId))
- {
- grades.Add(gId);
- }
- }
- }
- foreach (var gId in grades)
- {
- var c = records.Where(r => r.grade.Contains(gId)).Count();
- gCount.Add((gId, c));
- }
- return gCount;
- }
- private List<(string name, int count)> getTypeCount(List<LessonRecord> records)
- {
- List<string> tags = new();
- List<(string name, int count)> tyCount = new();
- foreach (var record in records)
- {
- foreach (string tag in record.category)
- {
- if (!tags.Contains(tag))
- {
- tags.Add(tag);
- }
- }
- }
- foreach (var tag in tags)
- {
- var c = records.Where(r => r.category.Contains(tag)).Count();
- tyCount.Add((tag, c));
- }
- return tyCount;
- }
- //课列趋势图
- private async Task<List<(int week, List<double> count)>> getTPCount(CosmosClient client, long stime, long etime, string code, string tId)
- {
- try
- {
- List<(int week, List<double>)> wks = new();
- var syear = DateTimeOffset.FromUnixTimeMilliseconds(stime).Year;
- //var eyear = DateTimeOffset.FromUnixTimeMilliseconds(etime).Year;
- var sday = DateTimeOffset.FromUnixTimeMilliseconds(stime).DayOfYear;
- //var eday = DateTimeOffset.FromUnixTimeMilliseconds(etime).DayOfYear;
- int dayOfweek = (int)DateTimeOffset.FromUnixTimeMilliseconds(stime).DayOfWeek;
- var tyear = DateTimeOffset.UtcNow.Year;
- //求开学年多少天
- int tdays = DateTimeHelper.getDays(syear);
- //如果跨年 求今年多少天
- //int pydays = DateTimeHelper.getDays(eyear);
- List<LessonCount> scount = new List<LessonCount>();
- List<LessonCount> tcount = new List<LessonCount>();
- var queryClass = $"select value(c) from c ";
- if (!string.IsNullOrEmpty(tId))
- {
- queryClass = $"select value(c) from c where c.id = '{tId}'";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<LessonCount>(
- queryText: queryClass,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{syear}") }))
- {
- scount.Add(item);
- }
- }
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonCount>(
- queryText: queryClass,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{code}-{syear}") }))
- {
- scount.Add(item);
- }
- List<List<double>> begin = new();
- List<List<double>> t = new();
- List<List<double>> p = new();
- List<List<double>> pt = new();
- if (scount.Count > 0)
- {
- foreach (LessonCount lesson in scount)
- {
- begin.Add(lesson.beginCount);
- t.Add(lesson.tCount);
- p.Add(lesson.pCount);
- pt.Add(lesson.ptCount);
- }
- var bmatrix = DenseMatrix.OfColumns(begin);
- var tmatrix = DenseMatrix.OfColumns(t);
- var pmatrix = DenseMatrix.OfColumns(p);
- var ptmatrix = DenseMatrix.OfColumns(pt);
- //开学第一周周内开课
- if (dayOfweek == 0) {
- dayOfweek = 7;
- }
- //补齐第一周
- //var fd = dayOfweek - 1;
- //本周有多少天
- var dd = 7 - dayOfweek + 1;
- int startdays = tdays - sday + dayOfweek;
- //有几周
- int sweeks = startdays / 7;
- //余几天
- int rweeks = startdays % 7;
- if (sweeks > 0)
- {
- for (int i = 1; i <= sweeks; i++)
- {
- if (i == 1)
- {
- var bsum = bmatrix.SubMatrix(sday - 1, dd, 0, bmatrix.ColumnCount).ColumnSums().Sum();
- var tsum = tmatrix.SubMatrix(sday - 1, dd, 0, tmatrix.ColumnCount).ColumnSums().Sum();
- var psum = pmatrix.SubMatrix(sday - 1, dd, 0, pmatrix.ColumnCount).ColumnSums().Sum();
- var ptsum = ptmatrix.SubMatrix(sday - 1, dd, 0, ptmatrix.ColumnCount).ColumnSums().Sum();
- sday += dd;
- wks.Add((i, new List<double>() { bsum, tsum, psum, ptsum }));
- }
- else {
- var bsum = bmatrix.SubMatrix(sday - 1, 7, 0, bmatrix.ColumnCount).ColumnSums().Sum();
- var tsum = tmatrix.SubMatrix(sday - 1, 7, 0, tmatrix.ColumnCount).ColumnSums().Sum();
- var psum = pmatrix.SubMatrix(sday - 1, 7, 0, pmatrix.ColumnCount).ColumnSums().Sum();
- var ptsum = ptmatrix.SubMatrix(sday - 1, 7, 0, ptmatrix.ColumnCount).ColumnSums().Sum();
- sday += 7;
- wks.Add((i, new List<double>() { bsum, tsum, psum, ptsum }));
- }
- }
- }
- if (rweeks > 0)
- {
- var bsum = bmatrix.SubMatrix(tdays - rweeks - 1, rweeks + 1, 0, bmatrix.ColumnCount).ColumnSums().Sum();
- var tsum = tmatrix.SubMatrix(tdays - rweeks - 1, rweeks + 1, 0, tmatrix.ColumnCount).ColumnSums().Sum();
- var psum = pmatrix.SubMatrix(tdays - rweeks - 1, rweeks + 1, 0, pmatrix.ColumnCount).ColumnSums().Sum();
- var ptsum = ptmatrix.SubMatrix(tdays - rweeks - 1, rweeks + 1, 0, ptmatrix.ColumnCount).ColumnSums().Sum();
- if (tyear > syear)
- {
- if (!string.IsNullOrEmpty(tId))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<LessonCount>(
- queryText: queryClass,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{tyear}") }))
- {
- tcount.Add(item);
- }
- }
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonCount>(
- queryText: queryClass,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{code}-{tyear}") }))
- {
- tcount.Add(item);
- }
- var eday = DateTimeOffset.UtcNow.DayOfYear;
- int pydays = DateTimeHelper.getDays(tyear);
- List<List<double>> e_begin = new();
- List<List<double>> e_t = new();
- List<List<double>> e_p = new();
- List<List<double>> e_pt = new();
- if (tcount.Count > 0)
- {
- foreach (LessonCount lesson in tcount)
- {
- e_begin.Add(lesson.beginCount);
- e_t.Add(lesson.tCount);
- e_p.Add(lesson.pCount);
- e_pt.Add(lesson.ptCount);
- }
- var tbmatrix = DenseMatrix.OfColumns(e_begin);
- var ttmatrix = DenseMatrix.OfColumns(e_t);
- var tpmatrix = DenseMatrix.OfColumns(e_p);
- var tptmatrix = DenseMatrix.OfColumns(e_pt);
- int day = 7 - rweeks;
- //补齐新年第一周
- int start = eday - day;
- int eweeks = start / 7;
- int erweeks = start % 7;
- var tbsum = tbmatrix.SubMatrix(0, day, 0, tbmatrix.ColumnCount).ColumnSums().Sum();
- var ttsum = ttmatrix.SubMatrix(0, day, 0, ttmatrix.ColumnCount).ColumnSums().Sum();
- var tpsum = tpmatrix.SubMatrix(0, day, 0, tpmatrix.ColumnCount).ColumnSums().Sum();
- var tptsum = tptmatrix.SubMatrix(0, day, 0, tptmatrix.ColumnCount).ColumnSums().Sum();
- wks.Add((sweeks + 1, new List<double>() { bsum + tbsum, tsum + ttsum, psum + tpsum, ptsum + tptsum }));
- if (eweeks > 0)
- {
- for (int i = 1; i <= eweeks; i++)
- {
- var newbsum = tbmatrix.SubMatrix(day, 7, 0, tbmatrix.ColumnCount).ColumnSums().Sum();
- var newtsum = ttmatrix.SubMatrix(day, 7, 0, ttmatrix.ColumnCount).ColumnSums().Sum();
- var newpsum = tpmatrix.SubMatrix(day, 7, 0, tpmatrix.ColumnCount).ColumnSums().Sum();
- var newptsum = tptmatrix.SubMatrix(day, 7, 0, tptmatrix.ColumnCount).ColumnSums().Sum();
- day += 7;
- wks.Add((sweeks += 1, new List<double>() { newbsum, newtsum, newpsum, newptsum }));
- }
- }
- if (erweeks > 0)
- {
- var newbsum = tbmatrix.SubMatrix(eday - day - 1, erweeks, 0, tbmatrix.ColumnCount).ColumnSums().Sum();
- var newtsum = ttmatrix.SubMatrix(eday - day - 1, erweeks, 0, ttmatrix.ColumnCount).ColumnSums().Sum();
- var newpsum = tpmatrix.SubMatrix(eday - day - 1, erweeks, 0, tpmatrix.ColumnCount).ColumnSums().Sum();
- var newptsum = tptmatrix.SubMatrix(eday - day - 1, erweeks, 0, tptmatrix.ColumnCount).ColumnSums().Sum();
- wks.Add((sweeks + 1, new List<double>() { newbsum, newtsum, newpsum, newptsum }));
- }
- }
- }
- else
- {
- wks.Add((sweeks + 1, new List<double>() { bsum, tsum, psum, ptsum }));
- }
- }
- }
- return wks;
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"OS,{_option.Location},analysis/getTPCount()\n{e.Message}{e.StackTrace}", GroupNames.成都开发測試群組);
- return null;
- }
- }
- private async Task<List<(string time, double count)>> getRecordCount(CosmosClient client, List<LessonRecord> records, long stime, long etime, string code, string tId)
- {
- List<(string time, double count)> counts = new();
- counts.Add(("total", records.Count()));
- var syear = DateTimeOffset.FromUnixTimeMilliseconds(stime).Year;
- //var eyear = DateTimeOffset.FromUnixTimeMilliseconds(etime).Year;
- var tyear = DateTimeOffset.UtcNow.Year;
- var tday = DateTimeOffset.UtcNow.DayOfYear;
- //求今年多少天
- int tdays = DateTimeHelper.getDays(tyear);
- //如果跨年 求前年多少天
- int pydays = DateTimeHelper.getDays(syear);
- List<LessonCount> scount = new();
- List<LessonCount> tcount = new();
- DenseMatrix dense = null;
- var queryClass = $"select value(c) from c ";
- if (!string.IsNullOrEmpty(tId))
- {
- queryClass = $"select value(c) from c where c.id = '{tId}'";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<LessonCount>(
- queryText: queryClass,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{syear}") }))
- {
- scount.Add(item);
- }
- }
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonCount>(
- queryText: queryClass,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{code}-{syear}") }))
- {
- scount.Add(item);
- }
- //var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(syear.ToString(), new PartitionKey($"ClassCount-" + code));
- if (tyear > syear)
- {
- //跨年
- if (!string.IsNullOrEmpty(tId))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<LessonCount>(
- queryText: queryClass,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{tyear}") }))
- {
- tcount.Add(item);
- }
- }
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonCount>(
- queryText: queryClass,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonCount-{code}-{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 List<List<double>>();
- foreach (LessonCount lesson in scount)
- {
- begin.Add(lesson.beginCount);
- }
- var matrix = DenseMatrix.OfColumns(begin);
- //求本周
- int sday = (int)DateTimeOffset.UtcNow.DayOfWeek;
- //本月
- var tmonth = DateTimeOffset.UtcNow.Day;
- if (tyear > syear)
- {
- //今日
- double tcounts = dense.Row(tday - 1).Sum();
- counts.Add(("today", tcounts));
- if (sday == 0)
- {
- sday = 7;
- }
- if (tday - sday < 0)
- {
- //var tmatrix = DenseMatrix.OfColumns(scount.beginCount);
- //新的一年不超过7天的值
- double tsum = 0;
- if (null != dense)
- {
- tsum = dense.SubMatrix(tday, tday, 0, dense.ColumnCount).ColumnSums().Sum();
- }
- //前一年余下的值
- var pysum = matrix.SubMatrix(pydays - (sday - tday) - 1, sday - tday + 1, 0, matrix.ColumnCount).ColumnSums().Sum();
- counts.Add(("week", tsum + pysum));
- }
- else {
- var subDay = dense.SubMatrix(tday - sday - 1, sday + 1, 0, dense.ColumnCount).ColumnSums().Sum();
- counts.Add(("week", subDay));
- }
- var subMonth = dense.SubMatrix(tday - tmonth - 1, tmonth + 1, 0, dense.ColumnCount).ColumnSums().Sum();
- counts.Add(("month", subMonth));
- }
- else
- {
- //今日
- double tcounts = matrix.Row(tday - 1).Sum();
- counts.Add(("today", tcounts));
- var subDay = matrix.SubMatrix(tday - sday - 1, sday + 1, 0, matrix.ColumnCount).ColumnSums().Sum();
- counts.Add(("week", subDay));
- var subMonth = matrix.SubMatrix(tday - tmonth - 1, tmonth + 1, 0, matrix.ColumnCount).ColumnSums().Sum();
- counts.Add(("month", subMonth));
- }
- //求本学期
- var sdays = DateTimeOffset.FromUnixTimeMilliseconds(stime).DayOfYear;
- //var edays = DateTimeOffset.FromUnixTimeMilliseconds(etime).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();
- counts.Add(("semester", endMonth + startMonth));
- }
- else
- {
- var allMonth = matrix.SubMatrix(sdays - 1, tday - sdays + 1, 0, matrix.ColumnCount).ColumnSums().Sum();
- counts.Add(("semester", allMonth));
- }
- /* //本月
- var tmonth = DateTimeOffset.UtcNow.Day;
- var subMonth = matrix.SubMatrix(tmonth - 1, tmonth + 1, 0, matrix.ColumnCount).ColumnSums().Sum();
- counts.Add(("month", subMonth));*/
- //求今年
- var subYear = matrix.SubMatrix(0, tdays, 0, matrix.ColumnCount).ColumnSums().Sum();
- }
- return counts;
- }
- //取得该学校当前学期所有评测活动进行分类计算
- private async Task<List<double>> getExamTypeCount(CosmosClient client, long stime, long etime, string code)
- {
- List<double> counts = new List<double>();
- try
- {
- List<(string id, string source, string scope)> ps = new List<(string id, string source, string scope)>();
- var queryClass = $"select c.id,c.source,c.scope from c where c.school = '{code}' and c.pk = 'Exam' and c.startTime >= {stime} and c.endTime <= {etime}";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: queryClass))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- ps.Add((account.GetProperty("id").GetString(), account.GetProperty("source").GetString(), account.GetProperty("scope").GetString()));
- }
- break;
- }
- }
- var sc_online = ps.Where(x => x.source.Equals("0") && x.scope.Equals("school")).ToList().Count;
- var p_online = ps.Where(x => x.source.Equals("0") && x.scope.Equals("private")).ToList().Count;
- var sc_class = ps.Where(x => x.source.Equals("1") && x.scope.Equals("school")).ToList().Count;
- var p_class = ps.Where(x => x.source.Equals("1") && x.scope.Equals("private")).ToList().Count;
- var sc_mark = ps.Where(x => x.source.Equals("2") && x.scope.Equals("school")).ToList().Count;
- var p_mark = ps.Where(x => x.source.Equals("2") && x.scope.Equals("private")).ToList().Count;
- counts.Add(sc_online);
- counts.Add(sc_class);
- counts.Add(sc_mark);
- counts.Add(p_online);
- counts.Add(p_class);
- counts.Add(p_mark);
- return counts;
- }
- catch (Exception e)
- {
- return counts;
- }
- }
- }
- }
|