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 { [Route("teacherrep")] [ApiController] public class TeacherREPController : ControllerBase { private readonly AzureCosmosFactory _azureCosmos; public TeacherREPController(AzureCosmosFactory azureCosmos) { _azureCosmos = azureCosmos; } List 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" }; /// /// 修改教师学校信息 /// /// /// [HttpPost("get-teachersc")] public async Task SetTeacherSchool(JsonElement json) { var cosmosClient = _azureCosmos.GetCosmosClient(); foreach (var item in schools) { School school = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemAsync($"{item}", new PartitionKey("Base")); List teachers = new(); await foreach (var tch in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator(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") })) { teachers.Add(tch); } if (teachers.Count > 0) { foreach (var teacher in teachers) { teacher.schools.ForEach(tch => { if (tch.schoolId.Equals(item)) { tch.areaId = school.areaId; } }); await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(teacher, teacher.id, new PartitionKey("Base")); } } } return Ok(new { state = 200 }); } /// /// 修复教师个人信息学校重复的问题 /// /// /// [HttpPost("set-tchsc")] public async Task 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 tchs = new(); await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator(queryText: tchSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") })) { tchs.Add(item); } foreach (Teacher tch in tchs) { int oldScCnt = 0; List 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(); if (teacher.areaId != school.areaId) teacher.areaId = school.areaId; } else { remoSc.Add(teacher); } } } remoSc.ForEach(fe => tch.schools.Remove(fe)); List 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(tch, tch.id, new PartitionKey("Base")); } } return Ok(new { state = 200 }); } } }