TeacherREPController.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Microsoft.Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Pipelines.Sockets.Unofficial.Arenas;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK.Models;
  13. using static TEAMModelOS.SDK.Models.Teacher;
  14. namespace TEAMModelBI.Controllers.RepairApi
  15. {
  16. [Route("teacherrep")]
  17. [ApiController]
  18. public class TeacherREPController : ControllerBase
  19. {
  20. private readonly AzureCosmosFactory _azureCosmos;
  21. public TeacherREPController(AzureCosmosFactory azureCosmos)
  22. {
  23. _azureCosmos = azureCosmos;
  24. }
  25. List<string> schools = new() { "dghznx", "cxhhlx", "xjaxx", "zjqxx", "xnygxx", "lxxcfx", "gxjrxx", "cdgxsx", "yzxx", "kjyxx", "wjylxx", "dsgjxx", "ydzt", "xndbxx", "sqtszx", "cdxczx", "ghsyzx", "lqsx", "cdxxps", "xcfx", "xyqxx", "xncbyy", "cdlqjk", "lqdmxx", "lqyx", "pclxxx", "cdfzx", "xnblxx", "ghsx", "khdycz", "khbmxx", "khdecz", "khzx", "khhbzx", "gxxcxx", "khhtxx", "khdxxx", "khsyxx" };
  26. /// <summary>
  27. /// 修改教师学校信息
  28. /// </summary>
  29. /// <param name="json"></param>
  30. /// <returns></returns>
  31. [HttpPost("get-teachersc")]
  32. public async Task<IActionResult> SetTeacherSchool(JsonElement json)
  33. {
  34. var cosmosClient = _azureCosmos.GetCosmosClient();
  35. foreach (var item in schools)
  36. {
  37. School school = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemAsync<School>($"{item}", new PartitionKey("Base"));
  38. List<Teacher> teachers = new();
  39. await foreach (var tch in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIteratorSql<Teacher>(queryText: $"SELECT value(c) FROM c join s in c.schools where s.schoolId='dghznx' and c.code='Base'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  40. {
  41. teachers.Add(tch);
  42. }
  43. if (teachers.Count > 0)
  44. {
  45. foreach (var teacher in teachers)
  46. {
  47. teacher.schools.ForEach(tch => { if (tch.schoolId.Equals(item)) { tch.areaId = school.areaId; } });
  48. await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
  49. }
  50. }
  51. }
  52. return Ok(new { state = 200 });
  53. }
  54. /// <summary>
  55. /// 修复教师个人信息学校重复的问题
  56. /// </summary>
  57. /// <param name="jsonElement"></param>
  58. /// <returns></returns>
  59. [HttpPost("set-tchsc")]
  60. public async Task<IActionResult> SetTchSchool(JsonElement jsonElement)
  61. {
  62. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  63. var cosmosClient = _azureCosmos.GetCosmosClient();
  64. StringBuilder tchSql = new($"select * from c");
  65. if (!string.IsNullOrEmpty($"{tmdId}"))
  66. tchSql.Append($" where c.id='{tmdId}'");
  67. List<Teacher> tchs = new();
  68. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIteratorSql<Teacher>(queryText: tchSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  69. {
  70. tchs.Add(item);
  71. }
  72. foreach (Teacher tch in tchs)
  73. {
  74. int oldScCnt = 0;
  75. List<TeacherSchool> remoSc = new();
  76. oldScCnt = tch.schools.Count();
  77. if (tch != null)
  78. {
  79. foreach (var teacher in tch.schools)
  80. {
  81. var resSc = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{teacher.schoolId}", new PartitionKey("Base"));
  82. if (resSc.Status == 200)
  83. {
  84. using var areaBase = await JsonDocument.ParseAsync(resSc.Content);
  85. School school = areaBase.ToObject<School>();
  86. if (teacher.areaId != school.areaId)
  87. teacher.areaId = school.areaId;
  88. }
  89. else
  90. {
  91. remoSc.Add(teacher);
  92. }
  93. }
  94. }
  95. remoSc.ForEach(fe => tch.schools.Remove(fe));
  96. List<TeacherSchool> tchSc = tch.schools.Where((x, i) => tch.schools.FindIndex(y => y.schoolId.Equals(x.schoolId)) == i).ToList();
  97. if (oldScCnt > tchSc.Count)
  98. {
  99. tch.schools = tchSc;
  100. await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Teacher>(tch, tch.id, new PartitionKey("Base"));
  101. }
  102. }
  103. return Ok(new { state = 200 });
  104. }
  105. }
  106. }