TriggerStudy.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 table_cancel.DeleteSingle<ChangeRecord>(record.PartitionKey, record.RowKey);
  39. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
  40. }
  41. catch (Exception ) {
  42. continue;
  43. }
  44. }
  45. return;
  46. }
  47. var adid = tdata.id;
  48. var adcode = "";
  49. string blobcntr = null;
  50. if (tdata.scope.Equals("school"))
  51. {
  52. adcode = $"Activity-{tdata.school}";
  53. blobcntr = tdata.school;
  54. }
  55. else
  56. {
  57. adcode = $"Activity-{tdata.creatorId}";
  58. blobcntr = tdata.creatorId;
  59. }
  60. Study study = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Study>(tdata.id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  61. if (study != null)
  62. {
  63. string PartitionKey = string.Format("{0}{1}{2}", study.code, "-", study.progress);
  64. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  65. List<ChangeRecord> changeRecords = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id }, { "PartitionKey", PartitionKey } });
  66. switch (study.progress)
  67. {
  68. case "pending":
  69. var messageWork = new ServiceBusMessage(new { id = tdata.id, progress = "going", code = tdata.code }.ToJsonString());
  70. messageWork.ApplicationProperties.Add("name", "Study");
  71. if (changeRecords.Count > 0)
  72. {
  73. try
  74. {
  75. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  76. }
  77. catch (Exception)
  78. {
  79. }
  80. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  81. changeRecords[0].sequenceNumber = start;
  82. await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  83. }
  84. else
  85. {
  86. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  87. ChangeRecord changeRecord = new()
  88. {
  89. RowKey = tdata.id,
  90. PartitionKey = PartitionKey,
  91. sequenceNumber = start,
  92. msgId = messageWork.MessageId
  93. };
  94. await table.Save<ChangeRecord>(changeRecord);
  95. }
  96. break;
  97. case "going":
  98. try {
  99. List<(string pId, List<string> gid)> ps = new List<(string pId, List<string> gid)>();
  100. if (study.groupLists.Count > 0)
  101. {
  102. var group = study.groupLists;
  103. foreach (var gp in group)
  104. {
  105. foreach (KeyValuePair<string, List<string>> pp in gp)
  106. {
  107. ps.Add((pp.Key, pp.Value));
  108. }
  109. }
  110. }
  111. (List<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, study.tchLists, study.school, ps);
  112. List<StuActivity> tchActivities = new List<StuActivity>();
  113. (string standard, List<string> tmdids, string school, List<string> update, int statistics) list = (null, null, null, new List<string> { StatisticsService.OfflineRecord }, 0);
  114. if (tchList.IsNotEmpty())
  115. {
  116. list.tmdids = tchList.Select(x => x.id).ToList();
  117. School school = null;
  118. if (!string.IsNullOrEmpty(study.school))
  119. {
  120. school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(study.school, new Azure.Cosmos.PartitionKey("Base"));
  121. list.school = school.id;
  122. list.standard = school.standard;
  123. }
  124. tchList.ForEach(x =>
  125. {
  126. tchActivities.Add(new StuActivity
  127. {
  128. pk = "Activity",
  129. id = study.id,
  130. code = $"Activity-{x.id}",
  131. type = "Study",
  132. name = study.name,
  133. startTime = study.startTime,
  134. endTime = study.endTime,
  135. scode = study.code,
  136. scope = study.scope,
  137. school = study.school,
  138. creatorId = study.creatorId,
  139. subjects = new List<string> { "" },
  140. blob = null,
  141. owner = study.owner,
  142. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  143. taskStatus = -1,
  144. classIds = study.tchLists
  145. });
  146. });
  147. }
  148. await ActivityService.SaveStuActivity(client, _dingDing, null, null, tchActivities);
  149. await StatisticsService.SendServiceBus(list, _configuration, _serviceBus,client);
  150. var messageWorkEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString());
  151. messageWorkEnd.ApplicationProperties.Add("name", "Study");
  152. if ( study.teacIds != null && study.teacIds.Count == 0) {
  153. study.teacIds = list.tmdids;
  154. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(study, study.id, new PartitionKey($"{study.code}"));
  155. }
  156. if (changeRecords.Count > 0)
  157. {
  158. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  159. try
  160. {
  161. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  162. }
  163. catch (Exception)
  164. {
  165. }
  166. changeRecords[0].sequenceNumber = end;
  167. await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  168. }
  169. else
  170. {
  171. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  172. ChangeRecord changeRecord = new()
  173. {
  174. RowKey = tdata.id,
  175. PartitionKey = PartitionKey,
  176. sequenceNumber = end,
  177. msgId = messageWorkEnd.MessageId
  178. };
  179. await table.Save<ChangeRecord>(changeRecord);
  180. }
  181. }
  182. catch (Exception ex) {
  183. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动异常-going {ex.Message}\n{ex.StackTrace}{tdata.ToJsonString()}{input}", GroupNames.醍摩豆服務運維群組);
  184. }
  185. break;
  186. case "finish":
  187. List<(string pId, List<string> gid)> gls = new List<(string pId, List<string> gid)>();
  188. if (study.groupLists.Count > 0)
  189. {
  190. var group = study.groupLists;
  191. foreach (var gp in group)
  192. {
  193. foreach (KeyValuePair<string, List<string>> pp in gp)
  194. {
  195. gls.Add((pp.Key, pp.Value));
  196. }
  197. }
  198. }
  199. //处理教研活动结束统计账户信息
  200. List<FMember> idList = await GroupListService.GetFinishMemberInfo(_coreAPIHttpService, client, _dingDing, study.school, study.classes, study.stuLists, study.stuLists, gls);
  201. study.staffIds = idList;
  202. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Study>(study, study.id, new Azure.Cosmos.PartitionKey(study.code));
  203. break;
  204. }
  205. }
  206. }
  207. catch (CosmosException e)
  208. {
  209. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}", GroupNames.醍摩豆服務運維群組);
  210. }
  211. catch (Exception ex)
  212. {
  213. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动异常{ex.Message}\n{ex.StackTrace}{tdata.ToJsonString()}{input}", GroupNames.醍摩豆服務運維群組);
  214. }
  215. }
  216. }
  217. }