123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- 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.Helper.Common.CollectionHelper;
- 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<Vote>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
- List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "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<ChangeRecord>(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>(changeRecord);
- //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
- }
- break;
- case "going":
- ActivityData data;
- if (vote.scope == "school" )
- {
- 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.IsNotEmpty() ? vote.classes : new List<string> { "" },
- tmdids = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List<string> { "" },
- progress= "going",
- owner =vote.owner,
- subjects = new List<string> { "" }
- };
- await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(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<string> { "" },
- owner = vote.owner,
- tmdids = new List<string> { "" },
- subjects= new List<string> { ""}
- };
- await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(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<ChangeRecord>(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>(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<Task<string>> tasks = new List<Task<string>>();
- List<dynamic> recordsBlob = new List<dynamic>();
- foreach (var rcd in records) {
- var key = rcd.Name.ToString().Split("-")[0];
- var value = rcd.Value.ToString().ToObject<JsonElement>();
- 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")
- {
- await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<Vote>(vote,vote.id, new Azure.Cosmos.PartitionKey(vote.code));
- }
- else if (vote.scope == "private")
- {
- await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
- }
- //更新结束状态
- if (vote.scope == "school")
- {
- 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,
- progress = "finish",
- classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
- tmdids = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List<string> { "" },
- owner = vote.owner,
- subjects = new List<string> { "" }
-
- };
- await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(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.IsNotEmpty() ? vote.classes : new List<string> { "" },
- owner = vote.owner,
- tmdids = new List<string> { "" },
- subjects = new List<string> { "" }
- };
- await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data,data.id, new Azure.Cosmos.PartitionKey(data.code));
- }
- break;
- }
- }
- }
- }
- }
|