StudentController.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Microsoft.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.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.Models;
  11. using TEAMModelOS.SDK.Context.BI;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.SDK.Models;
  15. namespace TEAMModelBI.Controllers.BIStudent
  16. {
  17. [Route("student")]
  18. [ApiController]
  19. public class StudentController : ControllerBase
  20. {
  21. private readonly AzureCosmosFactory _azureCosmos;
  22. private readonly AzureStorageFactory _azureStorage;
  23. private readonly DingDing _dingDing;
  24. private readonly Option _option;
  25. public StudentController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  26. {
  27. _azureCosmos = azureCosmos;
  28. _azureStorage = azureStorage;
  29. _dingDing = dingDing;
  30. _option = option?.Value;
  31. }
  32. /// <summary>
  33. /// 依据学生Id查询详情 数据管理工具——查询工具
  34. /// </summary>
  35. /// <param name="jsonElement"></param>
  36. /// <returns></returns>
  37. [HttpPost("get-info")]
  38. public async Task<IActionResult> GetInfo(JsonElement jsonElement)
  39. {
  40. if (!jsonElement.TryGetProperty("studentId", out JsonElement studentId)) return BadRequest();
  41. jsonElement.TryGetProperty("code", out JsonElement code);
  42. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  43. var cosmosClient = _azureCosmos.GetCosmosClient();
  44. ////分开部署,就不需要,一站多用时,取消注释
  45. //if ($"{site}".Equals(BIConst.Global))
  46. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  47. List<object> objs = new();
  48. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryStreamIteratorSql(queryText: $"select value(c) from c where c.id='{studentId}'", requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  49. {
  50. using var json = await JsonDocument.ParseAsync(item.Content);
  51. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  52. {
  53. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  54. {
  55. objs.Add(obj);
  56. }
  57. }
  58. }
  59. return Ok(new { state = 200, students = objs });
  60. }
  61. /// <summary>
  62. /// 学生活动中间表ClassIds去重
  63. /// </summary>
  64. /// <param name="jsonElement"></param>
  65. /// <returns></returns>
  66. [ProducesDefaultResponseType]
  67. [HttpPost("get-classIds")]
  68. public async Task<IActionResult> CorrectActivityClassIds(JsonElement jsonElement)
  69. {
  70. try
  71. {
  72. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  73. var cosmosClient = _azureCosmos.GetCosmosClient();
  74. //if ($"{site}".Equals(BIConst.Global))
  75. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  76. //string sqlTxt = "SELECT select c.id,c.code,c.classIds FROM c where c.pk='Activity' and c.classIds <> []";
  77. string sqlTxt = "select c.id,c.code,c.classIds from c where c.school='cswznb' and c.classIds <> []";
  78. List<CorrectStu> correctStus = new();
  79. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryStreamIteratorSql(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  80. {
  81. using var json = await JsonDocument.ParseAsync(item.Content);
  82. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  83. {
  84. obj.TryGetProperty("id", out JsonElement tempTd);
  85. obj.TryGetProperty("code", out JsonElement code);
  86. obj.TryGetProperty("classIds", out JsonElement _classIds);
  87. List<string> templist = _classIds.ToObject<List<string>>();
  88. //List<string> newList = templist.Distinct().ToList(); //Equals实现去重
  89. //List<string> newLis1 = templist.Where((x, i) => templist.FindIndex(z => z == x) == i).ToList(); //Lambda表达式去重
  90. HashSet<string> hashSet = new(templist);//哈希自动去重
  91. if (hashSet.Count != templist.Count)
  92. {
  93. CorrectStu correctList = new() { id = $"{tempTd}", code = $"{code}", classIds = hashSet.ToList() };
  94. correctStus.Add(correctList);
  95. }
  96. }
  97. }
  98. if (correctStus.Count > 0)
  99. {
  100. /*foreach (var correct in correctStus)
  101. {
  102. StuActivity stuActivity = await cosmosClient.GetContainer("TEAMModelOS", "Student").ReadItemAsync<StuActivity>(correct.id, new PartitionKey(correct.code));
  103. if (stuActivity != null)
  104. {
  105. stuActivity.classIds = correct.classIds;
  106. await cosmosClient.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync<StuActivity>(stuActivity, stuActivity.id, new PartitionKey(stuActivity.code));
  107. }
  108. }*/
  109. return Ok(new { state = 200, msg = "去重成功" });
  110. }
  111. else
  112. {
  113. return Ok(new { state = 201, msg = "学生活动中间表无重复班级ID" });
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. await _dingDing.SendBotMsg($"BI,{_option.Location} /stuactivity/get-classIds \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  119. return Ok(new { state = 401 });
  120. }
  121. }
  122. public record CorrectStu
  123. {
  124. public string id { get; set; }
  125. public string code { get; set; }
  126. public List<string> classIds { get; set; }
  127. }
  128. }
  129. }