using Azure.Cosmos; using Azure.Messaging.ServiceBus; using Microsoft.Azure.Documents; using System; using System.Collections.Generic; 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; using TEAMModelOS.SDK.Models.Cosmos.Common.Inner; namespace TEAMModelFunction { public static class TriggerVote { public static async void Trigger(AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing, CosmosClient client, Document input, TriggerData tdata, AzureRedisFactory _azureRedis) { if ((tdata.status != null && tdata.status.Value == 404)||tdata.ttl>0) { return; } var adid = tdata.id; var adcode = ""; if (tdata.scope == "school") { adcode = $"Activity-{tdata.school}"; } else if (tdata.scope == "private") { adcode = $"Activity-{tdata.creatorId}"; } ActivityData data = null; try { if (tdata.scope == "school") { data = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync(adid, new Azure.Cosmos.PartitionKey($"{adcode}")); } else if (tdata.scope == "private") { data = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync(adid, new Azure.Cosmos.PartitionKey($"{adcode}")); } } catch { data = null; } await _dingDing.SendBotMsg($"投票活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.成都开发測試群組); Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}")); List voteRecords = await _azureStorage.FindListByDict(new Dictionary() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } }); if (vote != null) { switch (vote.progress) { case "pending": var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString()); messageVote.ApplicationProperties.Add("name", "Vote"); if (voteRecords.Count > 0) { long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime)); await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber); voteRecords[0].sequenceNumber = start; await _azureStorage.SaveOrUpdate(voteRecords[0]); } else { long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime)); ChangeRecord changeRecord = new ChangeRecord { RowKey = input.Id, PartitionKey = "pending", sequenceNumber = start, msgId = messageVote.MessageId }; await _azureStorage.Save(changeRecord); } break; case "going": if (vote.scope == "school") { data = new ActivityData { id = vote.id, code = $"Activity-{vote.school}", type = "vote", name = vote.name, startTime = vote.startTime, endTime = vote.endTime, scode = vote.code, scope = vote.scope, classes = vote.classes.IsNotEmpty() ? vote.classes : new List { "" }, tmdids = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List { "" }, progress = "going", subjects = new List { "" } }; 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.IsNotEmpty() ? vote.classes : new List { "" }, tmdids = new List { "" }, subjects = new List { "" } }; await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(data, new Azure.Cosmos.PartitionKey(data.code)); } var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString()); messageVoteEnd.ApplicationProperties.Add("name", "Vote"); if (voteRecords.Count > 0) { long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.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(tdata.etime)); ChangeRecord changeRecord = new ChangeRecord { RowKey = input.Id, PartitionKey = "going", sequenceNumber = end, msgId = messageVoteEnd.MessageId }; await _azureStorage.Save(changeRecord); } await _dingDing.SendBotMsg($"投票活动{tdata.id}将于:{tdata.etime}完成并结算!", GroupNames.成都开发測試群組); break; case "finish": await _dingDing.SendBotMsg($"投票活动{tdata.id}开始结算{tdata.etime}!", GroupNames.成都开发測試群組); //获取投票活动的所有投票记录 var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}"); //获取投票活动的选项及投票数 var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}"); List countcds = new List(); if (counts != null && counts.Length > 0) { foreach (var count in counts) { countcds.Add(new { code = count.Element.ToString(), count = (int)count.Score }); } } List> tasks = new List>(); List recordsBlob = new List(); foreach (var rcd in records) { var value = rcd.Value.ToString().ToObject(); recordsBlob.Add(value); } //分组每个人的 var gp = recordsBlob.GroupBy(x => x.userid).Select(x => new { key = x.Key, list = x.ToList() }); foreach (var g in gp) { tasks.Add(_azureStorage.UploadFileByContainer(vote.blobcntr, g.list.ToJsonString(), "vote", $"{vote.id}/urecord/{g.key}.json")); } //处理活动方的记录, string url = $"/vote/{vote.id}/record.json"; tasks.Add(_azureStorage.UploadFileByContainer(vote.blobcntr, new { options = countcds, records = recordsBlob }.ToJsonString(), "vote", $"{vote.id}/record.json")); //处理投票者的记录 if (string.IsNullOrEmpty(vote.recordUrl)) { vote.recordUrl = url; await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code)); } else { //异动,且已经有结算记录则不必再继续。 _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}"); _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}"); break; } await Task.WhenAll(tasks); data.progress = "finish"; //更新结束状态 if (vote.scope == "school") { await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(data, data.id, new Azure.Cosmos.PartitionKey(data.code)); } else if (vote.scope == "private") { await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(data, data.id, new Azure.Cosmos.PartitionKey(data.code)); } break; } } } } }