TriggerStudy.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.Linq;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelOS.SDK;
  13. using TEAMModelOS.SDK.DI;
  14. using TEAMModelOS.SDK.Extension;
  15. using TEAMModelOS.SDK.Models;
  16. using TEAMModelOS.SDK.Models.Cosmos.Common;
  17. using TEAMModelOS.SDK.Models.Service;
  18. namespace TEAMModelOS.FunctionV4
  19. {
  20. public static class TriggerStudy
  21. {
  22. public static async Task Trigger(CoreAPIHttpService _coreAPIHttpService, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  23. CosmosClient client, JsonElement input, TriggerData tdata, AzureRedisFactory _azureRedis, IConfiguration _configuration)
  24. {
  25. try
  26. {
  27. if ((tdata.status != null && tdata.status.Value == 404) || tdata.publish == 1)
  28. {
  29. await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(tdata.id, new PartitionKey(tdata.code));
  30. ActivityList data = input.ToObject<ActivityList>();
  31. await ActivityService.DeleteActivity(_coreAPIHttpService, client, _dingDing, data);
  32. var table_cancel = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  33. List<ChangeRecord> records = await table_cancel.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id } });
  34. foreach (var record in records)
  35. {
  36. try
  37. {
  38. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
  39. }
  40. catch (Exception ) {
  41. continue;
  42. }
  43. }
  44. return;
  45. }
  46. var adid = tdata.id;
  47. var adcode = "";
  48. string blobcntr = null;
  49. if (tdata.scope.Equals("school"))
  50. {
  51. adcode = $"Activity-{tdata.school}";
  52. blobcntr = tdata.school;
  53. }
  54. else
  55. {
  56. adcode = $"Activity-{tdata.creatorId}";
  57. blobcntr = tdata.creatorId;
  58. }
  59. Study study = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Study>(tdata.id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  60. if (study != null)
  61. {
  62. string PartitionKey = string.Format("{0}{1}{2}", study.code, "-", study.progress);
  63. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  64. List<ChangeRecord> changeRecords = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id }, { "PartitionKey", PartitionKey } });
  65. switch (study.progress)
  66. {
  67. case "pending":
  68. var messageWork = new ServiceBusMessage(new { id = tdata.id, progress = "going", code = tdata.code }.ToJsonString());
  69. messageWork.ApplicationProperties.Add("name", "Study");
  70. if (changeRecords.Count > 0)
  71. {
  72. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  73. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  74. changeRecords[0].sequenceNumber = start;
  75. await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  76. }
  77. else
  78. {
  79. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  80. ChangeRecord changeRecord = new()
  81. {
  82. RowKey = tdata.id,
  83. PartitionKey = PartitionKey,
  84. sequenceNumber = start,
  85. msgId = messageWork.MessageId
  86. };
  87. await table.Save<ChangeRecord>(changeRecord);
  88. }
  89. break;
  90. case "going":
  91. try {
  92. List<(string pId, List<string> gid)> ps = new List<(string pId, List<string> gid)>();
  93. if (study.groupLists.Count > 0)
  94. {
  95. var group = study.groupLists;
  96. foreach (var gp in group)
  97. {
  98. foreach (KeyValuePair<string, List<string>> pp in gp)
  99. {
  100. ps.Add((pp.Key, pp.Value));
  101. }
  102. }
  103. }
  104. (List<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, study.tchLists, study.school, ps);
  105. List<StuActivity> tchActivities = new List<StuActivity>();
  106. (string standard, List<string> tmdids, string school, List<string> update, int statistics) list = (null, null, null, new List<string> { StatisticsService.OfflineRecord }, 0);
  107. if (tchList.IsNotEmpty())
  108. {
  109. list.tmdids = tchList.Select(x => x.id).ToList();
  110. School school = null;
  111. if (!string.IsNullOrEmpty(study.school))
  112. {
  113. school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(study.school, new Azure.Cosmos.PartitionKey("Base"));
  114. list.school = school.id;
  115. list.standard = school.standard;
  116. }
  117. tchList.ForEach(x =>
  118. {
  119. tchActivities.Add(new StuActivity
  120. {
  121. pk = "Activity",
  122. id = study.id,
  123. code = $"Activity-{x.id}",
  124. type = "Study",
  125. name = study.name,
  126. startTime = study.startTime,
  127. endTime = study.endTime,
  128. scode = study.code,
  129. scope = study.scope,
  130. school = study.school,
  131. creatorId = study.creatorId,
  132. subjects = new List<string> { "" },
  133. blob = null,
  134. owner = study.owner,
  135. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  136. taskStatus = -1,
  137. classIds = study.tchLists
  138. });
  139. });
  140. }
  141. await ActivityService.SaveStuActivity(client, _dingDing, null, null, tchActivities);
  142. await StatisticsService.SendServiceBus(list, _configuration, _serviceBus,client);
  143. var messageWorkEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString());
  144. messageWorkEnd.ApplicationProperties.Add("name", "Study");
  145. if ( study.teacIds != null && study.teacIds.Count == 0) {
  146. study.teacIds = list.tmdids;
  147. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(study, study.id, new PartitionKey($"{study.code}"));
  148. }
  149. if (changeRecords.Count > 0)
  150. {
  151. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  152. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  153. changeRecords[0].sequenceNumber = end;
  154. await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  155. }
  156. else
  157. {
  158. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  159. ChangeRecord changeRecord = new()
  160. {
  161. RowKey = tdata.id,
  162. PartitionKey = PartitionKey,
  163. sequenceNumber = end,
  164. msgId = messageWorkEnd.MessageId
  165. };
  166. await table.Save<ChangeRecord>(changeRecord);
  167. }
  168. }
  169. catch (Exception ex) {
  170. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动异常-going {ex.Message}{ex.StackTrace}{tdata.ToJsonString()}{input}", GroupNames.成都开发測試群組);
  171. }
  172. break;
  173. case "finish":
  174. break;
  175. }
  176. }
  177. }
  178. catch (CosmosException e)
  179. {
  180. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.StackTrace}{e.Message}\n{e.Status}", GroupNames.成都开发測試群組);
  181. }
  182. catch (Exception ex)
  183. {
  184. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动异常{ex.Message}{ex.StackTrace}{tdata.ToJsonString()}{input}", GroupNames.成都开发測試群組);
  185. }
  186. }
  187. }
  188. }