TriggerHomework.cs 12 KB

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