using Azure.Cosmos; using Azure.Storage.Blobs.Models; using HTEXLib.COMM.Helpers; using Microsoft.Azure.Functions.Worker; using Microsoft.Azure.Functions.Worker.Http; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Text.Json; using System.Threading.Tasks; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Models; namespace TEAMModelOS.FunctionV4.HttpTrigger { public class IESHttpTrigger { private readonly AzureCosmosFactory _azureCosmos; private readonly DingDing _dingDing; private readonly AzureStorageFactory _azureStorage; private readonly AzureRedisFactory _azureRedis; public IESHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage , AzureRedisFactory azureRedis) { _azureCosmos = azureCosmos; _dingDing = dingDing; _azureStorage = azureStorage; _azureRedis = azureRedis; } /// /// 数据推送接口 /// /// /// /// [Function("knowledge-change")] public async Task KnowledgeChange([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData req) { var response = req.CreateResponse(HttpStatusCode.OK); string data = await new StreamReader(req.Body).ReadToEndAsync(); var json = JsonDocument.Parse(data).RootElement; List old_new = null; string school = null; if (json.TryGetProperty("school", out JsonElement _school)) { school = _school.GetString(); } if (json.TryGetProperty("old_new", out JsonElement _old_new)) { old_new = _old_new.ToObject>(); } if (old_new.IsNotEmpty() && !string.IsNullOrWhiteSpace(school)) { foreach (var on in old_new) { List items = new List(); string sql = $"select value(c) from c where array_contains(c.knowledge,'{on._old}') "; await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator (queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{_school}") })) { items.Add(item); } items.ForEach(item => { //修改知识点 if (!string.IsNullOrEmpty(on._new)) { for (int i = 0; i < item.knowledge.Count; i++) { if (item.knowledge[i].Equals(on._old)) { item.knowledge[i] = on._new; } } } else { //表示删除知识点 item.knowledge.RemoveAll(x => x.Equals(on._old)); } }); foreach (var item in items) { ItemBlob itemBlob = null; try { BlobDownloadInfo blobDownloadResult = await _azureStorage.GetBlobContainerClient($"{school}").GetBlobClient($"/item/{item.id}/{item.id}.json").DownloadAsync(); if (blobDownloadResult != null) { var blob = JsonDocument.Parse(blobDownloadResult.Content); itemBlob = blob.RootElement.ToObject(); itemBlob.exercise.knowledge=item.knowledge; await _azureStorage.UploadFileByContainer($"{school}", itemBlob.ToJsonString(), "item", $"{item.id}/{item.id}.json", true) ; } } catch (Exception ex) { itemBlob = null; } await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(item, item.id, new PartitionKey(item.code)); } } } await response.WriteAsJsonAsync(new { data = json }); return response; } } }