using Azure.Cosmos; using Azure.Messaging.ServiceBus; using Microsoft.Azure.Documents; using System; using System.Collections.Generic; using System.Text; using System.Text.Json; using System.Threading.Tasks; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Models; using TEAMModelOS.SDK.Models.Cosmos; namespace TEAMModelFunction { public static class TriggerVote { public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing, CosmosClient client, Document input, string code, long stime, long etime, string school, AzureRedisFactory _azureRedis) { Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync(input.Id, new Azure.Cosmos.PartitionKey($"{code}")); List voteRecords = await _azureStorage.FindListByDict(new Dictionary() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } }); //ChangeRecord voteRecord = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync(input.Id, new Azure.Cosmos.PartitionKey($"{vote.progress}")); if (vote.ttl >= 1) { //TODO 处理TTL删除业务 _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}_{vote.code}"); _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}_{vote.code}"); return; } else { switch (vote.progress) { case "pending": var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString()); messageVote.ApplicationProperties.Add("name", "Vote"); if (voteRecords.Count > 0) { long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime)); await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber); voteRecords[0].sequenceNumber = start; await _azureStorage.SaveOrUpdate(voteRecords[0]); //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}")); } else { long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime)); ChangeRecord changeRecord = new ChangeRecord { RowKey = input.Id, PartitionKey = "pending", sequenceNumber = start, msgId = messageVote.MessageId }; await _azureStorage.Save(changeRecord); //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}")); } break; case "going": ActivityData data; if (vote.scope == "school" || vote.scope == "teacher") { data = new ActivityData { id = vote.id, code = $"Activity-{vote.owner}", type = "vote", name = vote.name, startTime = vote.startTime, endTime = vote.endTime, scode = vote.code, scope = vote.scope, classes = vote.classes, tmdids = vote.tmdids, progress= "going", owner =vote.owner }; await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync(data, new Azure.Cosmos.PartitionKey(data.code)); } else if (vote.scope == "private") { data = new ActivityData { id = vote.id, code = $"Activity-Common", type = "vote", name = vote.name, startTime = vote.startTime, endTime = vote.endTime, scode = vote.code, scope = vote.scope, progress = "going", classes = vote.classes, owner = vote.owner // tmdids = vote.tmdids }; await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(data, new Azure.Cosmos.PartitionKey(data.code)); } var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString()); messageVoteEnd.ApplicationProperties.Add("name", "Vote"); if (voteRecords.Count > 0) { long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime)); await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber); voteRecords[0].sequenceNumber = end; await _azureStorage.SaveOrUpdate(voteRecords[0]); } else { long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime)); ChangeRecord changeRecord = new ChangeRecord { RowKey = input.Id, PartitionKey = "going", sequenceNumber = end, msgId = messageVoteEnd.MessageId }; await _azureStorage.Save(changeRecord); } break; case "finish": //获取投票活动的所有投票记录 var records= await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}_{vote.code}"); //获取投票活动的选项及投票数 var counts= _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}_{vote.code}"); if (counts != null && counts.Length > 0) { foreach (var count in counts) { vote.options.ForEach(x => { //重新赋值 if (x.code.Equals(count.Element.ToString())) { x.count = (int)count.Score; } }); } } List> tasks = new List>(); List recordsBlob = new List(); foreach (var rcd in records) { var key = rcd.Name.ToString().Split("-")[0]; var value = rcd.Value.ToString().ToObject(); recordsBlob.Add(new { key,value}); tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, value.ToJsonString(), "vote", $"{vote.id}/{key}.json")); } //处理活动方的记录 string url = $"vote/{vote.id}/index.json"; vote.recordUrl = url; tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, recordsBlob.ToJsonString(), "vote", $"{vote.id}/index.json")); //处理投票者的记录 await Task.WhenAll(tasks); if (vote.scope == "school" || vote.scope == "teacher") { await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(vote,vote.id, new Azure.Cosmos.PartitionKey(vote.code)); } else if (vote.scope == "private") { await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code)); } //更新结束状态 if (vote.scope == "school" || vote.scope == "teacher") { data = new ActivityData { id = vote.id, code = $"Activity-{vote.owner}", type = "vote", name = vote.name, startTime = vote.startTime, endTime = vote.endTime, scode = vote.code, scope = vote.scope, classes = vote.classes, tmdids = vote.tmdids, progress = "finish", owner = vote.owner }; await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(data,data.id, new Azure.Cosmos.PartitionKey(data.code)); } else if (vote.scope == "private") { //更新结束状态 data = new ActivityData { id = vote.id, code = $"Activity-Common", type = "vote", name = vote.name, startTime = vote.startTime, endTime = vote.endTime, scode = vote.code, scope = vote.scope, progress = "finish", classes = vote.classes, owner = vote.owner // tmdids = vote.tmdids }; await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(data,data.id, new Azure.Cosmos.PartitionKey(data.code)); } break; } } } } }