TriggerStudy.cs 16 KB

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