TriggerStudy.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using HTEXLib.COMM.Helpers;
  4. using Microsoft.Azure.Documents;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using TEAMModelOS.SDK;
  9. using TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. using TEAMModelOS.SDK.Models;
  12. using TEAMModelOS.SDK.Models.Cosmos.Common;
  13. namespace TEAMModelFunction
  14. {
  15. public static class TriggerStudy
  16. {
  17. public static async void Trigger(AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  18. CosmosClient client, Document input, TriggerData tdata, AzureRedisFactory _azureRedis)
  19. {
  20. try
  21. {
  22. if ((tdata.status != null && tdata.status.Value == 404) || tdata.ttl > 0)
  23. {
  24. return;
  25. }
  26. var adid = tdata.id;
  27. var adcode = "";
  28. string blobcntr = null;
  29. if (tdata.scope.Equals("school"))
  30. {
  31. adcode = $"Activity-{tdata.school}";
  32. blobcntr = tdata.school;
  33. }
  34. else
  35. {
  36. adcode = $"Activity-{tdata.creatorId}";
  37. blobcntr = tdata.creatorId;
  38. }
  39. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.成都开发測試群組);
  40. Study study = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Study>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  41. List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", study.progress } });
  42. if (study != null)
  43. {
  44. switch (study.progress)
  45. {
  46. case "pending":
  47. var messageWork = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
  48. messageWork.ApplicationProperties.Add("name", "Study");
  49. if (changeRecords.Count > 0)
  50. {
  51. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  52. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  53. changeRecords[0].sequenceNumber = start;
  54. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  55. }
  56. else
  57. {
  58. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  59. ChangeRecord changeRecord = new ChangeRecord
  60. {
  61. RowKey = input.Id,
  62. PartitionKey = "pending",
  63. sequenceNumber = start,
  64. msgId = messageWork.MessageId
  65. };
  66. await _azureStorage.Save<ChangeRecord>(changeRecord);
  67. }
  68. break;
  69. case "going":
  70. (List<TmdInfo> tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, study.tchLists, study.school);
  71. List<StuActivity> tchActivities = new List<StuActivity>();
  72. if (tchList.IsNotEmpty())
  73. {
  74. tchList.ForEach(x =>
  75. {
  76. tchActivities.Add(new StuActivity
  77. {
  78. pk = "Activity",
  79. id = study.id,
  80. code = $"Activity-{x.id}",
  81. type = "Study",
  82. name = study.name,
  83. startTime = study.startTime,
  84. endTime = study.endTime,
  85. scode = study.code,
  86. scope = study.scope,
  87. school = study.school,
  88. creatorId = study.creatorId,
  89. subjects = new List<string> { "" },
  90. blob = null,
  91. owner = study.owner,
  92. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  93. taskStatus = -1,
  94. classIds = study.tchLists
  95. });
  96. });
  97. }
  98. await TriggerStuActivity.SaveStuActivity(client, _dingDing, null, null, tchActivities);
  99. var messageWorkEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
  100. messageWorkEnd.ApplicationProperties.Add("name", "Study");
  101. if (changeRecords.Count > 0)
  102. {
  103. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  104. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  105. changeRecords[0].sequenceNumber = end;
  106. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  107. }
  108. else
  109. {
  110. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  111. ChangeRecord changeRecord = new ChangeRecord
  112. {
  113. RowKey = input.Id,
  114. PartitionKey = "going",
  115. sequenceNumber = end,
  116. msgId = messageWorkEnd.MessageId
  117. };
  118. await _azureStorage.Save<ChangeRecord>(changeRecord);
  119. }
  120. break;
  121. case "finish":
  122. break;
  123. }
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动异常{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  129. }
  130. }
  131. }
  132. }