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 TriggerExamLite { 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(); await IESActivityService.DeleteActivity(_coreAPIHttpService, client, _dingDing, data); var table_cancel = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord"); List records = await table_cancel.FindListByDict(new Dictionary() { { "RowKey", tdata.id } }); foreach (var record in records) { try { await table_cancel.DeleteSingle(record.PartitionKey, record.RowKey); await _serviceBus.GetServiceBusClient().CancelMessageAsync(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.醍摩豆服務運維群組); ExamLite lite = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync(tdata.id, new Azure.Cosmos.PartitionKey($"{tdata.code}")); var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord"); if (lite != null) { string PartitionKey = string.Format("{0}{1}{2}", lite.code, "-", lite.progress); List changeRecords = await table.FindListByDict(new Dictionary() { { "RowKey", tdata.id }, { "PartitionKey", PartitionKey } }); switch (lite.progress) { case "pending": var messageWork = new ServiceBusMessage(new { id = tdata.id, progress = "going", code = tdata.code }.ToJsonString()); messageWork.ApplicationProperties.Add("name", "ExamLite"); if (changeRecords.Count > 0) { try { await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber); } catch (Exception) { } long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime)); changeRecords[0].sequenceNumber = start; await table.SaveOrUpdate(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); } break; case "going": List<(string pId, List gid)> ps = new(); if (lite.groupLists.Count > 0) { var group = lite.groupLists; foreach (var gp in group) { foreach (KeyValuePair> pp in gp) { ps.Add((pp.Key, pp.Value)); } } } (List tchList, List classInfos) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, lite.tchLists, lite.school, ps); List tchActivities = new(); (string standard, List tmdids, string school, List update, int statistics) list = (null,null, null, new List { StatisticsService.TeacherExamLite }, 0) ; if (tchList.IsNotEmpty()) { list.tmdids = tchList.Select(x => x.id).ToList(); School school = null; if (!string.IsNullOrEmpty(lite.school)) { school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync(lite.school, new Azure.Cosmos.PartitionKey("Base")); list.school = school.id; list.standard = school.standard; } tchList.ForEach(x => { tchActivities.Add(new StuActivity { pk = "Activity", id = lite.id, code = $"Activity-{x.id}", type = "ExamLite", name = lite.name, startTime = lite.startTime, endTime = lite.endTime, scode = lite.code, scope = lite.scope, school = lite.school, creatorId = lite.creatorId, subjects = new List { "" }, blob = null, owner = lite.owner, createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), taskStatus = -1, classIds = lite.tchLists }); }); } await IESActivityService.SaveStuActivity(client, _dingDing, null, null, tchActivities); await StatisticsService.SendServiceBus(list, _configuration, _serviceBus, client); var messageWorkEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString()); messageWorkEnd.ApplicationProperties.Add("name", "ExamLite"); if (changeRecords.Count > 0) { long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime)); try { await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber); } catch (Exception) { } changeRecords[0].sequenceNumber = end; await table.SaveOrUpdate(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); } break; case "finish": List<(string pId, List gid)> gls = new List<(string pId, List gid)>(); if (lite.groupLists.Count > 0) { var group = lite.groupLists; foreach (var gp in group) { foreach (KeyValuePair> pp in gp) { gls.Add((pp.Key, pp.Value)); } } } if (lite.staffIds.Count == 0) { // 处理试卷练习活动结束统计账户信息 List idList = await GroupListService.GetFinishMemberInfo(_coreAPIHttpService, client, _dingDing, lite.school, lite.classes, lite.stuLists, lite.tchLists, gls); lite.staffIds = idList; await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(lite, lite.id, new Azure.Cosmos.PartitionKey(lite.code)); } break; } } } catch (CosmosException e) { await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}", GroupNames.醍摩豆服務運維群組); } catch (Exception ex) { await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修评测异常{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組); } } } }