123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- 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;
- 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 = "";
- string blobcntr = null;
- if (tdata.scope == "school")
- {
- adcode = $"Activity-{tdata.school}";
- blobcntr = tdata.school;
- }
- else if (tdata.scope == "private")
- {
- adcode = $"Activity-{tdata.creatorId}";
- blobcntr = tdata.creatorId;
- }
- #if DEBUG
- await _dingDing.SendBotMsg($"投票活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.成都开发測試群組);
- #endif
- Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
- List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "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(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
- await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), voteRecords[0].sequenceNumber);
- voteRecords[0].sequenceNumber = start;
- await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
- }
- else
- {
- long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
- ChangeRecord changeRecord = new ChangeRecord
- {
- RowKey = input.Id,
- PartitionKey = "pending",
- sequenceNumber = start,
- msgId = messageVote.MessageId
- };
- await _azureStorage.Save<ChangeRecord>(changeRecord);
- }
- break;
- case "going":
-
- (List<string> tmdids, List<Students> students) = await TriggerStuActivity.GetStuList(client, vote.classes, vote.school);
- List<string> tmds = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List<string>() ;
- if (tmdids.IsNotEmpty()) {
- tmds.AddRange(tmdids);
- }
- List<StuActivity> stuActivities = new List<StuActivity>();
- List<StuActivity> tmdActivities = new List<StuActivity>();
- if (tmds.IsNotEmpty())
- {
- tmds.ForEach(x => {
- tmdActivities.Add(new StuActivity
- {
- pk = "Activity",
- id = vote.id,
- code = $"Activity-{x}",
- type = "Vote",
- name = vote.name,
- startTime = vote.startTime,
- endTime = vote.endTime,
- scode = vote.code,
- scope = vote.scope,
- school = vote.school,
- creatorId = vote.creatorId,
- subjects = new List<string> { "" } ,
- blob = null
- });
- });
- }
- if (students.IsNotEmpty())
- {
- students.ForEach(x => {
- stuActivities.Add(new StuActivity
- {
- pk = "Activity",
- id = vote.id,
- code = $"Activity-{vote.school}-{x.id}",
- type = "Vote",
- name = vote.name,
- startTime = vote.startTime,
- endTime = vote.endTime,
- scode = vote.code,
- scope = vote.scope,
- school = vote.school,
- creatorId = vote.creatorId,
- subjects = new List<string> { "" },
- blob=null
- });
- });
- }
- await TriggerStuActivity.SaveStuActivity(client, stuActivities, tmdActivities);
- //向学生或醍摩豆账号发起通知
- #region
- Notice notice = new Notice()
- {
-
- creation = vote.startTime,
- expire = vote.endTime,
- creatorId = vote.creatorId,
- stuids = students,
- tmdids = tmdids,
- type = "notice",//问卷参加参加通知
- priority = "normal",
- msgId=vote.id,
- school = vote.school,
- scope = vote.scope,
- //data = new { }.ToJsonString()
- body = new Body { sid = vote.id, scode = vote.code, spk = vote.pk, biztype = "vote-join" }
- };
- //var messageBlob = new ServiceBusMessage(notice.ToJsonString());
- //messageBlob.ApplicationProperties.Add("name", "Notice");
- //await _serviceBus.GetServiceBusClient().SendMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:NoticeTask"), messageBlob);
- #endregion
- 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(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
- await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), voteRecords[0].sequenceNumber);
- voteRecords[0].sequenceNumber = end;
- await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
- }
- else
- {
- long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
- ChangeRecord changeRecord = new ChangeRecord
- {
- RowKey = input.Id,
- PartitionKey = "going",
- sequenceNumber = end,
- msgId = messageVoteEnd.MessageId
- };
- await _azureStorage.Save<ChangeRecord>(changeRecord);
- }
- #if DEBUG
- await _dingDing.SendBotMsg($"投票活动{tdata.id}将于:{tdata.etime}完成并结算!", GroupNames.成都开发測試群組);
- #endif
- break;
- case "finish":
- #if DEBUG
- await _dingDing.SendBotMsg($"投票活动{tdata.id}开始结算{tdata.etime}!", GroupNames.成都开发測試群組);
- #endif
- //获取投票活动的所有投票记录
- var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}");
- //获取投票活动的选项及投票数
- var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
- List<dynamic> countcds = new List<dynamic>();
- if (counts != null && counts.Length > 0)
- {
- foreach (var count in counts)
- {
- countcds.Add(new { code = count.Element.ToString(), count = (int)count.Score });
- }
- }
- List<Task<string>> tasks = new List<Task<string>>();
- List<VoteRecord> recordsBlob = new List<VoteRecord>();
- foreach (var rcd in records)
- {
- var value = rcd.Value.ToString().ToObject<VoteRecord>();
- 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(blobcntr, g.list.ToJsonString(), "vote", $"{vote.id}/urecord/{g.key}.json"));
- }
- //处理活动方的记录,
- string url = $"/vote/{vote.id}/record.json";
- tasks.Add(_azureStorage.UploadFileByContainer(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, 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);
- break;
- }
- }
-
- }
- }
- }
|