ActivityWay.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Microsoft.Azure.Cosmos;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using System.Threading.Tasks;
  5. using TEAMModelOS.SDK.DI;
  6. namespace TEAMModelBI.Tool.CosmosBank
  7. {
  8. public class ActivityWay
  9. {
  10. public static List<string> types = new() { "Exam", "Survey", "Vote", "Homework" };
  11. /// <summary>
  12. /// 依据学校Id、教师Id统计活动总数
  13. /// </summary>
  14. /// <param name="cosmosClient"></param>
  15. /// <param name="scIds"></param>
  16. /// <param name="tecIds"></param>
  17. /// <returns></returns>
  18. public async static Task<int> GetAll(CosmosClient cosmosClient, List<string> scIds = null, List<string> tecIds = null)
  19. {
  20. int totals = 0;
  21. try
  22. {
  23. string sqlTxt = "select count(c.id) as totals from c";
  24. foreach (string type in types)
  25. {
  26. if (scIds.Count > 0)
  27. {
  28. foreach (string sc in scIds)
  29. {
  30. await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{sc}") }))
  31. {
  32. using var json = await JsonDocument.ParseAsync(itemTeac.Content);
  33. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
  34. {
  35. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  36. {
  37. totals += obj.GetProperty("totals").GetInt32();
  38. }
  39. }
  40. }
  41. }
  42. }
  43. if (tecIds.Count > 0)
  44. {
  45. foreach (string sc in scIds)
  46. {
  47. await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{sc}") }))
  48. {
  49. using var json = await JsonDocument.ParseAsync(itemTeac.Content);
  50. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
  51. {
  52. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  53. {
  54. totals += obj.GetProperty("totals").GetInt32();
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. catch { }
  63. return totals;
  64. }
  65. /// <summary>
  66. /// 统计活动
  67. /// </summary>
  68. /// <param name="cosmosClient">连接字符</param>
  69. /// <param name="container">容器名称:默认Common</param>
  70. /// <param name="condSql">条件语句</param>
  71. /// <param name="code"></param>
  72. /// <returns></returns>
  73. public static async Task<int> GetCnt(CosmosClient cosmosClient, string container = "Common", string condSql = null, string code = null)
  74. {
  75. int total = 0;
  76. try
  77. {
  78. foreach (string type in types)
  79. {
  80. string sqlTxt = $"select value(count(c.id)) from c where c.pk='{type}' {condSql}";
  81. await foreach (var cnt in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql<int>(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  82. {
  83. total += cnt;
  84. }
  85. }
  86. }
  87. catch { }
  88. return total;
  89. }
  90. /// <summary>
  91. /// 统计活动多个容器获取
  92. /// </summary>
  93. /// <param name="cosmosClient"></param>
  94. /// <param name="containers"></param>
  95. /// <param name="condSql"></param>
  96. /// <param name="code"></param>
  97. /// <returns></returns>
  98. public static async Task<int> GetCnt(CosmosClient cosmosClient, List<string> containers, string condSql = null, string code = null)
  99. {
  100. int total = 0;
  101. try
  102. {
  103. foreach (var container in containers)
  104. {
  105. foreach (string type in types)
  106. {
  107. string sqlTxt = $"select value(count(c.id)) from c where c.pk='{type}' {condSql}";
  108. await foreach (var cnt in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql<int>(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  109. {
  110. total += cnt;
  111. }
  112. }
  113. }
  114. }
  115. catch { }
  116. return total;
  117. }
  118. }
  119. }