FixDataService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using Azure;
  2. using Azure.Cosmos;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  13. namespace TEAMModelOS.SDK.Models.Service
  14. {
  15. public static class FixDataService
  16. {
  17. /// <summary>
  18. /// 修复学生数据
  19. /// </summary>
  20. /// <param name="client"></param>
  21. /// <param name="_dingDing"></param>
  22. /// <param name="_azureStorage"></param>
  23. /// <param name="data"></param>
  24. /// <returns></returns>
  25. public static async Task FixStudentInfo(CosmosClient client, DingDing _dingDing, AzureStorageFactory _azureStorage, JsonElement data) {
  26. var code = data.GetProperty("code").GetString();
  27. var ids = data.GetProperty("ids").ToObject<List<string>>();
  28. var dict = data.GetProperty("dict").ToObject<Dictionary<string, object>>();
  29. string queryText = $"SELECT VALUE c FROM c WHERE c.id IN ({string.Join(",", ids.Select(o => $"'{o}'"))})";
  30. List<Student> students = new List<Student>();
  31. await foreach (var item in client.GetContainer("TEAMModelOS", "Student")
  32. .GetItemQueryIterator<Student>(
  33. queryText: queryText,
  34. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{code}") })) {
  35. foreach (var key in dict.Keys) {
  36. switch (key) {
  37. case "classId":
  38. item.classId =$"{ dict[key]}";
  39. break;
  40. case "periodId":
  41. item.periodId = $"{ dict[key]}";
  42. break;
  43. case "schoolId":
  44. item.schoolId = $"{ dict[key]}";
  45. break;
  46. case "year":
  47. int year = DateTime.Now.Year;
  48. int.TryParse($"dict[key]", out year);
  49. item.year = year;
  50. break;
  51. default:
  52. break;
  53. }
  54. students.Add(item);
  55. }
  56. }
  57. List<Task<ItemResponse<Student>>> studen = new List<Task<ItemResponse<Student>>>();
  58. students.ForEach(x => {
  59. studen.Add(client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync<Student>(x,x.id,new PartitionKey(x.code)));
  60. });
  61. await Task.WhenAll(studen);
  62. }
  63. /// <summary>
  64. /// 修复内容模块数据
  65. /// </summary>
  66. /// <param name="client"></param>
  67. /// <param name="_dingDing"></param>
  68. /// <param name="_azureStorage"></param>
  69. /// <param name="data"></param>
  70. /// <returns></returns>
  71. public static async Task FixBlobContent(CosmosClient client, DingDing _dingDing, AzureStorageFactory _azureStorage, JsonElement data)
  72. {
  73. if (data.TryGetProperty("name", out JsonElement _name))
  74. {
  75. List<string> names = _name.ToObject<List<string>>();
  76. foreach (string name in names)
  77. {
  78. List<string> prefixs = new List<string>() { "audio", "doc", "image", "other", "res", "video", "thum" };
  79. var ContainerClient = _azureStorage.GetBlobContainerClient($"{name}");
  80. string scope = "private";
  81. if (data.TryGetProperty("scope", out JsonElement _scope))
  82. {
  83. scope = $"{_scope}";
  84. }
  85. var tb = "Teacher";
  86. if (scope != "private")
  87. {
  88. tb = "School";
  89. }
  90. List<string> ids = new List<string>();
  91. await foreach (var item in client.GetContainer("TEAMModelOS", tb).GetItemQueryIterator<Bloblog>(queryDefinition: new QueryDefinition("select c.id from c "), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  92. {
  93. ids.Add(item.id);
  94. }
  95. await client.GetContainer("TEAMModelOS", tb).DeleteItemsStreamAsync(ids, $"Bloblog-{name}");
  96. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  97. foreach (var prefix in prefixs)
  98. {
  99. if (prefix.Equals("res"))
  100. {
  101. List<string> itemres = await ContainerClient.List(prefix);
  102. if (itemres.IsNotEmpty()) {
  103. HashSet<string> set = new HashSet<string>();
  104. itemres.ForEach(x =>
  105. {
  106. var uri = x.Split("/");
  107. set.Add($"res/{uri[1]}");
  108. });
  109. foreach (var item in set)
  110. {
  111. var urlsSize = await ContainerClient.GetBlobsSize(item);
  112. var url = item;
  113. if (!item.EndsWith(".hte", StringComparison.OrdinalIgnoreCase) && !item.EndsWith(".HTEX", StringComparison.OrdinalIgnoreCase))
  114. {
  115. url += ".HTEX";
  116. }
  117. 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 };
  118. await client.GetContainer("TEAMModelOS", tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code));
  119. }
  120. }
  121. }
  122. else {
  123. List<string> items = await ContainerClient.List(prefix);
  124. if (items.IsNotEmpty()) {
  125. foreach (var item in items)
  126. {
  127. var urlsSize = await ContainerClient.GetBlobsSize(item);
  128. 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 };
  129. await client.GetContainer("TEAMModelOS", tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code));
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }