123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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;
- }
- /// <summary>
- /// 数据推送接口
- /// </summary>
- /// <param name="req"></param>
- /// <param name="log"></param>
- /// <returns></returns>
- [Function("knowledge-change")]
- public async Task<HttpResponseData> 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<OldNew> 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<List<OldNew>>();
- }
- if (old_new.IsNotEmpty() && !string.IsNullOrWhiteSpace(school))
- {
- foreach (var on in old_new)
- {
- List<ItemInfo> items = new List<ItemInfo>();
- 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<ItemInfo>
- (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>();
- 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;
- }
- }
- }
|