TriggerExamLite.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 TriggerExamLite
  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. ExamLite lite = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamLite>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  41. if (lite != null)
  42. {
  43. string PartitionKey = string.Format("{0}{1}{2}", lite.code, "-", lite.progress);
  44. List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", PartitionKey } });
  45. switch (lite.progress)
  46. {
  47. case "pending":
  48. var messageWork = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
  49. messageWork.ApplicationProperties.Add("name", "ExamLite");
  50. if (changeRecords.Count > 0)
  51. {
  52. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  53. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  54. changeRecords[0].sequenceNumber = start;
  55. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  56. }
  57. else
  58. {
  59. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  60. ChangeRecord changeRecord = new ChangeRecord
  61. {
  62. RowKey = input.Id,
  63. PartitionKey = PartitionKey,
  64. sequenceNumber = start,
  65. msgId = messageWork.MessageId
  66. };
  67. await _azureStorage.Save<ChangeRecord>(changeRecord);
  68. }
  69. break;
  70. case "going":
  71. (List<TmdInfo> tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, lite.tchLists, lite.school);
  72. List<StuActivity> tchActivities = new List<StuActivity>();
  73. if (tchList.IsNotEmpty())
  74. {
  75. tchList.ForEach(x =>
  76. {
  77. tchActivities.Add(new StuActivity
  78. {
  79. pk = "Activity",
  80. id = lite.id,
  81. code = $"Activity-{x.id}",
  82. type = "ExamLite",
  83. name = lite.name,
  84. startTime = lite.startTime,
  85. endTime = lite.endTime,
  86. scode = lite.code,
  87. scope = lite.scope,
  88. school = lite.school,
  89. creatorId = lite.creatorId,
  90. subjects = new List<string> { "" },
  91. blob = null,
  92. owner = lite.owner,
  93. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  94. taskStatus = -1,
  95. classIds = lite.tchLists
  96. });
  97. });
  98. }
  99. await TriggerStuActivity.SaveStuActivity(client, _dingDing, null, null, tchActivities);
  100. var messageWorkEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
  101. messageWorkEnd.ApplicationProperties.Add("name", "ExamLite");
  102. if (changeRecords.Count > 0)
  103. {
  104. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  105. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  106. changeRecords[0].sequenceNumber = end;
  107. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  108. }
  109. else
  110. {
  111. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  112. ChangeRecord changeRecord = new ChangeRecord
  113. {
  114. RowKey = input.Id,
  115. PartitionKey = PartitionKey,
  116. sequenceNumber = end,
  117. msgId = messageWorkEnd.MessageId
  118. };
  119. await _azureStorage.Save<ChangeRecord>(changeRecord);
  120. }
  121. break;
  122. case "finish":
  123. break;
  124. }
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修评测异常{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  130. }
  131. }
  132. }
  133. }