AzureCosmosExtensions.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Microsoft.Azure.Cosmos.Table;
  2. using Microsoft.Azure.Cosmos.Table.Queryable;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Linq;
  10. using Azure;
  11. using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
  12. using System.IO;
  13. using TEAMModelOS.SDK.DI;
  14. using System.Diagnostics;
  15. using Azure.Cosmos;
  16. using System.Text.Json;
  17. using System.Net;
  18. using System.Linq.Expressions;
  19. using TEAMModelOS.SDK;
  20. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  21. using TEAMModelOS.SDK.Extension;
  22. using TEAMModelOS.SDK.Models;
  23. namespace TEAMModelOS.SDK.DI
  24. {
  25. public static class AzureCosmosExtensions
  26. {
  27. public static double RU(this Response response)
  28. {
  29. try
  30. {
  31. response.Headers.TryGetValue("x-ms-request-charge", out var value);
  32. var ru = Convert.ToDouble(value);
  33. return ru;
  34. }
  35. catch
  36. {
  37. return 0;
  38. }
  39. }
  40. public static string GetContinuationToken(this Response response)
  41. {
  42. try
  43. {
  44. response.Headers.TryGetValue("x-ms-continuation", out var value);
  45. return value;
  46. }
  47. catch
  48. {
  49. return null;
  50. }
  51. }
  52. /// <summary>
  53. /// 取得当前容器指定分区键的Count数,支持SQL Where条件,不支持排序
  54. /// </summary>
  55. /// <param name="container"></param>
  56. /// <param name="partitionkey"></param>
  57. /// <param name="queryWhere"></param>
  58. /// <returns></returns>
  59. public static async Task<int> GetCount(this CosmosContainer container, string partitionkey, string queryWhere = "WHERE 1=1")
  60. {
  61. int totalCount = 0;
  62. await foreach (var item in container.GetItemQueryStreamIterator(
  63. queryText: $"SELECT VALUE COUNT(1) From c {queryWhere}",
  64. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(partitionkey), MaxItemCount = -1 }))
  65. {
  66. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  67. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  68. {
  69. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  70. {
  71. totalCount = obj.GetInt32();
  72. }
  73. }
  74. }
  75. return totalCount;
  76. }
  77. public static async Task<List<Response>> DeleteItemsStreamAsync(this CosmosContainer container, List<string> ids, string partitionkey )
  78. {
  79. List<Response> responses = new List<Response>();
  80. foreach (var id in ids)
  81. {
  82. try
  83. {
  84. responses.Add(await container.DeleteItemStreamAsync(id, new PartitionKey(partitionkey)));
  85. }
  86. catch
  87. {
  88. continue;
  89. }
  90. }
  91. return responses;
  92. }
  93. public static async Task<List<ItemResponse<T>>> DeleteItemsAsync<T>(this CosmosContainer container, List<string> ids, string partitionkey)
  94. {
  95. List<ItemResponse<T>> responses = new List<ItemResponse<T>>();
  96. foreach (var id in ids)
  97. {
  98. try
  99. {
  100. responses.Add(await container.DeleteItemAsync<T>(id, new PartitionKey(partitionkey)));
  101. }
  102. catch
  103. {
  104. continue;
  105. }
  106. }
  107. return responses;
  108. }
  109. }
  110. }