123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- 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 VoteTrigger
- {
- 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)
- {
- 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 } });
- //ChangeRecord voteRecord = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{vote.progress}"));
- 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":
- var tcode = code.Replace("Vote-", "");
- //处理教师可投票信息初始化
- if (vote.teachers != null && vote.scope == "school")
- {
- List<CommonData> teacherDatas = new List<CommonData>();
- vote.teachers.ForEach(x => {
- teacherDatas.Add(new CommonData
- {
- id = vote.id,
- code = $"Common-{x}",
- type = "vote",
- name = vote.name,
- startTime = vote.startTime,
- endTime = vote.endTime,
- scode = vote.code,
- // spcode = tcode
- });
- });
- List<Task<ItemResponse<CommonData>>> tasks = new List<Task<ItemResponse<CommonData>>>();
- foreach (var data in teacherDatas) {
- tasks.Add(client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
- }
- var response= await Task.WhenAll(tasks);
- }
- //处理学校教室或私人教师可投票信息初始化
- if (vote.classes != null) {
- List<Task<ItemResponse<CommonData>>> tasks = new List<Task<ItemResponse<CommonData>>>();
- //学校教室
- if (vote.scope == "school")
- {
- List<CommonData> classDatas = new List<CommonData>();
- foreach (var clazz in vote.classes) {
- var response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(clazz, new Azure.Cosmos.PartitionKey($"Class-{tcode}"));
- if (response.Status == 200) {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- Class clss = json.ToObject<Class>();
- if (clss.students != null && clss.students.Count > 0) {
- clss.students.ForEach(x => {
- classDatas.Add(new CommonData
- {
- id = vote.id,
- code = $"Common-{x.id}",
- type = "vote",
- name = vote.name,
- startTime = vote.startTime,
- endTime = vote.endTime,
- scode = vote.code,
- // spcode = x.scode
- });
- });
- }
- }
- }
- foreach (var data in classDatas)
- {
- tasks.Add(client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
- }
- }
- //私人教室
- else if (vote.scope == "private") {
-
- foreach (var clazz in vote.classes)
- {
- var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(clazz, new Azure.Cosmos.PartitionKey($"Class-{tcode}"));
- if (response.Status == 200)
- {
- using var json = await JsonDocument.ParseAsync(response.ContentStream);
- Class clss = json.ToObject<Class>();
- //处理私人教室的学生数据来源问题
- //从不同学校的学生账号
- if (clss.source == 1) {
- List<CommonData> classDatas = new List<CommonData>();
- if (clss.students != null && clss.students.Count > 0)
- {
- clss.students.ForEach(x => {
- classDatas.Add(new CommonData
- {
- id = vote.id,
- code = $"Common-{x.id}",
- type = "vote",
- name = vote.name,
- startTime = vote.startTime,
- endTime = vote.endTime,
- scode = vote.code,
- // spcode = x.scode
- });
- });
- foreach (var data in classDatas)
- {
- tasks.Add(client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
- }
- }
- }
- //从扫码加入的醍摩豆ID
- if (clss.source == 2) {
- List<CommonData> classDatas = new List<CommonData>();
- if (clss.students != null && clss.students.Count > 0)
- {
- clss.students.ForEach(x => {
- classDatas.Add(new CommonData
- {
- id = vote.id,
- code = $"Common-{x.id}",
- type = "vote",
- name = vote.name,
- startTime = vote.startTime,
- endTime = vote.endTime,
- scode = vote.code,
- //spcode = x.scode
- }) ;
- });
- foreach (var data in classDatas)
- {
- tasks.Add(client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
- }
- }
- }
- }
- }
- }
- var task = await Task.WhenAll(tasks);
- }
- 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]);
- //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
- }
- 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);
- //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
- }
- break;
- case "finish":
- break;
- }
- }
- }
- }
|