TriggerExamLite.cs 11 KB

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