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; namespace TEAMModelFunction { public class TriggerStuActivity { public static async Task RefreshStuActivity(CosmosClient client, DingDing _dingDing, string id, string code) { MQActivity activity = null; try { var aactivity = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id, new Azure.Cosmos.PartitionKey(code)); using var da = await JsonDocument.ParseAsync(aactivity.ContentStream); activity = da.ToObject(); } catch (CosmosException ex) { } if (activity != null) { (List tmdids, List students,List classLists) = await GetStuList(client, _dingDing, activity.classes, activity.school); if (tmdids.IsNotEmpty()) { foreach (TmdInfo tmdid in tmdids) { var stucourse = new StuActivity { id = activity.id, scode = activity.code, name = activity.name, code = $"Activity-{tmdid.id}", 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() { activity.subjects[0].id } : new List() { "" }, startTime = activity.startTime, endTime = activity.endTime, blob = activity.blob, owner = activity.owner, createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), taskStatus = -1 }; await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code)); } } if (students.IsNotEmpty()) { foreach (StuInfo 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, school = activity.school, creatorId = activity.creatorId, pk = "Activity", type = activity.pk, subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List() { activity.subjects[0].id } : new List() { "" }, startTime = activity.startTime, endTime = activity.endTime, blob = activity.blob, owner = activity.owner, createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), taskStatus = -1 }; await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code)); } } } } public static async Task SaveStuActivity(CosmosClient client, DingDing _dingDing, List stuActivities,List tmdActivities) { try { if (stuActivities.IsNotEmpty()) { foreach (var x in stuActivities) { await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(x, new PartitionKey(x.code)); } } if (tmdActivities.IsNotEmpty()) { foreach (var x in tmdActivities) { await client.GetContainer("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> GetClassInfo(CosmosClient client, DingDing _dingDing, List classes, string school) { try { List classInfos = new List(); if (classes.IsNotEmpty()) { List sqlList = new List(); classes.ForEach(x => { sqlList.Add($" '{x}' "); }); string sql = string.Join(" , ", sqlList); if (!string.IsNullOrEmpty(school)) { await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator(queryText: $"select c.id,c.name ,c.periodId,c.year from c where c.id in ({sql})", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") })) { classInfos.Add(item); } await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator(queryText: $"select c.id,c.name from c where c.id in ({sql})", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") })) { //item.from = "SchStuList"; classInfos.Add(item); } } List tchLists = new List(); await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator(queryText: $"select c.id,c.name from c where c.id in ({sql})", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") })) { // item.from = "TchStuList"; classInfos.Add(item); } } return classInfos; } catch (Exception ex) { await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetClassInfo\n{ex.Message}\n{ex.StackTrace}\n class{classes.ToJsonString()},{school}", GroupNames.醍摩豆服務運維群組); throw new Exception(ex.Message, ex); } } public static async Task<(List tmdinfos, List students, List classInfo)> GetStuListInStu(CosmosClient client, DingDing _dingDing, List claes, string school) { try { List classes = new List(); foreach (string ss in claes) { classes.Add(ss); } List tmdinfos = new List(); List studentss = new List(); List tmdids = new List(); List stuInfos = new List(); if (!classes.IsNotEmpty()) { return (tmdinfos, new List(), null); } List sqlList = new List(); classes.ForEach(x => { sqlList.Add($" '{x}' "); }); string sql = string.Join(" , ", sqlList); List schList = new List(); List students = new List(); if (!string.IsNullOrEmpty(school)) { await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator(queryText: $"select value(c) from c where c.id in ({sql})", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") })) { schList.Add(item); } await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator(queryText: $"select value(c) from c where c.classId in ({sql})", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") })) { students.Add(item); } } List tchLists = new List(); await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator(queryText: $"select value(c) from c where c.id in ({sql})", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") })) { tchLists.Add(item); } foreach (var x in schList) { if (x.students.IsNotEmpty()) { studentss.AddRange(x.students); } if (x.tmids.IsNotEmpty()) { tmdids.AddRange(x.tmids); } classes.Remove(x.id); } foreach (var x in tchLists) { if (x.students.IsNotEmpty()) { studentss.AddRange(x.students); } if (x.tmids.IsNotEmpty()) { tmdids.AddRange(x.tmids); } classes.Remove(x.id); } if (tmdids.IsNotEmpty()) { List infos = new List(); List inids = new List(); tmdids.ForEach(x => { inids.Add($"'{x}'"); }); var insql = string.Join(",", inids); var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})"; await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") })) { infos.Add(item); } tmdinfos.AddRange(infos); } if (studentss.IsNotEmpty()) { var stuGroups = studentss.GroupBy(x => x.code).ToList().Select(x => new { key = x.Key, list = x.ToList() }) ; foreach (var gp in stuGroups) { List inidstus = new List(); gp.list.Select(x => x.id).ToList().ForEach(x => { inidstus.Add($"'{x}'"); }); PartitionKey partitionKey = new PartitionKey($"Base-{gp.key.Replace("Base-", "")}"); var insqlstu = string.Join(",", inidstus); var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id in ({insqlstu})"; await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey= partitionKey })) { stuInfos.Add(item); } } } students.ForEach(x => { if (!stuInfos.Select(y => y.classId).ToList().Contains(x.id)) { stuInfos.Add(new StuInfo { id = x.id, code = x.code, schoolId = x.schoolId, classId = x.classId, name = x.name, picture = x.picture, year = x.year }); } }); List classInfo = new List(); schList.ForEach(x => { ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name }; if (x.students.IsNotEmpty()) { x.students.ForEach(y => { var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault(); if (stuinfo != null) { classListInfo.stuInfos.Add(stuinfo); } }); } if (x.tmids.IsNotEmpty()) { x.tmids.ForEach(y => { var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault(); if (tmdinfo != null) { classListInfo.tmdInfos.Add(tmdinfo); } }); } classInfo.Add(classListInfo); }); tchLists.ForEach(x => { ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name }; if (x.students.IsNotEmpty()) { x.students.ForEach(y => { var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault(); if (stuinfo != null) { classListInfo.stuInfos.Add(stuinfo); } }); } if (x.tmids.IsNotEmpty()) { x.tmids.ForEach(y => { var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault(); if (tmdinfo != null) { classListInfo.tmdInfos.Add(tmdinfo); } }); } classInfo.Add(classListInfo); }); //var classeids= students.GroupBy(x => x.classId).Select(x => x.Key).ToList(); List classInfos = await GetClassInfo(client, _dingDing, classes, school); classInfos.ForEach(x => { ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name }; var list = students.Where(y => y.classId .Equals(x.id)).ToList(); list.ForEach(z => { classListInfo.stuInfos.Add(new StuInfo { id = z.id, code = z.code, schoolId = z.schoolId, classId = z.classId, name = z.name, picture = z.picture, year = z.year }); }); classInfo.Add(classListInfo); }); return (tmdinfos, stuInfos, classInfo); } catch (Exception ex) { await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetStuList\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組); } return (null, null, null); } public static async Task<(List tmdinfos, List students,ListclassInfo)> GetStuList(CosmosClient client, DingDing _dingDing, List claes, string school) { try { List classes = new List(); foreach (string ss in claes) { classes.Add(ss); } List tmdinfos = new List(); List studentss = new List(); List tmdids = new List(); List stuInfos = new List(); if (!classes.IsNotEmpty()) { return (tmdinfos, new List(),null); } List sqlList = new List(); classes.ForEach(x => { sqlList.Add($" '{x}' "); }); string sql = string.Join(" , ", sqlList); List schList = new List(); List students = new List(); if (!string.IsNullOrEmpty(school)) { await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator(queryText: $"select value(c) from c where c.id in ({sql})", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") })) { schList.Add(item); } await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator(queryText: $"select value(c) from c where c.classId in ({sql})", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") })) { students.Add(item); } } List tchLists = new List(); await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator(queryText: $"select value(c) from c where c.id in ({sql})", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") })) { tchLists.Add(item); } foreach (var x in schList) { if (x.students.IsNotEmpty()) { studentss.AddRange(x.students); } if (x.tmids.IsNotEmpty()) { tmdids.AddRange(x.tmids); } classes.Remove(x.id); } foreach (var x in tchLists) { if (x.students.IsNotEmpty()) { studentss.AddRange(x.students); } if (x.tmids.IsNotEmpty()) { tmdids.AddRange(x.tmids); } classes.Remove(x.id); } if (tmdids.IsNotEmpty()) { List infos = new List(); List inids = new List(); tmdids.ForEach(x => { inids.Add($"'{x}'"); }); var insql = string.Join(",", inids); var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})"; await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") })) { infos.Add(item); } tmdinfos.AddRange(infos); } if (studentss.IsNotEmpty()) { List inidstus = new List(); foreach (Students stu in studentss) { var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id = '{stu.id}'"; await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{stu.code}") })) { stuInfos.Add(item); } } /*studentss.Select(x => x.id).ToList().ForEach(x => { inidstus.Add($"'{x}'"); }); var insqlstu = string.Join(",", inidstus); var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id in ({insqlstu})"; await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{school}") })) { stuInfos.Add(item); }*/ } students.ForEach(x => { if (!stuInfos.Select(y => y.classId).ToList().Contains(x.id)) { stuInfos.Add(new StuInfo { id = x.id, code = x.code, schoolId = x.schoolId, classId = x.classId, name = x.name, picture = x.picture, year = x.year }); } }); List classInfo = new List(); schList.ForEach(x => { ClassListInfo classListInfo = new ClassListInfo {id=x.id,name=x.name }; if (x.students.IsNotEmpty()) { x.students.ForEach(y => { var stuinfo= stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault(); if (stuinfo != null) { classListInfo.stuInfos.Add(stuinfo); } }); } if (x.tmids.IsNotEmpty()) { x.tmids.ForEach(y => { var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault(); if (tmdinfo != null) { classListInfo.tmdInfos.Add(tmdinfo); } }); } classInfo.Add(classListInfo); }); tchLists.ForEach(x => { ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name }; if (x.students.IsNotEmpty()) { x.students.ForEach(y => { var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault(); if (stuinfo != null) { classListInfo.stuInfos.Add(stuinfo); } }); } if (x.tmids.IsNotEmpty()) { x.tmids.ForEach(y => { var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault(); if (tmdinfo != null) { classListInfo.tmdInfos.Add(tmdinfo); } }); } classInfo.Add(classListInfo); }); //var classeids= students.GroupBy(x => x.classId).Select(x => x.Key).ToList(); List classInfos= await GetClassInfo(client, _dingDing, classes, school); classInfos.ForEach(x => { ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name }; var list= students.Where(y => y.classId .Equals(x.id)).ToList(); list.ForEach(z => { classListInfo.stuInfos.Add(new StuInfo { id = z.id, code = z.code, schoolId = z.schoolId, classId = z.classId, name = z.name, picture = z.picture, year = z.year }); }); classInfo.Add(classListInfo); }); return (tmdinfos, stuInfos, classInfo) ; } catch (Exception ex) { await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetStuList\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組); } return (null, null,null); } } public class ClassListInfo { public string id { get; set; } public string name { get; set; } // public string from { get; set; } public List stuInfos { get; set; } = new List(); public List tmdInfos { get; set; } = new List(); } }