using Microsoft.Azure.Cosmos; using System; using System.Collections.Generic; using System.Text; using System.Text.Json; using System.Threading.Tasks; using TEAMModelBI.Models; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Models; namespace TEAMModelBI.Tool { public class CommonFind { /// /// 查询学校教师角色列表 /// /// /// 学校Id /// 查询的角色 /// 返回学校角色列表 public static async Task> FindSchoolRoles(CosmosClient cosmosClient, string schoolId, string roles) { List strs = new(); try { string managerSql = $"SELECT DISTINCT REPLACE(c.code, 'Teacher-', '') AS schoolId, c.id, c.name FROM c WHERE ARRAY_CONTAINS(c.roles, '{roles}', true) AND c.pk = 'Teacher' AND c.status = 'join' AND c.code = 'Teacher-{schoolId}'"; await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIteratorSql(queryText: managerSql, requestOptions: new QueryRequestOptions() { })) { using var json = await JsonDocument.ParseAsync(item.Content); foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray()) { SchoolTeacherRoles str = new() { tmdId = obj.GetProperty("id").GetString(), tmdName = obj.GetProperty("name").GetString() }; strs.Add(str); } } return strs; } catch { return strs; } } /// /// 通过醍摩豆账户查询关联学校ID /// /// cosmosDB连接 /// 醍摩豆账户 /// 返回顾问相关的学校ID集合 public static async Task> FindSchoolIds(CosmosClient cosmosClient, string tmdId,string roles = "assist", bool isMany = false) { List schoolIds = new(); //string schoolSql = $"SELECT DISTINCT REPLACE(c.code,'Teacher-','') AS schoolId,c.code,c.roles,c.id,c.name From c where ARRAY_CONTAINS(c.roles,'assist',true) AND c.status = 'join' AND c.id='{tmdId}'"; //await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIteratorSql(queryText: schoolSql, requestOptions: new QueryRequestOptions() { })) //{ // using var json = await JsonDocument.ParseAsync(item.Content); // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray()) // { // schoolIds.Add(obj.GetProperty("schoolId").GetString()); // } //} StringBuilder schoolSql = new($"SELECT value(REPLACE(c.code, 'Teacher-', '')) FROM c where c.pk='Teacher' and c.id='{tmdId}'"); if (isMany == true) schoolSql.Append($" and (array_contains(c.roles,'assist',true) or array_contains(c.roles,'sales',true))"); else schoolSql.Append($" and array_contains(c.roles,'{roles}',true)"); await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql(queryText: schoolSql.ToString(), requestOptions: new QueryRequestOptions() { })) { schoolIds.Add(item); } return schoolIds; } /// /// 通过sql语句查询单列集合 /// /// 连接字符 /// 容器名称 /// sql语句 带value /// /// public static async Task> GetValueSingle(CosmosClient cosmosClient, string container, string sqlTxt, string code = null) { List ids = new(); await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql(queryText: sqlTxt, requestOptions: !string.IsNullOrEmpty(code) ? new QueryRequestOptions() { PartitionKey = new PartitionKey(code) } : new QueryRequestOptions() { })) { ids.Add(item); } return ids; } /// /// 通过sql语句查询单列集合 /// /// /// /// /// /// /// public async Task> GetStreamSingle(CosmosClient cosmosClient, string container, string sqlTxt, string single = "id", string code = null) { List ids = new(); await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: !string.IsNullOrEmpty(code) ? new QueryRequestOptions() { PartitionKey = new PartitionKey(code) } : new QueryRequestOptions() { })) { using var json = await JsonDocument.ParseAsync(item.Content); foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray()) { ids.Add(obj.GetProperty(single).GetString()); } } return ids; } /// /// 通过语句查询学校ID /// /// cosmosDB连接 /// sql语句 /// 数据分区键 /// 返回学校ID的集合 public static async Task> FindScIds(CosmosClient cosmosClient, string sqlTxt, string code) { List schoolIds = new(); await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) })) { schoolIds.Add(item); } //await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) })) //{ // using var json = await JsonDocument.ParseAsync(item.Content); // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray()) // { // schoolIds.Add(obj.GetProperty("id").GetString()); // } //} return schoolIds; } /// /// 依据学校查询教师列表 /// /// /// 学校列表 /// 不传默认教师角色 /// public static async Task> FindRolesId(CosmosClient cosmosClient, List schools, string roles = null) { string rolesName = "teacher"; if (roles != null) { rolesName = roles; } List teachers = new(); foreach (var school in schools) { string sqlTxt = $"select value(c.id) from c where ARRAY_CONTAINS(c.roles,'{rolesName}',true) and c.status = 'join'"; await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") })) { teachers.Add(itemTeac); } //string sqlTxt = $"select c.id from c where ARRAY_CONTAINS(c.roles,'{rolesName}',true) and c.status = 'join'"; //await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() {PartitionKey =new PartitionKey($"Teacher-{school}") })) //{ // using var json = await JsonDocument.ParseAsync(itemTeac.Content); // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray()) // { // teachers.Add(obj.GetProperty("id").GetString()); // } //} } return teachers; } /// /// 单个容器数据统计 /// /// /// /// /// /// public static async Task GetSqlValueCount(CosmosClient cosmosClient, string container, string SqlTxt,string code = null) { int totals = 0; try { await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql(queryText: SqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") })) { totals = item; } } catch{ } return totals; } /// /// 多个容器数据统计 /// /// /// /// /// /// public static async Task GetSqlValueCount(CosmosClient cosmosClient, List containers, string SqlTxt, string code = null) { int totals = 0; foreach (var container in containers) { await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql(queryText: SqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) })) { totals += item; } } return totals; } /// /// 单个容器数据统计 double /// /// /// /// /// /// public static async Task GetSqlValueDoubleCounnt(CosmosClient cosmosClient, string container, string SqlTxt, string code = null) { double totals = 0; await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql(queryText: SqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) })) { totals = item; } return totals; } /// /// 多个容器数据统计 double /// /// /// /// /// /// public static async Task GetSqlValueDoubleCounnt(CosmosClient cosmosClient, List containers, string SqlTxt, string code = null) { double totals = 0; foreach (var container in containers) { await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql(queryText: SqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) })) { totals += item; } } return totals; } /// /// 通过SQL 语句返回实体信息 /// /// /// /// /// /// /// public static async Task> GetObject(CosmosClient cosmosClient, List containers, string sqlTxt, string code = null) { List temps = new(); foreach (var container in containers) { await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", container).GetItemQueryIteratorSql(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) })) { temps.Add(items); } } return temps; } /// /// 通过SQL 语句返回实体信息 /// /// /// /// /// /// /// public static async Task> GetObject(CosmosClient cosmosClient, string containers, string sqlTxt, string code = null) { List temps = new(); await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", containers).GetItemQueryIteratorSql(queryText: sqlTxt, requestOptions: string.IsNullOrEmpty(code) ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey(code) })) { temps.Add(items); } return temps; } } }