using Azure;
using Azure.Cosmos;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK;
namespace TEAMModelOS.SDK.Models.Service
{
public static class FixDataService
{
///
/// 修复学生数据
///
///
///
///
///
///
public static async Task> FixStudentInfo(CosmosClient client, DingDing _dingDing, AzureStorageFactory _azureStorage, JsonElement data) {
var code = data.GetProperty("code").GetString();
var ids = data.GetProperty("ids").ToObject>();
var dict = data.GetProperty("dict").ToObject>();
string queryText = $"SELECT VALUE c FROM c WHERE c.id IN ({string.Join(",", ids.Select(o => $"'{o}'"))})";
List students = new List();
await foreach (var item in client.GetContainer("TEAMModelOS", "Student")
.GetItemQueryIterator(
queryText: queryText,
requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{code}") })) {
foreach (var key in dict.Keys) {
switch (key) {
case "classId":
item.classId =$"{ dict[key]}";
break;
case "periodId":
item.periodId = $"{ dict[key]}";
break;
case "schoolId":
item.schoolId = $"{ dict[key]}";
break;
case "year":
int year = DateTime.Now.Year;
int.TryParse($"dict[key]", out year);
item.year = year;
break;
default:
break;
}
await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(item, item.id, new PartitionKey(item.code));
students.Add(item);
}
}
return students;
}
///
/// 修复内容模块数据
///
///
///
///
///
///
public static async Task FixBlobContent(CosmosClient client, DingDing _dingDing, AzureStorageFactory _azureStorage, JsonElement data)
{
if (data.TryGetProperty("doPrivate", out JsonElement _doPrivate)&& $"{_doPrivate}".Equals("yes",StringComparison.OrdinalIgnoreCase)) {
foreach (var cnt in _azureStorage.GetBlobServiceClient().GetBlobContainers())
{
if (cnt.Name.Length == 10 && int.TryParse(cnt.Name, out _))
{
await doFixBlob(client, _azureStorage, cnt.Name, "private");
}
}
}
List schools = new List();
await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator(queryText: "select value(c) from c", requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") })) {
schools.Add(item);
}
foreach (var school in schools) {
await doFixBlob(client, _azureStorage, school.id, "school");
}
}
private static async Task doFixBlob(CosmosClient client, AzureStorageFactory _azureStorage,string name,string scope) {
List prefixs = new List() { "audio", "doc", "image", "other", "res", "video"};
var ContainerClient = _azureStorage.GetBlobContainerClient($"{name}");
var tb = "Teacher";
if (scope != "private")
{
tb = "School";
}
List ids = new List();
await foreach (var item in client.GetContainer("TEAMModelOS", tb).GetItemQueryIterator(queryDefinition: new QueryDefinition("select c.id from c "), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
{
ids.Add(item.id);
}
await client.GetContainer("TEAMModelOS", tb).DeleteItemsStreamAsync(ids, $"Bloblog-{name}");
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
foreach (var prefix in prefixs)
{
if (prefix.Equals("res"))
{
List itemres = await ContainerClient.List(prefix);
if (itemres.IsNotEmpty())
{
HashSet set = new HashSet();
itemres.ForEach(x =>
{
var uri = x.Split("/");
set.Add($"res/{uri[1]}");
});
foreach (var item in set)
{
var urlsSize = await ContainerClient.GetBlobsSize(item);
var url = item;
if (!item.EndsWith(".hte", StringComparison.OrdinalIgnoreCase) && !item.EndsWith(".HTEX", StringComparison.OrdinalIgnoreCase))
{
url += ".HTEX";
}
Bloblog bloblog = new Bloblog { id = Guid.NewGuid().ToString(), code = $"Bloblog-{name}", pk = "Bloblog", time = now, url = url, size = urlsSize != null && urlsSize.HasValue ? urlsSize.Value : 0, type = prefix };
await client.GetContainer("TEAMModelOS", tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code));
}
}
}
else
{
List items = await ContainerClient.List(prefix);
if (items.IsNotEmpty())
{
foreach (var item in items)
{
var urlsSize = await ContainerClient.GetBlobsSize(item);
Bloblog bloblog = new Bloblog { id = Guid.NewGuid().ToString(), code = $"Bloblog-{name}", pk = "Bloblog", time = now, url = item, size = urlsSize != null && urlsSize.HasValue ? urlsSize.Value : 0, type = prefix };
await client.GetContainer("TEAMModelOS", tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code));
}
}
}
}
}
}
}