|
@@ -121,83 +121,100 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
[HttpPost("copy-infos")]
|
|
|
public async Task<IActionResult> CopyInfos(JsonElement jsonElement)
|
|
|
{
|
|
|
- if (!jsonElement.TryGetProperty("papers", out JsonElement jsPapers)) return BadRequest();
|
|
|
-
|
|
|
- List<CopyPaper> papers = jsPapers.ToObject<List<CopyPaper>>();
|
|
|
-
|
|
|
- var cosmosClinet = _azureCosmos.GetCosmosClient();
|
|
|
- foreach (var cItem in papers)
|
|
|
+ try
|
|
|
{
|
|
|
- Paper paper = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<Paper>(cItem.oldId, new PartitionKey($"Paper-{cItem.oldSc}"));
|
|
|
- paper.periodId = cItem.newPrdId;
|
|
|
- paper.subjectId = cItem.newSubId;
|
|
|
- paper.subjectName = cItem.newSubName;
|
|
|
- paper.gradeIds = cItem.newGrId;
|
|
|
- paper.code = $"Paper-{cItem.newSc}";
|
|
|
-
|
|
|
- var resState = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(paper.id, new PartitionKey(paper.code));
|
|
|
- if (resState.Status == 200)
|
|
|
- paper.id = Guid.NewGuid().ToString();
|
|
|
+ if (!jsonElement.TryGetProperty("papers", out JsonElement jsPapers)) return BadRequest();
|
|
|
+ if (!jsonElement.TryGetProperty("newSc", out JsonElement _newSchool)) return BadRequest();
|
|
|
|
|
|
- paper = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<Paper>(paper, new PartitionKey(paper.code));
|
|
|
-
|
|
|
-
|
|
|
- var oldBobCilent = _azureStorage.GetBlobContainerClient(containerName: $"{cItem.oldSc}");
|
|
|
- var newBobCilent = _azureStorage.GetBlobContainerClient(containerName: $"{cItem.newSc}");
|
|
|
-
|
|
|
- string blobUrl = "";
|
|
|
- if (paper.blob.IndexOf('/') == 0)
|
|
|
- blobUrl = $"{paper.blob}".Substring(1, paper.blob.Length - 1);
|
|
|
- else
|
|
|
- blobUrl = $"{paper.blob}";
|
|
|
+ List<CopyPaper> papers = jsPapers.ToObject<List<CopyPaper>>();
|
|
|
+ List<newSchool> newSchools = _newSchool.ToObject<List<newSchool>>();
|
|
|
|
|
|
List<Task<CopyFromUriOperation>> filelist = new(); //可复制256M以上文件
|
|
|
- await foreach (BlobItem blobItem in oldBobCilent.GetBlobsAsync(BlobTraits.None, BlobStates.None, blobUrl))
|
|
|
+ List<dynamic> existScPaper = new();
|
|
|
+ var cosmosClinet = _azureCosmos.GetCosmosClient();
|
|
|
+ foreach (var itemSc in newSchools)
|
|
|
{
|
|
|
- var urlSas = _azureStorage.GetBlobSAS($"{cItem.oldSc}", blobItem.Name, BlobSasPermissions.Read | BlobSasPermissions.List); //获取容器sas和有效期
|
|
|
- filelist.Add(newBobCilent.GetBlobClient(blobItem.Name).StartCopyFromUriAsync(new Uri(urlSas)));
|
|
|
- ////序列化数据修改数据
|
|
|
- //if (blobItem.Name.EndsWith(".json"))
|
|
|
- //{
|
|
|
- // BlobClient tempClient = oldBobCilent.GetBlobClient(blobItem.Name);
|
|
|
- // using (var meomoryStream = new MemoryStream())
|
|
|
- // {
|
|
|
- // StringBuilder itemStr = new();
|
|
|
- // BlobClient tempBlobClient = oldBobCilent.GetBlobClient(blobItem.Name);
|
|
|
- // BlobDownloadInfo download = tempBlobClient.Download();
|
|
|
- // var content = download.Content;
|
|
|
- // string text;
|
|
|
- // using (var streamReader = new StreamReader(content))
|
|
|
- // {
|
|
|
- // while ((text = streamReader.ReadLine()) != null)
|
|
|
- // {
|
|
|
- // if (streamReader.EndOfStream)
|
|
|
- // itemStr.Append($"{text.ToString()}");
|
|
|
- // else
|
|
|
- // itemStr.Append($"{text.ToString()},");
|
|
|
- // }
|
|
|
- // streamReader.Close();
|
|
|
- // }
|
|
|
- // string input = itemStr.ToString();
|
|
|
- // BlobItemInfo blobbItemInfo = input.ToObject<BlobItemInfo>();
|
|
|
-
|
|
|
- // //var response = oldBobCilent.GetBlobClient($"{blobItem.Name}").DownloadTo(meomoryStream);
|
|
|
- // //BlobItemInfo blobbItemInfo = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToString().ToObject<BlobItemInfo>();
|
|
|
- // blobbItemInfo.exercise.periodId = cItem.newPrdId;
|
|
|
- // blobbItemInfo.exercise.gradeIds = cItem.newGrId;
|
|
|
-
|
|
|
- // blobbItemInfo.exercise.subjectId = cItem.newSubId;
|
|
|
-
|
|
|
- // var urlSas = _azureStorage.GetBlobSAS($"{cItem.newSc}", blobItem.Name, BlobSasPermissions.Read | BlobSasPermissions.List); //获取容器sas和有效期
|
|
|
+ List<string> existPaper = new();
|
|
|
+ foreach (var cItem in papers)
|
|
|
+ {
|
|
|
+ Paper paper = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<Paper>(cItem.oldId, new PartitionKey($"Paper-{cItem.oldSc}"));
|
|
|
+ paper.periodId = itemSc.newPrdId;
|
|
|
+ paper.subjectId = itemSc.newSubId;
|
|
|
+ paper.subjectName = itemSc.newSubName;
|
|
|
+ paper.gradeIds = itemSc.newGrId;
|
|
|
+ paper.code = $"Paper-{itemSc.newSc}";
|
|
|
+
|
|
|
+ var resState = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(paper.id, new PartitionKey(paper.code));
|
|
|
+ if (resState.Status == 200)
|
|
|
+ paper.id = Guid.NewGuid().ToString();
|
|
|
+
|
|
|
+ await foreach (var item in cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<string>(queryText: $"select value(c.name) from c where c.name='{paper.name}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{itemSc.newSc}") }))
|
|
|
+ {
|
|
|
+ existPaper.Add(item);
|
|
|
+ }
|
|
|
|
|
|
- // var ster = newBobCilent.UploadFileByContainer(blobbItemInfo.ToJsonString(), blobItem.Name, blobbItemInfo.id);
|
|
|
- // }
|
|
|
- //}
|
|
|
+ if (existPaper.Count == 0)
|
|
|
+ {
|
|
|
+ paper = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<Paper>(paper, new PartitionKey(paper.code));
|
|
|
+
|
|
|
+ var oldBobCilent = _azureStorage.GetBlobContainerClient(containerName: $"{cItem.oldSc}");
|
|
|
+ var newBobCilent = _azureStorage.GetBlobContainerClient(containerName: $"{itemSc.newSc}");
|
|
|
+
|
|
|
+ string blobUrl = "";
|
|
|
+ if (paper.blob.IndexOf('/') == 0)
|
|
|
+ blobUrl = $"{paper.blob}".Substring(1, paper.blob.Length - 1);
|
|
|
+ else
|
|
|
+ blobUrl = $"{paper.blob}";
|
|
|
+
|
|
|
+ await foreach (BlobItem blobItem in oldBobCilent.GetBlobsAsync(BlobTraits.None, BlobStates.None, blobUrl))
|
|
|
+ {
|
|
|
+ var urlSas = _azureStorage.GetBlobSAS($"{cItem.oldSc}", blobItem.Name, BlobSasPermissions.Read | BlobSasPermissions.List); //获取容器sas和有效期
|
|
|
+ filelist.Add(newBobCilent.GetBlobClient(blobItem.Name).StartCopyFromUriAsync(new Uri(urlSas)));
|
|
|
+ ////序列化数据修改数据
|
|
|
+ //if (blobItem.Name.EndsWith(".json"))
|
|
|
+ //{
|
|
|
+ // BlobClient tempClient = oldBobCilent.GetBlobClient(blobItem.Name);
|
|
|
+ // using (var meomoryStream = new MemoryStream())
|
|
|
+ // {
|
|
|
+ // StringBuilder itemStr = new();
|
|
|
+ // BlobClient tempBlobClient = oldBobCilent.GetBlobClient(blobItem.Name);
|
|
|
+ // BlobDownloadInfo download = tempBlobClient.Download();
|
|
|
+ // var content = download.Content;
|
|
|
+ // string text;
|
|
|
+ // using (var streamReader = new StreamReader(content))
|
|
|
+ // {
|
|
|
+ // while ((text = streamReader.ReadLine()) != null)
|
|
|
+ // {
|
|
|
+ // if (streamReader.EndOfStream)
|
|
|
+ // itemStr.Append($"{text.ToString()}");
|
|
|
+ // else
|
|
|
+ // itemStr.Append($"{text.ToString()},");
|
|
|
+ // }
|
|
|
+ // streamReader.Close();
|
|
|
+ // }
|
|
|
+ // string input = itemStr.ToString();
|
|
|
+ // BlobItemInfo blobbItemInfo = input.ToObject<BlobItemInfo>();
|
|
|
+
|
|
|
+ // //var response = oldBobCilent.GetBlobClient($"{blobItem.Name}").DownloadTo(meomoryStream);
|
|
|
+ // //BlobItemInfo blobbItemInfo = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToString().ToObject<BlobItemInfo>();
|
|
|
+ // blobbItemInfo.exercise.periodId = cItem.newPrdId;
|
|
|
+ // blobbItemInfo.exercise.gradeIds = cItem.newGrId;
|
|
|
+
|
|
|
+ // blobbItemInfo.exercise.subjectId = cItem.newSubId;
|
|
|
+
|
|
|
+ // var urlSas = _azureStorage.GetBlobSAS($"{cItem.newSc}", blobItem.Name, BlobSasPermissions.Read | BlobSasPermissions.List); //获取容器sas和有效期
|
|
|
+
|
|
|
+ // var ster = newBobCilent.UploadFileByContainer(blobbItemInfo.ToJsonString(), blobItem.Name, blobbItemInfo.id);
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (existPaper.Count > 0)
|
|
|
+ existScPaper.Add(new { schoolId = itemSc.newSc, papers = existPaper });
|
|
|
}
|
|
|
if (filelist.Count <= 256)
|
|
|
- {
|
|
|
await Task.WhenAll(filelist);
|
|
|
- }
|
|
|
else
|
|
|
{
|
|
|
int pages = (filelist.Count + 255) / 256;
|
|
@@ -208,9 +225,14 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
await Task.WhenAll(rspBlobCopyInfos);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ return Ok(new { state = RespondCode.Ok , existScPaper });
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ //await _dingDing.SendBotMsg($"BI,{_option.Location} /paper/copy-infos \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
|
|
|
+ return BadRequest();
|
|
|
}
|
|
|
- return Ok(new { state = RespondCode.Ok});
|
|
|
}
|
|
|
|
|
|
|
|
@@ -554,12 +576,15 @@ namespace TEAMModelBI.Controllers.Census
|
|
|
{
|
|
|
public string oldId { get; set; }
|
|
|
public string oldSc { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ public record newSchool
|
|
|
+ {
|
|
|
public string newSc { get; set; }
|
|
|
public string newPrdId { get; set; }
|
|
|
public string newSubId { get; set; }
|
|
|
public string newSubName { get; set; }
|
|
|
public List<string> newGrId { get; set; }
|
|
|
-
|
|
|
}
|
|
|
|
|
|
}
|