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.Helper.Common.CollectionHelper; using TEAMModelOS.SDK.Models; using TEAMModelOS.SDK.Models.Cosmos.Common; namespace TEAMModelFunction { public class TriggerStuActivity { 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<(List tmdids, List studentss)> GetStuList(CosmosClient client, DingDing _dingDing, List classes, string school) { try { List tmdids = new List(); List studentss = new List(); if (!classes.IsNotEmpty()) { return (tmdids, studentss); } 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); } schList.ForEach(x => { if (x.students.IsNotEmpty()) { studentss.AddRange(x.students); } if (x.tmids.IsNotEmpty()) { tmdids.AddRange(x.tmids); } }); tchLists.ForEach(x => { if (x.students.IsNotEmpty()) { studentss.AddRange(x.students); } if (x.tmids.IsNotEmpty()) { tmdids.AddRange(x.tmids); } }); students.ForEach(x => { studentss.Add(new Students { id = x.id, code = x.code, schoolId = x.schoolId }); }); return (tmdids, studentss); } catch (Exception ex) { await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-SaveStuActivity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組); } return (null, null); } } }