TriggerHomework.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 TriggerHomework
  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 ActivityService.DeleteActivity(_coreAPIHttpService, client, _dingDing, data);
  32. return;
  33. }
  34. var adid = tdata.id;
  35. var adcode = "";
  36. string blobcntr = null;
  37. if (tdata.scope.Equals("school"))
  38. {
  39. adcode = $"Activity-{tdata.school}";
  40. blobcntr = tdata.school;
  41. }
  42. else
  43. {
  44. adcode = $"Activity-{tdata.creatorId}";
  45. blobcntr = tdata.creatorId;
  46. }
  47. //await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}作业活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.成都开发測試群組);
  48. Homework work = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Homework>(tdata.id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  49. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  50. if (work != null)
  51. {
  52. string PartitionKey = string.Format("{0}{1}{2}", work.code, "-", work.progress);
  53. List<ChangeRecord> changeRecords = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id }, { "PartitionKey", PartitionKey } });
  54. switch (work.progress)
  55. {
  56. case "pending":
  57. var messageWork = new ServiceBusMessage(new { tdata.id, progress = "going", tdata.code }.ToJsonString());
  58. messageWork.ApplicationProperties.Add("name", "Homework");
  59. if (changeRecords.Count > 0)
  60. {
  61. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  62. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  63. changeRecords[0].sequenceNumber = start;
  64. await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  65. }
  66. else
  67. {
  68. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  69. ChangeRecord changeRecord = new()
  70. {
  71. RowKey = tdata.id,
  72. PartitionKey = PartitionKey,
  73. sequenceNumber = start,
  74. msgId = messageWork.MessageId
  75. };
  76. await table.Save<ChangeRecord>(changeRecord);
  77. }
  78. break;
  79. case "going":
  80. List<(string pId, List<string> gid)> ps = new List<(string pId, List<string> gid)>();
  81. if (work.groupLists.Count > 0)
  82. {
  83. var group = work.groupLists;
  84. foreach (var gp in group)
  85. {
  86. foreach (KeyValuePair<string, List<string>> pp in gp)
  87. {
  88. ps.Add((pp.Key, pp.Value));
  89. }
  90. }
  91. }
  92. List<string> classes = ExamService.getClasses(work.classes, work.stuLists);
  93. (List<RMember> tmdids, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, classes, work.school, ps);
  94. var addStudentsCls = tmdids.FindAll(x => x.type == 2);
  95. var addTmdidsCls = tmdids.FindAll(x => x.type == 1);
  96. List<StuActivity> stuActivities = new();
  97. List<StuActivity> tmdActivities = new();
  98. List<StuActivity> tchActivities = new();
  99. if (addTmdidsCls.IsNotEmpty())
  100. {
  101. addTmdidsCls.ForEach(x =>
  102. {
  103. HashSet<string> classIds = new HashSet<string>();
  104. classLists.ForEach(z => {
  105. z.members.ForEach(y => {
  106. if (y.id.Equals(x) && y.type == 1)
  107. {
  108. classIds.Add(z.id);
  109. }
  110. });
  111. });
  112. tmdActivities.Add(new StuActivity
  113. {
  114. pk = "Activity",
  115. id = work.id,
  116. code = $"Activity-{x.id}",
  117. type = "Homework",
  118. name = work.name,
  119. startTime = work.startTime,
  120. endTime = work.endTime,
  121. scode = work.code,
  122. scope = work.scope,
  123. school = work.school,
  124. creatorId = work.creatorId,
  125. subjects = new List<string> { "" },
  126. blob = work.blob,
  127. owner = work.owner,
  128. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  129. taskStatus = -1,
  130. classIds = classIds.ToList()
  131. });
  132. });
  133. }
  134. if (addStudentsCls.IsNotEmpty())
  135. {
  136. addStudentsCls.ForEach(x =>
  137. {
  138. HashSet<string> classIds = new HashSet<string>();
  139. classLists.ForEach(z => {
  140. z.members.ForEach(y => {
  141. if (y.id.Equals(x) &&y.code.Equals(work.school) && y.type ==2)
  142. {
  143. classIds.Add(z.id);
  144. }
  145. });
  146. });
  147. stuActivities.Add(new StuActivity
  148. {
  149. pk = "Activity",
  150. id = work.id,
  151. code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
  152. type = "Homework",
  153. name = work.name,
  154. startTime = work.startTime,
  155. endTime = work.endTime,
  156. scode = work.code,
  157. scope = work.scope,
  158. school = work.school,
  159. creatorId = work.creatorId,
  160. subjects = new List<string> { "" },
  161. blob = work.blob,
  162. owner = work.owner,
  163. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  164. taskStatus = -1,
  165. classIds = classIds.ToList()
  166. });
  167. });
  168. }
  169. (List<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, work.tchLists, work.school,ps);
  170. (string standard, List<string> tmdids, string school, List<string> update, int statistics) list = (null, null, null, new List<string> { StatisticsService.OfflineRecord }, 0);
  171. if (tchList.IsNotEmpty())
  172. {
  173. list.tmdids = tchList.Select(x => x.id).ToList();
  174. School school = null;
  175. if (!string.IsNullOrEmpty(work.school))
  176. {
  177. school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(work.school, new Azure.Cosmos.PartitionKey("Base"));
  178. list.school = school.id;
  179. list.standard = school.standard;
  180. }
  181. tchList.ForEach(x =>
  182. {
  183. HashSet<string> classIds = new();
  184. classInfos.ForEach(z => {
  185. z.members.ForEach(y => {
  186. if (y.id.Equals(x) && y.type == 1)
  187. {
  188. classIds.Add(z.id);
  189. }
  190. });
  191. });
  192. tchActivities.Add(new StuActivity
  193. {
  194. pk = "Activity",
  195. id = work.id,
  196. code = $"Activity-{x.id}",
  197. type = "Homework",
  198. name = work.name,
  199. startTime = work.startTime,
  200. endTime = work.endTime,
  201. scode = work.code,
  202. scope = work.scope,
  203. school = work.school,
  204. creatorId = work.creatorId,
  205. subjects = new List<string> { "" },
  206. blob = work.blob,
  207. owner = work.owner,
  208. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  209. taskStatus = -1,
  210. classIds = classIds.ToList()
  211. });
  212. });
  213. }
  214. await ActivityService.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, tchActivities);
  215. await StatisticsService.SendServiceBus(list, _configuration, _serviceBus, client);
  216. var messageWorkEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString());
  217. messageWorkEnd.ApplicationProperties.Add("name", "Homework");
  218. if (changeRecords.Count > 0)
  219. {
  220. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  221. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  222. changeRecords[0].sequenceNumber = end;
  223. await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  224. }
  225. else
  226. {
  227. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  228. ChangeRecord changeRecord = new()
  229. {
  230. RowKey = tdata.id,
  231. PartitionKey = PartitionKey,
  232. sequenceNumber = end,
  233. msgId = messageWorkEnd.MessageId
  234. };
  235. await table.Save<ChangeRecord>(changeRecord);
  236. }
  237. //if (bustasks.IsNotEmpty())
  238. //{
  239. // await Task.WhenAll(bustasks);
  240. //}
  241. break;
  242. case "finish":
  243. break;
  244. }
  245. }
  246. }
  247. catch (CosmosException e)
  248. {
  249. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.StackTrace}{e.Message}\n{e.Status}", GroupNames.成都开发測試群組);
  250. }
  251. catch (Exception ex)
  252. {
  253. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修作业活动异常{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  254. }
  255. }
  256. }
  257. }