123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- using Microsoft.Azure.Cosmos;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- namespace TEAMModelOS.SDK.Models.Service.BI
- {
- public class LessonStatisWay
- {
- /// <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;
- 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;
- }
- /// <summary>
- /// 依据学校Id查询学校和学校里面教师的课例统计
- /// </summary>
- /// <param name="cosmosClient"></param>
- /// <param name="schoolId"></param>
- /// <returns></returns>
- public async static Task<int> 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<int>(queryText: LessonSqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{schoolId}") }))
- {
- totals += item;
- }
- List<string> 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<string>(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<int>(queryText: tecLessSQL, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("LessonRecord") }))
- {
- totals += itemCount;
- }
- }
- return totals;
- }
- /// <summary>
- /// 学校按年级统计课例
- /// </summary>
- /// <param name="records"></param>
- /// <returns></returns>
- public static List<(string name, string periodId, int count)> GetGradeCount(List<LessAnalyse> 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;
- }
- /// <summary>
- /// 学校按年级统计课例
- /// </summary>
- /// <param name="records"></param>
- /// <returns></returns>
- public static List<(string name, string periodId, int count)> GetGradeCount(List<LessonRecord> 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;
- }
- }
- }
|