|
@@ -0,0 +1,380 @@
|
|
|
+using Azure;
|
|
|
+using Azure.Cosmos;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+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;
|
|
|
+using TEAMModelOS.SDK.Models;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos.Common;
|
|
|
+using TEAMModelOS.SDK.Models.Service;
|
|
|
+using HTEXLib.COMM.Helpers;
|
|
|
+
|
|
|
+namespace TEAMModelOS.SDK
|
|
|
+{
|
|
|
+ public class ActivityService
|
|
|
+ {
|
|
|
+ public static async Task FixActivity(CosmosClient client, DingDing _dingDing, GroupChange groupChange, string type)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var query = $"SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime FROM c where c.pk='{type}' " +
|
|
|
+ $" and (( array_contains(c.classes,'{groupChange.listid}')) or ( array_contains(c.stuLists,'{groupChange.listid}'))or ( array_contains(c.tchLists,'{groupChange.listid}')))";
|
|
|
+ //$"and A1 in('{groupChange.listid}') ";
|
|
|
+ List<MQActivity> datas = new List<MQActivity>();
|
|
|
+ if (groupChange.scope.Equals("school", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(groupChange.school))
|
|
|
+ {
|
|
|
+ await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<MQActivity>(queryText: query,
|
|
|
+ requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{type}-{groupChange.school}") }))
|
|
|
+ {
|
|
|
+ datas.Add(item);
|
|
|
+ }
|
|
|
+ ///还要处理该学校每个老师发布的班级的
|
|
|
+ List<SchoolTeacher> teachers = new List<SchoolTeacher>();
|
|
|
+ await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<SchoolTeacher>(queryText: $"SELECT c.id, c.name FROM c",
|
|
|
+ requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{groupChange.school}") }))
|
|
|
+ {
|
|
|
+ teachers.Add(item);
|
|
|
+ }
|
|
|
+ foreach (var techer in teachers)
|
|
|
+ {
|
|
|
+ var queryTech = $"SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime FROM c " +
|
|
|
+ $" where c.school='{groupChange.school}' and c.pk='{type}'" +
|
|
|
+ $" and (( array_contains(c.classes,'{groupChange.listid}')) or ( array_contains(c.stuLists,'{groupChange.listid}')))";
|
|
|
+ // $" and A1 in('{groupChange.listid}') ";
|
|
|
+ await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<MQActivity>(queryText: queryTech,
|
|
|
+ requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{type}-{techer.id}") }))
|
|
|
+ {
|
|
|
+ datas.Add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (groupChange.scope.Equals("private", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(groupChange.creatorId))
|
|
|
+ {
|
|
|
+ await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<MQActivity>(queryText: query,
|
|
|
+ requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{type}-{groupChange.creatorId}") }))
|
|
|
+ {
|
|
|
+ datas.Add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ long nowtime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
+ foreach (MQActivity activity in datas)
|
|
|
+ {
|
|
|
+ //已经完结的不再允许加入,还未开始的。
|
|
|
+ if (activity.progress.Equals("finish") || activity.progress.Equals("pending"))
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ List<string> classes = ExamService.getClasses(activity.classes, activity.stuLists);
|
|
|
+ //stujoin新加入名单的
|
|
|
+ foreach (Member member in groupChange.stujoin)
|
|
|
+ {
|
|
|
+ var stucourse = new StuActivity
|
|
|
+ {
|
|
|
+ id = activity.id,
|
|
|
+ scode = activity.code,
|
|
|
+ name = activity.name,
|
|
|
+ code = $"Activity-{member.code.Replace("Base-", "")}-{member.id}",
|
|
|
+ scope = activity.scope,
|
|
|
+ school = activity.school,
|
|
|
+ creatorId = activity.creatorId,
|
|
|
+ pk = "Activity",
|
|
|
+ type = type,
|
|
|
+ subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
|
|
|
+ startTime = activity.startTime,
|
|
|
+ endTime = activity.endTime,
|
|
|
+ blob = activity.blob,
|
|
|
+ owner = activity.owner,
|
|
|
+ createTime = nowtime,
|
|
|
+ taskStatus = -1,
|
|
|
+ classIds = classes
|
|
|
+ };
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
|
|
|
+ }
|
|
|
+
|
|
|
+ //tmdjoin新加入的
|
|
|
+ foreach (Member member in groupChange.tmdjoin)
|
|
|
+ {
|
|
|
+ var stucourse = new StuActivity
|
|
|
+ {
|
|
|
+ id = activity.id,
|
|
|
+ scode = activity.code,
|
|
|
+ name = activity.name,
|
|
|
+ code = $"Activity-{member.id}",
|
|
|
+ scope = activity.scope,
|
|
|
+ school = activity.school,
|
|
|
+ creatorId = activity.creatorId,
|
|
|
+ pk = "Activity",
|
|
|
+ type = type,
|
|
|
+ subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
|
|
|
+ startTime = activity.startTime,
|
|
|
+ endTime = activity.endTime,
|
|
|
+ blob = activity.blob,
|
|
|
+ owner = activity.owner,
|
|
|
+ createTime = nowtime,
|
|
|
+ taskStatus = -1,
|
|
|
+ classIds = classes
|
|
|
+ };
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
|
|
|
+ }
|
|
|
+ //tchjoin新加入的
|
|
|
+ foreach (Member member in groupChange.tchjoin)
|
|
|
+ {
|
|
|
+ var stucourse = new StuActivity
|
|
|
+ {
|
|
|
+ id = activity.id,
|
|
|
+ scode = activity.code,
|
|
|
+ name = activity.name,
|
|
|
+ code = $"Activity-{member.id}",
|
|
|
+ scope = activity.scope,
|
|
|
+ school = activity.school,
|
|
|
+ creatorId = activity.creatorId,
|
|
|
+ pk = "Activity",
|
|
|
+ type = type,
|
|
|
+ subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
|
|
|
+ startTime = activity.startTime,
|
|
|
+ endTime = activity.endTime,
|
|
|
+ blob = activity.blob,
|
|
|
+ owner = activity.owner,
|
|
|
+ createTime = nowtime,
|
|
|
+ taskStatus = -1,
|
|
|
+ classIds = classes
|
|
|
+ };
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
|
|
|
+ }
|
|
|
+ foreach (Member member in groupChange.stuleave)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemAsync<StuActivity>(activity.id, new PartitionKey($"Activity-{member.code.Replace("Base-", "")}-{member.id}"));
|
|
|
+ }
|
|
|
+ catch (CosmosException)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ // 继续执行 删除失败
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach (Member member in groupChange.tmdleave)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemAsync<StuActivity>(activity.id, new PartitionKey($"Activity-{member.id}"));
|
|
|
+ }
|
|
|
+ catch (CosmosException)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ // 继续执行 删除失败
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach (Member member in groupChange.tchleave)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemAsync<StuActivity>(activity.id, new PartitionKey($"Activity-{member.id}"));
|
|
|
+ }
|
|
|
+ catch (CosmosException)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ // 继续执行 删除失败
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-GroupListService-FixActivity\n{ex.Message}{ex.StackTrace}{groupChange.ToJsonString()}{type}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static async Task FixStuCourse(CosmosClient client, DingDing _dingDing, GroupChange groupChange)
|
|
|
+ {
|
|
|
+ //1.查找学校或教师的课程是否包含该名单的课程。
|
|
|
+ var query = $"select distinct c.code,c.id,c.no,c.name,c.scope, c.creatorId,c.school from c join A0 in c.schedule where A0.stulist = '{groupChange.listid}'";
|
|
|
+ List<Course> courses = new List<Course>();
|
|
|
+ if (groupChange.scope.Equals("school") && !string.IsNullOrEmpty(groupChange.school))
|
|
|
+ {
|
|
|
+ await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Course>(queryText: query,
|
|
|
+ requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{groupChange.school}") }))
|
|
|
+ {
|
|
|
+ courses.Add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (groupChange.scope.Equals("private") && !string.IsNullOrEmpty(groupChange.creatorId))
|
|
|
+ {
|
|
|
+ await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Course>(queryText: query,
|
|
|
+ requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{groupChange.creatorId}") }))
|
|
|
+ {
|
|
|
+ courses.Add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ long nowtime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
+ // await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-GroupListService-FixStuCourse\n名单发生变更 需要处理的课程\n{courses.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ //2.获取课程的id 并尝试添加或移除对应的学生课程记录StuCourse。
|
|
|
+ foreach (var course in courses)
|
|
|
+ {
|
|
|
+ //学生新加入名单的
|
|
|
+ foreach (Member member in groupChange.stujoin)
|
|
|
+ {
|
|
|
+ var stucourse = new StuCourse
|
|
|
+ {
|
|
|
+ id = course.id,
|
|
|
+ scode = course.code,
|
|
|
+ name = course.name,
|
|
|
+ code = $"StuCourse-{member.code.Replace("Base-", "")}-{member.id}",
|
|
|
+ scope = course.scope,
|
|
|
+ school = course.school,
|
|
|
+ creatorId = course.creatorId,
|
|
|
+ pk = "StuCourse",
|
|
|
+ stulist = new List<string> { groupChange.listid },
|
|
|
+ createTime = nowtime
|
|
|
+ };
|
|
|
+ // await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-GroupListService-FixStuCourse\n名单发生变更 新建课程中间表\n{stucourse.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
|
|
|
+ }
|
|
|
+ //tmd新加入的
|
|
|
+ foreach (Member member in groupChange.tmdjoin)
|
|
|
+ {
|
|
|
+ var stucourse = new StuCourse
|
|
|
+ {
|
|
|
+ id = course.id,
|
|
|
+ scode = course.code,
|
|
|
+ name = course.name,
|
|
|
+ code = $"StuCourse-{member.id}",
|
|
|
+ scope = course.scope,
|
|
|
+ school = course.school,
|
|
|
+ creatorId = course.creatorId,
|
|
|
+ pk = "StuCourse",
|
|
|
+ stulist = new List<string> { groupChange.listid },
|
|
|
+ createTime = nowtime
|
|
|
+ };
|
|
|
+ // await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-GroupListService-FixStuCourse\n名单发生变更 新建课程中间表\n{stucourse.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
|
|
|
+ }
|
|
|
+ //移除名单的。 在点击相关的课程,再去二次校验是否存在,不存在则再去删除。
|
|
|
+ foreach (var delStu in groupChange.stuleave)
|
|
|
+ {
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemStreamAsync(course.id, new PartitionKey($"StuCourse-{delStu.code.Replace("Base-", "")}-{delStu.id}"));
|
|
|
+ }
|
|
|
+ foreach (var delTmd in groupChange.tmdleave)
|
|
|
+ {
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemStreamAsync(course.id, new PartitionKey($"StuCourse-{delTmd}"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static async Task<string> SaveStuActivity(CosmosClient client, DingDing _dingDing, List<StuActivity> stuActivities, List<StuActivity> tmdActivities, List<StuActivity> tchActivities)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (stuActivities.IsNotEmpty())
|
|
|
+ {
|
|
|
+ foreach (var x in stuActivities)
|
|
|
+ {
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(x, new PartitionKey(x.code));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (tmdActivities.IsNotEmpty())
|
|
|
+ {
|
|
|
+ foreach (var x in tmdActivities)
|
|
|
+ {
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(x, new PartitionKey(x.code));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (tchActivities.IsNotEmpty())
|
|
|
+ {
|
|
|
+ foreach (var x in tchActivities)
|
|
|
+ {
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync(x, new PartitionKey(x.code));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-SaveStuActivity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ public static async Task RefreshStuActivity(CosmosClient client, DingDing _dingDing, string id, string code)
|
|
|
+ {
|
|
|
+ MQActivity activity = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var aactivity = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync(id, new Azure.Cosmos.PartitionKey(code));
|
|
|
+ using var da = await JsonDocument.ParseAsync(aactivity.ContentStream);
|
|
|
+ activity = da.ToObject<MQActivity>();
|
|
|
+ }
|
|
|
+ catch (CosmosException)
|
|
|
+ {
|
|
|
+ activity = null;
|
|
|
+ }
|
|
|
+ if (activity != null)
|
|
|
+ {
|
|
|
+ List<string> classes = ExamService.getClasses(activity.classes, activity.stuLists);
|
|
|
+ (List<RMember> tmdIds, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(client, _dingDing, classes, activity.school);
|
|
|
+ var students = tmdIds.FindAll(x => x.type == 2);
|
|
|
+ var tmdids = tmdIds.FindAll(x => x.type == 1);
|
|
|
+ if (tmdids.IsNotEmpty())
|
|
|
+ {
|
|
|
+ foreach (RMember tmdid in tmdids)
|
|
|
+ {
|
|
|
+ var stucourse = new StuActivity
|
|
|
+ {
|
|
|
+ id = activity.id,
|
|
|
+ scode = activity.code,
|
|
|
+ name = activity.name,
|
|
|
+ code = $"Activity-{tmdid.id}",
|
|
|
+ source = activity.source,
|
|
|
+ scope = activity.scope,
|
|
|
+ school = activity.school,
|
|
|
+ creatorId = activity.creatorId,
|
|
|
+ pk = "Activity",
|
|
|
+ type = activity.pk,
|
|
|
+ subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
|
|
|
+ startTime = activity.startTime,
|
|
|
+ endTime = activity.endTime,
|
|
|
+ blob = activity.blob,
|
|
|
+ owner = activity.owner,
|
|
|
+ createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
|
|
+ taskStatus = -1,
|
|
|
+ classIds = classes
|
|
|
+ };
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (students.IsNotEmpty())
|
|
|
+ {
|
|
|
+ foreach (RMember student in students)
|
|
|
+ {
|
|
|
+ var stucourse = new StuActivity
|
|
|
+ {
|
|
|
+ id = activity.id,
|
|
|
+ scode = activity.code,
|
|
|
+ name = activity.name,
|
|
|
+ code = $"Activity-{activity.school}-{student.id}",
|
|
|
+ scope = activity.scope,
|
|
|
+ source = activity.source,
|
|
|
+ school = activity.school,
|
|
|
+ creatorId = activity.creatorId,
|
|
|
+ pk = "Activity",
|
|
|
+ type = activity.pk,
|
|
|
+ subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
|
|
|
+ startTime = activity.startTime,
|
|
|
+ endTime = activity.endTime,
|
|
|
+ blob = activity.blob,
|
|
|
+ owner = activity.owner,
|
|
|
+ createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
|
|
+ taskStatus = -1,
|
|
|
+ classIds = classes
|
|
|
+ };
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|