JointlySingleQuery.cs 3.4 KB

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