123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Text.Json;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.Models;
- using Azure.Cosmos;
- using Microsoft.Extensions.Options;
- using TEAMModelOS.SDK.Models;
- namespace TEAMModeBI.Controllers.BIHome
- {
- [Route("homestatis")]
- [ApiController]
- public class HomeStatisController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- public HomeStatisController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option)
- {
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _option = option?.Value;
- }
- /// <summary>
- /// 获取人数
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-numberpeople")]
- public async Task<IActionResult> GetNumberPeople(JsonElement jsonElement)
- {
- try
- {
- jsonElement.TryGetProperty("schooolId", out JsonElement schoolId);
- var client = _azureCosmos.GetCosmosClient();
- //依据学校查询教师人数
- List<string> teacherCount_list = new();
- //依据学校查询学生信息
- List<string> studentCount_List = new();
- //学校人数
- List<string> schoolCount_List = new();
- //学校空间大小
- int schoolsize = 0;
- if (!string.IsNullOrEmpty($"{schoolId}"))
- {
- //查询学校教师人数
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select c.id from c join S1 in c.schools where S1.schoolId='{schoolId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- teacherCount_list.Add(account.GetProperty("id").GetString());
- }
- }
- }
- //查询学校学生人数
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{schoolId}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- studentCount_List.Add(account.GetProperty("id").GetString());
- }
- }
- }
- return Ok(new { TeacherCount = teacherCount_list.Count, StudentCount = studentCount_List.Count });
- }
- else
- {
- //查询全部教师人数
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select * from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- teacherCount_list.Add(account.GetProperty("id").GetString());
- }
- }
- }
- //查询全部学生人数
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.pk='Base'"))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- studentCount_List.Add(account.GetProperty("id").GetString());
- }
- }
- }
- //查询已创建多少学校
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id,c.size from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- schoolCount_List.Add(account.GetProperty("id").GetString());
- schoolsize += int.Parse(account.GetProperty("size").ToString());
- }
- }
- }
- return Ok(new { TeacherCount = teacherCount_list.Count, StudentCount = studentCount_List.Count, schoolCount = schoolCount_List.Count, schoolsize = schoolsize });
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-numberpeople \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 其它类型的统计
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-othertypes")]
- public async Task<IActionResult> GetOtherTypes(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
- int surveyJoinCount = 0;
- int surveyDoneCount = 0;
- int surveyAreaJoinCount = 0;
- int surveyAreaDoneCount = 0;
- int examJoinCount = 0;
- int examDoneCount = 0;
- int examAreaJoinCount = 0;
- int examAreaDoneCount = 0;
- int voteJoinCount = 0;
- int voteDoneCount = 0;
- int voteAreaJoinCount = 0;
- int voteAreaDoneCount = 0;
- //问卷调查
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
- .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Survey' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
- {
- if (!string.IsNullOrEmpty(item.owner))
- {
- if (item.owner.Equals("school"))
- {
- surveyJoinCount += 1;
- if (item.taskStatus > 0)
- {
- surveyDoneCount += 1;
- }
- }
- else if (item.owner.Equals("area"))
- {
- surveyAreaJoinCount += 1;
- if (item.taskStatus > 0)
- {
- surveyAreaDoneCount += 1;
- }
- }
- }
- }
- //评量检测
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
- .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'ExamLite' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
- {
- if (!string.IsNullOrEmpty(item.owner))
- {
- if (item.owner.Equals("school"))
- {
- examJoinCount += 1;
- if (item.taskStatus > 0)
- {
- examDoneCount += 1;
- }
- }
- else if (item.owner.Equals("area"))
- {
- examAreaJoinCount += 1;
- if (item.taskStatus > 0)
- {
- examAreaDoneCount += 1;
- }
- }
- }
- }
- //投票活动
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
- .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Vote' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
- {
- if (!string.IsNullOrEmpty(item.owner))
- {
- if (item.owner.Equals("school"))
- {
- voteJoinCount += 1;
- if (item.taskStatus > 0)
- {
- voteDoneCount += 1;
- }
- }
- else if (item.owner.Equals("area"))
- {
- voteAreaJoinCount += 1;
- if (item.taskStatus > 0)
- {
- voteAreaDoneCount += 1;
- }
- }
- }
- }
- return Ok(new { surveyJoinCount, surveyDoneCount, surveyAreaJoinCount, surveyAreaDoneCount, examJoinCount, examDoneCount, examAreaJoinCount, examAreaDoneCount, voteJoinCount, voteDoneCount, voteAreaJoinCount, voteAreaDoneCount, });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-othertypes \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- }
- }
|