StuActivityController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.DI;
  12. using TEAMModelOS.SDK.Extension;
  13. using TEAMModelOS.SDK.Models;
  14. namespace TEAMModelBI.Controllers.BIStudent
  15. {
  16. [Route("stuactivity")]
  17. [ApiController]
  18. public class StuActivityController : ControllerBase
  19. {
  20. private readonly AzureCosmosFactory _azureCosmos;
  21. private readonly AzureStorageFactory _azureStorage;
  22. private readonly DingDing _dingDing;
  23. private readonly Option _option;
  24. public StuActivityController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  25. {
  26. _azureCosmos = azureCosmos;
  27. _azureStorage = azureStorage;
  28. _dingDing = dingDing;
  29. _option = option?.Value;
  30. }
  31. /// <summary>
  32. /// 学生活动中间表ClassIds去重
  33. /// </summary>
  34. /// <param name="jsonElement"></param>
  35. /// <returns></returns>
  36. [HttpPost("get-classIds")]
  37. public async Task<IActionResult> CorrectActivityClassIds()
  38. {
  39. try
  40. {
  41. var cosmosClient = _azureCosmos.GetCosmosClient();
  42. //string sqlTxt = "SELECT select c.id,c.code,c.classIds FROM c where c.pk='Activity' and c.classIds <> []";
  43. string sqlTxt = "select c.id,c.code,c.classIds from c where c.school='cswznb' and c.classIds <> []";
  44. List<CorrectStu> correctStus = new List<CorrectStu>();
  45. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  46. {
  47. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  48. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  49. {
  50. obj.TryGetProperty("id", out JsonElement tempTd);
  51. obj.TryGetProperty("code", out JsonElement code);
  52. obj.TryGetProperty("classIds", out JsonElement _classIds);
  53. List<string> templist = _classIds.ToObject<List<string>>();
  54. //List<string> newList = templist.Distinct().ToList(); //Equals实现去重
  55. //List<string> newLis1 = templist.Where((x, i) => templist.FindIndex(z => z == x) == i).ToList(); //Lambda表达式去重
  56. HashSet<string> hashSet = new HashSet<string>(templist);//哈希自动去重
  57. if (hashSet.Count != templist.Count)
  58. {
  59. CorrectStu correctList = new CorrectStu() { id = $"{tempTd}", code = $"{code}", classIds = hashSet.ToList() };
  60. correctStus.Add(correctList);
  61. }
  62. }
  63. }
  64. if (correctStus.Count > 0)
  65. {
  66. foreach (var correct in correctStus)
  67. {
  68. StuActivity stuActivity = await cosmosClient.GetContainer("TEAMModelOS", "Student").ReadItemAsync<StuActivity>(correct.id, new PartitionKey(correct.code));
  69. if (stuActivity != null)
  70. {
  71. stuActivity.classIds = correct.classIds;
  72. await cosmosClient.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync<StuActivity>(stuActivity, stuActivity.id, new PartitionKey(stuActivity.code));
  73. }
  74. }
  75. return Ok(new { state = 200, msg = "去重成功" });
  76. }
  77. else
  78. {
  79. return Ok(new { state = 201, msg = "学生活动中间表无重复班级ID" });
  80. }
  81. }
  82. catch (Exception ex)
  83. {
  84. await _dingDing.SendBotMsg($"BI,{_option.Location} /stuactivity/get-classIds \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  85. return Ok(new { state = 401 });
  86. }
  87. }
  88. public record CorrectStu
  89. {
  90. public string id { get; set; }
  91. public string code { get; set; }
  92. public List<string> classIds { get; set; }
  93. }
  94. }
  95. }