IESHttpTrigger.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.Text;
  12. using System.Text.Json;
  13. using System.Threading.Tasks;
  14. using TEAMModelOS.SDK.DI;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Models;
  17. namespace TEAMModelOS.FunctionV4.HttpTrigger
  18. {
  19. public class IESHttpTrigger
  20. {
  21. private readonly AzureCosmosFactory _azureCosmos;
  22. private readonly DingDing _dingDing;
  23. private readonly AzureStorageFactory _azureStorage;
  24. private readonly AzureRedisFactory _azureRedis;
  25. public IESHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage
  26. , AzureRedisFactory azureRedis)
  27. {
  28. _azureCosmos = azureCosmos;
  29. _dingDing = dingDing;
  30. _azureStorage = azureStorage;
  31. _azureRedis = azureRedis;
  32. }
  33. /// <summary>
  34. /// 数据推送接口
  35. /// </summary>
  36. /// <param name="req"></param>
  37. /// <param name="log"></param>
  38. /// <returns></returns>
  39. [Function("knowledge-change")]
  40. public async Task<HttpResponseData> KnowledgeChange([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData req)
  41. {
  42. var response = req.CreateResponse(HttpStatusCode.OK);
  43. string data = await new StreamReader(req.Body).ReadToEndAsync();
  44. var json = JsonDocument.Parse(data).RootElement;
  45. List<OldNew> old_new = null;
  46. string school = null;
  47. if (json.TryGetProperty("school", out JsonElement _school))
  48. {
  49. school = _school.GetString();
  50. }
  51. if (json.TryGetProperty("old_new", out JsonElement _old_new))
  52. {
  53. old_new = _old_new.ToObject<List<OldNew>>();
  54. }
  55. if (old_new.IsNotEmpty() && !string.IsNullOrWhiteSpace(school))
  56. {
  57. foreach (var on in old_new)
  58. {
  59. List<ItemInfo> items = new List<ItemInfo>();
  60. string sql = $"select value(c) from c where array_contains(c.knowledge,'{on._old}') ";
  61. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ItemInfo>
  62. (queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{_school}") }))
  63. {
  64. items.Add(item);
  65. }
  66. items.ForEach(item => {
  67. //修改知识点
  68. if (!string.IsNullOrEmpty(on._new))
  69. {
  70. for (int i = 0; i < item.knowledge.Count; i++)
  71. {
  72. if (item.knowledge[i].Equals(on._old))
  73. {
  74. item.knowledge[i] = on._new;
  75. }
  76. }
  77. }
  78. else
  79. {
  80. //表示删除知识点
  81. item.knowledge.RemoveAll(x => x.Equals(on._old));
  82. }
  83. });
  84. foreach (var item in items)
  85. {
  86. ItemBlob itemBlob = null;
  87. try
  88. {
  89. BlobDownloadInfo blobDownloadResult = await _azureStorage.GetBlobContainerClient($"{school}").GetBlobClient($"/item/{item.id}/{item.id}.json").DownloadAsync();
  90. if (blobDownloadResult != null)
  91. {
  92. var blob = JsonDocument.Parse(blobDownloadResult.Content);
  93. itemBlob = blob.RootElement.ToObject<ItemBlob>();
  94. itemBlob.exercise.knowledge=item.knowledge;
  95. await _azureStorage.UploadFileByContainer($"{school}", itemBlob.ToJsonString(), "item", $"{item.id}/{item.id}.json", true) ;
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. itemBlob = null;
  101. }
  102. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(item, item.id, new PartitionKey(item.code));
  103. }
  104. }
  105. }
  106. await response.WriteAsJsonAsync(new { data = json });
  107. return response;
  108. }
  109. }
  110. }