TriggerStudy.cs 10 KB

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