123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using Microsoft.Azure.Cosmos;
- using System.Collections.Generic;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.DI;
- namespace TEAMModelBI.Tool.CosmosBank
- {
- public class ActivityWay
- {
- public static List<string> types = new() { "Exam", "Survey", "Vote", "Homework" };
- /// <summary>
- /// 依据学校Id、教师Id统计活动总数
- /// </summary>
- /// <param name="cosmosClient"></param>
- /// <param name="scIds"></param>
- /// <param name="tecIds"></param>
- /// <returns></returns>
- public async static Task<int> GetAll(CosmosClient cosmosClient, List<string> scIds = null, List<string> tecIds = null)
- {
- int totals = 0;
- try
- {
- string sqlTxt = "select count(c.id) as totals from c";
- foreach (string type in types)
- {
- if (scIds.Count > 0)
- {
- foreach (string sc in scIds)
- {
- await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{sc}") }))
- {
- using var json = await JsonDocument.ParseAsync(itemTeac.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- totals += obj.GetProperty("totals").GetInt32();
- }
- }
- }
- }
- }
- if (tecIds.Count > 0)
- {
- foreach (string sc in scIds)
- {
- await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{sc}") }))
- {
- using var json = await JsonDocument.ParseAsync(itemTeac.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- totals += obj.GetProperty("totals").GetInt32();
- }
- }
- }
- }
- }
- }
- }
- catch { }
- return totals;
- }
- /// <summary>
- /// 统计活动
- /// </summary>
- /// <param name="cosmosClient">连接字符</param>
- /// <param name="container">容器名称:默认Common</param>
- /// <param name="condSql">条件语句</param>
- /// <param name="code"></param>
- /// <returns></returns>
- public static async Task<int> GetCnt(CosmosClient cosmosClient, string container = "Common", string condSql = null, string code = null)
- {
- int total = 0;
- try
- {
- foreach (string type in types)
- {
- string sqlTxt = $"select value(count(c.id)) from c where c.pk='{type}' {condSql}";
- 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}") }))
- {
- total += cnt;
- }
- }
- }
- catch { }
- return total;
- }
- /// <summary>
- /// 统计活动多个容器获取
- /// </summary>
- /// <param name="cosmosClient"></param>
- /// <param name="containers"></param>
- /// <param name="condSql"></param>
- /// <param name="code"></param>
- /// <returns></returns>
- public static async Task<int> GetCnt(CosmosClient cosmosClient, List<string> containers, string condSql = null, string code = null)
- {
- int total = 0;
- try
- {
- foreach (var container in containers)
- {
- foreach (string type in types)
- {
- string sqlTxt = $"select value(count(c.id)) from c where c.pk='{type}' {condSql}";
- 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}") }))
- {
- total += cnt;
- }
- }
- }
- }
- catch { }
- return total;
- }
- }
- }
|