TriggerStudy.cs 9.9 KB

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