TriggerHomework.cs 13 KB

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