using Microsoft.Azure.Cosmos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
namespace TEAMModelOS.SDK.Models.Service.BI
{
public class LessonStatisWay
{
///
/// 依据学校Id、教师Id统计课例总数
///
///
///
///
///
public async static Task GetAll(CosmosClient cosmosClient, List scIds = null, List tecIds = null)
{
int totals = 0;
string sqlTxt = "select count(c.id) as totals from c";
if (scIds.Count > 0)
{
foreach (string sc in scIds)
{
await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{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)
{
string sqlTecTxt = $"select count(c.id) from c where c.id='{sc}'";
await foreach (var itemTeac in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord") }))
{
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();
}
}
}
}
}
return totals;
}
///
/// 依据学校Id查询学校和学校里面教师的课例统计
///
///
///
///
public async static Task GetSchoolIdLessonCount(CosmosClient cosmosClient, string schoolId)
{
int totals = 0;
string LessonSqlTxt = "select value(count(c.id)) from c";
await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql(queryText: LessonSqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{schoolId}") }))
{
totals += item;
}
List tecIdS = new();
string managerSql = $"SELECT value(c.id) FROM c WHERE ARRAY_CONTAINS(c.roles, 'teacher', true) AND c.pk = 'Teacher' AND c.status = 'join'";
await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql(queryText: managerSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{schoolId}") }))
{
tecIdS.Add(item);
}
foreach (var itemId in tecIdS)
{
string tecLessSQL = $"select value(count(c.id)) from c where c.tmdid='{itemId}'";
await foreach (var itemCount in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIteratorSql(queryText: tecLessSQL, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("LessonRecord") }))
{
totals += itemCount;
}
}
return totals;
}
///
/// 学校按年级统计课例
///
///
///
public static List<(string name, string periodId, int count)> GetGradeCount(List records)
{
List<(string gradeIn, string periodId)> grades = new();
List<(string name, string periodId, int count)> gCount = new();
foreach (var record in records)
{
foreach (string gId in record.grade)
{
var temp = grades.Find(x => x.gradeIn.Equals(gId) && x.periodId.Equals(record.periodId));
if (temp.gradeIn == null && temp.periodId == null)
{
grades.Add((gId, record.periodId));
}
}
}
foreach (var gId in grades)
{
var c = records.Where(r => r.grade.Contains(gId.gradeIn) && r.periodId.Equals(gId.periodId)).Count();
gCount.Add((gId.gradeIn, gId.periodId, c));
}
return gCount;
}
///
/// 学校按年级统计课例
///
///
///
public static List<(string name, string periodId, int count)> GetGradeCount(List records)
{
List<(string gradeIn, string periodId)> grades = new();
List<(string name, string periodId, int count)> gCount = new();
foreach (var record in records)
{
foreach (string gId in record.grade)
{
var temp = grades.Find(x => x.gradeIn.Equals(gId) && x.periodId.Equals(record.periodId));
if (temp.gradeIn == null && temp.periodId == null)
{
grades.Add((gId, record.periodId));
}
}
}
foreach (var gId in grades)
{
var c = records.Where(r => r.grade.Contains(gId.gradeIn) && r.periodId.Equals(gId.periodId)).Count();
gCount.Add((gId.gradeIn, gId.periodId, c));
}
return gCount;
}
}
}