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 types = new() { "Exam", "Survey", "Vote", "Homework" }; /// /// 依据学校Id、教师Id统计活动总数 /// /// /// /// /// public async static Task GetAll(CosmosClient cosmosClient, List scIds = null, List 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; } /// /// 统计活动 /// /// 连接字符 /// 容器名称:默认Common /// 条件语句 /// /// public static async Task 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(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") })) { total += cnt; } } } catch { } return total; } /// /// 统计活动多个容器获取 /// /// /// /// /// /// public static async Task GetCnt(CosmosClient cosmosClient, List 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(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") })) { total += cnt; } } } } catch { } return total; } } }