123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- using Azure.Cosmos;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Models;
- using System.Text.Json;
- namespace TEAMModelBI.Controllers.Activity
- {
- [Route("activity")]
- [ApiController]
- public class ActivitySticsController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly AzureStorageFactory _azureStorage;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- public readonly List<string> types = new List<string> { "Exam", "Survey", "Vote", "Homework" };
- public ActivitySticsController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
- {
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _azureStorage = azureStorage;
- _option = option?.Value;
- }
- /// <summary>
- /// 统计所有的评量活动,问卷调查,投票活动,作业
- /// </summary>
- /// <returns></returns>
- [HttpPost("get-allactivity")]
- public async Task<IActionResult> GetAllActivity()
- {
- try
- {
- var cosmosClient = _azureCosmos.GetCosmosClient();
- List<KeyValuePair<string, int>> typeCount = new List<KeyValuePair<string, int>>();
- foreach (var type in types)
- {
- int acount = 0;
- string querySql = $"SELECT distinct c.id,c.code,c.name,c.pk FROM c where c.pk='{type}' ";
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: querySql, requestOptions: new QueryRequestOptions() { }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
- {
- acount += count.GetInt32();
- //foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- //{
- // acount += 1;
- //}
- }
- }
- KeyValuePair<string, int> valuePair = new KeyValuePair<string, int>(type, acount);
- typeCount.Add(valuePair);
- }
- return Ok(new { state = 200, typeCount });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /activity/get-allactivity \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 统计顾问关联的学校活动数量:评测、问卷、投票
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [HttpPost("get-assistactivity")]
- public async Task<IActionResult> GetAssistSchoolActivity(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
- var cosmosClient = _azureCosmos.GetCosmosClient();
- List<string> schoolIds = new List<string>();
- 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.pk = 'Teacher' AND c.status = 'join' AND c.id='{tmdId}'";
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText:schoolSql,requestOptions:new QueryRequestOptions() { }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- schoolIds.Add(obj.GetProperty("schoolId").GetString());
- }
- }
- List<KeyValuePair<string, int>> typeCount = new List<KeyValuePair<string, int>>();
- foreach (var type in types)
- {
- int acount = 0;
- foreach (var itemId in schoolIds)
- {
- string activitySql = $"SELECT DISTINCT c.id,c.code,c.name FROM c WHERE c.pk='{type}' AND c.school='{itemId}'";
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: activitySql, requestOptions: new QueryRequestOptions() { }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- acount += count.GetInt32();
- //foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- //{
- // acount += 1;
- //}
- }
- }
- }
- KeyValuePair<string, int> valuePair = new KeyValuePair<string, int>(type, acount);
- typeCount.Add(valuePair);
- }
- return Ok(new { state = 200, typeCount });
- }
- }
- }
|