TriggerExamLite.cs 13 KB

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