TriggerHomework.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using HTEXLib.COMM.Helpers;
  4. using Microsoft.Azure.Documents;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using TEAMModelOS.SDK;
  9. using TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. using TEAMModelOS.SDK.Models;
  12. using TEAMModelOS.SDK.Models.Cosmos.Common;
  13. using TEAMModelOS.SDK.Models.Service;
  14. namespace TEAMModelFunction
  15. {
  16. public static class TriggerHomework
  17. {
  18. public static async void Trigger(AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  19. CosmosClient client, Document input, TriggerData tdata, AzureRedisFactory _azureRedis)
  20. {
  21. try
  22. {
  23. if ((tdata.status != null && tdata.status.Value == 404) || tdata.ttl > 0)
  24. {
  25. return;
  26. }
  27. var adid = tdata.id;
  28. var adcode = "";
  29. string blobcntr = null;
  30. if (tdata.scope.Equals("school"))
  31. {
  32. adcode = $"Activity-{tdata.school}";
  33. blobcntr = tdata.school;
  34. }
  35. else
  36. {
  37. adcode = $"Activity-{tdata.creatorId}";
  38. blobcntr = tdata.creatorId;
  39. }
  40. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}作业活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.成都开发測試群組);
  41. Homework work = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Homework>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  42. if (work != null)
  43. {
  44. string PartitionKey = string.Format("{0}{1}{2}", work.code, "-", work.progress);
  45. List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", PartitionKey } });
  46. switch (work.progress)
  47. {
  48. case "pending":
  49. var messageWork = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
  50. messageWork.ApplicationProperties.Add("name", "Homework");
  51. if (changeRecords.Count > 0)
  52. {
  53. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  54. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  55. changeRecords[0].sequenceNumber = start;
  56. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  57. }
  58. else
  59. {
  60. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  61. ChangeRecord changeRecord = new ChangeRecord
  62. {
  63. RowKey = input.Id,
  64. PartitionKey = PartitionKey,
  65. sequenceNumber = start,
  66. msgId = messageWork.MessageId
  67. };
  68. await _azureStorage.Save<ChangeRecord>(changeRecord);
  69. }
  70. break;
  71. case "going":
  72. List<string> classes = ExamService.getClasses(work.classes, work.stuLists);
  73. (List<RMember> tmdids, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(client, _dingDing, classes, work.school);
  74. var addStudentsCls = tmdids.FindAll(x => x.type == 2);
  75. var addTmdidsCls = tmdids.FindAll(x => x.type == 1);
  76. List<StuActivity> stuActivities = new List<StuActivity>();
  77. List<StuActivity> tmdActivities = new List<StuActivity>();
  78. List<StuActivity> tchActivities = new List<StuActivity>();
  79. if (addTmdidsCls.IsNotEmpty())
  80. {
  81. addTmdidsCls.ForEach(x =>
  82. {
  83. tmdActivities.Add(new StuActivity
  84. {
  85. pk = "Activity",
  86. id = work.id,
  87. code = $"Activity-{x.id}",
  88. type = "Homework",
  89. name = work.name,
  90. startTime = work.startTime,
  91. endTime = work.endTime,
  92. scode = work.code,
  93. scope = work.scope,
  94. school = work.school,
  95. creatorId = work.creatorId,
  96. subjects = new List<string> { "" },
  97. blob = work.blob,
  98. owner = work.owner,
  99. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  100. taskStatus = -1,
  101. classIds = classes
  102. });
  103. });
  104. }
  105. if (addStudentsCls.IsNotEmpty())
  106. {
  107. addStudentsCls.ForEach(x =>
  108. {
  109. stuActivities.Add(new StuActivity
  110. {
  111. pk = "Activity",
  112. id = work.id,
  113. code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
  114. type = "Homework",
  115. name = work.name,
  116. startTime = work.startTime,
  117. endTime = work.endTime,
  118. scode = work.code,
  119. scope = work.scope,
  120. school = work.school,
  121. creatorId = work.creatorId,
  122. subjects = new List<string> { "" },
  123. blob = work.blob,
  124. owner = work.owner,
  125. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  126. taskStatus = -1,
  127. classIds = classes
  128. });
  129. });
  130. }
  131. (List<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetStutmdidListids(client, _dingDing, work.tchLists, work.school);
  132. // (List<TmdInfo> tmdInfos, List<ClassListInfo> classInfos) = await TriggerStuActivity.GetTchList(client, _dingDing, ids, $"{school}");
  133. // (List<TmdInfo> tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, work.tchLists, work.school);
  134. if (tchList.IsNotEmpty())
  135. {
  136. tchList.ForEach(x =>
  137. {
  138. tchActivities.Add(new StuActivity
  139. {
  140. pk = "Activity",
  141. id = work.id,
  142. code = $"Activity-{x.id}",
  143. type = "Homework",
  144. name = work.name,
  145. startTime = work.startTime,
  146. endTime = work.endTime,
  147. scode = work.code,
  148. scope = work.scope,
  149. school = work.school,
  150. creatorId = work.creatorId,
  151. subjects = new List<string> { "" },
  152. blob = work.blob,
  153. owner = work.owner,
  154. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  155. taskStatus = -1,
  156. classIds = work.tchLists
  157. });
  158. });
  159. }
  160. await TriggerStuActivity.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, tchActivities);
  161. var messageWorkEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
  162. messageWorkEnd.ApplicationProperties.Add("name", "Homework");
  163. if (changeRecords.Count > 0)
  164. {
  165. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  166. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  167. changeRecords[0].sequenceNumber = end;
  168. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  169. }
  170. else
  171. {
  172. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  173. ChangeRecord changeRecord = new ChangeRecord
  174. {
  175. RowKey = input.Id,
  176. PartitionKey = PartitionKey,
  177. sequenceNumber = end,
  178. msgId = messageWorkEnd.MessageId
  179. };
  180. await _azureStorage.Save<ChangeRecord>(changeRecord);
  181. }
  182. break;
  183. case "finish":
  184. break;
  185. }
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修作业活动异常{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  191. }
  192. }
  193. }
  194. }