TriggerStudy.cs 11 KB

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