LessonSticsController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using TEAMModelOS.Models;
  8. using TEAMModelOS.SDK.DI;
  9. using Microsoft.Extensions.Options;
  10. using Azure.Cosmos;
  11. using System.Text.Json;
  12. using TEAMModelOS.SDK.Models.Cosmos.Common;
  13. using TEAMModelOS.SDK.Models;
  14. namespace TEAMModelBI.Controllers.Lesson
  15. {
  16. [Route("lesson")]
  17. [ApiController]
  18. public class LessonSticsController : ControllerBase
  19. {
  20. private readonly AzureCosmosFactory _azureCosmos;
  21. private readonly AzureStorageFactory _azureStorage;
  22. private readonly DingDing _dingDing;
  23. private readonly Option _option;
  24. public LessonSticsController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureFactory, DingDing dingDing, IOptionsSnapshot<Option> option)
  25. {
  26. _azureCosmos = azureCosmos;
  27. _azureStorage = azureFactory;
  28. _dingDing = dingDing;
  29. _option = option?.Value;
  30. }
  31. /// <summary>
  32. /// 统计课例数量
  33. /// </summary>
  34. /// <returns></returns>
  35. [HttpPost("get-total")]
  36. public async Task<IActionResult> GetCount()
  37. {
  38. try
  39. {
  40. List<string> schoolIds = new List<string>();
  41. var cosmosClient = _azureCosmos.GetCosmosClient();
  42. string lessonSql = $"select c.id,c.tmid,c.scope from c where c.pk='LessonRecord'";
  43. int total = 0;
  44. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: lessonSql, requestOptions: new QueryRequestOptions() { }))
  45. {
  46. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  47. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
  48. {
  49. total = count.GetInt32();
  50. }
  51. }
  52. return Ok(new { state = 200, total });
  53. }
  54. catch (Exception ex)
  55. {
  56. await _dingDing.SendBotMsg($"BI,{_option.Location} /lesson/get-total {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  57. return BadRequest();
  58. }
  59. }
  60. /// <summary>
  61. /// 管家所关联的课例数据
  62. /// </summary>
  63. /// <param name="jsonElement"></param>
  64. /// <returns></returns>
  65. [ProducesDefaultResponseType]
  66. [HttpPost("get-assiist")]
  67. public async Task<IActionResult> GetAssiist(JsonElement jsonElement)
  68. {
  69. try
  70. {
  71. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  72. var cosmosClient = _azureCosmos.GetCosmosClient();
  73. List<SchoolLen> schoolLens = new List<SchoolLen>();
  74. List<string> schoolIds = new List<string>();
  75. 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}'";
  76. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: schoolSql, requestOptions: new QueryRequestOptions() { }))
  77. {
  78. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  79. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  80. {
  81. schoolIds.Add(obj.GetProperty("schoolId").GetString());
  82. }
  83. }
  84. foreach (var itemId in schoolIds)
  85. {
  86. SchoolLen schoolLen = new SchoolLen() { id = itemId };
  87. School school = new();
  88. try
  89. {
  90. school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(itemId, new PartitionKey("Base"));
  91. }
  92. catch
  93. {
  94. }
  95. schoolLen.name = school != null ? school.name : itemId;
  96. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: "SELECT DISTINCT c.id,c.code,c.name FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{itemId}") }))
  97. {
  98. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  99. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt32() > 0)
  100. {
  101. schoolLen.total = count.GetInt32();
  102. }
  103. }
  104. schoolLens.Add(schoolLen);
  105. }
  106. return Ok(new { state = 200, schoolLens });
  107. }
  108. catch (Exception ex)
  109. {
  110. await _dingDing.SendBotMsg($"BI, {_option.Location} /lesson/get-assiist {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  111. return BadRequest();
  112. }
  113. }
  114. public record SchoolLen
  115. {
  116. public string id { get; set; }
  117. public string name { get; set;}
  118. public int total { get; set; }
  119. }
  120. }
  121. }