using Azure.Cosmos;
using Azure.Storage.Blobs;
using DocumentFormat.OpenXml.Math;
using MathNet.Numerics.LinearAlgebra.Double;
using Microsoft.Azure.Cosmos.Table;
using NUnit.Framework.Constraints;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK.Models.Cosmos.BI;
using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
namespace TEAMModelOS.SDK.Models.Service.BI
{
public static class BIStats
{
///
/// 依据类型增/减量统计方法
///
/// 连接字符
/// 学校id
/// 类型:Exam(测验) Survey(问卷调查) Vote(投票) Homework(作业) Less(课例) Student(学生) Tchaer(教师)
/// 增减数量
/// 互动
/// 时间
///
public static async Task SetTypeAddStats(CosmosClient cosmosClient, DingDing _dingDing, string scId, string type, int count, int lessType = 0, int interaction = 0, long careDate = 0)
{
try
{
if (string.IsNullOrEmpty(scId))
return;
DateTimeOffset cuurDateOff = DateTimeOffset.UtcNow;
DateTimeOffset dateOff = DateTimeOffset.UtcNow;
if (careDate > 1000000000000)
dateOff = DateTimeOffset.FromUnixTimeMilliseconds(careDate);
else
dateOff = DateTimeOffset.FromUnixTimeSeconds(careDate);
if (careDate == 0)
{
dateOff = DateTimeOffset.UtcNow;
careDate = dateOff.ToUnixTimeMilliseconds();
}
var (lastWeekS, lastWeekE) = TimeHelper.GetStartOrEnd(dateOff, "lastweek"); //计算上周开始/结束时间
var (weekS, weekE) = TimeHelper.GetStartOrEnd(dateOff, "week"); //计算本周开始/结束时间
var (lastMonthS, lastMonthE) = TimeHelper.GetStartOrEnd(dateOff, "lastMonth"); //上月开始/结束时间
var (monthS, monthE) = TimeHelper.GetStartOrEnd(dateOff, "month"); //本月开始/结束时间
var (lastTermS, lastTermE) = TimeHelper.GetStartOrEnd(dateOff, "lastterm"); //计算上学期开始/结束时间
var (termS, termE) = TimeHelper.GetStartOrEnd(dateOff, "term"); //计算本学期开始/结束时间
var (yearS, yearE) = TimeHelper.GetStartOrEnd(cuurDateOff, "year"); //计算今年开始/结束时间
var (lWeekDayS, lWeekDayE) = TimeHelper.GetLongToTime(lastWeekS, lastWeekE);
var (weekDayS, weekDayE) = TimeHelper.GetLongToTime(weekS, weekE);
var (lTermDayS, lTermDayE) = TimeHelper.GetLongToTime(lastTermS, lastTermE);
var (termDayS, termDayE) = TimeHelper.GetLongToTime(termS, termE);
var (lMonthS, lMonthE) = TimeHelper.GetLongToTime(lastMonthS, lastMonthE);
var (monthDayS, monthDayE) = TimeHelper.GetLongToTime(monthS, monthE);
var lastDay = dateOff.AddDays(-1);
int lmthDay = TimeHelper.GetDateDay(lMonthE);
StatsInfo statsInfo = null;
var resStsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync($"{dateOff.Year}-{scId}", new PartitionKey("Statistics"));
if (resStsInfo.Status == 200)
{
using var fileJson = await JsonDocument.ParseAsync(resStsInfo.ContentStream);
statsInfo = fileJson.ToObject();
if (string.IsNullOrEmpty(statsInfo.schoolId))
{
statsInfo.schoolId = scId;
}
}
else
{
var resLastStsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync($"{dateOff.Year - 1}-{scId}", new PartitionKey("Statistics"));
if (resLastStsInfo.Status == 200)
{
using var fileJson = await JsonDocument.ParseAsync(resLastStsInfo.ContentStream);
statsInfo = fileJson.ToObject();
statsInfo.id = $"{dateOff.Year}-{scId}";
statsInfo.year = dateOff.Year;
statsInfo.schoolId = scId;
statsInfo.stuYear = new double[366].ToList();
statsInfo.tchYear = new double[366].ToList();
statsInfo.lesson.year = new double[366].ToList();
statsInfo.lesson.yearInters = new double[366].ToList();
statsInfo.activity.year = new double[366].ToList();
}
else
{
ScBase scBase = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemAsync(scId, new PartitionKey("Base"));
statsInfo = new StatsInfo
{
id = $"{dateOff.Year}-{scBase.id}",
schoolId = scBase.id,
name = scBase.name,
picture = scBase.picture,
size = scBase.size,
scCreateTime = scBase.createTime,
areaId = scBase.areaId,
upTime = careDate,
stuYear = new double[366].ToList(),
tchYear = new double[366].ToList()
};
statsInfo.lesson.year = new double[366].ToList();
statsInfo.lesson.yearInters = new double[366].ToList();
statsInfo.activity.year = new double[366].ToList();
}
}
switch ($"{type}")
{
case "Exam":
statsInfo.activity.exam += count;
break;
case "Survey":
statsInfo.activity.survey += count;
break;
case "Vote":
statsInfo.activity.vote += count;
break;
case "Homework":
statsInfo.activity.homework += count;
break;
case "Less":
//课例
statsInfo.lesson.year[dateOff.DayOfYear - 1] += count;
statsInfo.lesson.all += count;
if (lessType == 0)
statsInfo.lesson.open += count;
else
statsInfo.lesson.less += count;
var lessList = DenseMatrix.OfColumns(new List>() { statsInfo.lesson.year });
DenseMatrix lastLesson = null;
DenseMatrix lastInter = null;
StatsInfo lastStats = null;
var resLastStats = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync($"{dateOff.Year - 1}-{scId}", new PartitionKey("Statistics"));
if (resLastStats.Status == 200)
{
using var fileJson = await JsonDocument.ParseAsync(resLastStats.ContentStream);
lastStats = fileJson.ToObject();
lastLesson = DenseMatrix.OfColumns(new List>() { lastStats.lesson.year });
lastInter = DenseMatrix.OfColumns(new List>() { lastStats.lesson.yearInters });
}
int lastDayCnt = 0;
if (lastDay.Year < dateOff.Year)
{
lastDayCnt = ((int)lastStats.lesson.year[lastDay.DayOfYear - 1]);
}
else
{
lastDayCnt = ((int)statsInfo.lesson.year[lastDay.DayOfYear - 1]);
}
statsInfo.lesson.lastDay = lastDayCnt;
statsInfo.lesson.day = ((int)statsInfo.lesson.year[dateOff.DayOfYear - 1]);
int lastWeek = 0;
if (lWeekDayS.Year != dateOff.Year)
{
if (lastLesson != null)
{
lastWeek += ((int)lastLesson.SubMatrix(lWeekDayS.DayOfYear - 1, lastLesson.RowCount - lWeekDayS.DayOfYear + 1, 0, lessList.ColumnCount).ColumnSums().Sum());
}
lastWeek += ((int)lessList.SubMatrix(0, lWeekDayE.DayOfYear, 0, lessList.ColumnCount).ColumnSums().Sum());
}
else
{
lastWeek = ((int)lessList.SubMatrix(lWeekDayS.DayOfYear - 1, lWeekDayE.DayOfYear - lWeekDayS.DayOfYear + 1, 0, lessList.ColumnCount).ColumnSums().Sum());
}
statsInfo.lesson.lastWeek = lastWeek;
statsInfo.lesson.week = ((int)lessList.SubMatrix(weekDayS.DayOfYear - 1, (dateOff.DayOfYear - weekDayS.DayOfYear + 1), 0, lessList.ColumnCount).ColumnSums().Sum());
//var resLast = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync($"{lTermDayS.Year}-{scId}", new PartitionKey("Statistics"));
//if (resLast.Status == 200)
//{
// using var fileJson = await JsonDocument.ParseAsync(resLast.ContentStream);
// lastStats = fileJson.ToObject();
//}
int lastTerm = 0;
if (lTermDayS.Year < dateOff.Year)
{
if (lastLesson != null)
{
lastTerm += ((int)lastLesson.SubMatrix(lTermDayS.DayOfYear - 1, lastStats.lesson.year.Count - lTermDayS.DayOfYear + 1, 0, 1).ColumnSums().Sum());
}
lastTerm += ((int)lessList.SubMatrix(0, lTermDayE.DayOfYear, 0, lessList.ColumnCount).ColumnSums().Sum());
}
else
{
lastTerm += ((int)lessList.SubMatrix(lTermDayS.DayOfYear - 1, lTermDayE.DayOfYear - lTermDayS.DayOfYear + 1, 0, lessList.ColumnCount).ColumnSums().Sum());
}
statsInfo.lesson.lastTerm = lastTerm;
int term = 0;
if (termDayE.Year > dateOff.Year)
{
if (lastStats != null)
{
term = ((int)lessList.SubMatrix(termDayS.DayOfYear - 1, (statsInfo.lesson.year.Count - termDayS.DayOfYear + 1), 0, lessList.ColumnCount).ColumnSums().Sum());
}
}
else
{
if (termDayS.Year < dateOff.Year)
{
if (lastLesson != null)
{
term += ((int)lastLesson.SubMatrix(termDayS.DayOfYear - 1, lastStats.lesson.year.Count - termDayS.DayOfYear + 1, 0, 1).ColumnSums().Sum());
}
term += ((int)lessList.SubMatrix(0, termDayE.DayOfYear, 0, lessList.ColumnCount).ColumnSums().Sum());
}
else
{
term = ((int)lessList.SubMatrix(termDayS.DayOfYear - 1, termDayE.DayOfYear - termDayS.DayOfYear + 1, 0, lessList.ColumnCount).ColumnSums().Sum());
}
}
statsInfo.lesson.term = term;
int lastMonth = 0;
if (lMonthS.Year < dateOff.Year)
{
if (lastLesson != null)
{
lastMonth = ((int)lastLesson.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, lastLesson.ColumnCount).ColumnSums().Sum());
}
}
else
{
lastMonth = ((int)lessList.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, lessList.ColumnCount).ColumnSums().Sum());
}
statsInfo.lesson.lastMonth = lastMonth;
statsInfo.lesson.month = ((int)lessList.SubMatrix(monthDayS.DayOfYear - 1, (dateOff.DayOfYear - monthDayS.DayOfYear + 1), 0, lessList.ColumnCount).ColumnSums().Sum());
//课例互动
statsInfo.lesson.yearInters[dateOff.DayOfYear - 1] += interaction;
statsInfo.lesson.allInter += interaction;
int lastDayInter = 0;
if (lastDay.Year < dateOff.Year)
{
lastDayInter = ((int)lastStats.lesson.yearInters[lastDay.DayOfYear - 1]);
}
else
{
lastDayInter = ((int)statsInfo.lesson.yearInters[lastDay.DayOfYear - 1]);
}
statsInfo.lesson.lastDayInter = lastDayInter;
statsInfo.lesson.dayInter = ((int)statsInfo.lesson.yearInters[dateOff.DayOfYear - 1]);
var interList = DenseMatrix.OfColumns(new List>() { statsInfo.lesson.yearInters });
int lastMonthInter = 0;
if (lMonthS.Year < dateOff.Year)
{
if (lastInter != null)
{
lastMonthInter = ((int)lastInter.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, lastInter.ColumnCount).ColumnSums().Sum());
}
}
else
{
lastMonthInter = ((int)interList.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, interList.ColumnCount).ColumnSums().Sum());
}
statsInfo.lesson.lastMonthInter = lastMonthInter;
statsInfo.lesson.monthInter = ((int)interList.SubMatrix(monthDayS.DayOfYear - 1, (dateOff.DayOfYear - monthDayS.DayOfYear + 1), 0, interList.ColumnCount).ColumnSums().Sum());
int lastTermI = 0;
if (lastStats != null)
{
if (lastInter != null)
{
lastTermI = ((int)lastInter.SubMatrix(0, lastStats.lesson.year.Count, 0, lastInter.ColumnCount).ColumnSums().Sum());
}
}
statsInfo.lesson.lastYearInter = lastTermI;
statsInfo.lesson.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
break;
case "Student":
statsInfo.stuYear[dateOff.DayOfYear - 1] += count;
statsInfo.stu += count;
statsInfo.dayStu = ((int)statsInfo.stuYear[dateOff.DayOfYear - 1]);
var stuList = DenseMatrix.OfColumns(new List>() { statsInfo.stuYear });
statsInfo.weekStu = ((int)stuList.SubMatrix(weekDayS.DayOfYear - 1, (dateOff.DayOfYear - weekDayS.DayOfYear + 1), 0, stuList.ColumnCount).ColumnSums().Sum());
statsInfo.monthStu = ((int)stuList.SubMatrix(monthDayS.DayOfYear - 1, (dateOff.DayOfYear - monthDayS.DayOfYear + 1), 0, stuList.ColumnCount).ColumnSums().Sum());
statsInfo.stuUpTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
break;
case "Teacher":
statsInfo.tchYear[dateOff.DayOfYear - 1] += count;
statsInfo.tch += count;
statsInfo.dayTch = ((int)statsInfo.tchYear[dateOff.DayOfYear - 1]);
var tchList = DenseMatrix.OfColumns(new List>() { statsInfo.tchYear });
statsInfo.weekTch = ((int)tchList.SubMatrix(weekDayS.DayOfYear - 1, (dateOff.DayOfYear - weekDayS.DayOfYear + 1), 0, tchList.ColumnCount).ColumnSums().Sum());
statsInfo.monthTch = ((int)tchList.SubMatrix(monthDayS.DayOfYear - 1, (dateOff.DayOfYear - monthDayS.DayOfYear + 1), 0, tchList.ColumnCount).ColumnSums().Sum());
statsInfo.tchUpTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
break;
}
if ($"{type}".Equals("Exam") || $"{type}".Equals("Survey") || $"{type}".Equals("Vote") || $"{type}".Equals("Homework"))
{
statsInfo.activity.all += count;
if (careDate >= yearS && careDate <= yearE)
{
if (count > 0)
{
statsInfo.activity.year[dateOff.DayOfYear - 1] += count;
DenseMatrix matris = DenseMatrix.OfColumns(new List>() { statsInfo.activity.year });
DenseMatrix lastAct = null;
StatsInfo lastStats = null;
var resLastStats = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync($"{dateOff.Year - 1}-{scId}", new PartitionKey("Statistics"));
if (resLastStats.Status == 200)
{
using var fileJson = await JsonDocument.ParseAsync(resLastStats.ContentStream);
lastStats = fileJson.ToObject();
lastAct = DenseMatrix.OfColumns(new List>() { lastStats.activity.year });
}
int lastDayCnt = 0;
if (lastDay.Year < dateOff.Year)
{
if (lastStats != null)
{
lastDayCnt = ((int)lastStats.activity.year[lastDay.DayOfYear - 1]);
}
}
else
{
lastDayCnt = ((int)statsInfo.activity.year[lastDay.DayOfYear - 1]);
}
statsInfo.activity.lastDay = lastDayCnt;
statsInfo.activity.dayCnt = ((int)statsInfo.activity.year[dateOff.DayOfYear - 1]);
int lastWeek = 0;
if (lWeekDayS.Year < dateOff.Year)
{
if (lastAct != null)
{
lastWeek += ((int)lastAct.SubMatrix(lWeekDayS.DayOfYear - 1, lastAct.RowCount - lWeekDayS.DayOfYear + 1, 0, lastAct.ColumnCount).ColumnSums().Sum());
}
lastWeek += ((int)matris.SubMatrix(0, lWeekDayE.DayOfYear, 0, matris.ColumnCount).ColumnSums().Sum());
}
else
{
lastWeek = ((int)matris.SubMatrix(lWeekDayS.DayOfYear - 1, lWeekDayE.DayOfYear - lWeekDayS.DayOfYear + 1, 0, matris.ColumnCount).ColumnSums().Sum());
}
statsInfo.activity.lastWeek = lastWeek;
statsInfo.activity.week = ((int)matris.SubMatrix(weekDayS.DayOfYear - 1, dateOff.DayOfYear - weekDayS.DayOfYear + 1, 0, matris.ColumnCount).ColumnSums().Sum());
int lastTerm = 0;
if (lTermDayS.Year < dateOff.Year)
{
if (lastStats != null)
{
lastTerm += ((int)lastAct.SubMatrix(lTermDayS.DayOfYear - 1, lastStats.activity.year.Count - lTermDayS.DayOfYear + 1, 0, lastAct.ColumnCount).ColumnSums().Sum());
}
lastTerm += ((int)matris.SubMatrix(0, lTermDayE.DayOfYear, 0, matris.ColumnCount).ColumnSums().Sum());
}
else
{
lastTerm += ((int)matris.SubMatrix(lTermDayS.DayOfYear - 1, lTermDayE.DayOfYear - lTermDayS.DayOfYear + 1, 0, matris.ColumnCount).ColumnSums().Sum());
}
statsInfo.activity.lastTerm = lastTerm;
int term = 0;
if (termDayS.Year > dateOff.Year)
{
if (lastAct != null)
{
term += ((int)lastAct.SubMatrix(termDayS.DayOfYear - 1, lastStats.activity.year.Count - termDayS.DayOfYear + 1, 0, lastAct.ColumnCount).ColumnSums().Sum());
}
term += ((int)matris.SubMatrix(0, termDayE.DayOfYear + 1, 0, matris.ColumnCount).ColumnSums().Sum());
}
else
{
if (lTermDayS.Year < dateOff.Year)
{
if (lastAct != null)
{
term += ((int)lastAct.SubMatrix(termDayS.DayOfYear - 1, lastStats.activity.year.Count - termDayS.DayOfYear + 1, 0, 1).ColumnSums().Sum());
}
term += ((int)matris.SubMatrix(0, termDayE.DayOfYear, 0, matris.ColumnCount).ColumnSums().Sum());
}
else
{
term += ((int)matris.SubMatrix(termDayS.DayOfYear - 1, termDayE.DayOfYear - termDayS.DayOfYear + 1, 0, matris.ColumnCount).ColumnSums().Sum());
}
}
statsInfo.activity.term = term;
int lastMonth = 0;
if (lMonthS.Year < dateOff.Year)
{
if (lastAct != null)
{
lastMonth = ((int)lastAct.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, matris.ColumnCount).ColumnSums().Sum());
}
}
else
{
lastMonth = ((int)matris.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, matris.ColumnCount).ColumnSums().Sum());
}
statsInfo.activity.lastMonth = lastMonth;
statsInfo.activity.month += ((int)matris.SubMatrix(monthDayS.DayOfYear, (dateOff.DayOfYear - monthDayS.DayOfYear), 0, matris.ColumnCount).ColumnSums().Sum());
statsInfo.activity.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}
}
}
if (resStsInfo.Status == 200)
{
statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(statsInfo, statsInfo.id, new PartitionKey("Statistics"));
}
else
{
statsInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(statsInfo, new PartitionKey("Statistics"));
}
}
catch (Exception ex)
{
await _dingDing.SendBotMsg($"BI,{Environment.GetEnvironmentVariable("Option:Location")},SetTypeAddStats(),参数: 学校:{scId},统计类型:{type}, 增/减数:{count},课例类型(0:开课,1:课例):{lessType},学生互动数:{interaction},时间戳:{careDate}\n {ex.Message}\n {ex.StackTrace}", GroupNames.成都开发測試群組);
}
}
///
/// 创建/创建BI统计信息接口
///
///
///
///
public static async Task SetScStatsInfo(CosmosClient cosmosClient, DingDing _dingDing, School school)
{
try
{
DateTimeOffset dateOff = DateTimeOffset.UtcNow;
StatsInfo statsInfo = new();
var resStats = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync($"{dateOff.Year}-{school.id}", new PartitionKey("Statistics"));
if (resStats.Status == 200)
{
using var fileJson = await JsonDocument.ParseAsync(resStats.ContentStream);
statsInfo = fileJson.ToObject();
}
else
{
double[] tchYear = new double[366];
tchYear[0] = 1;
statsInfo.id = $"{dateOff.Year}-{school.id}";
statsInfo.year = dateOff.Year;
statsInfo.stuYear = new double[366].ToList();
statsInfo.tchYear = tchYear.ToList();
statsInfo.tch = 1;
statsInfo.weekTch = 1;
statsInfo.monthTch = 1;
statsInfo.tchUpTime = dateOff.ToUnixTimeMilliseconds();
statsInfo.lesson.year = new double[366].ToList();
statsInfo.lesson.yearInters = new double[366].ToList();
statsInfo.activity.year = new double[366].ToList();
}
statsInfo.schoolId = school.id;
statsInfo.name = school.name;
statsInfo.picture = school.picture;
statsInfo.size = school.size;
statsInfo.scCreateTime = school.createTime;
statsInfo.areaId = school.areaId;
statsInfo.upTime = dateOff.ToUnixTimeMilliseconds();
if (resStats.Status == 200)
{
await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(statsInfo, statsInfo.id, new PartitionKey("Statistics"));
}
else
{
await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(statsInfo, new PartitionKey("Statistics"));
}
}
catch (Exception ex)
{
await _dingDing.SendBotMsg($"BI,{Environment.GetEnvironmentVariable("Option:Location")},SetScStatsInfo() 学校:{school} \n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
}
}
///
/// 新增/修改学校列表中间件
///
///
///
///
///
///
///
public static async Task SetSchoolBIRelation(CosmosClient cosmosClient, BlobContainerClient blobClient, CloudTableClient tableClient, DingDing _dingDing, School school)
{
try
{
string type = "";
BIRelation bIRelation = new();
var resBiRel = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(school.id, new PartitionKey("BIRel"));
if (resBiRel.Status == 200)
{
using var fileJson = await JsonDocument.ParseAsync(resBiRel.ContentStream);
bIRelation = fileJson.ToObject();
}
else
{
bIRelation.id = school.id;
}
string aName = null;
if (!string.IsNullOrEmpty($"{school.areaId}"))
{
aName = await CosmosQueryHelper.GetStr(cosmosClient, "Normal", $"select value(c.name) from c where c.id='{school.areaId}'", "Base-Area");
}
bIRelation.name = school.name;
bIRelation.picture = school.picture;
bIRelation.region = school.region;
bIRelation.province = school.province;
bIRelation.city = school.city;
bIRelation.dist = school.dist;
bIRelation.address = school.address;
bIRelation.areaId = school.areaId;
bIRelation.size = school.size;
bIRelation.scale = school.scale;
bIRelation.upDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
bIRelation.areaName = aName;
bIRelation.edition = school.edition;
if (resBiRel.Status == 200)
{
await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(bIRelation, bIRelation.id, new PartitionKey("BIRel"));
type = "update";
}
else
{
await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(bIRelation, new PartitionKey("BIRel"));
type = "add";
}
//保存操作记录
await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, $"birelation-{type}", $"触发更新学校信息表中间件", _dingDing);
}
catch (Exception ex)
{
await _dingDing.SendBotMsg($"BI,{Environment.GetEnvironmentVariable("Option:Location")},SetSchoolBIRelation() 学校:{school} \n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
}
}
///
/// 每天00:00执行统计清零方法;zore时间
///
///
///
///
public static async Task SetStatsZeroPoint(AzureCosmosFactory _azureCosmos, DingDing _dingDing)
{
List scId = new();
try
{
var cosmosClient = _azureCosmos.GetCosmosClient();
DateTimeOffset dateOff = DateTimeOffset.Now;
long upTime = dateOff.ToUnixTimeMilliseconds();
var (DayS, DayE) = TimeHelper.GetStartOrEnd(dateOff); //今天开始时间结束时间
var (lastWeekS, lastWeekE) = TimeHelper.GetStartOrEnd(dateOff, "lastweek"); //计算上周开始/结束时间
var (weekS, weekE) = TimeHelper.GetStartOrEnd(dateOff, "week"); //计算本周开始/结束时间
var (lastMonthS, lastMonthE) = TimeHelper.GetStartOrEnd(dateOff, "lastMonth"); //上月开始/结束时间
var (monthS, monthE) = TimeHelper.GetStartOrEnd(dateOff, "month"); //本月开始/结束时间
var (lastTermS, lastTermE) = TimeHelper.GetStartOrEnd(dateOff, "lastterm"); //计算上学期开始/结束时间
var (termS, termE) = TimeHelper.GetStartOrEnd(dateOff, "term"); //计算本学期开始/结束时间
var (lWeekDayS, lWeekDayE) = TimeHelper.GetLongToTime(lastWeekS, lastWeekE);
var (weekDayS, weekDayE) = TimeHelper.GetLongToTime(weekS, weekE);
var (lTermDayS, lTermDayE) = TimeHelper.GetLongToTime(lastTermS, lastTermE);
var (termDayS, termDayE) = TimeHelper.GetLongToTime(termS, termE);
var (lMonthS, lMonthE) = TimeHelper.GetLongToTime(lastMonthS, lastMonthE);
var (monthDayS, monthDayE) = TimeHelper.GetLongToTime(monthS, monthE);
var lastDay = dateOff.AddDays(-1);
int lmthDay = TimeHelper.GetDateDay(lMonthE);
List>> statsInfos = new();
List tempStatsInfos = new();
StringBuilder statsSql = new($"select value(c) from c where c.year={dateOff.Year}");
List scIds = new();
await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator(queryText: $"select value(c.id) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BIRel") }))
{
scIds.Add(item);
}
if (scIds.Count > 0)
{
statsSql.Append($" and {BICommonWay.ManyScSql("c.id", scIds, $"{dateOff.Year}-")}");
}
await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator(queryText: statsSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Statistics") }))
{
tempStatsInfos.Add(item);
}
if (tempStatsInfos.Count > 0)
{
foreach (var item in tempStatsInfos)
{
try
{
StatsInfo lastStatsInfo = null;
DenseMatrix lastTch = null;
DenseMatrix lastStu = null;
DenseMatrix lastLesson = null;
DenseMatrix lastInter = null;
DenseMatrix lastAct = null;
var lastSts = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync($"{dateOff.Year - 1}-{item.schoolId}", new PartitionKey("Statistics"));
if (lastSts.Status == 200)
{
using var fileJson = await JsonDocument.ParseAsync(lastSts.ContentStream);
lastStatsInfo = fileJson.ToObject();
item.schoolId = lastStatsInfo.schoolId;
item.year = dateOff.Year;
item.picture = lastStatsInfo.picture;
item.name = lastStatsInfo.name;
item.areaId = lastStatsInfo.areaId;
item.size = lastStatsInfo.size;
item.useSize = lastStatsInfo.useSize;
item.tch = lastStatsInfo.tch;
lastTch = DenseMatrix.OfColumns(new List>() { lastStatsInfo.tchYear });
lastStu = DenseMatrix.OfColumns(new List>() { lastStatsInfo.stuYear });
lastLesson = DenseMatrix.OfColumns(new List>() { lastStatsInfo.lesson.year });
lastInter = DenseMatrix.OfColumns(new List>() { lastStatsInfo.lesson.yearInters });
lastAct = DenseMatrix.OfColumns(new List>() { lastStatsInfo.activity.year });
}
var tchList = DenseMatrix.OfColumns(new List>() { item.tchYear });
var stuList = DenseMatrix.OfColumns(new List>() { item.stuYear });
int dayTch = 0;
int dayStu = 0;
int lastDayLesson = 0;
int lastDayAct = 0;
if (lastDay.Year < dateOff.Year)
{
if (lastStatsInfo != null)
{
dayTch = ((int)lastStatsInfo.tchYear[lastDay.DayOfYear - 1]);
dayStu = ((int)lastStatsInfo.stuYear[lastDay.DayOfYear - 1]);
lastDayLesson = ((int)lastStatsInfo.lesson.year[lastDay.DayOfYear - 1]);
lastDayAct = ((int)lastStatsInfo.activity.year[lastDay.DayOfYear - 1]);
}
}
else
{
dayTch = ((int)item.tchYear[dateOff.DayOfYear - 1]);
dayStu = ((int)item.stuYear[dateOff.DayOfYear - 1]);
lastDayLesson = ((int)item.lesson.year[lastDay.DayOfYear - 1]);
lastDayAct = ((int)item.activity.year[lastDay.DayOfYear - 1]);
}
item.dayTch = dayTch;
int weekTch = 0;
int weekStu = 0;
if (weekDayS.Year < dateOff.Year)
{
if (lastTch != null)
{
weekTch += ((int)lastTch.SubMatrix(weekDayS.DayOfYear - 1, lastTch.RowCount - weekDayS.DayOfYear + 1, 0, lastTch.ColumnCount).ColumnSums().Sum());
}
weekTch += ((int)tchList.SubMatrix(0, weekDayE.DayOfYear + 1, 0, tchList.ColumnCount).ColumnSums().Sum());
if (lastStu != null)
{
weekStu += ((int)lastStu.SubMatrix(weekDayS.DayOfYear - 1, lastStu.RowCount - weekDayS.DayOfYear + 1, 0, lastStu.ColumnCount).ColumnSums().Sum());
}
weekStu += ((int)lastStu.SubMatrix(0, weekDayE.DayOfYear + 1, 0, lastStu.ColumnCount).ColumnSums().Sum());
}
else
{
weekTch = ((int)tchList.SubMatrix(weekDayS.DayOfYear - 1, 7, 0, tchList.ColumnCount).ColumnSums().Sum());
weekStu = ((int)stuList.SubMatrix(weekDayS.DayOfYear - 1, 7, 0, stuList.ColumnCount).ColumnSums().Sum());
}
item.weekTch = weekTch;
item.monthTch = ((int)tchList.SubMatrix(monthDayS.DayOfYear - 1, (dateOff.DayOfYear - monthDayS.DayOfYear + 1), 0, tchList.ColumnCount).ColumnSums().Sum());
item.tchUpTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
item.dayStu = dayStu;
item.weekStu = weekStu;
item.monthStu = ((int)stuList.SubMatrix(monthDayS.DayOfYear - 1, (dateOff.DayOfYear - monthDayS.DayOfYear + 1), 0, stuList.ColumnCount).ColumnSums().Sum());
item.stuUpTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var lessList = DenseMatrix.OfColumns(new List>() { item.lesson.year });
var interList = DenseMatrix.OfColumns(new List>() { item.lesson.yearInters });
var actList = DenseMatrix.OfColumns(new List>() { item.activity.year });
item.lesson.lastDay = lastDayLesson;
int lastWeekLesson = 0;
int lastWeekAct = 0;
if (lWeekDayS.Year != dateOff.Year)
{
if (lastLesson != null)
{
lastWeekLesson += ((int)lastLesson.SubMatrix(lWeekDayS.DayOfYear - 1, lastLesson.RowCount - lWeekDayS.DayOfYear + 1, 0, lessList.ColumnCount).ColumnSums().Sum());
}
lastWeekLesson += ((int)lessList.SubMatrix(0, lWeekDayE.DayOfYear, 0, lessList.ColumnCount).ColumnSums().Sum());
if (lastAct != null)
{
lastWeekAct += ((int)lastAct.SubMatrix(lWeekDayS.DayOfYear - 1, lastAct.RowCount - lWeekDayS.DayOfYear + 1, 0, lastAct.ColumnCount).ColumnSums().Sum());
}
lastWeekAct += ((int)lastAct.SubMatrix(0, lWeekDayE.DayOfYear, 0, lastAct.ColumnCount).ColumnSums().Sum());
}
else
{
lastWeekLesson = ((int)lessList.SubMatrix(lWeekDayS.DayOfYear - 1, lWeekDayE.DayOfYear - lWeekDayS.DayOfYear + 1, 0, lessList.ColumnCount).ColumnSums().Sum());
lastWeekAct = ((int)actList.SubMatrix(lWeekDayS.DayOfYear - 1, 7, 0, actList.ColumnCount).ColumnSums().Sum());
}
item.lesson.lastWeek = lastWeekLesson;
item.lesson.week = ((int)lessList.SubMatrix(weekDayS.DayOfYear - 1, (dateOff.DayOfYear - weekDayS.DayOfYear + 1), 0, lessList.ColumnCount).ColumnSums().Sum());
int lastTerm = 0;
if (lTermDayS.Year < dateOff.Year)
{
if (lastLesson != null)
{
lastTerm += ((int)lastLesson.SubMatrix(lTermDayS.DayOfYear - 1, lastStatsInfo.lesson.year.Count - lTermDayS.DayOfYear + 1, 0, 1).ColumnSums().Sum());
}
lastTerm += ((int)lessList.SubMatrix(0, lTermDayE.DayOfYear, 0, lessList.ColumnCount).ColumnSums().Sum());
}
else
{
lastTerm += ((int)lessList.SubMatrix(lTermDayS.DayOfYear - 1, lTermDayE.DayOfYear - lTermDayS.DayOfYear + 1, 0, lessList.ColumnCount).ColumnSums().Sum());
}
item.lesson.lastTerm = lastTerm;
int term = 0;
if (termDayE.Year > dateOff.Year)
{
if (lessList != null)
{
term = ((int)lessList.SubMatrix(termDayS.DayOfYear - 1, (lessList.RowCount - termDayS.DayOfYear + 1), 0, lessList.ColumnCount).ColumnSums().Sum());
}
}
else
{
if (termDayS.Year < dateOff.Year)
{
if (lastLesson != null)
{
term += ((int)lastLesson.SubMatrix(termDayS.DayOfYear - 1, lastLesson.RowCount - termDayS.DayOfYear + 1, 0, lastLesson.ColumnCount).ColumnSums().Sum());
}
term += ((int)lessList.SubMatrix(0, termDayE.DayOfYear, 0, lessList.ColumnCount).ColumnSums().Sum());
}
else
{
term = ((int)lessList.SubMatrix(termDayS.DayOfYear - 1, termDayE.DayOfYear - termDayS.DayOfYear + 1, 0, lessList.ColumnCount).ColumnSums().Sum());
}
}
item.lesson.term = term;
item.lesson.day = 0;
int lastMonth = 0;
int lastMonthInter = 0;
int lastMonthAct = 0;
if (lMonthS.Year < dateOff.Year)
{
if (lastLesson != null)
{
lastMonth = ((int)lastLesson.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, lastLesson.ColumnCount).ColumnSums().Sum());
}
if (lastInter != null)
{
lastMonthInter = ((int)lastInter.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, lastInter.ColumnCount).ColumnSums().Sum());
}
if (lastAct != null)
{
lastMonthAct = ((int)lastAct.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, lastAct.ColumnCount).ColumnSums().Sum());
}
}
else
{
lastMonth = ((int)lessList.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, lessList.ColumnCount).ColumnSums().Sum());
lastMonthInter = ((int)interList.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, interList.ColumnCount).ColumnSums().Sum());
lastMonthAct = ((int)actList.SubMatrix(lMonthS.DayOfYear - 1, lmthDay, 0, actList.ColumnCount).ColumnSums().Sum());
}
item.lesson.lastMonth = lastMonth;
item.lesson.month += ((int)lessList.SubMatrix(monthDayS.DayOfYear - 1, (dateOff.DayOfYear - monthDayS.DayOfYear + 1), 0, lessList.ColumnCount).ColumnSums().Sum());
item.lesson.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
int lastDayInter = 0;
if (lastDay.Year < dateOff.Year)
{
if (lastStatsInfo.lesson.yearInters.Count > 0)
{
lastDayInter = ((int)lastStatsInfo.lesson.yearInters[lastDay.DayOfYear - 1]);
}
}
else
{
lastDayInter = ((int)item.lesson.yearInters[lastDay.DayOfYear - 1]);
}
item.lesson.lastDayInter = lastDayInter;
item.lesson.dayInter = 0;
item.lesson.lastMonthInter = lastMonthInter;
item.lesson.monthInter = ((int)interList.SubMatrix(monthDayS.DayOfYear - 1, (dateOff.DayOfYear - monthDayS.DayOfYear + 1), 0, interList.ColumnCount).ColumnSums().Sum());
if (lastStatsInfo != null)
{
if (lastInter != null)
{
item.lesson.lastYearInter = ((int)lastInter.SubMatrix(0, lastInter.RowCount, 0, lastInter.ColumnCount).ColumnSums().Sum());
}
}
item.lesson.yearInter = ((int)item.lesson.yearInters.Sum());
item.activity.dayCnt = ((int)item.activity.year[dateOff.DayOfYear - 1]);
item.activity.lastDay = lastDayAct;
item.activity.lastWeek = lastWeekAct;
item.activity.week = ((int)actList.SubMatrix(weekDayS.DayOfYear - 1, (dateOff.DayOfYear - monthDayS.DayOfYear + 1), 0, actList.ColumnCount).ColumnSums().Sum());
item.activity.lastMonth = lastMonthAct;
int lastTermAct = 0;
if (lTermDayS.Year < dateOff.Year)
{
if (lastAct != null && lastAct.RowCount.Equals(actList.RowCount))
{
lastTermAct += ((int)lastAct.SubMatrix(lTermDayS.DayOfYear - 1, lastStatsInfo.activity.year.Count - lTermDayS.DayOfYear + 1, 0, lastAct.ColumnCount).ColumnSums().Sum());
}
lastTermAct += ((int)actList.SubMatrix(0, lTermDayE.DayOfYear, 0, actList.ColumnCount).ColumnSums().Sum());
}
else
{
lastTermAct += ((int)actList.SubMatrix(lTermDayS.DayOfYear - 1, lTermDayE.DayOfYear - lTermDayS.DayOfYear + 1, 0, actList.ColumnCount).ColumnSums().Sum());
}
item.activity.lastTerm = lastTerm;
int termAct = 0;
if (termDayS.Year > dateOff.Year)
{
if (lastAct != null)
{
termAct += ((int)lastAct.SubMatrix(termDayS.DayOfYear - 1, lastStatsInfo.activity.year.Count - termDayS.DayOfYear + 1, 0, lastAct.ColumnCount).ColumnSums().Sum());
}
termAct += ((int)actList.SubMatrix(0, termDayE.DayOfYear + 1, 0, actList.ColumnCount).ColumnSums().Sum());
}
else
{
if (lTermDayS.Year < dateOff.Year)
{
if (lastAct != null && lastAct.RowCount.Equals(actList.RowCount))
{
termAct += ((int)lastAct.SubMatrix(termDayS.DayOfYear - 1, lastStatsInfo.lesson.year.Count - termDayS.DayOfYear + 1, 0, 1).ColumnSums().Sum());
}
termAct += ((int)actList.SubMatrix(0, termDayE.DayOfYear, 0, actList.ColumnCount).ColumnSums().Sum());
}
else
{
termAct += ((int)actList.SubMatrix(termDayS.DayOfYear - 1, termDayE.DayOfYear - termDayS.DayOfYear + 1, 0, actList.ColumnCount).ColumnSums().Sum());
}
}
item.activity.term = term;
item.activity.upTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
statsInfos.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(item, item.id, new PartitionKey("Statistics")));
}
catch
{
scId.Add(item.schoolId);
}
}
if (statsInfos.Count < 256)
{
await Task.WhenAll(statsInfos);
}
else
{
int pages = (statsInfos.Count + 255) / 256;
for (int i = 0; i < pages; i++)
{
List>> itemResponses = statsInfos.Skip((i) * 256).Take(256).ToList();
await Task.WhenAll(itemResponses);
}
}
}
string str = "";
if (scId.Count > 0)
{
str = $"定时未清理学校id:{scId.ToJsonString()}";
}
else
{
str = "定时清理学校统计成功";
}
await _dingDing.SendBotMsg($"BI,{Environment.GetEnvironmentVariable("Option:Location")},SetStatsZeroPoint() {str}\n ", GroupNames.醍摩豆服務運維群組);
}
catch (Exception ex)
{
await _dingDing.SendBotMsg($"BI,{Environment.GetEnvironmentVariable("Option:Location")},SetStatsZeroPoint() \n{scId.ToJsonString()} \n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
}
}
}
}