TriggerStudy.cs 11 KB

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