JointlySingleQuery1.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Microsoft.Azure.Cosmos;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using TEAMModelOS.SDK.DI;
  5. namespace TEAMModelBI.Tool.CosmosBank
  6. {
  7. public class JointlySingleQuery1
  8. {
  9. /// <summary>
  10. /// 多个容器返回整数 使用
  11. /// </summary>
  12. /// <param name="cosmosClient"></param>
  13. /// <param name="containerId"></param>
  14. /// <param name="sqlTxt"></param>
  15. /// <param name="code"></param>
  16. /// <returns></returns>
  17. public static async Task<int> GetValueInt(CosmosClient cosmosClient, List<string> containerId, string sqlTxt = null, string code = null)
  18. {
  19. int totals = 0;
  20. string sql = $"select value(count(c.id)) from c";
  21. if (!string.IsNullOrEmpty(sqlTxt))
  22. {
  23. sql = sqlTxt;
  24. }
  25. foreach (string conId in containerId)
  26. {
  27. await foreach (var itemInt in cosmosClient.GetContainer("TEAMModelOS", conId).GetItemQueryIteratorSql<int>(queryText: sql, requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  28. {
  29. totals += itemInt;
  30. }
  31. }
  32. return totals;
  33. }
  34. /// <summary>
  35. /// 单个容器返回整数 统计使用
  36. /// </summary>
  37. /// <param name="cosmosClient"></param>
  38. /// <param name="container"></param>
  39. /// <param name="sqlTxt"></param>
  40. /// <param name="code"></param>
  41. /// <returns></returns>
  42. public static async Task<int> GetValueInt(CosmosClient cosmosClient, string container, string sqlTxt = null, string code = null)
  43. {
  44. int totals = 0;
  45. string sql = $"select value(count(c.id)) from c";
  46. if (!string.IsNullOrEmpty(sqlTxt))
  47. {
  48. sql = sqlTxt;
  49. }
  50. await foreach (var itemInt in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql<int>(queryText: sql, requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  51. {
  52. totals += itemInt;
  53. }
  54. return totals;
  55. }
  56. /// <summary>
  57. /// 返回字符集合
  58. /// </summary>
  59. /// <param name="cosmosClient"></param>
  60. /// <param name="containerId"></param>
  61. /// <param name="sqlTxt"></param>
  62. /// <param name="code"></param>
  63. /// <returns></returns>
  64. public static async Task<List<string>> GetListString(CosmosClient cosmosClient, string containerId, string sqlTxt, string code = null)
  65. {
  66. List<string> strList = new();
  67. await foreach (var itemInt in cosmosClient.GetContainer("TEAMModelOS", containerId).GetItemQueryIteratorSql<string>(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  68. {
  69. strList.Add(itemInt);
  70. }
  71. return strList;
  72. }
  73. }
  74. }