ActivitySticsController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.Models;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Models;
  12. using System.Text.Json;
  13. namespace TEAMModelBI.Controllers.Activity
  14. {
  15. [Route("activity")]
  16. [ApiController]
  17. public class ActivitySticsController : ControllerBase
  18. {
  19. private readonly AzureCosmosFactory _azureCosmos;
  20. private readonly AzureStorageFactory _azureStorage;
  21. private readonly DingDing _dingDing;
  22. private readonly Option _option;
  23. public readonly List<string> types = new List<string> { "Exam", "Survey", "Vote", "Homework" };
  24. public ActivitySticsController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  25. {
  26. _azureCosmos = azureCosmos;
  27. _dingDing = dingDing;
  28. _azureStorage = azureStorage;
  29. _option = option?.Value;
  30. }
  31. /// <summary>
  32. /// 统计所有的评量活动,问卷调查,投票活动,作业
  33. /// </summary>
  34. /// <returns></returns>
  35. [HttpPost("get-allactivity")]
  36. public async Task<IActionResult> GetAllActivity()
  37. {
  38. try
  39. {
  40. var cosmosClient = _azureCosmos.GetCosmosClient();
  41. List<KeyValuePair<string, int>> typeCount = new List<KeyValuePair<string, int>>();
  42. foreach (var type in types)
  43. {
  44. int acount = 0;
  45. string querySql = $"SELECT distinct c.id,c.code,c.name,c.pk FROM c where c.pk='{type}' ";
  46. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: querySql, requestOptions: new QueryRequestOptions() { }))
  47. {
  48. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  49. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
  50. {
  51. acount += count.GetInt32();
  52. //foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  53. //{
  54. // acount += 1;
  55. //}
  56. }
  57. }
  58. KeyValuePair<string, int> valuePair = new KeyValuePair<string, int>(type, acount);
  59. typeCount.Add(valuePair);
  60. }
  61. return Ok(new { state = 200, typeCount });
  62. }
  63. catch (Exception ex)
  64. {
  65. await _dingDing.SendBotMsg($"BI,{_option.Location} /activity/get-allactivity \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  66. return BadRequest();
  67. }
  68. }
  69. /// <summary>
  70. /// 统计顾问关联的学校活动数量:评测、问卷、投票
  71. /// </summary>
  72. /// <param name="jsonElement"></param>
  73. /// <returns></returns>
  74. [HttpPost("get-assistactivity")]
  75. public async Task<IActionResult> GetAssistSchoolActivity(JsonElement jsonElement)
  76. {
  77. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  78. var cosmosClient = _azureCosmos.GetCosmosClient();
  79. List<string> schoolIds = new List<string>();
  80. 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}'";
  81. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText:schoolSql,requestOptions:new QueryRequestOptions() { }))
  82. {
  83. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  84. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  85. {
  86. schoolIds.Add(obj.GetProperty("schoolId").GetString());
  87. }
  88. }
  89. List<KeyValuePair<string, int>> typeCount = new List<KeyValuePair<string, int>>();
  90. foreach (var type in types)
  91. {
  92. int acount = 0;
  93. foreach (var itemId in schoolIds)
  94. {
  95. string activitySql = $"SELECT DISTINCT c.id,c.code,c.name FROM c WHERE c.pk='{type}' AND c.school='{itemId}'";
  96. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: activitySql, requestOptions: new QueryRequestOptions() { }))
  97. {
  98. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  99. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  100. {
  101. acount += count.GetInt32();
  102. //foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  103. //{
  104. // acount += 1;
  105. //}
  106. }
  107. }
  108. }
  109. KeyValuePair<string, int> valuePair = new KeyValuePair<string, int>(type, acount);
  110. typeCount.Add(valuePair);
  111. }
  112. return Ok(new { state = 200, typeCount });
  113. }
  114. }
  115. }