12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052 |
- using Azure.Core;
- using Azure.Cosmos;
- using Azure.Messaging.ServiceBus;
- using DocumentFormat.OpenXml.Drawing.Charts;
- using DocumentFormat.OpenXml.VariantTypes;
- using HTEXLib.COMM.Helpers;
- using MathNet.Numerics.Distributions;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Hosting;
- using OpenXmlPowerTools;
- using StackExchange.Redis;
- using System;
- using System.Collections.Generic;
- 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.Helper.Common.DateTimeHelper;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- using TEAMModelOS.SDK.Models.Cosmos.OpenEntity;
- using TEAMModelOS.SDK.Services;
- namespace TEAMModelOS.SDK.Models.Service
- {
- public class LessonService
- {
- public static readonly DateTime dateTime1970 = new DateTime(1970, 1, 1).ToLocalTime();
- public static Dictionary<string, object> GetLessonCond(JsonElement request)
- {
- Dictionary<string, object> dict = new Dictionary<string, object>();
- if (request.TryGetProperty("tmdid", out JsonElement tmdid))
- {
- if (tmdid.ValueKind.Equals(JsonValueKind.String) && !string.IsNullOrWhiteSpace($"{tmdid}"))
- {
- dict.Add("tmdid", tmdid);
- }
- else if(tmdid.ValueKind.Equals(JsonValueKind.Array)) {
- dict.Add("tmdid", tmdid);
- }
- }
- if (request.TryGetProperty("courseId", out JsonElement courseId) && !string.IsNullOrWhiteSpace($"{courseId}"))
- {
- dict.Add("courseId", courseId);
- }
- if (request.TryGetProperty("courseIds", out JsonElement courseIds))
- {
- dict.Add("courseId[*]", courseIds);
- }
- if (request.TryGetProperty("periodId", out JsonElement periodId) && !string.IsNullOrWhiteSpace($"{periodId}"))
- {
- dict.Add("periodId", periodId);
- }
- if (request.TryGetProperty("subjectId", out JsonElement subjectId))
- {
- dict.Add("subjectId", subjectId);
- }
- if (request.TryGetProperty("groupIds", out JsonElement groupIds))
- {
- dict.Add("groupIds[*]", groupIds);
- }
- if (request.TryGetProperty("grade", out JsonElement grade))
- {
- dict.Add("grade[*]", grade);
- }
- if (request.TryGetProperty("category", out JsonElement category))
- {
- dict.Add("category[*]", category);
- }
- if (request.TryGetProperty("doubleGreen", out JsonElement doubleGreen) && doubleGreen.GetBoolean())
- {
- dict.Add("=.tLevel", 2);
- dict.Add("=.pLevel", 2);
- //dict.Add(">=.tScore", 70);
- //dict.Add(">=.pScore", 70);
- }
- if (request.TryGetProperty("doubleRed", out JsonElement doubleRed) && doubleRed.GetBoolean())
- {
- dict.Add("=.tLevel", 0);
- dict.Add("=.pLevel", 0);
- //dict.Add(">=.tScore", 70);
- //dict.Add(">=.pScore", 70);
- }
- if (request.TryGetProperty("quality", out JsonElement quality) && quality.GetBoolean())
- {
- dict.Add(">=.discuss", 1);
- }
- if (request.TryGetProperty("excellent", out JsonElement excellent) && excellent.GetBoolean())
- {
- dict.Add(">=.excellent", 1);
- }
- if (request.TryGetProperty("name", out JsonElement name) && !string.IsNullOrWhiteSpace($"{name}"))
- {
- dict.Add("$.name", name);
- }
- if (request.TryGetProperty("today", out JsonElement today) && today.GetBoolean())
- {
- DateTime dateTimeA = Convert.ToDateTime(DateTimeOffset.UtcNow.ToString("D"));
- DateTime dateTimeB = Convert.ToDateTime(DateTimeOffset.UtcNow.ToString("D")).AddDays(1);
- double dayOf00_00_00 = (dateTimeA - dateTime1970).TotalMilliseconds;
- double day1Of00_00_00 = (dateTimeB - dateTime1970).TotalMilliseconds;
- dict.Add(">=.startTime", dayOf00_00_00);
- dict.Add("<.startTime", day1Of00_00_00);
- }
- if (request.TryGetProperty("week", out JsonElement week) && week.GetBoolean())
- {
- // DateTime dateTimeA = Convert.ToDateTime(DateTimeOffset.UtcNow.ToString("D"));
- DateTime dateTimeB = Convert.ToDateTime(DateTimeOffset.UtcNow.ToString("D")).AddDays(-7);
- double now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- double dayB = (dateTimeB - dateTime1970).TotalMilliseconds;
- dict.Add("<=.startTime", now);
- dict.Add(">=.startTime", dayB);
- }
- //查询即将到期的且没有被清理的。
- if (request.TryGetProperty("expire", out JsonElement expire) && expire.ValueKind.Equals(JsonValueKind.True))
- {
- dict.Add(">.expire", 0);
- dict.Add("!=.status", 404);
- }
- //查询已经清理的
- if (request.TryGetProperty("is404", out JsonElement is404) && is404.ValueKind.Equals(JsonValueKind.True))
- {
- dict.Add("=.status", 404);
- }
- //只查有效的
- if (request.TryGetProperty("isOk", out JsonElement isOk) && isOk.ValueKind.Equals(JsonValueKind.True))
- {
- dict.Add("<=.expire", 0);
- dict.Add("!=.status", 404);
- }
- if (request.TryGetProperty("month", out JsonElement month) && month.GetBoolean())
- {
- //DateTime dateTimeA = Convert.ToDateTime(DateTimeOffset.UtcNow.ToString("D"));
- DateTime dateTimeB = Convert.ToDateTime(DateTimeOffset.UtcNow.ToString("D")).AddDays(-30);
- double now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- double dayB = (dateTimeB - dateTime1970).TotalMilliseconds;
- dict.Add("<=.startTime", now);
- dict.Add(">=.startTime", dayB);
- }
- if (request.TryGetProperty("stime", out JsonElement stime) && !string.IsNullOrWhiteSpace($"{stime}"))
- {
- dict.Add(">=.startTime", stime);
- }
- if (request.TryGetProperty("etime", out JsonElement etime) && !string.IsNullOrWhiteSpace($"{etime}"))
- {
- dict.Add("<=.startTime", etime);
- }
- if (request.TryGetProperty("conds", out JsonElement conds) && conds.ValueKind.Equals(JsonValueKind.Array))
- {
- List<LessonSettingCond> settingConds = conds.Deserialize<List<LessonSettingCond>>();
- foreach (var item in settingConds)
- {
- dict.TryAdd($"{item.type}.{item.key}", item.val);
- //switch (item.type)
- //{
- // case ">=":
- // dict.TryAdd($">=.{item.key}",item.val);
- // break;
- // case "<=":
- // dict.TryAdd($"<=.{item.key}", item.val);
- // break;
- //}
- }
- }
- return dict;
- }
- public static async void DoLessonStudentRecord(DingDing _dingding, SnowflakeId snowflakeId, LessonRecord lessonRecord, string scope, CosmosClient client, string school, string tmdid,
- Teacher teacher, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, IConfiguration _configuration, LessonBase lessonBase,AzureRedisFactory _azureRedis)
- {
- try
- {
- int year = DateTimeOffset.UtcNow.Year;
- string hs = lessonBase?.report?.clientSummaryList.ToJsonString();
- var clientSummaryList = lessonBase?.report?.clientSummaryList?.Where(x => x.groupTaskCompleteCount != 0 || x.groupScore != 0 || x.score != 0 || x.tnteractScore != 0 || x.taskCompleteCount != 0);
- IEnumerable<LessonStudent> students = new List<LessonStudent>();
- if (clientSummaryList.Any())
- {
- var ids = clientSummaryList.Select(x => x.seatID);
- students = lessonBase.student.Where(x => ids.Contains(x.seatID));
- }
- List<Student> studentsBase = new List<Student>();
- var stuids = students.Where(x => x.type == 2);
- if (stuids.Any())
- {
- stuids.ToList().ForEach(x => {
- x.school = string.IsNullOrWhiteSpace(x.school) ? school : x.school;
- });
- var result= await client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetList<Student>($"select value c from c where c.id in ({string.Join(",",stuids.Select(d=>$"'{d.id}'"))})", $"Base-{school}");
- if (result.list.Any()) {
- studentsBase = result.list;
-
- }
- }
- var groups = stuids.Where(z => !string.IsNullOrWhiteSpace(z.school)).GroupBy(x => x.school).Select(y => new { code = y.Key, list = y.ToList() });
- List<StudentScoreRecord> lessonStudentRecords = new List<StudentScoreRecord>();
- foreach (var group in groups)
- {
- string stusql = $"select value(c) from c where c.stuid in({string.Join(",", group.list.Select(x => $"'{x.id}'"))}) and c.school='{group.code}' and c.year={year}";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryIterator<StudentScoreRecord>(queryText: stusql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StudentScoreRecord") }))
- {
- lessonStudentRecords.Add(item);
- }
- }
- var tmdids = students.Where(x => x.type == 1);
- if (tmdids.Any())
- {
- string tmdsql = $"select value(c) from c where c.tmdid in({string.Join(",", tmdids.Select(x => $"'{x}'"))}) and c.year={year}";
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryIterator<StudentScoreRecord>(queryText: tmdsql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StudentScoreRecord") }))
- {
- lessonStudentRecords.Add(item);
- }
- }
- School schoolBase = null;
- Period period = null;
- (Semester currSemester, int studyYear, DateTimeOffset date, DateTimeOffset nextSemester) dataSemester = new(null,-1, DateTimeOffset.UtcNow, DateTimeOffset.UtcNow) ;
- if (!string.IsNullOrWhiteSpace(school)) {
- schoolBase = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(school, new PartitionKey("Base"));
- period = schoolBase.period.Find(x => x.id.Equals($"{lessonRecord.periodId}"));
- dataSemester= SchoolService.GetSemester(period);
- }
- List <Task<ItemResponse<StudentScoreRecord>>> records = new List<Task<ItemResponse<StudentScoreRecord>>>();
- HashSet <OverallEducation> overallEducations = new HashSet<OverallEducation>();
- stuids.ToList().ForEach(async x => {
- var record = lessonStudentRecords.Find(l => l.stuid.Equals(x.id) && l.code.Equals($"StudentScoreRecord") && l.school.Equals(x.school));
- ClientSummaryList clientSummaryList = lessonBase.report.clientSummaryList.Find(c => c.seatID == x.seatID);
- if (record != null)
- {
- if (clientSummaryList != null)
- {
- var hasrecord= record.lessonRecords.Find(x => x.lessonId.Equals(lessonRecord.id));
- if (hasrecord != null)
- {
- hasrecord.gscore = clientSummaryList.groupScore;
- hasrecord.pscore = clientSummaryList.score;
- hasrecord.tscore = clientSummaryList.tnteractScore;
- hasrecord.tmdid = teacher.id;
- hasrecord.school = school;
- hasrecord.scope = lessonRecord.scope;
- hasrecord.lessonId = lessonRecord.id;
- hasrecord.courseId = lessonRecord.courseId;
- hasrecord.periodId = lessonRecord.periodId;
- hasrecord.subjectId = lessonRecord.subjectId;
- hasrecord.time = lessonRecord.startTime;
- }
- else {
- record.lessonRecords.Add(
- new StudentLessonRecord
- {
- gscore = clientSummaryList.groupScore,
- pscore = clientSummaryList.score,
- tscore = clientSummaryList.tnteractScore,
- tmdid = teacher.id,
- school = school,
- scope = lessonRecord.scope,
- lessonId = lessonRecord.id,
- courseId = lessonRecord.courseId,
- periodId = lessonRecord.periodId,
- subjectId = lessonRecord.subjectId,
- time = lessonRecord.startTime
- }
- );
- }
- }
- }
- else
- {
- record = new StudentScoreRecord
- {
- userType = Constant.ScopeStudent,
- id = $"{snowflakeId.NextId()}",
- year = year,
- stuid = x.id,
- school = x.school,
- code = $"StudentScoreRecord",
- pk = "StudentScoreRecord",
- ttl = -1,
- lessonRecords = new List<StudentLessonRecord> { new StudentLessonRecord
- {
- gscore = clientSummaryList.groupScore,
- pscore = clientSummaryList.score,
- tscore = clientSummaryList.tnteractScore,
- tmdid = teacher.id,
- school = school,
- scope = lessonRecord.scope,
- lessonId = lessonRecord.id,
- courseId = lessonRecord.courseId,
- periodId = lessonRecord.periodId,
- subjectId = lessonRecord.subjectId,
- time= lessonRecord.startTime
- }}
- };
- }
- record.userType = Constant.ScopeStudent;
- record.gscore = record.lessonRecords.Select(x => x.gscore).Sum();
- record.pscore = record.lessonRecords.Select(x => x.pscore).Sum();
- record.tscore = record.lessonRecords.Select(x => x.tscore).Sum();
- if (dataSemester.currSemester != null && (clientSummaryList.groupScore>0 || clientSummaryList.score > 0 || clientSummaryList.tnteractScore > 0)) {
- string oid = $"{dataSemester.studyYear}-{dataSemester.currSemester.id}-{record.stuid}";
- string ocode = $"OverallEducation-{school}";
- var student= studentsBase.Find(stu => stu.id.Equals(x.id));
- Azure.Response response = await client.GetContainer(Constant.TEAMModelOS, Constant.Student).ReadItemStreamAsync(oid, new PartitionKey(ocode));
- OverallEducation overallEducation = null;
- if (response.Status != 200) {
- overallEducation = new OverallEducation
- {
- id =oid,
- code = $"OverallEducation-{school}",
- pk = "OverallEducation",
- ttl = -1,
- name = x.name,
- classId = student?.classId,
- schoolCode = $"{school}",
- semesterId = dataSemester.currSemester.id,
- year = dataSemester.studyYear,
- periodId = $"{period.id}",
- stuYear = student.year,
- studentId = x.id,
- lessonScore= new List<StudentLessonRecord> { new StudentLessonRecord
- {
- gscore = clientSummaryList.groupScore,
- pscore = clientSummaryList.score,
- tscore = clientSummaryList.tnteractScore,
- tmdid = teacher.id,
- school = school,
- scope = lessonRecord.scope,
- lessonId = lessonRecord.id,
- courseId = lessonRecord.courseId,
- periodId = lessonRecord.periodId,
- subjectId = lessonRecord.subjectId,
- time= lessonRecord.startTime
- }}
- };
- }
- else {
- overallEducation=JsonDocument.Parse(response.Content).RootElement.ToObject<OverallEducation>();
- var hasrecord = overallEducation.lessonScore.Find(x => x.lessonId.Equals(lessonRecord.id));
- if (hasrecord != null)
- {
- hasrecord.gscore = clientSummaryList.groupScore;
- hasrecord.pscore = clientSummaryList.score;
- hasrecord.tscore = clientSummaryList.tnteractScore;
- hasrecord.tmdid = teacher.id;
- hasrecord.school = school;
- hasrecord.scope = lessonRecord.scope;
- hasrecord.lessonId = lessonRecord.id;
- hasrecord.courseId = lessonRecord.courseId;
- hasrecord.periodId = lessonRecord.periodId;
- hasrecord.subjectId = lessonRecord.subjectId;
- hasrecord.time = lessonRecord.startTime;
- }
- else
- {
- overallEducation.lessonScore.Add(
- new StudentLessonRecord
- {
- gscore = clientSummaryList.groupScore,
- pscore = clientSummaryList.score,
- tscore = clientSummaryList.tnteractScore,
- tmdid = teacher.id,
- school = school,
- scope = lessonRecord.scope,
- lessonId = lessonRecord.id,
- courseId = lessonRecord.courseId,
- periodId = lessonRecord.periodId,
- subjectId = lessonRecord.subjectId,
- time = lessonRecord.startTime
- }
- );
- }
- }
- overallEducations.Add(overallEducation);
- }
- records.Add(client.GetContainer(Constant.TEAMModelOS, Constant.Student).UpsertItemAsync(record, partitionKey: new PartitionKey(record.code)));
- });
- tmdids.ToList().ForEach(x => {
- var record = lessonStudentRecords.Find(l => l.tmdid.Equals(x.id) && l.code.Equals($"StudentScoreRecord"));
- ClientSummaryList clientSummaryList = lessonBase.report.clientSummaryList.Find(c => c.seatID == x.seatID);
- if (record != null)
- {
- if (clientSummaryList != null)
- {
- var hasrecord = record.lessonRecords.Find(x => x.lessonId.Equals(lessonRecord.id));
- if (hasrecord != null)
- {
- hasrecord.gscore = clientSummaryList.groupScore;
- hasrecord.pscore = clientSummaryList.score;
- hasrecord.tscore = clientSummaryList.tnteractScore;
- hasrecord.tmdid = teacher.id;
- hasrecord.school = school;
- hasrecord.scope = lessonRecord.scope;
- hasrecord.lessonId = lessonRecord.id;
- hasrecord.courseId = lessonRecord.courseId;
- hasrecord.periodId = lessonRecord.periodId;
- hasrecord.subjectId = lessonRecord.subjectId;
- hasrecord.time = lessonRecord.startTime;
- }
- else
- {
- record.lessonRecords.Add(
- new StudentLessonRecord
- {
- gscore = clientSummaryList.groupScore,
- pscore = clientSummaryList.score,
- tscore = clientSummaryList.tnteractScore,
- tmdid = teacher.id,
- school = school,
- scope = lessonRecord.scope,
- lessonId = lessonRecord.id,
- courseId = lessonRecord.courseId,
- periodId = lessonRecord.periodId,
- subjectId = lessonRecord.subjectId,
- time = lessonRecord.startTime
- }
- );
- }
- }
- }
- else
- {
- record = new StudentScoreRecord
- {
- userType = Constant.ScopeTmdUser,
- id = $"{snowflakeId.NextId()}",
- code = $"StudentScoreRecord",
- pk = "StudentScoreRecord",
- ttl = -1,
- year = year,
- tmdid = x.id,
- lessonRecords = new List<StudentLessonRecord>
- {
- new StudentLessonRecord
- {
- gscore = clientSummaryList.groupScore,
- pscore = clientSummaryList.score,
- tscore = clientSummaryList.tnteractScore,
- tmdid = teacher.id,
- school = school,
- scope = lessonRecord.scope,
- lessonId = lessonRecord.id,
- courseId = lessonRecord.courseId,
- periodId = lessonRecord.periodId,
- subjectId = lessonRecord.subjectId,
- time=lessonRecord.startTime
- }
- }
- };
- }
- record.userType = Constant.ScopeStudent;
- record.gscore = record.lessonRecords.Select(x => x.gscore).Sum();
- record.pscore = record.lessonRecords.Select(x => x.pscore).Sum();
- record.tscore = record.lessonRecords.Select(x => x.tscore).Sum();
- records.Add(client.GetContainer(Constant.TEAMModelOS, Constant.Student).UpsertItemAsync(record, partitionKey: new PartitionKey(record.code)));
-
- });
- if (records.Any())
- {
- await Task.WhenAll(records);
- }
- if (overallEducations.Any())
- {
- foreach (var item in overallEducations) {
- await client.GetContainer(Constant.TEAMModelOS, Constant.Student).UpsertItemAsync(item, partitionKey: new PartitionKey(item.code));
- string key = $"OverallEducation:{item.schoolCode}:{item.periodId}:{item.year}:{item.semesterId}:{item.classId}";
- await _azureRedis.GetRedisClient(8).HashSetAsync(key, item.studentId, item.ToJsonString());
- await _azureRedis.GetRedisClient(8).KeyExpireAsync(key,new TimeSpan(180 *24 ,0,0));
- }
- }
- }
- catch (Exception ex)
- {
- await _dingding.SendBotMsg($"学生个人课例统计信息异常,{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- }
- }
- public static async void DoAutoDeleteSchoolLessonRecord(LessonRecord lessonRecord, string scope, CosmosClient client, string school, string tmdid,
- Teacher teacher, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, IConfiguration _configuration, CoreAPIHttpService _coreAPIHttpService,DingDing _dingDing,AzureRedisFactory _azureRedis)
- {
- if (lessonRecord.scope.Equals("school"))
- {
- SchoolSetting setting = null;
- Azure.Response schoolSetting = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemStreamAsync(school, new PartitionKey("SchoolSetting"));
- School schoolBase = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(school, new PartitionKey("Base"));
- if (schoolSetting.Status == 200)
- {
- setting = JsonDocument.Parse(schoolSetting.Content).RootElement.Deserialize<SchoolSetting>();
- if (setting.lessonSetting != null)
- {
- //两种状态都不属于,则默认未开启。
- if (setting.lessonSetting.openAutoClean != 0 && setting.lessonSetting.openAutoClean != 1)
- {
- setting.lessonSetting.openAutoClean = 0;
- setting.lessonSetting.expireDays = Constant.school_lesson_expire;
- }
- else {
- //属于 要么开启1,要么关闭0
- }
- }
- else
- {
- setting.lessonSetting = new LessonSetting() { openAutoClean = 0, expireDays = Constant.school_lesson_expire };
- }
- }
- else
- {
- setting = new SchoolSetting() { lessonSetting = new LessonSetting { openAutoClean = 0, expireDays = Constant.school_lesson_expire } };
- }
- int school_lesson_expire = 0;
- bool save = true;
- List<string> msg = new List<string>();
- if (setting.lessonSetting.openAutoClean == 1)
- {
- if (setting.lessonSetting.conds.IsEmpty())
- {
- save = false;
- }
- else
- {
- school_lesson_expire = setting.lessonSetting.expireDays;
- foreach (var item in setting.lessonSetting.conds)
- {
- switch (item.type)
- {
- case ">=":
- switch (item.key)
- {
- case "attendRate":
- if (!(lessonRecord.attendRate >= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.attendRate}{item.type}{item.val}");
- }
- break;
- case "groupCount":
- if (!(lessonRecord.groupCount >= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.groupCount}{item.type}{item.val}");
- }
- break;
- case "totalPoint":
- if (!(lessonRecord.totalPoint >= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.totalPoint}{item.type}{item.val}");
- }
- break;
- case "collateTaskCount":
- if (!(lessonRecord.collateTaskCount >= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.collateTaskCount}{item.type}{item.val}");
- }
- break;
- case "collateCount":
- if (!(lessonRecord.collateCount >= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.collateCount}{item.type}{item.val}");
- }
- break;
- case "pushCount":
- if (!(lessonRecord.pushCount >= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.pushCount}{item.type}{item.val}");
- }
- break;
- case "totalInteractPoint":
- if (!(lessonRecord.totalInteractPoint >= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.totalInteractPoint}{item.type}{item.val}");
- }
- break;
- case "interactionCount":
- if (!(lessonRecord.interactionCount >= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.interactionCount}{item.type}{item.val}");
- }
- break;
- case "clientInteractionCount":
- if (!(lessonRecord.clientInteractionCount >= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.clientInteractionCount}{item.type}{item.val}");
- }
- break;
- case "examQuizCount":
- if (!(lessonRecord.examQuizCount >= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.examQuizCount}{item.type}{item.val}");
- }
- break;
- case "examPointRate":
- if (!(lessonRecord.examPointRate >= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.examPointRate}{item.type}{item.val}");
- }
- break;
- }
- break;
- case "<=":
- switch (item.key)
- {
- case "attendRate":
- if (!(lessonRecord.attendRate <= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.attendRate}{item.type}{item.val}");
- }
- break;
- case "groupCount":
- if (!(lessonRecord.groupCount <= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.groupCount}{item.type}{item.val}");
- }
- break;
- case "totalPoint":
- if (!(lessonRecord.totalPoint <= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.totalPoint}{item.type}{item.val}");
- }
- break;
- case "collateTaskCount":
- if (!(lessonRecord.collateTaskCount <= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.collateTaskCount}{item.type}{item.val}");
- }
- break;
- case "collateCount":
- if (!(lessonRecord.collateCount <= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.collateCount}{item.type}{item.val}");
- }
- break;
- case "pushCount":
- if (!(lessonRecord.pushCount <= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.pushCount}{item.type}{item.val}");
- }
- break;
- case "totalInteractPoint":
- if (!(lessonRecord.totalInteractPoint <= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.totalInteractPoint}{item.type}{item.val}");
- }
- break;
- case "interactionCount":
- if (!(lessonRecord.interactionCount <= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.interactionCount}{item.type}{item.val}");
- }
- break;
- case "clientInteractionCount":
- if (!(lessonRecord.clientInteractionCount <= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.clientInteractionCount}{item.type}{item.val}");
- }
- break;
- case "examQuizCount":
- if (!(lessonRecord.examQuizCount <= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.examQuizCount}{item.type}{item.val}");
- }
- break;
- case "examPointRate":
- if (!(lessonRecord.examPointRate <= item.val))
- {
- save = false;
- msg.Add($"{item.key}:{lessonRecord.examPointRate}{item.type}{item.val}");
- }
- break;
- }
- break;
- }
- }
- }
- }
- /* 注释start 2023.3.22,该段逻辑在开课的时候已经处理,详见ActiveTaskTopic. LessonRecordEvent create ,无论有没有开启setting.lessonSetting.openAutoClean 自动删除策略,只要空间不足都会对课例进行自动清理管理。
- else
- {
- ///未设置openAutoClean=1时,则检查学校使用空间是否充足。
-
- double usize = 0;
- int tsize = schoolBase.tsize;
- //schoolBase.tsize
- //計算學校或個人的使用空間
- RedisValue redisValue = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", $"{schoolBase.id}");
- if (redisValue.HasValue && long.TryParse(redisValue.ToString(), out var bsize))
- {
- usize = Math.Round(bsize / 1073741824.0 - (tsize), 2, MidpointRounding.AwayFromZero); //1073741824 1G
- }
- else //如果檢測不到緩存,觸發刷新計算空間
- {
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = "records", name = $"{schoolBase.id}" }, _serviceBus, _configuration, _azureRedis);
- }
- ///空间充足的情况保存。
- if (schoolBase.size - usize > 0)
- {
- save = true;
- }
- else {
- save = false;
- school_lesson_expire = Constant.school_lesson_expire;
- }
- } 注释end 2023.3.22,该段逻辑在开课的时候已经处理,详见ActiveTaskTopic. LessonRecordEvent create ,无论有没有开启setting.lessonSetting.openAutoClean 自动删除策略,只要空间不足都会对课例进行自动清理管理。
- */
- if (!save && school_lesson_expire > 0)
- {
- // 1-时间戳,7-时间戳
- Dictionary<int, ExpireTag> result = new Dictionary<int, ExpireTag>();
- //暂定7天
- var now = DateTimeOffset.UtcNow;
- //剩余3天的通知
- //var day3= now.AddDays(school_lesson_expire - 3).ToUnixTimeMilliseconds();
- //result.Add(3, day3);
- //剩余1天的通知
- var day1 = now.AddDays(school_lesson_expire - (school_lesson_expire - 1)).ToUnixTimeMilliseconds();
- result.Add(1, new ExpireTag { expire = day1, tag = "notification" });
- //到期通知
- //不到五点上传的课例,七天之后直接删除。
- int addSecond = 0;
- if (now.Hour > 5)
- {
- // 到凌晨00点还差 (24 - now.Hour) *60 * 60 分钟,再加天数;
- addSecond = school_lesson_expire * 86400 + (24 - now.Hour) * 3600 - (now.Hour * 3600);
- //再加 00到05小时内的 随机秒数
- Random rand = new Random();
- int randInt = rand.Next(0, 18000);
- addSecond += randInt;
- }
- else
- {
- addSecond = school_lesson_expire * 24 * 60 * 60;
- }
- lessonRecord.expire = now.AddSeconds(addSecond).ToUnixTimeMilliseconds();
- result.Add(school_lesson_expire, new ExpireTag { expire = lessonRecord.expire, tag = "delete" });
- // result.Add(school_lesson_expire, lessonRecord.expire);
- string biz = "expire";
-
- string expireTime = DateTimeOffset.FromUnixTimeMilliseconds(lessonRecord.expire).ToString("yyyy-MM-dd HH:mm:ss");
- Teacher targetTeacher = await client.GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>($"{tmdid}", new PartitionKey($"Base"));
- _coreAPIHttpService.PushNotify(new List<IdNameCode> { new IdNameCode { id = targetTeacher.id, name = targetTeacher.name, code = targetTeacher.lang } }, "expire-school_lessonRecord", Constant.NotifyType_IES5_Course,
- new Dictionary<string, object> { { "tmdid", teacher.id }, { "tmdname", teacher.name }, { "schoolName", schoolBase.name },
- { "schoolId", $"{school}" },{ "lessonId",lessonRecord.id }, { "expireTime", expireTime },{ "lessonName",lessonRecord.name } }, $"{Environment.GetEnvironmentVariable("Option:Location")}", _configuration, _dingDing, "");
- var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
- List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", lessonRecord.id } });
- if (records.Count <= 0)
- {
- foreach (var item in result)
- {
- string PartitionKey = string.Format("{0}{1}{2}", lessonRecord.code, "-", $"expire-{item.Key}");
- //课堂的id ,
- //课堂的通知时间类型progress, 默认就会发送一条,到期前一天发送一条,最后已到期发送一条。
- var message = new ServiceBusMessage(new
- {
- id = lessonRecord.id,
- progress = item.Key,
- code = lessonRecord.code,
- scope = lessonRecord.scope,
- school = lessonRecord.school,
- opt = "delete",
- expire = lessonRecord.expire,
- tmdid = tmdid,
- tmdname = teacher.name,
- name = lessonRecord.name,
- startTime = lessonRecord.startTime,
- tag = item.Value.tag
- }.ToJsonString());
- message.ApplicationProperties.Add("name", "LessonRecordExpire");
- long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(item.Value.expire));
- ChangeRecord changeRecord = new ChangeRecord
- {
- RowKey = lessonRecord.id,
- PartitionKey = PartitionKey,
- sequenceNumber = start,
- msgId = message.MessageId
- };
- await table.Save<ChangeRecord>(changeRecord);
- }
- }
- }
- else
- {
- if (lessonRecord.expire > 0)
- {
- var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
- List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", lessonRecord.id } });
- foreach (var record in records)
- {
- try
- {
- await table.DeleteSingle<ChangeRecord>(record.PartitionKey, record.RowKey);
- await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
- }
- catch (Exception)
- {
- continue;
- }
- }
- }
- lessonRecord.save = 1;
- lessonRecord.expire = -1;
- }
- }
- }
- public record ExpireTag
- {
- public long expire { get; set; }
- public string tag { get; set; }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="client"></param>
- /// <param name="_dingDing"></param>
- /// <param name="data"></param>
- /// <returns></returns>
- public static LessonDis DisLessonCount(LessonRecord oldRecord, LessonRecord newRecord, LessonDis lessonDis)
- {
- //创建课堂的情况
- if (oldRecord == null && newRecord != null)
- {
- lessonDis.record = 1;
- }
- //删除数据的情况
- //不再对LessonCount进行减
- else if (oldRecord != null && newRecord == null)
- {
- /*lessonDis.record = -1;
- //P分数量加减
- if (oldRecord.pScore >= 70)
- {
- lessonDis.disPCount = -1;
- }
- //T分数量加减
- if (oldRecord.tScore >= 70)
- {
- lessonDis.disTCount = -1;
- }
- if (oldRecord.tScore >= 70 && oldRecord.pScore >= 70)
- {
- lessonDis.disTCount = -1;
- }*/
- }
- //无效操作
- else if (oldRecord == null && newRecord == null)
- {
- }
- //前后操作都有值,则表示更新
- else
- {
- //P分数量加减
- if (oldRecord.pScore >= 70)
- {
- if (newRecord.pScore < 70)
- {
- lessonDis.disPCount = -1;
- }
- }
- else
- {
- if (newRecord.pScore >= 70)
- {
- lessonDis.disPCount = 1;
- }
- }
- //T分数量加减
- if (oldRecord.tScore >= 70)
- {
- if (newRecord.tScore < 70)
- {
- lessonDis.disTCount = -1;
- }
- }
- else
- {
- if (newRecord.tScore >= 70)
- {
- lessonDis.disTCount = 1;
- }
- }
- //双绿灯数量
- if (oldRecord.tScore >= 70 && oldRecord.pScore >= 70)
- {
- if (newRecord.tScore < 70 || newRecord.pScore < 70)
- {
- lessonDis.disDCount = -1;
- }
- }
- else
- {
- if (newRecord.tScore >= 70 && newRecord.pScore >= 70)
- {
- lessonDis.disDCount = 1;
- }
- }
- }
- return lessonDis;
- }
- public static LessonDis DisLessonCount_2(LessonRecord oldRecord, LessonRecord newRecord, LessonDis lessonDis)
- {
- //创建课堂的情况
- if (oldRecord == null && newRecord != null)
- {
- lessonDis.record = 1;
- //P分数量加减
- if (newRecord.pScore >= 70)
- {
- lessonDis.disPCount = 1;
- }
- //T分数量加减
- if (newRecord.tScore >= 70)
- {
- lessonDis.disTCount = 1;
- }
- //双绿灯数量
- if (newRecord.tScore >= 70 && newRecord.pScore >= 70)
- {
- lessonDis.disDCount = 1;
- }
- }
- return lessonDis;
- }
- public static async Task FixLessonCount(CosmosClient client, DingDing _dingDing, LessonRecord record, LessonRecord oldRecord, LessonDis lessonDis)
- {
- LessonRecord data = null;
- try
- {
- if (record != null && oldRecord == null)
- {
- data = record;
- }
- if (record == null && oldRecord != null)
- {
- data = oldRecord;
- }
- if (record != null && oldRecord != null)
- {
- data = record;
- }
- int day = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).DayOfYear;
- int year = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).Year;
- int days = DateTimeHelper.getDays(year);
- //int years = DateTimeOffset.UtcNow.DayOfYear;
- string tbname = string.Empty;
- string code = string.Empty;
- if (data.groupIds.Any()) {
- if (data.scope != null && data.scope.Equals("school"))
- {
- if (string.IsNullOrEmpty(data.periodId))
- {
- code = $"LessonCount-{data.school}-{year}";
- tbname = "School";
- }
- else
- {
- code = $"LessonCount-{data.school}-{year}-{data.periodId}";
- tbname = "School";
- }
- }
- else
- {
- code = $"LessonCount-{year}";
- tbname = "Teacher";
- }
- var response = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync(data.tmdid.ToString(), new PartitionKey(code));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- LessonCount count = json.ToObject<LessonCount>();
- count.tCount[day - 1] += lessonDis.disTCount;
- count.pCount[day - 1] += lessonDis.disPCount;
- count.ptCount[day - 1] += lessonDis.disDCount;
- count.beginCount[day - 1] += lessonDis.record;
- await client.GetContainer("TEAMModelOS", tbname).ReplaceItemAsync(count, count.id, new PartitionKey(code));
- }
- else
- {
- LessonCount count = new()
- {
- id = data.tmdid,
- code = code,
- ttl = -1
- };
- double[] da = new double[days];
- List<double> list = new(da);
- List<double> listT = new(da);
- List<double> listP = new(da);
- List<double> listPT = new(da);
- list[day - 1] += lessonDis.record;
- listT[day - 1] += lessonDis.disTCount;
- listP[day - 1] += lessonDis.disPCount;
- listPT[day - 1] += lessonDis.disDCount;
- count.beginCount.AddRange(list);
- count.tCount.AddRange(listT);
- count.pCount.AddRange(listP);
- count.ptCount.AddRange(listPT);
- //count.courseIds.Add(data.courseId);
- await client.GetContainer("TEAMModelOS", tbname).CreateItemAsync(count, new PartitionKey(code));
- }
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-LessonCount-FixLessonCount\n{ex.Message}\n{ex.StackTrace}{data.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
- }
- }
- }
- }
|