123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- using Azure.Cosmos;
- using Azure.Messaging.ServiceBus;
- using HTEXLib.COMM.Helpers;
- using Microsoft.Azure.Documents;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- using TEAMModelOS.SDK.Models.Service;
- namespace TEAMModelOS.FunctionV4
- {
- public static class TriggerHomework
- {
- public static async Task Trigger(CoreAPIHttpService _coreAPIHttpService, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
- CosmosClient client, JsonElement input, TriggerData tdata, AzureRedisFactory _azureRedis, IConfiguration _configuration)
- {
- try
- {
- if ((tdata.status != null && tdata.status.Value == 404))
- {
- await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(tdata.id, new PartitionKey(tdata.code));
- ActivityList data = input.ToObject<ActivityList>();
- await ActivityService.DeleteActivity(_coreAPIHttpService, client, _dingDing, data);
- var table_cancel = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
- List<ChangeRecord> records = await table_cancel.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id } });
- foreach (var record in records)
- {
- try
- {
- await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
- }
- catch (Exception)
- {
- continue;
- }
- }
- return;
- }
- var adid = tdata.id;
- var adcode = "";
- string blobcntr = null;
- if (tdata.scope.Equals("school"))
- {
- adcode = $"Activity-{tdata.school}";
- blobcntr = tdata.school;
- }
- else
- {
- adcode = $"Activity-{tdata.creatorId}";
- blobcntr = tdata.creatorId;
- }
- //await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}作业活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.成都开发測試群組);
- Homework work = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Homework>(tdata.id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
- var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
- if (work != null)
- {
- string PartitionKey = string.Format("{0}{1}{2}", work.code, "-", work.progress);
- List<ChangeRecord> changeRecords = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id }, { "PartitionKey", PartitionKey } });
- switch (work.progress)
- {
- case "pending":
- var messageWork = new ServiceBusMessage(new { tdata.id, progress = "going", tdata.code }.ToJsonString());
- messageWork.ApplicationProperties.Add("name", "Homework");
- if (changeRecords.Count > 0)
- {
- await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
- long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
- changeRecords[0].sequenceNumber = start;
- await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
- }
- else
- {
- long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
- ChangeRecord changeRecord = new()
- {
- RowKey = tdata.id,
- PartitionKey = PartitionKey,
- sequenceNumber = start,
- msgId = messageWork.MessageId
- };
- await table.Save<ChangeRecord>(changeRecord);
- }
- break;
- case "going":
- await Activity(_coreAPIHttpService, _serviceBus, _dingDing, client, _configuration, work);
- var messageWorkEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString());
- messageWorkEnd.ApplicationProperties.Add("name", "Homework");
- if (changeRecords.Count > 0)
- {
- long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
- await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
- changeRecords[0].sequenceNumber = end;
- await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
- }
- else
- {
- long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
- ChangeRecord changeRecord = new()
- {
- RowKey = tdata.id,
- PartitionKey = PartitionKey,
- sequenceNumber = end,
- msgId = messageWorkEnd.MessageId
- };
- await table.Save<ChangeRecord>(changeRecord);
- }
- //if (bustasks.IsNotEmpty())
- //{
- // await Task.WhenAll(bustasks);
- //}
- break;
- case "finish":
- await Activity(_coreAPIHttpService, _serviceBus, _dingDing, client, _configuration, work);
- break;
- }
- }
- }
- catch (CosmosException e)
- {
- await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.StackTrace}{e.Message}\n{e.Status}", GroupNames.成都开发測試群組);
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修作业活动异常{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
- }
- }
- private static async Task Activity(CoreAPIHttpService _coreAPIHttpService, AzureServiceBusFactory _serviceBus, DingDing _dingDing, CosmosClient client, IConfiguration _configuration, Homework work)
- {
- List<(string pId, List<string> gid)> ps = new List<(string pId, List<string> gid)>();
- if (work.groupLists.Count > 0)
- {
- var group = work.groupLists;
- foreach (var gp in group)
- {
- foreach (KeyValuePair<string, List<string>> pp in gp)
- {
- ps.Add((pp.Key, pp.Value));
- }
- }
- }
- List<string> classes = ExamService.getClasses(work.classes, work.stuLists);
- (List<RMember> tmdids, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, classes, work.school, ps);
- var addStudentsCls = tmdids.FindAll(x => x.type == 2);
- var addTmdidsCls = tmdids.FindAll(x => x.type == 1);
- List<StuActivity> stuActivities = new();
- List<StuActivity> tmdActivities = new();
- List<StuActivity> tchActivities = new();
- List<string> tIds = addStudentsCls.Select(x => x.id).ToList();
- List<StuActivity> tac = new();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<StuActivity>(queryText: $"select value(c) from c where c.id ='{work.id}' and c.type = 'Homework'"))
- {
- tac.Add(item);
- }
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<StuActivity>(queryText: $"select value(c) from c where c.id ='{work.id}' and c.type = 'Homework'"))
- {
- tac.Add(item);
- }
- if (addTmdidsCls.IsNotEmpty())
- {
- addTmdidsCls.ForEach(x =>
- {
- HashSet<string> classIds = new HashSet<string>();
- classLists.ForEach(z =>
- {
- z.members.ForEach(y =>
- {
- if (y.id.Equals(x) && y.type == 1)
- {
- classIds.Add(z.id);
- }
- });
- });
- int sta = -1;
- if (tac.Count > 0)
- {
- StuActivity activity = tac.Where(t => t.code.Equals($"Activity-{x.id}")).FirstOrDefault();
- if (activity != null)
- {
- sta = activity.taskStatus;
- }
- }
- tmdActivities.Add(new StuActivity
- {
- pk = "Activity",
- id = work.id,
- code = $"Activity-{x.id}",
- type = "Homework",
- name = work.name,
- startTime = work.startTime,
- endTime = work.endTime,
- scode = work.code,
- scope = work.scope,
- school = work.school,
- creatorId = work.creatorId,
- subjects = new List<string> { "" },
- blob = work.blob,
- owner = work.owner,
- createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
- taskStatus = sta,
- mustSubmit = work.mustSubmit,
- classIds = classIds.ToList()
- });
- });
- }
- if (addStudentsCls.IsNotEmpty())
- {
- addStudentsCls.ForEach(x =>
- {
- HashSet<string> classIds = new HashSet<string>();
- classLists.ForEach(z =>
- {
- z.members.ForEach(y =>
- {
- if (y.id.Equals(x) && y.code.Equals(work.school) && y.type == 2)
- {
- classIds.Add(z.id);
- }
- });
- });
- int sta = -1;
- if (tac.Count > 0)
- {
- StuActivity activity = tac.Where(t => t.code.Equals($"Activity-{x.code.Replace("Base-", "")}-{x.id}")).FirstOrDefault();
- if (activity != null)
- {
- sta = activity.taskStatus;
- }
- }
- stuActivities.Add(new StuActivity
- {
- pk = "Activity",
- id = work.id,
- code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
- type = "Homework",
- name = work.name,
- startTime = work.startTime,
- endTime = work.endTime,
- scode = work.code,
- scope = work.scope,
- school = work.school,
- creatorId = work.creatorId,
- subjects = new List<string> { "" },
- blob = work.blob,
- owner = work.owner,
- createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
- taskStatus = sta,
- classIds = classIds.ToList(),
- mustSubmit = work.mustSubmit
- });
- });
- }
- (List<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, work.tchLists, work.school, ps);
- (string standard, List<string> tmdids, string school, List<string> update, int statistics) list = (null, null, null, new List<string> { StatisticsService.OfflineRecord }, 0);
- if (tchList.IsNotEmpty())
- {
- list.tmdids = tchList.Select(x => x.id).ToList();
- School school = null;
- if (!string.IsNullOrEmpty(work.school))
- {
- school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(work.school, new Azure.Cosmos.PartitionKey("Base"));
- list.school = school.id;
- list.standard = school.standard;
- }
- tchList.ForEach(x =>
- {
- HashSet<string> classIds = new();
- classInfos.ForEach(z =>
- {
- z.members.ForEach(y =>
- {
- if (y.id.Equals(x) && y.type == 1)
- {
- classIds.Add(z.id);
- }
- });
- });
- int sta = -1;
- if (tac.Count > 0)
- {
- StuActivity activity = tac.Where(t => t.code.Equals($"Activity-{x.id}")).FirstOrDefault();
- if (activity != null)
- {
- sta = activity.taskStatus;
- }
- }
- tchActivities.Add(new StuActivity
- {
- pk = "Activity",
- id = work.id,
- code = $"Activity-{x.id}",
- type = "Homework",
- name = work.name,
- startTime = work.startTime,
- endTime = work.endTime,
- scode = work.code,
- scope = work.scope,
- school = work.school,
- creatorId = work.creatorId,
- subjects = new List<string> { "" },
- blob = work.blob,
- owner = work.owner,
- createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
- taskStatus = sta,
- classIds = classIds.ToList()
- });
- });
- }
- await ActivityService.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, tchActivities);
- await StatisticsService.SendServiceBus(list, _configuration, _serviceBus, client);
- }
- }
- }
|