FixDataService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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("name", out JsonElement _name))
  71. {
  72. List<string> names = _name.ToObject<List<string>>();
  73. foreach (string name in names)
  74. {
  75. List<string> prefixs = new List<string>() { "audio", "doc", "image", "other", "res", "video", "thum" };
  76. var ContainerClient = _azureStorage.GetBlobContainerClient($"{name}");
  77. string scope = "private";
  78. if (data.TryGetProperty("scope", out JsonElement _scope))
  79. {
  80. scope = $"{_scope}";
  81. }
  82. var tb = "Teacher";
  83. if (scope != "private")
  84. {
  85. tb = "School";
  86. }
  87. List<string> ids = new List<string>();
  88. 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}") }))
  89. {
  90. ids.Add(item.id);
  91. }
  92. await client.GetContainer("TEAMModelOS", tb).DeleteItemsStreamAsync(ids, $"Bloblog-{name}");
  93. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  94. foreach (var prefix in prefixs)
  95. {
  96. if (prefix.Equals("res"))
  97. {
  98. List<string> itemres = await ContainerClient.List(prefix);
  99. if (itemres.IsNotEmpty()) {
  100. HashSet<string> set = new HashSet<string>();
  101. itemres.ForEach(x =>
  102. {
  103. var uri = x.Split("/");
  104. set.Add($"res/{uri[1]}");
  105. });
  106. foreach (var item in set)
  107. {
  108. var urlsSize = await ContainerClient.GetBlobsSize(item);
  109. var url = item;
  110. if (!item.EndsWith(".hte", StringComparison.OrdinalIgnoreCase) && !item.EndsWith(".HTEX", StringComparison.OrdinalIgnoreCase))
  111. {
  112. url += ".HTEX";
  113. }
  114. 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 };
  115. await client.GetContainer("TEAMModelOS", tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code));
  116. }
  117. }
  118. }
  119. else {
  120. List<string> items = await ContainerClient.List(prefix);
  121. if (items.IsNotEmpty()) {
  122. foreach (var item in items)
  123. {
  124. var urlsSize = await ContainerClient.GetBlobsSize(item);
  125. 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 };
  126. await client.GetContainer("TEAMModelOS", tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code));
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }