JointlySingleQuery.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. namespace TEAMModelOS.SDK.Models.Service.BI
  6. {
  7. public class JointlySingleQuery
  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 code, string sqlTxt = null)
  18. {
  19. int totals = 0;
  20. try
  21. {
  22. string sql = $"select value(count(c.id)) from c";
  23. if (!string.IsNullOrEmpty(sqlTxt))
  24. {
  25. sql = sqlTxt;
  26. }
  27. foreach (string conId in containerId)
  28. {
  29. await foreach (var itemInt in cosmosClient.GetContainer("TEAMModelOS", conId).GetItemQueryIterator<int>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  30. {
  31. totals += itemInt;
  32. }
  33. }
  34. return totals;
  35. }
  36. catch (Exception)
  37. {
  38. return totals;
  39. }
  40. }
  41. /// <summary>
  42. /// 单个容器返回整数 统计使用
  43. /// </summary>
  44. /// <param name="cosmosClient"></param>
  45. /// <param name="container"></param>
  46. /// <param name="sqlTxt"></param>
  47. /// <param name="code"></param>
  48. /// <returns></returns>
  49. public static async Task<int> GetValueInt(CosmosClient cosmosClient, string container, string code, string sqlTxt = null)
  50. {
  51. int totals = 0;
  52. try
  53. {
  54. string sql = $"select value(count(c.id)) from c";
  55. if (!string.IsNullOrEmpty(sqlTxt))
  56. {
  57. sql = sqlTxt;
  58. }
  59. await foreach (var itemInt in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIterator<int>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  60. {
  61. totals += itemInt;
  62. }
  63. return totals;
  64. }
  65. catch (Exception)
  66. {
  67. return totals;
  68. }
  69. }
  70. /// <summary>
  71. /// 返回字符集合
  72. /// </summary>
  73. /// <param name="cosmosClient"></param>
  74. /// <param name="containerId"></param>
  75. /// <param name="sqlTxt"></param>
  76. /// <param name="code"></param>
  77. /// <returns></returns>
  78. public static async Task<List<string>> GetListString(CosmosClient cosmosClient, string containerId, string sqlTxt, string code)
  79. {
  80. List<string> strList = new();
  81. await foreach (var itemInt in cosmosClient.GetContainer("TEAMModelOS", containerId).GetItemQueryIterator<string>(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  82. {
  83. strList.Add(itemInt);
  84. }
  85. return strList;
  86. }
  87. }
  88. }