TriggerStudy.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. namespace TEAMModelFunction
  14. {
  15. public static class TriggerStudy
  16. {
  17. public static async void Trigger(AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  18. CosmosClient client, Document input, TriggerData tdata, AzureRedisFactory _azureRedis)
  19. {
  20. try
  21. {
  22. if ((tdata.status != null && tdata.status.Value == 404) || tdata.ttl > 0 || tdata.publish == 1)
  23. {
  24. return;
  25. }
  26. var adid = tdata.id;
  27. var adcode = "";
  28. string blobcntr = null;
  29. if (tdata.scope.Equals("school"))
  30. {
  31. adcode = $"Activity-{tdata.school}";
  32. blobcntr = tdata.school;
  33. }
  34. else
  35. {
  36. adcode = $"Activity-{tdata.creatorId}";
  37. blobcntr = tdata.creatorId;
  38. }
  39. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.成都开发測試群組);
  40. Study study = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Study>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  41. if (study != null)
  42. {
  43. string PartitionKey = string.Format("{0}{1}{2}", study.code, "-", study.progress);
  44. List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", PartitionKey } });
  45. switch (study.progress)
  46. {
  47. case "pending":
  48. var messageWork = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
  49. messageWork.ApplicationProperties.Add("name", "Study");
  50. if (changeRecords.Count > 0)
  51. {
  52. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  53. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  54. changeRecords[0].sequenceNumber = start;
  55. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  56. }
  57. else
  58. {
  59. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  60. ChangeRecord changeRecord = new ChangeRecord
  61. {
  62. RowKey = input.Id,
  63. PartitionKey = PartitionKey,
  64. sequenceNumber = start,
  65. msgId = messageWork.MessageId
  66. };
  67. await _azureStorage.Save<ChangeRecord>(changeRecord);
  68. }
  69. break;
  70. case "going":
  71. List<(string pId, List<string> gid)> ps = new List<(string pId, List<string> gid)>();
  72. if (study.groupLists.Count > 0)
  73. {
  74. foreach (var gp in study.groupLists)
  75. {
  76. ps.Add((gp.Key, gp.Value));
  77. }
  78. }
  79. (List<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetStutmdidListids(client, _dingDing, study.tchLists, study.school,ps);
  80. // (List<TmdInfo> tmdInfos, List<ClassListInfo> classInfos) = await TriggerStuActivity.GetTchList(client, _dingDing, ids, $"{school}");
  81. //(List<TmdInfo> tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, study.tchLists, study.school);
  82. List<StuActivity> tchActivities = new List<StuActivity>();
  83. if (tchList.IsNotEmpty())
  84. {
  85. tchList.ForEach(x =>
  86. {
  87. tchActivities.Add(new StuActivity
  88. {
  89. pk = "Activity",
  90. id = study.id,
  91. code = $"Activity-{x.id}",
  92. type = "Study",
  93. name = study.name,
  94. startTime = study.startTime,
  95. endTime = study.endTime,
  96. scode = study.code,
  97. scope = study.scope,
  98. school = study.school,
  99. creatorId = study.creatorId,
  100. subjects = new List<string> { "" },
  101. blob = null,
  102. owner = study.owner,
  103. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  104. taskStatus = -1,
  105. classIds = study.tchLists
  106. });
  107. });
  108. }
  109. await ActivityService.SaveStuActivity(client, _dingDing, null, null, tchActivities);
  110. var messageWorkEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
  111. messageWorkEnd.ApplicationProperties.Add("name", "Study");
  112. if (changeRecords.Count > 0)
  113. {
  114. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  115. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  116. changeRecords[0].sequenceNumber = end;
  117. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  118. }
  119. else
  120. {
  121. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  122. ChangeRecord changeRecord = new ChangeRecord
  123. {
  124. RowKey = input.Id,
  125. PartitionKey = PartitionKey,
  126. sequenceNumber = end,
  127. msgId = messageWorkEnd.MessageId
  128. };
  129. await _azureStorage.Save<ChangeRecord>(changeRecord);
  130. }
  131. break;
  132. case "finish":
  133. break;
  134. }
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动异常{ex.Message}{ex.StackTrace}{tdata.ToJsonString()}{input}", GroupNames.成都开发測試群組);
  140. }
  141. }
  142. }
  143. }