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 StuListService { public static 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, blob=activity.blob, owner= activity.owner }; 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() { "" }, startTime = activity.startTime, endTime = activity.endTime, blob = activity.blob, owner = activity.owner }; await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code)); } } } public static 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}")); //} } } } }