TriggerStudy.cs 14 KB

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