FixDataService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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;
  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<List<Student>> 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. await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync<Student>(item, item.id, new PartitionKey(item.code));
  55. students.Add(item);
  56. }
  57. }
  58. return students;
  59. }
  60. /// <summary>
  61. /// 修复内容模块数据
  62. /// </summary>
  63. /// <param name="client"></param>
  64. /// <param name="_dingDing"></param>
  65. /// <param name="_azureStorage"></param>
  66. /// <param name="data"></param>
  67. /// <returns></returns>
  68. public static async Task FixBlobContent(CosmosClient client, DingDing _dingDing, AzureStorageFactory _azureStorage, JsonElement data)
  69. {
  70. if (data.TryGetProperty("doPrivate", out JsonElement _doPrivate)&& $"{_doPrivate}".Equals("yes",StringComparison.OrdinalIgnoreCase)) {
  71. foreach (var cnt in _azureStorage.GetBlobServiceClient().GetBlobContainers())
  72. {
  73. if (cnt.Name.Length == 10 && int.TryParse(cnt.Name, out _))
  74. {
  75. await doFixBlob(client, _azureStorage, cnt.Name, "private");
  76. }
  77. }
  78. }
  79. if (data.TryGetProperty("name", out JsonElement _name))
  80. {
  81. List<string> names = _name.ToObject<List<string>>();
  82. foreach (string name in names)
  83. {
  84. await doFixBlob(client, _azureStorage, name, "school");
  85. }
  86. }
  87. }
  88. private static async Task doFixBlob(CosmosClient client, AzureStorageFactory _azureStorage,string name,string scope) {
  89. List<string> prefixs = new List<string>() { "audio", "doc", "image", "other", "res", "video", "thum" };
  90. var ContainerClient = _azureStorage.GetBlobContainerClient($"{name}");
  91. var tb = "Teacher";
  92. if (scope != "private")
  93. {
  94. tb = "School";
  95. }
  96. List<string> ids = new List<string>();
  97. 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}") }))
  98. {
  99. ids.Add(item.id);
  100. }
  101. await client.GetContainer("TEAMModelOS", tb).DeleteItemsStreamAsync(ids, $"Bloblog-{name}");
  102. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  103. foreach (var prefix in prefixs)
  104. {
  105. if (prefix.Equals("res"))
  106. {
  107. List<string> itemres = await ContainerClient.List(prefix);
  108. if (itemres.IsNotEmpty())
  109. {
  110. HashSet<string> set = new HashSet<string>();
  111. itemres.ForEach(x =>
  112. {
  113. var uri = x.Split("/");
  114. set.Add($"res/{uri[1]}");
  115. });
  116. foreach (var item in set)
  117. {
  118. var urlsSize = await ContainerClient.GetBlobsSize(item);
  119. var url = item;
  120. if (!item.EndsWith(".hte", StringComparison.OrdinalIgnoreCase) && !item.EndsWith(".HTEX", StringComparison.OrdinalIgnoreCase))
  121. {
  122. url += ".HTEX";
  123. }
  124. 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 };
  125. await client.GetContainer("TEAMModelOS", tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code));
  126. }
  127. }
  128. }
  129. else
  130. {
  131. List<string> items = await ContainerClient.List(prefix);
  132. if (items.IsNotEmpty())
  133. {
  134. foreach (var item in items)
  135. {
  136. var urlsSize = await ContainerClient.GetBlobsSize(item);
  137. 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 };
  138. await client.GetContainer("TEAMModelOS", tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code));
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }