JointlySingleQuery.cs 3.4 KB

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