Quellcode durchsuchen

修复教师个人信息学校重复的问题

Li vor 2 Jahren
Ursprung
Commit
ad19b89a29
1 geänderte Dateien mit 62 neuen und 0 gelöschten Zeilen
  1. 62 0
      TEAMModelBI/Controllers/RepairApi/TeacherREPController.cs

+ 62 - 0
TEAMModelBI/Controllers/RepairApi/TeacherREPController.cs

@@ -1,11 +1,16 @@
 using Azure.Cosmos;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using Pipelines.Sockets.Unofficial.Arenas;
 using System.Collections.Generic;
+using System.Linq;
+using System.Text;
 using System.Text.Json;
 using System.Threading.Tasks;
 using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Models;
+using static TEAMModelOS.SDK.Models.Teacher;
 
 namespace TEAMModelBI.Controllers.RepairApi
 {
@@ -53,5 +58,62 @@ namespace TEAMModelBI.Controllers.RepairApi
 
             return Ok(new { state = 200 });
         }
+
+        /// <summary>
+        /// 修复教师个人信息学校重复的问题
+        /// </summary>
+        /// <param name="jsonElement"></param>
+        /// <returns></returns>
+        [HttpPost("set-tchsc")]
+        public async Task<IActionResult> SetTchSchool(JsonElement jsonElement)
+        {
+            jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
+
+            var cosmosClient = _azureCosmos.GetCosmosClient();
+            StringBuilder tchSql = new($"select * from c");
+            if (!string.IsNullOrEmpty($"{tmdId}"))
+                tchSql.Append($" where c.id='{tmdId}'");
+
+            List<Teacher> tchs = new();
+            await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Teacher>(queryText: tchSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
+            {
+                tchs.Add(item);
+            }
+
+            foreach (Teacher tch in tchs)
+            {
+                int oldScCnt = 0;
+                List<TeacherSchool> remoSc = new();
+                oldScCnt = tch.schools.Count();
+                if (tch != null)
+                {
+                    foreach (var teacher in tch.schools)
+                    {
+                        var resSc = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{teacher.schoolId}", new PartitionKey("Base"));
+                        if (resSc.Status == 200)
+                        {
+                            using var areaBase = await JsonDocument.ParseAsync(resSc.ContentStream);
+                            School school = areaBase.ToObject<School>();
+                            if (teacher.areaId != school.areaId)
+                                teacher.areaId = school.areaId;
+                        }
+                        else
+                        {
+                            remoSc.Add(teacher);
+                        }
+                    }
+                }
+                remoSc.ForEach(fe => tch.schools.Remove(fe));
+
+                List<TeacherSchool> tchSc = tch.schools.Where((x, i) => tch.schools.FindIndex(y => y.schoolId.Equals(x.schoolId)) == i).ToList();
+                if (oldScCnt > tchSc.Count)
+                {
+                    tch.schools = tchSc;
+                    await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Teacher>(tch, tch.id, new PartitionKey("Base"));
+                }
+            }
+
+            return Ok(new { state = 200 });
+        }
     }
 }