TriggerExamLite.cs 13 KB

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