StudentController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.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. if ($"{site}".Equals(BIConst.Global))
  45. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  46. List<object> objs = new();
  47. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.id='{studentId}'", requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  48. {
  49. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  50. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  51. {
  52. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  53. {
  54. objs.Add(obj);
  55. }
  56. }
  57. }
  58. return Ok(new { state = 200, students = objs });
  59. }
  60. /// <summary>
  61. /// 学生活动中间表ClassIds去重
  62. /// </summary>
  63. /// <param name="jsonElement"></param>
  64. /// <returns></returns>
  65. [ProducesDefaultResponseType]
  66. [HttpPost("get-classIds")]
  67. public async Task<IActionResult> CorrectActivityClassIds(JsonElement jsonElement)
  68. {
  69. try
  70. {
  71. jsonElement.TryGetProperty("site", out JsonElement site);
  72. var cosmosClient = _azureCosmos.GetCosmosClient();
  73. if ($"{site}".Equals(BIConst.Global))
  74. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  75. //string sqlTxt = "SELECT select c.id,c.code,c.classIds FROM c where c.pk='Activity' and c.classIds <> []";
  76. string sqlTxt = "select c.id,c.code,c.classIds from c where c.school='cswznb' and c.classIds <> []";
  77. List<CorrectStu> correctStus = new();
  78. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  79. {
  80. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  81. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  82. {
  83. obj.TryGetProperty("id", out JsonElement tempTd);
  84. obj.TryGetProperty("code", out JsonElement code);
  85. obj.TryGetProperty("classIds", out JsonElement _classIds);
  86. List<string> templist = _classIds.ToObject<List<string>>();
  87. //List<string> newList = templist.Distinct().ToList(); //Equals实现去重
  88. //List<string> newLis1 = templist.Where((x, i) => templist.FindIndex(z => z == x) == i).ToList(); //Lambda表达式去重
  89. HashSet<string> hashSet = new(templist);//哈希自动去重
  90. if (hashSet.Count != templist.Count)
  91. {
  92. CorrectStu correctList = new() { id = $"{tempTd}", code = $"{code}", classIds = hashSet.ToList() };
  93. correctStus.Add(correctList);
  94. }
  95. }
  96. }
  97. if (correctStus.Count > 0)
  98. {
  99. foreach (var correct in correctStus)
  100. {
  101. StuActivity stuActivity = await cosmosClient.GetContainer("TEAMModelOS", "Student").ReadItemAsync<StuActivity>(correct.id, new PartitionKey(correct.code));
  102. if (stuActivity != null)
  103. {
  104. stuActivity.classIds = correct.classIds;
  105. await cosmosClient.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync<StuActivity>(stuActivity, stuActivity.id, new PartitionKey(stuActivity.code));
  106. }
  107. }
  108. return Ok(new { state = 200, msg = "去重成功" });
  109. }
  110. else
  111. {
  112. return Ok(new { state = 201, msg = "学生活动中间表无重复班级ID" });
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. await _dingDing.SendBotMsg($"BI,{_option.Location} /stuactivity/get-classIds \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  118. return Ok(new { state = 401 });
  119. }
  120. }
  121. public record CorrectStu
  122. {
  123. public string id { get; set; }
  124. public string code { get; set; }
  125. public List<string> classIds { get; set; }
  126. }
  127. }
  128. }