ActiveTaskTopic.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.Json;
  5. using System.Threading.Tasks;
  6. using Azure.Cosmos;
  7. using Azure.Messaging.ServiceBus;
  8. using Microsoft.Extensions.Logging;
  9. using StackExchange.Redis;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK;
  13. using TEAMModelOS.SDK.Models;
  14. using TEAMModelOS.SDK.Models.Cosmos;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common;
  16. using System.Linq;
  17. using TEAMModelOS.SDK.Models.Service;
  18. using TEAMModelOS.SDK.Models.Cosmos.BI;
  19. using TEAMModelOS.Models;
  20. using Microsoft.Extensions.Options;
  21. using Microsoft.Extensions.Configuration;
  22. using HTEXLib.COMM.Helpers;
  23. using Microsoft.Azure.Functions.Worker;
  24. using TEAMModelOS.SDK.Services;
  25. using Azure.Storage.Blobs.Models;
  26. using System.IO;
  27. using Azure;
  28. using static TEAMModelOS.SDK.Models.Service.LessonService;
  29. using DinkToPdf.Contracts;
  30. using TEAMModelOS.SDK.Helper.Common.DateTimeHelper;
  31. using static TEAMModelOS.SDK.StatisticsService;
  32. using DocumentFormat.OpenXml.Office2010.Excel;
  33. using DocumentFormat.OpenXml.Wordprocessing;
  34. using System.Xml.Linq;
  35. using Azure.Storage.Sas;
  36. using DocumentFormat.OpenXml.Drawing;
  37. using System.Net.Http;
  38. using System.Web;
  39. using System.Net;
  40. using System.Net.Http.Json;
  41. using System.Threading;
  42. using System.Collections.Concurrent;
  43. using Microsoft.Azure.Amqp.Framing;
  44. using System.Security.Policy;
  45. using TEAMModelOS.SDK.Models.Service.BI;
  46. namespace TEAMModelOS.FunctionV4.ServiceBus
  47. {
  48. public class ActiveTaskTopic
  49. {
  50. private readonly int psize = 20;
  51. private readonly AzureCosmosFactory _azureCosmos;
  52. private readonly DingDing _dingDing;
  53. private readonly AzureStorageFactory _azureStorage;
  54. private readonly AzureRedisFactory _azureRedis;
  55. private readonly AzureServiceBusFactory _serviceBus;
  56. private readonly Option _option;
  57. private readonly CoreAPIHttpService _coreAPIHttpService;
  58. private readonly IConfiguration _configuration;
  59. private readonly IConverter _converter;
  60. private readonly SnowflakeId _snowflakeId;
  61. private readonly HttpTrigger _httpTrigger;
  62. public ActiveTaskTopic(HttpTrigger httpTrigger, SnowflakeId snowflakeId, IConverter converter, CoreAPIHttpService coreAPIHttpService,
  63. AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis,
  64. AzureServiceBusFactory serviceBus, IOptionsSnapshot<Option> option,
  65. IConfiguration configuration)
  66. {
  67. _azureCosmos = azureCosmos;
  68. _dingDing = dingDing;
  69. _azureStorage = azureStorage;
  70. _azureRedis = azureRedis;
  71. _serviceBus = serviceBus;
  72. _option = option?.Value;
  73. _configuration = configuration;
  74. _coreAPIHttpService = coreAPIHttpService;
  75. _converter = converter;
  76. _snowflakeId = snowflakeId;
  77. _httpTrigger = httpTrigger;
  78. }
  79. [Function("Exam")]
  80. public async Task ExamFunc([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "exam", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  81. {
  82. try
  83. {
  84. var json = JsonDocument.Parse(msg);
  85. json.RootElement.TryGetProperty("id", out JsonElement id);
  86. json.RootElement.TryGetProperty("progress", out JsonElement progress);
  87. json.RootElement.TryGetProperty("code", out JsonElement code);
  88. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  89. var client = _azureCosmos.GetCosmosClient();
  90. ExamInfo exam = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  91. exam.progress = progress.ToString();
  92. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  93. }
  94. catch (CosmosException e)
  95. {
  96. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ExamBus()-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}\n{msg}", GroupNames.醍摩豆服務運維群組);
  97. }
  98. catch (Exception ex)
  99. {
  100. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ExamBus()\n{ex.Message}\n{ex.StackTrace}\n\n{msg}", GroupNames.醍摩豆服務運維群組);
  101. }
  102. }
  103. [Function("Vote")]
  104. public async Task VoteFunc([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "vote", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  105. {
  106. try
  107. {
  108. var jsonMsg = JsonDocument.Parse(msg);
  109. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  110. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  111. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  112. var client = _azureCosmos.GetCosmosClient();
  113. Vote vote = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Vote>(id.ToString(), new PartitionKey($"{code}"));
  114. vote.progress = progress.ToString();
  115. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  116. }
  117. catch (CosmosException e)
  118. {
  119. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,VoteBus()-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}\n{msg}", GroupNames.醍摩豆服務運維群組);
  120. }
  121. catch (Exception ex)
  122. {
  123. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,VoteBus()\n{ex.Message}\n{ex.StackTrace}\n\n{msg}", GroupNames.醍摩豆服務運維群組);
  124. }
  125. }
  126. [Function("Correct")]
  127. public async Task CorrectFunc([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "correct", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  128. {
  129. try
  130. {
  131. var jsonMsg = JsonDocument.Parse(msg);
  132. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  133. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  134. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  135. var client = _azureCosmos.GetCosmosClient();
  136. Correct correct = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Correct>(id.ToString(), new PartitionKey($"{code}"));
  137. correct.progress = progress.ToString();
  138. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(correct, id.ToString(), new PartitionKey($"{code}"));
  139. }
  140. catch (CosmosException e)
  141. {
  142. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Correct()-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}\n{msg}", GroupNames.醍摩豆服務運維群組);
  143. }
  144. catch (Exception ex)
  145. {
  146. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Correct()\n{ex.Message}\n{ex.StackTrace}\n\n{msg}", GroupNames.醍摩豆服務運維群組);
  147. }
  148. }
  149. [Function("Survey")]
  150. public async Task SurveyFunc([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "survey", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  151. {
  152. try
  153. {
  154. var jsonMsg = JsonDocument.Parse(msg);
  155. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  156. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  157. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  158. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  159. var client = _azureCosmos.GetCosmosClient();
  160. Survey survey = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  161. survey.progress = progress.ToString();
  162. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  163. }
  164. catch (CosmosException e)
  165. {
  166. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,SurveyBus()-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}\n{msg}", GroupNames.醍摩豆服務運維群組);
  167. }
  168. catch (Exception ex)
  169. {
  170. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,SurveyBus()\n{ex.Message}\n{ex.StackTrace}\n\n{msg}", GroupNames.醍摩豆服務運維群組);
  171. }
  172. }
  173. [Function("Homework")]
  174. public async Task HomeworkFunc([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "homework", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  175. {
  176. try
  177. {
  178. var jsonMsg = JsonDocument.Parse(msg);
  179. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  180. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  181. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  182. var client = _azureCosmos.GetCosmosClient();
  183. Homework homework = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Homework>(id.ToString(), new PartitionKey($"{code}"));
  184. homework.progress = progress.ToString();
  185. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(homework, id.ToString(), new PartitionKey($"{code}"));
  186. }
  187. catch (CosmosException e)
  188. {
  189. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Homework()-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}\n{msg}", GroupNames.醍摩豆服務運維群組);
  190. }
  191. catch (Exception ex)
  192. {
  193. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Homework()\n{ex.Message}\n{ex.StackTrace}\n\n{msg}", GroupNames.醍摩豆服務運維群組);
  194. }
  195. }
  196. [Function("Study")]
  197. public async Task StudyFunc([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "study", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  198. {
  199. string activityId = string.Empty;
  200. try
  201. {
  202. var jsonMsg = JsonDocument.Parse(msg);
  203. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  204. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  205. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  206. var client = _azureCosmos.GetCosmosClient();
  207. Study study = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Study>(id.ToString(), new PartitionKey($"{code}"));
  208. study.progress = progress.ToString();
  209. activityId = id.ToString();
  210. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(study, id.ToString(), new PartitionKey($"{code}"));
  211. }
  212. catch (CosmosException e)
  213. {
  214. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Study()-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}\n{msg}", GroupNames.醍摩豆服務運維群組);
  215. }
  216. catch (Exception ex)
  217. {
  218. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Study()\n{ex.Message}\n{ex.StackTrace}\n\n{msg}", GroupNames.醍摩豆服務運維群組);
  219. }
  220. }
  221. [Function("Art")]
  222. public async Task ArtFunc([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "art", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  223. {
  224. string activityId = string.Empty;
  225. try
  226. {
  227. var jsonMsg = JsonDocument.Parse(msg);
  228. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  229. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  230. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  231. var client = _azureCosmos.GetCosmosClient();
  232. ArtEvaluation art = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ArtEvaluation>(id.ToString(), new PartitionKey($"{code}"));
  233. art.progress = progress.ToString();
  234. activityId = id.ToString();
  235. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(art, id.ToString(), new PartitionKey($"{code}"));
  236. }
  237. catch (CosmosException e)
  238. {
  239. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Art()-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}\n{msg}", GroupNames.醍摩豆服務運維群組);
  240. }
  241. catch (Exception ex)
  242. {
  243. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Art()\n{ex.Message}\n{ex.StackTrace}\n\n{msg}", GroupNames.醍摩豆服務運維群組);
  244. }
  245. }
  246. [Function("ExamLite")]
  247. public async Task ExamLiteFunc([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "examlite", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  248. {
  249. try
  250. {
  251. var jsonMsg = JsonDocument.Parse(msg);
  252. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  253. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  254. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  255. var client = _azureCosmos.GetCosmosClient();
  256. ExamLite lite = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamLite>(id.ToString(), new PartitionKey($"{code}"));
  257. lite.progress = progress.ToString();
  258. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(lite, id.ToString(), new PartitionKey($"{code}"));
  259. }
  260. catch (CosmosException e)
  261. {
  262. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ExamLite()-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}\n{msg}", GroupNames.醍摩豆服務運維群組);
  263. }
  264. catch (Exception ex)
  265. {
  266. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ExamLite()\n{ex.Message}\n{ex.StackTrace}\n\n{msg}", GroupNames.醍摩豆服務運維群組);
  267. }
  268. }
  269. [Function("LessonRecordExpire")]
  270. public async Task LessonRecordExpireFunc([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "lesson-record-expire", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  271. {
  272. try
  273. {
  274. var jsonMsg = JsonDocument.Parse(msg).RootElement;
  275. jsonMsg.TryGetProperty("id", out JsonElement id);
  276. jsonMsg.TryGetProperty("progress", out JsonElement progress);
  277. jsonMsg.TryGetProperty("code", out JsonElement _code);
  278. jsonMsg.TryGetProperty("tmdid", out JsonElement tmdid);
  279. jsonMsg.TryGetProperty("tmdname", out JsonElement tmdname);
  280. jsonMsg.TryGetProperty("name", out JsonElement name);
  281. jsonMsg.TryGetProperty("startTime", out JsonElement startTime);
  282. jsonMsg.TryGetProperty("expire", out JsonElement expire);
  283. jsonMsg.TryGetProperty("scope", out JsonElement scope);
  284. jsonMsg.TryGetProperty("school", out JsonElement _school);
  285. jsonMsg.TryGetProperty("tag", out JsonElement _tag);
  286. var client = _azureCosmos.GetCosmosClient();
  287. //处理到期删除
  288. if (_tag.ValueKind.Equals(JsonValueKind.String) && $"{_tag}".Equals("delete"))
  289. {
  290. string lessonId = $"{id}";
  291. string tbname;
  292. string school = $"{_school}";
  293. string code = "";
  294. if ($"{scope}".Equals("school") && !string.IsNullOrEmpty($"{school}"))
  295. {
  296. code = $"LessonRecord-{school}";
  297. tbname = "School";
  298. }
  299. else if ($"{scope}".Equals("private"))
  300. {
  301. code = $"LessonRecord";
  302. tbname = "Teacher";
  303. }
  304. else
  305. {
  306. return;
  307. }
  308. Azure.Response response = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync(lessonId, new PartitionKey(code));
  309. if (response.Status == 200)
  310. {
  311. LessonRecord lessonRecord;
  312. var doc = JsonDocument.Parse(response.ContentStream);
  313. lessonRecord = doc.RootElement.ToObject<LessonRecord>();
  314. lessonRecord.status = 404;
  315. await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync(lessonRecord, lessonRecord.id, new PartitionKey(lessonRecord.code));
  316. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  317. var messageChange = new ServiceBusMessage(new { delete_id = lessonId, tmdid = tmdid, scope = scope, opt = "delete", school = school }.ToJsonString());
  318. messageChange.ApplicationProperties.Add("name", "LessonRecordEvent");
  319. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  320. await _dingDing.SendBotMsg($"课例:【{lessonRecord.name}】\n课例ID:【{lessonRecord.id}】\n因时间到期,即将被自动删除,到期时间:" +
  321. $"{lessonRecord.expire}\n{lessonRecord.ToJsonString()}", GroupNames.成都开发測試群組);
  322. }
  323. }
  324. string biz = "expire";
  325. Teacher targetTeacher = await client.GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>($"{tmdid}", new PartitionKey($"Base"));
  326. string expireTime = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse($"{expire}")).ToString("yyyy-MM-dd HH:mm:ss");
  327. _coreAPIHttpService.PushNotify(new List<IdNameCode> { new IdNameCode { id = targetTeacher.id, name = targetTeacher.name, code = targetTeacher.lang } }, "expire-private_lessonRecord", Constant.NotifyType_IES5_Course,
  328. new Dictionary<string, object> { { "tmdname", tmdname }, { "tmdid", tmdid }, { "lessonId", id }, { "expireTime", expireTime }, { "lessonName", name } }, $"{Environment.GetEnvironmentVariable("Option:Location")}", _configuration, _dingDing, "");
  329. }
  330. catch (Exception ex)
  331. {
  332. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,LessonRecordExpire()\n{ex.Message}\n{ex.StackTrace}\n\n{msg}", GroupNames.醍摩豆服務運維群組);
  333. }
  334. }
  335. }
  336. }