1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234 |
- using Microsoft.Azure.Cosmos;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Newtonsoft.Json;
- using StackExchange.Redis;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Json;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Models.Cosmos;
- using TEAMModelOS.SDK.Models.Service;
-
- using Azure;
- namespace TEAMModelOS.Controllers
- {
- [Route("data-migration")]
- [ApiController]
- public class DataMigrationController : ControllerBase
- {
- ///Teacher表
- ///
- /// 复制的数据
- /// Base ==>>Student //IES5&教研中心
- /// 迁移的数据 StuCourse ==>> Student //IES5
- /// Activity ==>> Student //IES5
- ///
- private readonly AzureStorageFactory _azureStorage;
- private readonly AzureRedisFactory _azureRedis;
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly DingDing _dingDing;
- private readonly CoreAPIHttpService _coreAPIHttpService;
- public DataMigrationController(CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, DingDing dingDing)
- {
- _azureCosmos = azureCosmos;
- _azureRedis = azureRedis;
- _azureStorage = azureStorage;
- _dingDing = dingDing;
- _coreAPIHttpService=coreAPIHttpService;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("data-course-to-class-for-global")]
- public async Task<IActionResult> DataMigrationToClassForGlobal(JsonElement request)
- {
- //参数 schoolId periodId(可选) 是否迁移历史数据 history=0;
- try
- {
- //if(!request.TryGetProperty("schoolId", out JsonElement _schoolId)) return BadRequest();
- //string schoolId = _schoolId.GetString();
- List<Course> schoolCourses = new List<Course>();
- HashSet<string> schoolIds = new HashSet<string>();
- StringBuilder sql = new StringBuilder($"select value c from c where c.pk='Course' ");
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School)
- .GetItemQueryIteratorSql<Course>(queryText: sql.ToString()))
- {
-
- if (!string.IsNullOrWhiteSpace(item.school))
- {
- schoolCourses.Add(item);
- schoolIds.Add(item.school);
- }
- }
- List<School> schools = new List<School>();
- if (schoolIds.Count>0)
- {
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<School>($"select value c from c where c.id in ({string.Join(",", schoolIds.Select(z => $"'{z}'"))})", "Base");
- if (result.list.IsNotEmpty())
- {
- schools.AddRange(result.list);
- }
- }
- List<CourseDto> schoolCourseDtos = new List<CourseDto>();
- foreach (var course in schoolCourses)
- {
- var school = schools.Find(z => z.id.Equals(course.school));
- if (school==null) { continue; }
- var period = school.period.Find(z => z.id.Equals(course.period?.id));
- if (period==null)
- {
- continue;
- }
- var subject = period.subjects.Find(z => z.id.Equals(course.subject.id) ||z.name.Equals(course.subject.name));
- if (subject== null)
- {
- subject=new Subject { id =course.subject.id, name=course.subject.name, type=1 };
- period.subjects.Add(subject);
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(school, new PartitionKey(school.code));
- }
- //ResponseMessage response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemStreamAsync(course.id, new PartitionKey($"CourseBase-{school.id}"));
- //if (response.StatusCode==System.Net.HttpStatusCode.OK)
- //{
- // CourseBase courseBase = JsonDocument.Parse(response.Content).RootElement.ToObject<CourseBase>();
- // if (courseBase!= null)
- // {
- // for (int grade = 0; grade<period.grades.Count; grade++)
- // {
- // if (courseBase.grade==grade)
- // {
- // continue;
- // }
- // string sqlBaseData = $"select value c from c where c.name ='{course.name}' and c.grade={grade} and c.period.id='{period.id}' and c.subject.id='{subject.id}'";
- // var resultBaseData = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<CourseBase>(sqlBaseData, $"CourseBase-{school.id}");
- // if (!resultBaseData.list.IsNotEmpty())
- // {
- // CourseBase courseBaseNew = new CourseBase()
- // {
- // id= Guid.NewGuid().ToString(),
- // code=$"CourseBase-{school.id}",
- // pk="CourseBase",
- // ttl=-1,
- // name=course.name,
- // subject= new IdName { id=subject?.id, name=subject?.name },
- // period= new IdName { id= course.period?.id, name =course.period?.name },
- // school=school.id,
- // scope="school",
- // creatorId=course.creatorId,
- // desc=course.desc,
- // status=1,
- // grade=grade,
- // };
- // await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(courseBaseNew, new PartitionKey(courseBaseNew.code));
- // }
- // }
- // }
- //}
- string sqlCourseBase = $"select value c from c where c.period.id='{period.id}' and c.subject.id='{subject.id}' ";
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<CourseBase>(sqlCourseBase, $"CourseBase-{school.id}");
- List<CourseBase> courseBases = result.list.OrderBy(z => z.grade).ToList();
- var subjectIndex = period.subjects.FindIndex(z => z.id.Equals(subject.id));
- if (subjectIndex>=0)
- {
- subjectIndex=subjectIndex+1;
- int index = 1;
- foreach (var couresBase in courseBases)
- {
- string no = $"{subjectIndex}0{index}";
- if (index>=10)
- {
- no = $"{subjectIndex}{index}";
- }
- index++;
- couresBase.no=no;
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(couresBase, new PartitionKey(couresBase.code));
- }
- }
- //当前学年
- var semester = SchoolService.GetSemester(period, DateTimeOffset.Now.ToUnixTimeMilliseconds());
- List<ScheduleTask> schedules = new List<ScheduleTask>();
- foreach (var schedule in course.schedule)
- {
- ScheduleTask scheduleSave = null;
- int grade = -1;
- if (!string.IsNullOrWhiteSpace(schedule.teacherId) && !string.IsNullOrWhiteSpace(schedule.classId))
- {
- scheduleSave =new ScheduleTask() { type="class", groupId=schedule.classId, teacherId=schedule.teacherId, roomId= schedule.room, school=school.id };
- try
- {
- Class clazz = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<Class>(schedule.classId, new PartitionKey($"Class-{school.id}"));
- if (semester.studyYear- clazz.year>=period.grades.Count)
- {
- grade=-1;
- }
- else
- {
- var gradeData = SchoolService.GetGrades(school, period.id, new List<int>() { clazz.year }, DateTimeOffset.Now.ToUnixTimeMilliseconds());
- if (gradeData.grades.Any() && gradeData.grades.Count>0)
- {
- int.TryParse(gradeData.grades.First(), out grade);
- }
- }
- }
- catch { }
- }
- if (!string.IsNullOrWhiteSpace(schedule.teacherId) && !string.IsNullOrWhiteSpace(schedule.stulist))
- {
- scheduleSave = new ScheduleTask() { type="teach", groupId=schedule.stulist, teacherId=schedule.teacherId, roomId= schedule.room, school=school.id };
- if (grade==-1)
- {
- _coreAPIHttpService.check=false;
- var groups = await GroupListService.GetMemberByListids(_coreAPIHttpService, _azureCosmos.GetCosmosClient(), _dingDing, new List<string> { schedule.stulist }, school.id);
- if (groups.rmembers.IsNotEmpty())
- {
- var a = groups.rmembers.Where(z => z.type==2 && z.year>0).GroupBy(z => z.year).Select(z => new { key = z.Key, list = z.ToList() }).OrderByDescending(y => y.list.Count).FirstOrDefault();
- if (a!=null)
- {
- if (semester.studyYear- a.key>=period.grades.Count)
- {
- grade=-1;
- }
- else
- {
- var gradeData = SchoolService.GetGrades(school, period.id, new List<int>() { a.key }, DateTimeOffset.Now.ToUnixTimeMilliseconds());
- if (gradeData.grades.Any() && gradeData.grades.Count>0)
- {
- int.TryParse(gradeData.grades.First(), out grade);
- }
- }
- }
- }
- }
- }
- if (grade>-1)
- {
- List<CourseBase> courseBasesExsit = new List<CourseBase>();
- string sqlCourse = $"select value c from c where c.name ='{course.name}' and c.grade={grade} and c.period.id='{period.id}' and c.subject.id='{course.subject?.id}' ";
- var resultCourseBase = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<CourseBase>(sqlCourse, $"CourseBase-{school.id}");
- if (!resultCourseBase.list.IsNotEmpty())
- {
- CourseBase courseBaseNew = new CourseBase()
- {
- id= Guid.NewGuid().ToString(),
- code=$"CourseBase-{school.id}",
- pk="CourseBase",
- ttl=-1,
- name=course.name,
- subject= new IdName { id=subject?.id, name=subject?.name },
- period= new IdName { id= course.period?.id, name =course.period?.name },
- school=school.id,
- scope="school",
- creatorId=course.creatorId,
- desc=course.desc,
- status=1,
- grade=grade,
- };
- courseBasesExsit.Add(courseBaseNew);
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(courseBaseNew, new PartitionKey(courseBaseNew.code));
- {
- //重新处理编号
- string sqlCourseBaseNo = $"select value c from c where c.period.id='{period.id}' and c.subject.id='{subject.id}' ";
- var resultNo = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<CourseBase>(sqlCourseBase, $"CourseBase-{school.id}");
- List<CourseBase> courseBasesNo = resultNo.list.OrderBy(z => z.grade).ToList();
- var subjectIndexNo = period.subjects.FindIndex(z => z.id.Equals(subject.id));
- if (subjectIndexNo>=0)
- {
- subjectIndexNo=subjectIndexNo+1;
- int index = 1;
- foreach (var couresBase in courseBasesNo)
- {
- string no = $"{subjectIndexNo}0{index}";
- if (index>=10)
- {
- no = $"{subjectIndexNo}{index}";
- }
- index++;
- couresBase.no=no;
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(couresBase, new PartitionKey(couresBase.code));
- }
- }
- }
- }
- else {
- courseBasesExsit.AddRange(resultCourseBase.list);
- }
- foreach (var item in courseBasesExsit)
- {
- if (semester.currSemester!=null)
- {
- string id = $"{semester.studyYear}-{semester.currSemester.id}-{item.id}";
- string code = $"CourseTask-{school.id}";
- ResponseMessage responseTask = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemStreamAsync(id, new PartitionKey(code));
- CourseTask courseTask = null;
- if (responseTask.StatusCode==System.Net.HttpStatusCode.OK)
- {
- courseTask = JsonDocument.Parse(responseTask.Content).RootElement.ToObject<CourseTask>();
- var scheduleExsit = courseTask.schedules.Find(z => z.groupId.Equals(scheduleSave.groupId));
- if (scheduleExsit==null)
- {
- courseTask.schedules.Add(scheduleSave);
- }
- else
- {
- scheduleExsit.teacherId=scheduleSave.teacherId;
- }
- }
- else
- {
- courseTask = new CourseTask
- {
- id = id,
- code = code,
- pk="CourseTask",
- ttl=-1,
- courseId=item.id,
- year= semester.studyYear,
- expire= semester.nextSemester.AddDays(-1).ToUnixTimeMilliseconds(),
- semesterId=semester.currSemester.id,
- schedules=new List<ScheduleTask>() { scheduleSave }
- };
- }
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(courseTask, new PartitionKey(code));
- }
- }
- }
- }
- }
- return Ok(new { schoolCourseDtos });
- }
- catch (Exception ex) { return Ok($"{ex.Message},{ex.StackTrace}"); }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("data-course-to-class")]
- public async Task<IActionResult> DataMigrationToClass(JsonElement request)
- {
- //参数 schoolId periodId(可选) 是否迁移历史数据 history=0;
- try
- {
- List<Course> schoolCourses = new List<Course>();
- HashSet<string> schoolIds = new HashSet<string>();
- int history = 0;
- if (request.TryGetProperty("history", out JsonElement _history) && $"{_history}".Equals("1"))
- {
- history=1;
- }
- StringBuilder sql = new StringBuilder($"select value c from c where c.pk='Course' ");
- if (request.TryGetProperty("periodId", out JsonElement periodId) && !string.IsNullOrWhiteSpace($"{periodId}"))
- {
- sql.Append($" and c.period.id ='{periodId}'");
- }
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIteratorSql<Course>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey= new PartitionKey($"Course-{request.GetProperty("schoolId")}") }))
- {
- schoolCourses.Add(item);
- if (!string.IsNullOrWhiteSpace(item.school))
- {
- if (item.school.Equals("ydzt") ||item.school.Equals("xcfx") ||item.school.Equals("hlgj") ||item.school.Equals("hbcn") ||item.school.Equals("kjyxx") ||item.school.Equals("bjgsex") || item.school.Equals("cdxxps") ||item.school.Equals("pclxxx"))
- {
- //大陆站强制不处理的学校
- }
- else
- {
- schoolCourses.Add(item);
- schoolIds.Add(item.school);
- }
- }
- }
- List<School> schools = new List<School>();
- if (schoolIds.Count>0)
- {
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<School>($"select value c from c where c.id in ({string.Join(",", schoolIds.Select(z => $"'{z}'"))})", "Base");
- if (result.list.IsNotEmpty())
- {
- schools.AddRange(result.list);
- }
- }
- List<CourseDto> schoolCourseDtos = new List<CourseDto>();
- foreach (var course in schoolCourses)
- {
- var school = schools.Find(z => z.id.Equals(course.school));
- if (school==null) { continue; }
- var period = school.period.Find(z => z.id.Equals(course.period?.id));
- if (period==null)
- {
- continue;
- }
- var subject = period.subjects.Find(z => z.id.Equals(course.subject.id) ||z.name.Equals(course.subject.name));
- if (subject== null)
- {
- subject=new Subject { id =course.subject.id, name=course.subject.name, type=1 };
- period.subjects.Add(subject);
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(school, new PartitionKey(school.code));
- }
- ResponseMessage response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemStreamAsync(course.id, new PartitionKey($"CourseBase-{school.id}"));
- if (response.StatusCode==System.Net.HttpStatusCode.OK)
- {
- CourseBase courseBase = JsonDocument.Parse(response.Content).RootElement.ToObject<CourseBase>();
- if (courseBase!= null)
- {
- for (int grade = 0; grade<period.grades.Count; grade++)
- {
- if (courseBase.grade==grade)
- {
- continue;
- }
- string sqlBaseData = $"select value c from c where c.name ='{course.name}' and c.grade={grade} and c.period.id='{period.id}' and c.subject.id='{subject.id}'";
- var resultBaseData = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<CourseBase>(sqlBaseData, $"CourseBase-{school.id}");
- if (!resultBaseData.list.IsNotEmpty())
- {
- CourseBase courseBaseNew = new CourseBase()
- {
- id= Guid.NewGuid().ToString(),
- code=$"CourseBase-{school.id}",
- pk="CourseBase",
- ttl=-1,
- name=course.name,
- subject= new IdName { id=subject?.id, name=subject?.name },
- period= new IdName { id= course.period?.id, name =course.period?.name },
- school=school.id,
- scope="school",
- creatorId=course.creatorId,
- desc=course.desc,
- status=1,
- grade=grade,
- };
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(courseBaseNew, new PartitionKey(courseBaseNew.code));
- }
- }
- }
- }
- string sqlCourseBase = $"select value c from c where c.name ='{course.name}' and c.period.id='{period.id}' and c.subject.id='{subject.id}' ";
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<CourseBase>(sqlCourseBase, $"CourseBase-{school.id}");
- List<CourseBase> courseBases = result.list.OrderBy(z => z.grade).ToList();
- var subjectIndex = period.subjects.FindIndex(z => z.id.Equals(subject.id));
- if (subjectIndex>=0)
- {
- subjectIndex=subjectIndex+1;
- int index = 1;
- foreach (var couresBase in courseBases)
- {
- string no = $"{subjectIndex}0{index}";
- if (index>=10)
- {
- no = $"{subjectIndex}{index}";
- }
- index++;
- couresBase.no=no;
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(couresBase, new PartitionKey(couresBase.code));
- }
- }
- if (history==1)
- {
- //当前学年
- // var semester = SchoolService.GetSemester(period, DateTimeOffset.Now.ToUnixTimeMilliseconds());
- var semester = SchoolService.GetSemester(period, 1714492800000);//2024-05-01
- List<ScheduleTask> schedules = new List<ScheduleTask>();
- foreach (var schedule in course.schedule)
- {
- ScheduleTask scheduleSave = null;
- int grade = -1;
- if (!string.IsNullOrWhiteSpace(schedule.teacherId) && !string.IsNullOrWhiteSpace(schedule.classId))
- {
- scheduleSave =new ScheduleTask() { type="class", groupId=schedule.classId, teacherId=schedule.teacherId, roomId= schedule.room, school=school.id };
- try
- {
- Class clazz = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<Class>(schedule.classId, new PartitionKey($"Class-{school.id}"));
- if (semester.studyYear- clazz.year>=period.grades.Count)
- {
- grade=-1;
- }
- else
- {
- var gradeData = SchoolService.GetGrades(school, period.id, new List<int>() { clazz.year }, DateTimeOffset.Now.ToUnixTimeMilliseconds());
- if (gradeData.grades.Any() && gradeData.grades.Count>0)
- {
- int.TryParse(gradeData.grades.First(), out grade);
- }
- }
- }
- catch { }
- }
- if (!string.IsNullOrWhiteSpace(schedule.teacherId) && !string.IsNullOrWhiteSpace(schedule.stulist))
- {
- scheduleSave = new ScheduleTask() { type="teach", groupId=schedule.stulist, teacherId=schedule.teacherId, roomId= schedule.room, school=school.id };
- if (grade==-1)
- {
- _coreAPIHttpService.check=false;
- var groups = await GroupListService.GetMemberByListids(_coreAPIHttpService, _azureCosmos.GetCosmosClient(), _dingDing, new List<string> { schedule.stulist }, school.id);
- if (groups.rmembers.IsNotEmpty())
- {
- var a = groups.rmembers.Where(z => z.type==2 && z.year>0).GroupBy(z => z.year).Select(z => new { key = z.Key, list = z.ToList() }).OrderByDescending(y => y.list.Count).FirstOrDefault();
- if (a!=null)
- {
- if (semester.studyYear- a.key>=period.grades.Count)
- {
- grade=-1;
- }
- else
- {
- var gradeData = SchoolService.GetGrades(school, period.id, new List<int>() { a.key }, DateTimeOffset.Now.ToUnixTimeMilliseconds());
- if (gradeData.grades.Any() && gradeData.grades.Count>0)
- {
- int.TryParse(gradeData.grades.First(), out grade);
- }
- }
- }
- }
- }
- }
- if (grade>-1)
- {
- string sqlCourse = $"select value c from c where c.name ='{course.name}' and c.grade={grade} and c.period.id='{period.id}' and c.subject.id='{course.subject?.id}' ";
- var resultCourseBase = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<CourseBase>(sqlCourse, $"CourseBase-{school.id}");
- foreach (var item in resultCourseBase.list)
- {
- if (semester.currSemester!=null)
- {
- string id = $"{semester.studyYear}-{semester.currSemester.id}-{item.id}";
- string code = $"CourseTask-{school.id}";
- ResponseMessage responseTask = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemStreamAsync(id, new PartitionKey(code));
- CourseTask courseTask = null;
- if (responseTask.StatusCode==System.Net.HttpStatusCode.OK)
- {
- courseTask = JsonDocument.Parse(responseTask.Content).RootElement.ToObject<CourseTask>();
- var scheduleExsit = courseTask.schedules.Find(z => z.groupId.Equals(scheduleSave.groupId));
- if (scheduleExsit==null)
- {
- courseTask.schedules.Add(scheduleSave);
- }
- else
- {
- scheduleExsit.teacherId=scheduleSave.teacherId;
- }
- }
- else
- {
- courseTask = new CourseTask
- {
- id = id,
- code = code,
- pk="CourseTask",
- ttl=-1,
- courseId=item.id,
- year= semester.studyYear,
- expire= semester.nextSemester.AddDays(-1).ToUnixTimeMilliseconds(),
- semesterId=semester.currSemester.id,
- schedules=new List<ScheduleTask>() { scheduleSave }
- };
- }
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(courseTask, new PartitionKey(code));
- }
- }
- }
- }
- }
- }
- return Ok(new { schoolCourseDtos });
- }
- catch (Exception ex) { return Ok($"{ex.Message},{ex.StackTrace}"); }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("data-migration")]
- public async Task<IActionResult> DataMigration(JsonElement request)
- {
- try
- {
- List<Course> schoolCourses = new List<Course>();
- HashSet<string> schoolIds = new HashSet<string>();
- string sql = "select value c from c where c.pk='Course' ";
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIteratorSql<Course>(queryText: sql))
- {
- if (!string.IsNullOrWhiteSpace(item.school))
- {
- List<string> ignores = new List<string>();
- if (request.TryGetProperty("ignores", out JsonElement _ignores))
- {
- ignores= _ignores.ToObject<List<string>>();
- }
- if (item.school.Equals("ydzt") ||item.school.Equals("xcfx") ||item.school.Equals("hlgj") ||item.school.Equals("hbcn")
- ||item.school.Equals("kjyxx") ||item.school.Equals("bjgsex") || item.school.Equals("cdxxps") ||item.school.Equals("pclxxx"))
- {
- //大陆站强制不处理的学校
- }
- else
- {
- if (!ignores.Contains(item.school))
- {
- schoolCourses.Add(item);
- schoolIds.Add(item.school);
- }
- }
- }
- }
- List<School> schools = new List<School>();
- if (schoolIds.Count>0)
- {
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<School>($"select value c from c where c.id in ({string.Join(",", schoolIds.Select(z => $"'{z}'"))})", "Base");
- if (result.list.IsNotEmpty())
- {
- schools.AddRange(result.list);
- }
- }
- List<CourseDto> schoolCourseDtos = new List<CourseDto>();
- foreach (var course in schoolCourses)
- {
- var school = schools.Find(z => z.id.Equals(course.school));
- if (school==null) { continue; }
- var period = school.period.Find(z => z.id.Equals(course.period?.id));
- if (period==null)
- {
- continue;
- }
- var semester = SchoolService.GetSemester(period, time: "2023-07-01");
- CourseBase courseBase = null;
- ResponseMessage response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemStreamAsync(course.id, new PartitionKey($"CourseBase-{school.id}"));
- if (response.StatusCode==System.Net.HttpStatusCode.OK)
- {
- courseBase = JsonDocument.Parse(response.Content).RootElement.ToObject<CourseBase>();
- }
- else
- {
- courseBase = new CourseBase()
- {
- id= course.id,
- code=$"CourseBase-{school.id}",
- pk="CourseBase",
- ttl=-1,
- name=course.name,
- subject= new IdName { id=course.subject?.id, name=course.subject?.name },
- period= new IdName { id= course.period?.id, name =course.period?.name },
- school=school.id,
- scope="school",
- no=course.no,
- creatorId=course.creatorId,
- desc=course.desc,
- status=1,
- grade=0,
- };
- }
- List<ScheduleTask> schedules = new List<ScheduleTask>();
- int grade = -1;
- foreach (var schedule in course.schedule)
- {
- if (!string.IsNullOrWhiteSpace(schedule.teacherId) && !string.IsNullOrWhiteSpace(schedule.classId))
- {
- schedules.Add(new ScheduleTask() { type="class", groupId=schedule.classId, teacherId=schedule.teacherId, roomId= schedule.room, school=school.id });
- try
- {
- Class clazz = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<Class>(schedule.classId, new PartitionKey($"Class-{school.id}"));
- var gradeData = SchoolService.GetGrades(school, period.id, new List<int>() { clazz.year }, 1688140800000);
- if (gradeData.grades.Any() && gradeData.grades.Count>0)
- {
- int.TryParse(gradeData.grades.First(), out grade);
- }
- }
- catch { }
- }
- if (!string.IsNullOrWhiteSpace(schedule.teacherId) && !string.IsNullOrWhiteSpace(schedule.stulist))
- {
- schedules.Add(new ScheduleTask() { type="teach", groupId=schedule.stulist, teacherId=schedule.teacherId, roomId= schedule.room, school=school.id });
- if (grade==-1)
- {
- _coreAPIHttpService.check=false;
- var groups = await GroupListService.GetMemberByListids(_coreAPIHttpService, _azureCosmos.GetCosmosClient(), _dingDing, new List<string> { schedule.stulist }, school.id);
- if (groups.rmembers.IsNotEmpty())
- {
- var a = groups.rmembers.Where(z => z.type==2 && z.year>0).GroupBy(z => z.year).Select(z => new { key = z.Key, list = z.ToList() }).OrderByDescending(y => y.list.Count).FirstOrDefault();
- if (a!=null)
- {
- var gradeData = SchoolService.GetGrades(school, period.id, new List<int>() { a.key }, 1688140800000);
- if (gradeData.grades.Any() && gradeData.grades.Count>0)
- {
- int.TryParse(gradeData.grades.First(), out grade);
- }
- }
- }
- }
- }
- }
- if (grade>-1)
- {
- courseBase.grade=grade;
- }
- //补全丢失的科目信息
- var subject = period.subjects.Find(z => z.id.Equals(courseBase.subject.id) ||z.name.Equals(courseBase.subject.name));
- if (subject== null)
- {
- period.subjects.Add(new Subject { id =courseBase.subject.id, name=courseBase.subject.name, type=1 });
- }
- int subjectIndex = period.subjects.FindIndex(z => z.id.Equals(courseBase.subject.id) ||z.name.Equals(courseBase.subject.name));
- if (subjectIndex>=0)
- {
- int index = subjectIndex+1;
- courseBase.no= $"{index}01";
- }
- CourseDto courseDto = new CourseDto();
- courseDto.courseBase=courseBase;
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(courseBase, new PartitionKey(courseBase.code));
- if (schedules.IsNotEmpty())
- {
- if (semester.currSemester!=null)
- {
- string id = $"{semester.studyYear}-{semester.currSemester.id}-{courseBase.id}";
- string code = $"CourseTask-{school.id}";
- CourseTask courseTask = new CourseTask
- {
- id = id,
- code = code,
- pk="CourseTask",
- ttl=-1,
- courseId=courseBase.id,
- year= semester.studyYear,
- expire= semester.nextSemester.AddDays(-1).ToUnixTimeMilliseconds(),
- semesterId=semester.currSemester.id,
- schedules=schedules
- };
- courseDto.courseTasks.Add(new CourseTaskDto { courseTask=courseTask, type="school" });
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(courseTask, new PartitionKey(code));
- }
- }
- schoolCourseDtos.Add(courseDto);
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(school, new PartitionKey(school.code));
- }
- List<CourseDto> teacherCourseDtos = new List<CourseDto>();
- if (request.TryGetProperty("private", out JsonElement _private) && $"{_private}".Equals("1"))
- {
- List<Course> teacherCourses = new List<Course>();
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).GetItemQueryIteratorSql<Course>(sql))
- {
- teacherCourses.Add(item);
- }
- foreach (var course in teacherCourses)
- {
- CourseBase courseBase = new CourseBase()
- {
- id= course.id,
- code=$"CourseBase",
- pk="CourseBase",
- ttl=-1,
- name=course.name,
- scope="private",
- no=course.no,
- creatorId=course.creatorId,
- desc=course.desc,
- status=1,
- };
- List<ScheduleTask> schedules = new List<ScheduleTask>();
- foreach (var schedule in course.schedule)
- {
- if (!string.IsNullOrWhiteSpace(schedule.teacherId) && !string.IsNullOrWhiteSpace(schedule.classId))
- {
- schedules.Add(new ScheduleTask() { type="class", groupId=schedule.classId, teacherId=schedule.teacherId, roomId= schedule.room });
- }
- if (!string.IsNullOrWhiteSpace(schedule.teacherId) && !string.IsNullOrWhiteSpace(schedule.stulist))
- {
- schedules.Add(new ScheduleTask() { type="teach", groupId=schedule.stulist, teacherId=schedule.teacherId, roomId= schedule.room });
- }
- }
- CourseDto courseDto = new CourseDto();
- courseDto.courseBase=courseBase;
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).UpsertItemAsync(courseBase, new PartitionKey(courseBase.code));
- if (schedules.IsNotEmpty())
- {
- string id = $"{courseBase.id}";
- string code = $"CourseTask";
- CourseTask courseTask = new CourseTask
- {
- id = id,
- code = code,
- pk="CourseTask",
- ttl=-1,
- courseId=courseBase.id,
- schedules=schedules
- };
- courseDto.courseTasks.Add(new CourseTaskDto { courseTask=courseTask, type="private" });
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).UpsertItemAsync(courseTask, new PartitionKey(code));
- }
- teacherCourseDtos.Add(courseDto);
- }
- }
- return Ok(new { schoolCourseDtos, teacherCourseDtos });
- }
- catch (Exception ex) { return Ok($"{ex.Message},{ex.StackTrace}"); }
- }
- [ProducesDefaultResponseType]
- [HttpPost("restore-video-record")]
- public async Task<IActionResult> RestoreVideoRecord(JsonElement json)
- {
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- List<School> schools = new List<School>(0);
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<School>(queryText: "SELECT value(c) FROM c", requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
- {
- schools.Add(item);
- }
- List<SchoolTeacher> tmdids = new List<SchoolTeacher>();
- foreach (var school in schools)
- {
- if (!string.IsNullOrEmpty(school.standard))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<SchoolTeacher>(queryText: "SELECT value(c) FROM c",
- requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Teacher-{school.id}") }))
- {
- tmdids.Add(item);
- }
- }
- }
- List<(SchoolTeacher Teacher, List<AbilitySub> abilitySubs)> ps = new List<(SchoolTeacher Teacher, List<AbilitySub> abilitySubs)>();
- await foreach ((SchoolTeacher tmdid, List<AbilitySub> subs) a in GetSubsAsyn(tmdids, client))
- {
- if (a.subs.IsNotEmpty())
- {
- ps.Add(a);
- }
- }
- List<dynamic> datas = new List<dynamic>();
- List<TeacherFile> teacherFiles = new List<TeacherFile>();
- foreach (var p in ps)
- {
- Dictionary<string, FileRecord> dict = new Dictionary<string, FileRecord>();
- School school = schools.Find(x => p.Teacher.code.Replace("Teacher-", "").Equals(x.id));
- if (school != null && !string.IsNullOrEmpty(school.standard))
- {
- var rcds = p.abilitySubs.SelectMany(x => x.videoRcds);
- foreach (var c in rcds)
- {
- try
- {
- AbilityTask abilityTask = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AbilityTask>(c.abilityTaskId, new PartitionKey($"AbilityTask-{school.standard}"));
- Rnode rnode = null;
- Tnode tnode = null;
- if (!string.IsNullOrEmpty(c.tnodeId))
- {
- foreach (var node in abilityTask.children)
- {
- rnode = node.rnodes.Find(y => y.link.Equals(c.url));
- if (rnode != null)
- {
- tnode = node;
- break;
- }
- }
- }
- else
- {
- tnode = abilityTask.children.Find(x => x.id.Equals(c.tnodeId));
- if (tnode == null)
- {
- foreach (var node in abilityTask.children)
- {
- rnode = node.rnodes.Find(y => y.link.Equals(c.url));
- if (rnode != null)
- {
- tnode = node;
- break;
- }
- }
- }
- else
- {
- rnode = tnode.rnodes.Find(y => y.link.Equals(c.url));
- }
- }
- if (rnode != null)
- {
- if (dict.ContainsKey(rnode.hash))
- {
- FileRecord fileRecord = dict[rnode.hash];
- if (!c.done)
- {
- if (fileRecord.type.Equals("video") && c.time * 60 > fileRecord.view)
- {
- fileRecord.view = (long)c.time * 60;
- }
- var file = fileRecord.files.Find(x => x.abilityId.Equals(abilityTask.abilityId)
- && x.taskId.Equals(abilityTask.id) && x.nodeId.Equals(tnode.id) && x.url.Equals(rnode.link));
- if (file == null)
- {
- fileRecord.files.Add(new FileAbility()
- {
- abilityId = abilityTask.abilityId,
- taskId = abilityTask.id,
- nodeId = tnode.id,
- url = rnode.link
- });
- }
- }
- else
- {
- var file = fileRecord.files.Find(x => x.abilityId.Equals(abilityTask.abilityId)
- && x.taskId.Equals(abilityTask.id) && x.nodeId.Equals(tnode.id) && x.url.Equals(rnode.link));
- if (file == null)
- {
- fileRecord.files.Add(new FileAbility()
- {
- abilityId = abilityTask.abilityId,
- taskId = abilityTask.id,
- nodeId = tnode.id,
- url = rnode.link
- });
- }
- fileRecord.done = true;
- fileRecord.view = (long)fileRecord.duration;
- }
- }
- else
- {
- FileRecord teacherFileRcd = new FileRecord();
- FileAbility fileAbility = new FileAbility()
- {
- abilityId = abilityTask.abilityId,
- taskId = abilityTask.id,
- nodeId = tnode.id,
- url = rnode.link
- };
- teacherFileRcd.files.Add(fileAbility);
- teacherFileRcd.hash = rnode.hash;
- teacherFileRcd.duration = rnode.duration;
- teacherFileRcd.type = rnode.type;
- teacherFileRcd.size = rnode.size != null ? rnode.size.Value : 0;
- teacherFileRcd.done = c.done;
- if (teacherFileRcd.done)
- {
- teacherFileRcd.view = (long)teacherFileRcd.duration;
- }
- else
- {
- teacherFileRcd.view = (long)(c.time * 60);
- }
- dict.Add(rnode.hash, teacherFileRcd);
- }
- }
- }
- catch (CosmosException ex)
- {
- Console.WriteLine(c.abilityTaskId);
- }
- }
- TeacherFile teacherFile = null;
- try
- {
- teacherFile = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<TeacherFile>(p.Teacher.id, new PartitionKey($"TeacherFile-{school.id}"));
- foreach (var ds in dict)
- {
- var file = teacherFile.fileRecords.Find(x => x.hash.Equals(ds.Key));
- if (file != null)
- {
- if (!file.done)
- {
- if (file.type.Equals("video") && file.view < ds.Value.view)
- {
- file.view = ds.Value.view;
- }
- }
- var nofiles = ds.Value.files.FindAll(x =>
- !file.files.Exists(y => x.abilityId.Equals(y.abilityId)
- && x.taskId.Equals(y.taskId)
- && x.nodeId.Equals(y.nodeId)
- && x.url.Equals(y.url)
- ));
- if (nofiles.IsNotEmpty())
- {
- file.files.AddRange(ds.Value.files);
- }
- }
- else
- {
- teacherFile.fileRecords.Add(ds.Value);
- }
- }
- teacherFile = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<TeacherFile>(teacherFile, teacherFile.id, new PartitionKey($"TeacherFile-{school.id}"));
- teacherFiles.Add(teacherFile);
- }
- catch (CosmosException ex)
- {
- teacherFile = new TeacherFile
- {
- id = p.Teacher.id,
- code = $"TeacherFile-{school.id}",
- fileRecords = dict.Values.ToList(),
- pk = "TeacherFile"
- };
- teacherFiles.Add(teacherFile);
- teacherFile = await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<TeacherFile>(teacherFile, new PartitionKey($"TeacherFile-{school.id}"));
- }
- datas.Add(new { teacher = p.Teacher, school = school, dict });
- }
- }
- return Ok(new { datas, teacherFiles });
- }
- catch (Exception ex)
- {
- }
- return Ok();
- }
- private async IAsyncEnumerable<(SchoolTeacher, List<AbilitySub>)> GetSubsAsyn(List<SchoolTeacher> tmdids, CosmosClient client)
- {
- foreach (var tmdid in tmdids)
- {
- List<AbilitySub> abilitySubs = new List<AbilitySub>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIteratorSql<AbilitySub>(queryText: "SELECT value(c) FROM c",
- requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"AbilitySub-{tmdid.code.Replace("Teacher-", "")}-{tmdid.id}") }))
- {
- abilitySubs.Add(item);
- }
- var abilitys = abilitySubs.FindAll(x => x.videoRcds.IsNotEmpty());
- yield return (tmdid, abilitys);
- }
- }
- /// <summary>
- ///迁移Stulist数据至GroupList
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpGet("restore-stulist")]
- public async Task<IActionResult> RestoreStulist()
- {
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- List<School> schools = new List<School>(0);
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<School>(queryText: "SELECT value(c) FROM c", requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
- //{
- // schools.Add(item);
- //}
- /*
- foreach (var school in schools)
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<StuList>(queryText: "SELECT value(c) FROM c", requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StuList-{school.id}") }))
- {
- List<Member> members = new List<Member>();
- if (item.students.IsNotEmpty())
- {
- item.students.ForEach(x =>
- {
- members.Add(new Member { id = x.id, code = x.code.Replace("Base-", ""), type = 2, });
- });
- }
- if (item.tmids.IsNotEmpty())
- {
- item.tmids.ForEach(x =>
- {
- members.Add(new Member { id = x, type = 1 });
- });
- }
- GroupList group = new GroupList
- {
- year = DateTimeOffset.UtcNow.Year,
- pk = "GroupList",
- id = item.id,
- code = $"GroupList-{school.id}",
- name = item.name,
- ttl = -1,
- no = item.no,
- periodId = string.IsNullOrEmpty(item.periodId) ? school.period[0]?.id : item.periodId,
- scope = "school",
- creatorId = item.creatorId,
- type = "teach",
- members = members,
- tcount = members.FindAll(x => x.type == 1).Count,
- scount = members.FindAll(x => x.type == 2).Count,
- school = school.id
- };
- await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync(group, new PartitionKey(group.code));
- }
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<TchList>(queryText: "SELECT value(c) FROM c", requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"TchList-{school.id}") }))
- {
- List<Member> members = new List<Member>();
- if (item.teachers.IsNotEmpty())
- {
- item.teachers.ForEach(x =>
- {
- members.Add(new Member { id = x, type = 1 });
- });
- }
- GroupList group = new GroupList
- {
- year = DateTimeOffset.UtcNow.Year,
- pk = "GroupList",
- id = item.id,
- code = $"GroupList-{school.id}",
- name = item.name,
- ttl = -1,
- no = item.no,
- scope = "school",
- creatorId = item.creatorId,
- type = "research",
- members = members,
- tcount = members.FindAll(x => x.type == 1).Count,
- scount = members.FindAll(x => x.type == 2).Count,
- school = school.id
- };
- await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync(group, new PartitionKey(group.code));
- }
- }*/
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIteratorSql<StuList>(queryText: "SELECT value(c) FROM c", requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StuList") }))
- {
- List<Member> members = new List<Member>();
- if (item.students.IsNotEmpty())
- {
- item.students.ForEach(x => {
- members.Add(new Member { id = x.id, code = x.code.Replace("Base-", ""), type = 2 });
- });
- }
- if (item.tmids.IsNotEmpty())
- {
- item.tmids.ForEach(x => {
- members.Add(new Member { id = x, type = 1 });
- });
- }
- GroupList group = new GroupList
- {
- expire=0,
- year = DateTimeOffset.UtcNow.Year,
- pk = "GroupList",
- id = item.id,
- code = $"GroupList",
- name = item.name,
- ttl = -1,
- no = item.no,
- scope = "private",
- creatorId = item.creatorId,
- type = "teach",
- tcount = members.FindAll(x => x.type == 1).Count,
- scount = members.FindAll(x => x.type == 2).Count,
- members = members,
- };
- await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync(group, new PartitionKey(group.code));
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"OS,restore-stulist()\n{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
- }
- return Ok();
- }
-
- /// <summary>
- /// 迁移教师基础信息,并处理历史数据。
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpGet("restore-tmduser")]
- public async Task<IActionResult> RestoreTmdUser()
- {
- var client = _azureCosmos.GetCosmosClient();
- List<TmdUser> users = new List<TmdUser>();
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIteratorSql<Teacher>(queryText:"select value c from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
- {
- ///修复历史数据
- if (item.size > 0)
- {
- if (item.pk == null)
- {
- item.pk = "Teacher";
- await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(item, item.id, partitionKey: new PartitionKey("Base"));
- }
- try
- {
- TmdUser user = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<TmdUser>(item.id, partitionKey: new PartitionKey("Base"));
- item.schools.ForEach(x => {
- if (user.schools.Find(y => y.schoolId.Equals(x.schoolId)) == null)
- {
- user.schools.Add(new TmdUser.School { name = x.name, schoolId = x.schoolId, time = x.time, status = x.status });
- }
- });
- users.Add(user);
- await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<TmdUser>(user, user.id, partitionKey: new PartitionKey("Base"));
- }
- catch (CosmosException ex)
- {
- if (ex.StatusCode == HttpStatusCode.NotFound)
- {
- TmdUser user = item.ToJsonString().ToObject<TmdUser>();
- await client.GetContainer(Constant.TEAMModelOS, "Student").CreateItemAsync<TmdUser>(user, partitionKey: new PartitionKey("Base"));
- users.Add(user);
- }
- }
- }
- else
- {
- await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemAsync<Teacher>(item.id, partitionKey: new PartitionKey("Base"));
- }
- }
- return Ok(new { users });
- }
- }
- }
|