IESHttpTrigger.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Azure.Cosmos;
  2. using Azure.Storage.Blobs.Models;
  3. using HTEXLib.COMM.Helpers;
  4. using Microsoft.Azure.Functions.Worker;
  5. using Microsoft.Azure.Functions.Worker.Http;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Text.Json;
  14. using System.Threading.Tasks;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Models;
  18. namespace TEAMModelOS.FunctionV4.HttpTrigger
  19. {
  20. public class IESHttpTrigger
  21. {
  22. private readonly AzureCosmosFactory _azureCosmos;
  23. private readonly DingDing _dingDing;
  24. private readonly AzureStorageFactory _azureStorage;
  25. private readonly AzureRedisFactory _azureRedis;
  26. public IESHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage
  27. , AzureRedisFactory azureRedis)
  28. {
  29. _azureCosmos = azureCosmos;
  30. _dingDing = dingDing;
  31. _azureStorage = azureStorage;
  32. _azureRedis = azureRedis;
  33. }
  34. /// <summary>
  35. /// 数据推送接口
  36. /// </summary>
  37. /// <param name="req"></param>
  38. /// <param name="log"></param>
  39. /// <returns></returns>
  40. [Function("system-info-function")]
  41. public async Task<HttpResponseData> SystemInfo([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestData req) {
  42. var response = req.CreateResponse(HttpStatusCode.OK);
  43. Type attr = this.GetType();
  44. string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
  45. Assembly assembly = Assembly.LoadFrom($"{currentDirectory}\\TEAMModelOS.FunctionV4.dll");
  46. var description = assembly.GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
  47. //var v1 = Assembly.GetEntryAssembly().GetName().Version;
  48. //var v2 = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
  49. // var description = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
  50. var version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
  51. long nowtime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  52. //Console.WriteLine($"Assembly.GetEntryAssembly().GetName().Version: " +
  53. // $"{Assembly.GetEntryAssembly().GetName().Version}");5.2107.12.1
  54. //Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version:" +
  55. // $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version}");5.2107.12.1
  56. //Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion:" +
  57. // $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion}");5.2107.12
  58. await response.WriteAsJsonAsync(new { version, description, nowtime });
  59. return response;
  60. }
  61. /// <summary>
  62. /// 数据推送接口
  63. /// </summary>
  64. /// <param name="req"></param>
  65. /// <param name="log"></param>
  66. /// <returns></returns>
  67. [Function("knowledge-change")]
  68. public async Task<HttpResponseData> KnowledgeChange([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData req)
  69. {
  70. var response = req.CreateResponse(HttpStatusCode.OK);
  71. string data = await new StreamReader(req.Body).ReadToEndAsync();
  72. var json = JsonDocument.Parse(data).RootElement;
  73. List<OldNew> old_new = null;
  74. string school = null;
  75. if (json.TryGetProperty("school", out JsonElement _school))
  76. {
  77. school = _school.GetString();
  78. }
  79. if (json.TryGetProperty("old_new", out JsonElement _old_new))
  80. {
  81. old_new = _old_new.ToObject<List<OldNew>>();
  82. }
  83. if (old_new.IsNotEmpty() && !string.IsNullOrWhiteSpace(school))
  84. {
  85. foreach (var on in old_new)
  86. {
  87. List<ItemInfo> items = new List<ItemInfo>();
  88. string sql = $"select value(c) from c where array_contains(c.knowledge,'{on._old}') ";
  89. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ItemInfo>
  90. (queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{_school}") }))
  91. {
  92. items.Add(item);
  93. }
  94. items.ForEach(item => {
  95. //修改知识点
  96. if (!string.IsNullOrEmpty(on._new))
  97. {
  98. for (int i = 0; i < item.knowledge.Count; i++)
  99. {
  100. if (item.knowledge[i].Equals(on._old))
  101. {
  102. item.knowledge[i] = on._new;
  103. }
  104. }
  105. }
  106. else
  107. {
  108. //表示删除知识点
  109. item.knowledge.RemoveAll(x => x.Equals(on._old));
  110. }
  111. });
  112. foreach (var item in items)
  113. {
  114. ItemBlob itemBlob = null;
  115. try
  116. {
  117. BlobDownloadInfo blobDownloadResult = await _azureStorage.GetBlobContainerClient($"{school}").GetBlobClient($"/item/{item.id}/{item.id}.json").DownloadAsync();
  118. if (blobDownloadResult != null)
  119. {
  120. var blob = JsonDocument.Parse(blobDownloadResult.Content);
  121. itemBlob = blob.RootElement.ToObject<ItemBlob>();
  122. itemBlob.exercise.knowledge=item.knowledge;
  123. await _azureStorage.UploadFileByContainer($"{school}", itemBlob.ToJsonString(), "item", $"{item.id}/{item.id}.json", true) ;
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. itemBlob = null;
  129. }
  130. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(item, item.id, new PartitionKey(item.code));
  131. }
  132. }
  133. }
  134. await response.WriteAsJsonAsync(new { data = json });
  135. return response;
  136. }
  137. }
  138. }