StuActivityController.cs 4.8 KB

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