TriggerHomework.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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<TmdInfo> tmdids, List<StuInfo> students, List<ClassListInfo> classLists) = await TriggerStuActivity.GetStuList(client, _dingDing, classes, work.school);
  74. (List<RMember> tmdids, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(client, _dingDing, classes, work.school);
  75. var addStudentsCls = tmdids.FindAll(x => x.type == 2);
  76. var addTmdidsCls = tmdids.FindAll(x => x.type == 1);
  77. List<StuActivity> stuActivities = new List<StuActivity>();
  78. List<StuActivity> tmdActivities = new List<StuActivity>();
  79. List<StuActivity> tchActivities = new List<StuActivity>();
  80. if (addTmdidsCls.IsNotEmpty())
  81. {
  82. addTmdidsCls.ForEach(x =>
  83. {
  84. tmdActivities.Add(new StuActivity
  85. {
  86. pk = "Activity",
  87. id = work.id,
  88. code = $"Activity-{x.id}",
  89. type = "Homework",
  90. name = work.name,
  91. startTime = work.startTime,
  92. endTime = work.endTime,
  93. scode = work.code,
  94. scope = work.scope,
  95. school = work.school,
  96. creatorId = work.creatorId,
  97. subjects = new List<string> { "" },
  98. blob = work.blob,
  99. owner = work.owner,
  100. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  101. taskStatus = -1,
  102. classIds = classes
  103. });
  104. });
  105. }
  106. if (addStudentsCls.IsNotEmpty())
  107. {
  108. addStudentsCls.ForEach(x =>
  109. {
  110. stuActivities.Add(new StuActivity
  111. {
  112. pk = "Activity",
  113. id = work.id,
  114. code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
  115. type = "Homework",
  116. name = work.name,
  117. startTime = work.startTime,
  118. endTime = work.endTime,
  119. scode = work.code,
  120. scope = work.scope,
  121. school = work.school,
  122. creatorId = work.creatorId,
  123. subjects = new List<string> { "" },
  124. blob = work.blob,
  125. owner = work.owner,
  126. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  127. taskStatus = -1,
  128. classIds = classes
  129. });
  130. });
  131. }
  132. (List<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetStutmdidListids(client, _dingDing, work.tchLists, work.school);
  133. // (List<TmdInfo> tmdInfos, List<ClassListInfo> classInfos) = await TriggerStuActivity.GetTchList(client, _dingDing, ids, $"{school}");
  134. // (List<TmdInfo> tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, work.tchLists, work.school);
  135. if (tchList.IsNotEmpty())
  136. {
  137. tchList.ForEach(x =>
  138. {
  139. tchActivities.Add(new StuActivity
  140. {
  141. pk = "Activity",
  142. id = work.id,
  143. code = $"Activity-{x.id}",
  144. type = "Homework",
  145. name = work.name,
  146. startTime = work.startTime,
  147. endTime = work.endTime,
  148. scode = work.code,
  149. scope = work.scope,
  150. school = work.school,
  151. creatorId = work.creatorId,
  152. subjects = new List<string> { "" },
  153. blob = work.blob,
  154. owner = work.owner,
  155. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  156. taskStatus = -1,
  157. classIds = work.tchLists
  158. });
  159. });
  160. }
  161. await TriggerStuActivity.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, tchActivities);
  162. var messageWorkEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
  163. messageWorkEnd.ApplicationProperties.Add("name", "Homework");
  164. if (changeRecords.Count > 0)
  165. {
  166. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  167. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  168. changeRecords[0].sequenceNumber = end;
  169. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  170. }
  171. else
  172. {
  173. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  174. ChangeRecord changeRecord = new ChangeRecord
  175. {
  176. RowKey = input.Id,
  177. PartitionKey = PartitionKey,
  178. sequenceNumber = end,
  179. msgId = messageWorkEnd.MessageId
  180. };
  181. await _azureStorage.Save<ChangeRecord>(changeRecord);
  182. }
  183. break;
  184. case "finish":
  185. break;
  186. }
  187. }
  188. }
  189. catch (Exception ex)
  190. {
  191. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修作业活动异常{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  192. }
  193. }
  194. }
  195. }