TriggerStudy.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using DocumentFormat.OpenXml.Bibliography;
  4. using DocumentFormat.OpenXml.Office2013.Excel;
  5. using DocumentFormat.OpenXml.Spreadsheet;
  6. using DocumentFormat.OpenXml.VariantTypes;
  7. using DocumentFormat.OpenXml.Vml;
  8. using HTEXLib.COMM.Helpers;
  9. using Microsoft.Azure.Documents;
  10. using Microsoft.Extensions.Configuration;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Text.Json;
  16. using System.Threading.Tasks;
  17. using TEAMModelOS.SDK;
  18. using TEAMModelOS.SDK.DI;
  19. using TEAMModelOS.SDK.Extension;
  20. using TEAMModelOS.SDK.Models;
  21. using TEAMModelOS.SDK.Models.Cosmos.Common;
  22. using TEAMModelOS.SDK.Models.Service;
  23. using TEAMModelOS.SDK.Models.Service.BI;
  24. using TEAMModelOS.Function;
  25. namespace TEAMModelOS.CosmosDBTriggers
  26. {
  27. public static class TriggerStudy
  28. {
  29. public static async Task Trigger(CoreAPIHttpService _coreAPIHttpService, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  30. CosmosClient client, JsonElement input, TriggerData tdata, AzureRedisFactory _azureRedis, IConfiguration _configuration)
  31. {
  32. try
  33. {
  34. if ((tdata.status != null && tdata.status.Value == 404) || tdata.publish == 1)
  35. {
  36. await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(tdata.id, new PartitionKey(tdata.code));
  37. ActivityList data = input.ToObject<ActivityList>();
  38. //await IESActivityService.DeleteActivity(_coreAPIHttpService, client, _dingDing, data);
  39. var table_cancel = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  40. List<ChangeRecord> records = await table_cancel.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id } });
  41. foreach (var record in records)
  42. {
  43. try
  44. {
  45. await table_cancel.DeleteSingle<ChangeRecord>(record.PartitionKey, record.RowKey);
  46. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
  47. }
  48. catch (Exception ) {
  49. continue;
  50. }
  51. }
  52. await BIStats.SetTypeAddStats(client, _dingDing, tdata.school, "Study", -1, careDate: tdata.startTime);//BI统计增/减量
  53. return;
  54. }
  55. var adid = tdata.id;
  56. var adcode = "";
  57. string blobcntr = null;
  58. if (tdata.scope.Equals("school"))
  59. {
  60. adcode = $"Activity-{tdata.school}";
  61. blobcntr = tdata.school;
  62. }
  63. else
  64. {
  65. adcode = $"Activity-{tdata.creatorId}";
  66. blobcntr = tdata.creatorId;
  67. }
  68. Study study = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Study>(tdata.id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  69. if (study != null)
  70. {
  71. string PartitionKey = string.Format("{0}{1}{2}", study.code, "-", study.progress);
  72. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  73. List<ChangeRecord> changeRecords = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id }, { "PartitionKey", PartitionKey } });
  74. switch (study.progress)
  75. {
  76. case "pending":
  77. var messageWork = new ServiceBusMessage(new { id = tdata.id, progress = "going", code = tdata.code }.ToJsonString());
  78. messageWork.ApplicationProperties.Add("name", "Study");
  79. if (changeRecords.Count > 0)
  80. {
  81. try
  82. {
  83. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  84. }
  85. catch (Exception)
  86. {
  87. }
  88. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  89. changeRecords[0].sequenceNumber = start;
  90. await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  91. }
  92. else
  93. {
  94. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  95. ChangeRecord changeRecord = new()
  96. {
  97. RowKey = tdata.id,
  98. PartitionKey = PartitionKey,
  99. sequenceNumber = start,
  100. msgId = messageWork.MessageId
  101. };
  102. await table.Save<ChangeRecord>(changeRecord);
  103. }
  104. break;
  105. case "going":
  106. try
  107. {
  108. List<(string pId, List<string> gid)> ps = new List<(string pId, List<string> gid)>();
  109. if (study.groupLists.Count > 0)
  110. {
  111. var group = study.groupLists;
  112. foreach (var gp in group)
  113. {
  114. foreach (KeyValuePair<string, List<string>> pp in gp)
  115. {
  116. ps.Add((pp.Key, pp.Value));
  117. }
  118. }
  119. }
  120. (List<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, study.tchLists, study.school, ps);
  121. List<StuActivity> tchActivities = new List<StuActivity>();
  122. (string standard, List<string> tmdids, string school, List<string> update, int statistics) list = (null, null, null, new List<string> { StatisticsService.OfflineRecord }, 0);
  123. if (tchList.IsNotEmpty())
  124. {
  125. list.tmdids = tchList.Select(x => x.id).ToList();
  126. School school = null;
  127. if (!string.IsNullOrEmpty(study.school))
  128. {
  129. school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(study.school, new Azure.Cosmos.PartitionKey("Base"));
  130. list.school = school.id;
  131. list.standard = school.standard;
  132. }
  133. string queryScore = $" select c.id from c where c.id ='{study.id}' and c.pk = 'Activity' ";
  134. List<string> ids = new();
  135. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Teacher).GetItemQueryStreamIterator
  136. (queryText: queryScore))
  137. {
  138. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  139. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  140. {
  141. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  142. {
  143. if (obj.TryGetProperty("id", out JsonElement acId))
  144. {
  145. ids.Add(acId.GetString());
  146. }
  147. }
  148. }
  149. }
  150. if (ids.Count == 0)
  151. {
  152. tchList.ForEach(x =>
  153. {
  154. tchActivities.Add(new StuActivity
  155. {
  156. pk = "Activity",
  157. id = study.id,
  158. code = $"Activity-{x.id}",
  159. type = "Study",
  160. name = study.name,
  161. startTime = study.startTime,
  162. endTime = study.endTime,
  163. scode = study.code,
  164. scope = study.scope,
  165. school = study.school,
  166. creatorId = study.creatorId,
  167. subjects = new List<string> { "" },
  168. blob = null,
  169. owner = study.owner,
  170. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  171. taskStatus = -1,
  172. classIds = study.tchLists
  173. });
  174. });
  175. }
  176. }
  177. await IESActivityService.SaveStuActivity(client, _dingDing, null, null, tchActivities);
  178. await StatisticsService.SendServiceBus(list, _configuration, _serviceBus, client);
  179. var messageWorkEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString());
  180. messageWorkEnd.ApplicationProperties.Add("name", "Study");
  181. if (study.teacIds != null && study.teacIds.Count == 0)
  182. {
  183. study.teacIds = list.tmdids;
  184. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(study, study.id, new PartitionKey($"{study.code}"));
  185. }
  186. if (changeRecords.Count > 0)
  187. {
  188. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  189. try
  190. {
  191. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  192. }
  193. catch (Exception)
  194. {
  195. }
  196. changeRecords[0].sequenceNumber = end;
  197. await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  198. }
  199. else
  200. {
  201. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  202. ChangeRecord changeRecord = new()
  203. {
  204. RowKey = tdata.id,
  205. PartitionKey = PartitionKey,
  206. sequenceNumber = end,
  207. msgId = messageWorkEnd.MessageId
  208. };
  209. await table.Save<ChangeRecord>(changeRecord);
  210. }
  211. }
  212. catch (Exception ex)
  213. {
  214. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动异常-going {ex.Message}\n{ex.StackTrace}{tdata.ToJsonString()}{input}", GroupNames.醍摩豆服務運維群組);
  215. }
  216. finally {
  217. string pk = string.Format("{0}{1}{2}", study.code, "-", "pending");
  218. await table.DeleteSingle<ChangeRecord>(pk, tdata.id);
  219. }
  220. break;
  221. case "finish":
  222. try
  223. {
  224. List<(string pId, List<string> gid)> gls = new List<(string pId, List<string> gid)>();
  225. if (study.groupLists.Count > 0)
  226. {
  227. var group = study.groupLists;
  228. foreach (var gp in group)
  229. {
  230. foreach (KeyValuePair<string, List<string>> pp in gp)
  231. {
  232. gls.Add((pp.Key, pp.Value));
  233. }
  234. }
  235. }
  236. //处理教研活动结束统计账户信息
  237. if (study.staffIds.Count == 0)
  238. {
  239. List<FMember> idList = await GroupListService.GetFinishMemberInfo(_coreAPIHttpService, client, _dingDing, study.school, study.classes, study.stuLists, study.tchLists, gls);
  240. study.staffIds = idList;
  241. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Study>(study, study.id, new Azure.Cosmos.PartitionKey(study.code));
  242. }
  243. }
  244. catch (Exception e)
  245. {
  246. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动异常-finish {e.Message}\n{e.StackTrace}{tdata.ToJsonString()}{input}", GroupNames.醍摩豆服務運維群組);
  247. }
  248. finally {
  249. string pk = string.Format("{0}{1}{2}", study.code, "-", "going");
  250. await table.DeleteSingle<ChangeRecord>(pk, tdata.id);
  251. }
  252. break;
  253. }
  254. }
  255. }
  256. catch (CosmosException e)
  257. {
  258. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}", GroupNames.醍摩豆服務運維群組);
  259. }
  260. catch (Exception ex)
  261. {
  262. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动异常{ex.Message}\n{ex.StackTrace}{tdata.ToJsonString()}{input}", GroupNames.醍摩豆服務運維群組);
  263. }
  264. }
  265. }
  266. }