123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- 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 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<MQActivity>();
- }
- catch (CosmosException ex)
- {
- }
- if (activity != null)
- {
- (List<TmdInfo> tmdids, List<StuInfo> students) = 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.tmdid}",
- 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
- };
- 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<string>() { activity.subjects[0].id } : new List<string>() { "" },
- startTime = activity.startTime,
- endTime = activity.endTime,
- blob = activity.blob,
- owner = activity.owner
- };
- await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
- }
- }
- }
- }
- public static async Task<string> SaveStuActivity(CosmosClient client, DingDing _dingDing, List<StuActivity> stuActivities,List<StuActivity> 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 class ClassListInfo
- {
- public string id { get; set; }
- public string name { get; set; }
- public string from { get; set; }
- //public string code { get; set; }
- }
- public static async Task<List<ClassListInfo>> GetClassInfo(CosmosClient client, DingDing _dingDing, List<string> classes, string school)
- {
- List<ClassListInfo> classInfos = new List<ClassListInfo>();
- List<string> sqlList = new List<string>();
- 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<ClassListInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
- requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
- {
- item.from = "ClsStuList";
- classInfos.Add(item);
- }
- await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<ClassListInfo>(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<StuList> tchLists = new List<StuList>();
- await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<ClassListInfo>(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;
- }
- public static async Task<(List<TmdInfo> tmdids, List<StuInfo> students)> GetStuList(CosmosClient client, DingDing _dingDing, List<string> classes, string school) {
- try {
- List<TmdInfo> tmdids = new List<TmdInfo>();
- List<Students> studentss = new List<Students>();
- List<StuInfo> stuInfos = new List<StuInfo>();
- if (!classes.IsNotEmpty()) { return (tmdids, new List<StuInfo>()); }
- List<string> sqlList = new List<string>();
- classes.ForEach(x => { sqlList.Add($" '{x}' "); });
- string sql = string.Join(" , ", sqlList);
- List<StuList> schList = new List<StuList>();
- List<Student> students = new List<Student>();
- if (!string.IsNullOrEmpty(school)) {
- await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<StuList>(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<Student>(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<StuList> tchLists = new List<StuList>();
- await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<StuList>(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())
- {
- List<TmdInfo> infos = new List<TmdInfo>();
- List<string> inids = new List<string>();
- x.tmids.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<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") })) {
- infos.Add(item);
- }
- tmdids.AddRange(infos);
- }
- }
- foreach (var x in tchLists)
- {
- if (x.students.IsNotEmpty()) {
- studentss.AddRange(x.students);
- }
- if (x.tmids.IsNotEmpty())
- {
- List<TmdInfo> infos = new List<TmdInfo>();
- List<string> inids = new List<string>();
- x.tmids.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<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
- {
- infos.Add(item);
- }
- tmdids.AddRange(infos);
- }
- }
- students.ForEach(x => {
- studentss.Add(new Students { id = x.id, code = x.code, schoolId = x.schoolId });
- });
- List<string> inidstus = new List<string>();
- 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<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{school}") }))
- {
- stuInfos.Add(item);
- }
- return (tmdids, stuInfos);
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-SaveStuActivity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- }
- return (null, null);
- }
- }
- }
|