VoteTrigger.cs 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using Microsoft.Azure.Documents;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. using TEAMModelOS.SDK.Models;
  12. namespace TEAMModelFunction
  13. {
  14. public static class VoteTrigger
  15. {
  16. public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  17. CosmosClient client, Document input, string code, long stime, long etime, string school)
  18. {
  19. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
  20. List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } });
  21. //ChangeRecord voteRecord = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{vote.progress}"));
  22. switch (vote.progress)
  23. {
  24. case "pending":
  25. var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
  26. messageVote.ApplicationProperties.Add("name", "Vote");
  27. if (voteRecords.Count > 0)
  28. {
  29. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  30. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  31. voteRecords[0].sequenceNumber = start;
  32. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  33. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
  34. }
  35. else
  36. {
  37. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  38. ChangeRecord changeRecord = new ChangeRecord
  39. {
  40. RowKey = input.Id,
  41. PartitionKey = "pending",
  42. sequenceNumber = start,
  43. msgId = messageVote.MessageId
  44. };
  45. await _azureStorage.Save<ChangeRecord>(changeRecord);
  46. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  47. }
  48. break;
  49. case "going":
  50. var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
  51. messageVoteEnd.ApplicationProperties.Add("name", "Vote");
  52. if (voteRecords.Count > 0)
  53. {
  54. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  55. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  56. voteRecords[0].sequenceNumber = end;
  57. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  58. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
  59. }
  60. else
  61. {
  62. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  63. ChangeRecord changeRecord = new ChangeRecord
  64. {
  65. RowKey = input.Id,
  66. PartitionKey = "going",
  67. sequenceNumber = end,
  68. msgId = messageVoteEnd.MessageId
  69. };
  70. await _azureStorage.Save<ChangeRecord>(changeRecord);
  71. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  72. }
  73. break;
  74. }
  75. }
  76. }
  77. }