TriggerStudy.cs 10 KB

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