TriggerStudy.cs 8.2 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 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. if (study != null)
  42. {
  43. string PartitionKey = string.Format("{0}{1}{2}", study.code, "-", study.progress);
  44. List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", PartitionKey } });
  45. switch (study.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", "Study");
  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<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetStutmdidListids(client, _dingDing, study.tchLists, study.school);
  72. // (List<TmdInfo> tmdInfos, List<ClassListInfo> classInfos) = await TriggerStuActivity.GetTchList(client, _dingDing, ids, $"{school}");
  73. //(List<TmdInfo> tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, study.tchLists, study.school);
  74. List<StuActivity> tchActivities = new List<StuActivity>();
  75. if (tchList.IsNotEmpty())
  76. {
  77. tchList.ForEach(x =>
  78. {
  79. tchActivities.Add(new StuActivity
  80. {
  81. pk = "Activity",
  82. id = study.id,
  83. code = $"Activity-{x.id}",
  84. type = "Study",
  85. name = study.name,
  86. startTime = study.startTime,
  87. endTime = study.endTime,
  88. scode = study.code,
  89. scope = study.scope,
  90. school = study.school,
  91. creatorId = study.creatorId,
  92. subjects = new List<string> { "" },
  93. blob = null,
  94. owner = study.owner,
  95. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  96. taskStatus = -1,
  97. classIds = study.tchLists
  98. });
  99. });
  100. }
  101. await ActivityService.SaveStuActivity(client, _dingDing, null, null, tchActivities);
  102. var messageWorkEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
  103. messageWorkEnd.ApplicationProperties.Add("name", "Study");
  104. if (changeRecords.Count > 0)
  105. {
  106. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  107. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  108. changeRecords[0].sequenceNumber = end;
  109. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  110. }
  111. else
  112. {
  113. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  114. ChangeRecord changeRecord = new ChangeRecord
  115. {
  116. RowKey = input.Id,
  117. PartitionKey = PartitionKey,
  118. sequenceNumber = end,
  119. msgId = messageWorkEnd.MessageId
  120. };
  121. await _azureStorage.Save<ChangeRecord>(changeRecord);
  122. }
  123. break;
  124. case "finish":
  125. break;
  126. }
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动异常{ex.Message}{ex.StackTrace}{tdata.ToJsonString()}{input}", GroupNames.成都开发測試群組);
  132. }
  133. }
  134. }
  135. }