using Azure.Cosmos; using Microsoft.Azure.WebJobs; using System; using System.Collections.Generic; using System.Text; using System.Text.Json; using System.Threading.Tasks; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Helper.Common.CollectionHelper; using TEAMModelOS.SDK.Models; using TEAMModelOS.SDK.Models.Cosmos.Common; namespace TEAMModelFunction { public class MQActivity { public string id { get; set; } public string code { get; set; } public string owner { get; set; } public List classes { get; set; } //如果已经完成则不写入??? public string progress { get; set; } public string scope { get; set; } public string school { get; set; } public string creatorId { get; set; } public string pk { get; set; } public string name { get; set; } public List subjects { get; set; } public string blob { get; set; } public long startTime { get; set; } public long endTime { get; set; } } public class StuListServiceBus { private readonly AzureCosmosFactory _azureCosmos; private readonly DingDing _dingDing; public StuListServiceBus(AzureCosmosFactory azureCosmos, DingDing dingDing) { _azureCosmos = azureCosmos; _dingDing = dingDing; } /// /// 完善课程变更,StuListChange, originCode是学校编码 则表示名单是学校自定义名单,如果是tmdid则表示醍摩豆的私有名单,scope=school,private。 /// /// /// CourseChange ///// /// /// [FunctionName("StuList")] public async Task StuList([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "stulist", Connection = "Azure:ServiceBus:ConnectionString")] string msg) { var client = _azureCosmos.GetCosmosClient(); try { await _dingDing.SendBotMsg($"ServiceBus,StuList:{msg}", GroupNames.醍摩豆服務運維群組); var jsonMsg = JsonDocument.Parse(msg); StuListChange stuListChange = msg.ToObject(); //名单变动修改学生课程关联信息 await FixStuCourse(client, stuListChange); //Vote投票 Survey问卷 Exam评测 Learn学习活动 Homework作业活动 //名单变动修改学生问卷关联信息 await FixActivity(client, stuListChange, "Survey"); //名单变动修改学生投票关联信息 await FixActivity(client, stuListChange, "Vote"); //名单变动修改学生评测关联信息 await FixActivity(client, stuListChange, "Exam"); //TODO学习活动 //await FixActivity(client, stuListChange, "Learn"); //TODO作业活动 // await FixActivity(client, stuListChange, "Homework"); } catch (Exception ex) { await _dingDing.SendBotMsg($"StuListServiceBus-StuList\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組); } } public async Task FixActivity(CosmosClient client, StuListChange stuListChange, string type) { var query = $"SELECT distinct c.owner, c.id,c.code, c.classes,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime FROM c join A1 in c.classes where c.pk='{type}' and A1 in('{stuListChange.listid}') "; List datas = new List(); await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{type}-{stuListChange.originCode}") })) { datas.Add(item); } foreach (MQActivity activity in datas) { //已经完结的不再允许加入 if (activity.progress.Equals("finish")||activity.progress.Equals("pending")) { continue; } //学生新加入名单的 foreach (Students students in stuListChange.stujoin) { var stucourse = new StuActivity { id = activity.id, scode = activity.code, name = activity.name, code = $"Activity-{activity.school}-{students.id}", scope = activity.scope, school = activity.school, creatorId = activity.creatorId, pk = "Activity", type = type, subjects = type.Equals("Exam") ? activity.subjects : new List() { "" }, startTime = activity.startTime, endTime = activity.endTime }; await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code)); }//tmd新加入的 foreach (string tmdid in stuListChange.tmdjoin) { var stucourse = new StuActivity { id = activity.id, scode = activity.code, name = activity.name, code = $"Activity-{tmdid}", scope = activity.scope, school = activity.school, creatorId = activity.creatorId, pk = "Activity", type = type, subjects= type.ToLower().Equals("Exam")?activity.subjects:new List() {"" } }; await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code)); } } } public async Task FixStuCourse(CosmosClient client, StuListChange stuListChange) { //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 = '{stuListChange.listid}'"; List courses = new List(); if (stuListChange.scope.Equals("school")) { await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{stuListChange.originCode}") })) { courses.Add(item); } } else { await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{stuListChange.originCode}") })) { courses.Add(item); } } //2.获取课程的id 并尝试添加或移除对应的学生课程记录StuCourse。 foreach (var course in courses) { //学生新加入名单的 foreach (Students students in stuListChange.stujoin) { var stucourse = new StuCourse { id = course.id, scode = course.code, name = course.name, code = $"StuCourse-{course.school}-{students.id}", scope = course.scope, school = course.school, creatorId = course.creatorId, pk = "StuCourse" }; await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code)); } //tmd新加入的 foreach (string tmdid in stuListChange.tmdjoin) { var stucourse = new StuCourse { id = course.id, scode = course.code, name = course.name, code = $"StuCourse-{tmdid}", scope = course.scope, school = course.school, creatorId = course.creatorId, pk = "StuCourse" }; await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code)); } //移除名单的。 在点击相关的课程,再去二次校验是否存在,不存在则再去删除。 //foreach (var delStu in stuListChange.stuleave) //{ // await client.GetContainer("TEAMModelOS", "Student").DeleteItemStreamAsync(course.id, new PartitionKey($"Course-{course.school}-{delStu.id}")); //} //foreach (var delTmd in stuListChange.tmdhleave) //{ // await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(course.id, new PartitionKey($"Course-{delTmd}")); //} } } } }