TriggerArt.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using Azure.Messaging.ServiceBus;
  2. using HTEXLib.COMM.Helpers;
  3. using Microsoft.Extensions.Configuration;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK.Models;
  13. using TEAMModelOS.SDK;
  14. using Azure.Cosmos;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common;
  16. using Azure.Core;
  17. using TEAMModelOS.SDK.Models.Service;
  18. namespace TEAMModelOS.FunctionV4.CosmosDB
  19. {
  20. internal class TriggerArt
  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))
  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. {
  43. continue;
  44. }
  45. }
  46. return;
  47. }
  48. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  49. var adid = tdata.id;
  50. var adcode = "";
  51. string blobcntr = null;
  52. if (tdata.scope.Equals("school"))
  53. {
  54. adcode = $"Activity-{tdata.school}";
  55. blobcntr = tdata.school;
  56. }
  57. else
  58. {
  59. adcode = $"Activity-{tdata.creatorId}";
  60. blobcntr = tdata.creatorId;
  61. }
  62. ArtEvaluation art = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ArtEvaluation>(tdata.id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  63. if (art != null)
  64. {
  65. string PartitionKey = string.Format("{0}{1}{2}", art.code, "-", art.progress);
  66. List<ChangeRecord> voteRecords = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id }, { "PartitionKey", PartitionKey } });
  67. switch (art.progress)
  68. {
  69. case "pending":
  70. var messageVote = new ServiceBusMessage(new { id = tdata.id, progress = "going", code = tdata.code }.ToJsonString());
  71. messageVote.ApplicationProperties.Add("name", "Art");
  72. if (voteRecords.Count > 0)
  73. {
  74. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  75. try
  76. {
  77. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), voteRecords[0].sequenceNumber);
  78. }
  79. catch (Exception)
  80. {
  81. }
  82. voteRecords[0].sequenceNumber = start;
  83. await table.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  84. }
  85. else
  86. {
  87. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  88. ChangeRecord changeRecord = new ChangeRecord
  89. {
  90. RowKey = tdata.id,
  91. PartitionKey = PartitionKey,
  92. sequenceNumber = start,
  93. msgId = messageVote.MessageId
  94. };
  95. await table.Save<ChangeRecord>(changeRecord);
  96. }
  97. break;
  98. case "going":
  99. List<string> classes = ExamService.getClasses(art.classes, art.stuLists);
  100. (List<RMember> tmdIds, List<RGroupList> classLists) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, classes, art.school, null);
  101. var addStudentsCls = tmdIds.FindAll(x => x.type == 2);
  102. var addTmdidsCls = tmdIds.FindAll(x => x.type == 1);
  103. List<string> tmds = new List<string>();
  104. if (addTmdidsCls.IsNotEmpty())
  105. {
  106. tmds.AddRange(addTmdidsCls.Select(x => x.id).ToList());
  107. }
  108. List<StuActivity> stuActivities = new List<StuActivity>();
  109. List<StuActivity> tmdActivities = new List<StuActivity>();
  110. List<StuActivity> tchActivities = new List<StuActivity>();
  111. List<string> sub = new();
  112. if (art.subjects.Count > 0)
  113. {
  114. foreach (var course in art.subjects)
  115. {
  116. sub.Add(course.id);
  117. }
  118. }
  119. if (tmds.IsNotEmpty())
  120. {
  121. tmds.ForEach(x =>
  122. {
  123. HashSet<string> classIds = new HashSet<string>();
  124. classLists.ForEach(z =>
  125. {
  126. z.members.ForEach(y =>
  127. {
  128. if (y.id.Equals(x) && y.type == 1)
  129. {
  130. classIds.Add(z.id);
  131. }
  132. });
  133. });
  134. tmdActivities.Add(new StuActivity
  135. {
  136. pk = "Activity",
  137. id = art.id,
  138. code = $"Activity-{x}",
  139. type = "Art",
  140. name = art.name,
  141. startTime = art.startTime,
  142. endTime = art.endTime,
  143. scode = art.code,
  144. scope = art.scope,
  145. school = art.school,
  146. creatorId = art.creatorId,
  147. subjects = sub,
  148. blob = null,
  149. owner = art.owner,
  150. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  151. taskStatus = -1,
  152. classIds = classIds.ToList()
  153. });
  154. });
  155. }
  156. if (addStudentsCls.IsNotEmpty())
  157. {
  158. addStudentsCls.ForEach(x =>
  159. {
  160. HashSet<string> classIds = new HashSet<string>();
  161. classLists.ForEach(z =>
  162. {
  163. z.members.ForEach(y =>
  164. {
  165. if (y.id.Equals(x.id) && y.code.Equals(art.school) && y.type == 2)
  166. {
  167. classIds.Add(z.id);
  168. }
  169. });
  170. });
  171. stuActivities.Add(new StuActivity
  172. {
  173. pk = "Activity",
  174. id = art.id,
  175. code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
  176. type = "Atr",
  177. name = art.name,
  178. startTime = art.startTime,
  179. endTime = art.endTime,
  180. scode = art.code,
  181. scope = art.scope,
  182. school = art.school,
  183. creatorId = art.creatorId,
  184. subjects = sub,
  185. blob = null,
  186. owner = art.owner,
  187. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  188. ext = new Dictionary<string, JsonElement>() {{ "subjects", art.subjects.ToJsonString().ToObject<JsonElement>() } },
  189. taskStatus = -1,
  190. classIds = classIds.ToList()
  191. });
  192. });
  193. }
  194. await ActivityService.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, tchActivities);
  195. //await StatisticsService.SendServiceBus(list, _configuration, _serviceBus, client);
  196. var messageVoteEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString());
  197. messageVoteEnd.ApplicationProperties.Add("name", "Art");
  198. if (voteRecords.Count > 0)
  199. {
  200. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  201. try
  202. {
  203. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), voteRecords[0].sequenceNumber);
  204. }
  205. catch (Exception)
  206. {
  207. }
  208. voteRecords[0].sequenceNumber = end;
  209. await table.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  210. }
  211. else
  212. {
  213. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  214. ChangeRecord changeRecord = new()
  215. {
  216. RowKey = tdata.id,
  217. PartitionKey = PartitionKey,
  218. sequenceNumber = end,
  219. msgId = messageVoteEnd.MessageId
  220. };
  221. await table.Save<ChangeRecord>(changeRecord);
  222. }
  223. break;
  224. case "finish":
  225. break;
  226. }
  227. }
  228. }
  229. catch (CosmosException e)
  230. {
  231. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}", GroupNames.醍摩豆服務運維群組);
  232. }
  233. catch (Exception ex)
  234. {
  235. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}艺术评价异常{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  236. }
  237. }
  238. }
  239. }