using Azure.Cosmos; using Azure.Messaging.ServiceBus; using HTEXLib.COMM.Helpers; using Microsoft.Azure.Documents; using System; using System.Collections.Generic; using System.Text; using TEAMModelOS.SDK; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Models; using TEAMModelOS.SDK.Models.Cosmos.Common; namespace TEAMModelFunction { public static class TriggerStudy { public static async void Trigger(AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing, CosmosClient client, Document input, TriggerData tdata, AzureRedisFactory _azureRedis) { try { if ((tdata.status != null && tdata.status.Value == 404) || tdata.ttl > 0) { 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.成都开发測試群組); Study study = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}")); if (study != null) { string PartitionKey = string.Format("{0}{1}{2}", study.code, "-", study.progress); List changeRecords = await _azureStorage.FindListByDict(new Dictionary() { { "RowKey", input.Id }, { "PartitionKey", PartitionKey } }); switch (study.progress) { case "pending": var messageWork = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString()); messageWork.ApplicationProperties.Add("name", "Study"); 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.stime)); changeRecords[0].sequenceNumber = start; await _azureStorage.SaveOrUpdate(changeRecords[0]); } else { long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime)); ChangeRecord changeRecord = new ChangeRecord { RowKey = input.Id, PartitionKey = PartitionKey, sequenceNumber = start, msgId = messageWork.MessageId }; await _azureStorage.Save(changeRecord); } break; case "going": (List tchList, List classInfos) = await GroupListService.GetStutmdidListids(client, _dingDing, study.tchLists, study.school); // (List tmdInfos, List classInfos) = await TriggerStuActivity.GetTchList(client, _dingDing, ids, $"{school}"); //(List tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, study.tchLists, study.school); List tchActivities = new List(); if (tchList.IsNotEmpty()) { tchList.ForEach(x => { tchActivities.Add(new StuActivity { pk = "Activity", id = study.id, code = $"Activity-{x.id}", type = "Study", name = study.name, startTime = study.startTime, endTime = study.endTime, scode = study.code, scope = study.scope, school = study.school, creatorId = study.creatorId, subjects = new List { "" }, blob = null, owner = study.owner, createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), taskStatus = -1, classIds = study.tchLists }); }); } await ActivityService.SaveStuActivity(client, _dingDing, null, null, tchActivities); var messageWorkEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString()); messageWorkEnd.ApplicationProperties.Add("name", "Study"); if (changeRecords.Count > 0) { long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime)); await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber); changeRecords[0].sequenceNumber = end; await _azureStorage.SaveOrUpdate(changeRecords[0]); } else { long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime)); ChangeRecord changeRecord = new ChangeRecord { RowKey = input.Id, PartitionKey = PartitionKey, sequenceNumber = end, msgId = messageWorkEnd.MessageId }; await _azureStorage.Save(changeRecord); } break; case "finish": break; } } } catch (Exception ex) { await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动异常{ex.Message}{ex.StackTrace}{tdata.ToJsonString()}{input}", GroupNames.成都开发測試群組); } } } }