|
@@ -1,253 +0,0 @@
|
|
|
-using System;
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.Linq;
|
|
|
-using System.Threading.Tasks;
|
|
|
-using Microsoft.AspNetCore.Http;
|
|
|
-using Microsoft.AspNetCore.Mvc;
|
|
|
-using TEAMModelOS.SDK.Context.Exception;
|
|
|
-using TEAMModelOS.SDK;
|
|
|
-using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
|
|
|
-using TEAMModelOS.SDK.Helper.Common.ValidateHelper;
|
|
|
-using TEAMModelOS.SDK.DI;
|
|
|
-using TEAMModelOS.Models;
|
|
|
-using System.Text.Json;
|
|
|
-using TEAMModelOS.SDK.Models;
|
|
|
-using Azure.Cosmos;
|
|
|
-using TEAMModelOS.SDK.Extension;
|
|
|
-
|
|
|
-namespace TEAMModelOS.Controllers
|
|
|
-{
|
|
|
- [ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
- //[Authorize(Roles = "IES5")]
|
|
|
- [Route("student/class")]
|
|
|
- [ApiController]
|
|
|
- public class ClassStudentController : ControllerBase
|
|
|
- {
|
|
|
-
|
|
|
- private AzureCosmosFactory _azureCosmos;
|
|
|
-
|
|
|
- public ClassStudentController(AzureCosmosFactory azureCosmos)
|
|
|
- {
|
|
|
- _azureCosmos = azureCosmos;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 获取实时的学生-班级关联信息
|
|
|
- /// </summary>
|
|
|
- /// <param name="request"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [ProducesDefaultResponseType]
|
|
|
- //[AuthToken(Roles = "Teacher")]
|
|
|
- [HttpPost("find")]
|
|
|
- public async Task<IActionResult> find(JsonElement request) {
|
|
|
- //ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- // 班级编码
|
|
|
- /* if (request.TryGetProperty("classroomCode", out JsonElement classroomCode)
|
|
|
- && request.TryGetProperty("schoolCode", out JsonElement schoolCode)
|
|
|
- )
|
|
|
- {*/
|
|
|
- if (!request.TryGetProperty("classroomCode", out JsonElement classroomCode)) return BadRequest();
|
|
|
- if (!request.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
|
|
|
- //List<ClassStudent> classroomStudents = await _azureCosmos.FindByDict<ClassStudent>(new Dictionary<string, object> { { "id", classroomCode } });
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
- List<object> classrooms = new List<object>();
|
|
|
- string info = "";
|
|
|
- for (int i = 0; i < classroomCode.GetArrayLength(); i++)
|
|
|
- {
|
|
|
- //ids.Add(id[i].ToJsonString());
|
|
|
- info += classroomCode[i].ToJsonString() + ",";
|
|
|
- }
|
|
|
- var query = $"select c.name, c.students from c where c.id in ({info[0..^1]}) ";
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Classroom-{school_code}") }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
- {
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- {
|
|
|
- classrooms.Add(obj.ToObject<object>());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //List<StudentSimple> students = new List<StudentSimple>();
|
|
|
- /* classrooms.ForEach(c =>
|
|
|
- {
|
|
|
-
|
|
|
- c.students.ForEach(s =>
|
|
|
- {
|
|
|
- StudentSimple student = new StudentSimple
|
|
|
- {
|
|
|
- id = s.id,
|
|
|
- name = s.name,
|
|
|
- no = s.no
|
|
|
- };
|
|
|
- students.Add(student);
|
|
|
- });
|
|
|
- //students = c.students;
|
|
|
- });*/
|
|
|
-
|
|
|
-
|
|
|
- return Ok(new { classrooms });
|
|
|
- /*List<Student> students = await _azureCosmos.FindByDict<Student>(
|
|
|
- new Dictionary<string, object>() { { "classroomCode", classroomCode }, { "code", schoolCode } },
|
|
|
- new List<string> { "id", "name", "code", "seatNo", "studentId", "classroomCode" });
|
|
|
- List<dynamic> stus = new List<dynamic>();
|
|
|
- List<ClassStudent> newClassStudents = new List<ClassStudent>();
|
|
|
- ///新增的学生
|
|
|
- if (students.IsNotEmpty())
|
|
|
- {
|
|
|
- students.ForEach(x =>
|
|
|
- {
|
|
|
- if (!classroomStudents.Select(m => m.code).Contains(x.studentId))
|
|
|
- {
|
|
|
- newClassStudents.Add(new ClassStudent() { id = x.classroomCode, code = x.studentId });
|
|
|
- }
|
|
|
- var stu = new { x.id, x.name, x.code, x.seatNo, x.studentId, x.classroomCode };
|
|
|
- stus.Add(stu);
|
|
|
- });
|
|
|
- }
|
|
|
- ///反射已存在的id信息
|
|
|
- if (classroomStudents.IsNotEmpty())
|
|
|
- {
|
|
|
- ///处理冗余的id查询
|
|
|
- List<string> ids = new List<string>();
|
|
|
- if (students.IsNotEmpty()) {
|
|
|
- classroomStudents.ForEach(x =>
|
|
|
- {
|
|
|
- if (!students.Select(m => m.studentId).Contains(x.code))
|
|
|
- {
|
|
|
- ids.Add(x.code);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- var sts = await _azureCosmos.FindByDict<Student>(new Dictionary<string, object>() { { "studentId", ids.ToArray() } });
|
|
|
- if (sts.IsNotEmpty())
|
|
|
- {
|
|
|
- sts.ForEach(x =>
|
|
|
- {
|
|
|
- var stu = new { x.id, x.name, x.code, x.seatNo, x.studentId, x.classroomCode };
|
|
|
- stus.Add(stu);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- //保存新增学生
|
|
|
- if (newClassStudents.IsNotEmpty())
|
|
|
- {
|
|
|
- classroomStudents.AddRange(await _azureCosmos.SaveOrUpdateAll(newClassStudents));
|
|
|
- }
|
|
|
- return builder.Data(classroomStudents).Extend(new Dictionary<string, object> { { "students", stus }, { "count", stus.Count } }).build();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常classroomCode,code!").build();
|
|
|
- }*/
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 保存或修改教室学生关联
|
|
|
- /// </summary>
|
|
|
- /// <param name="request"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [ProducesDefaultResponseType]
|
|
|
- //[AuthToken(Roles = "Teacher")]
|
|
|
- [HttpPost("upsert")]
|
|
|
- public async Task<BaseResponse> Upsert(List<ClassStudent> request)
|
|
|
- {
|
|
|
- ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- List<ClassStudent> students = await _azureCosmos.SaveOrUpdateAll(request);
|
|
|
- builder.Data(students);
|
|
|
- return builder.build();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- ///// <summary>
|
|
|
- ///// 学生加入教室
|
|
|
- ///// </summary>
|
|
|
- ///// <param name="request"></param>
|
|
|
- ///// <returns></returns>
|
|
|
- //[HttpPost("addStudent")]
|
|
|
- //public async Task<BaseResponse> addStudent(JosnRPCRequest<List<ClassStudent>> request)
|
|
|
- //{
|
|
|
- // JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
|
|
|
-
|
|
|
- // List<ClassStudent> students = await _cosmos.SaveOrUpdateAll(request.@params);
|
|
|
- // builder.Data(students);
|
|
|
- // return builder.build();
|
|
|
- //}
|
|
|
- /// <summary>
|
|
|
- /// 学生退出教室,只能退出非原生班级的教室
|
|
|
- /// </summary>
|
|
|
- /// <param name="request"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [ProducesDefaultResponseType]
|
|
|
- //[AuthToken(Roles = "Teacher")]
|
|
|
- [HttpPost("exit")]
|
|
|
- public async Task<BaseResponse> Exit(List<ClassStudent> request)
|
|
|
- {
|
|
|
- ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- if (ValidateHelper.IsValid(request) && request.Count>0)
|
|
|
- {
|
|
|
- List<ClassStudent> rm = new List<ClassStudent>();
|
|
|
- List<OldStudent> students = await _azureCosmos.FindByDict<OldStudent>(new Dictionary<string, object> { { "studentId", request.GroupBy(x => x.code).ToList().Select(x => x.Key).ToArray() } });
|
|
|
- students.ForEach(x => {
|
|
|
- if (!string.IsNullOrEmpty(x.classroomCode)) {
|
|
|
- request.ForEach(m =>
|
|
|
- {
|
|
|
- if (x.classroomCode.Equals(m.id) && x.studentId.Equals(m.code)) {
|
|
|
- rm.Add(m);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
- rm.ForEach(x=> { request.Remove(x); });
|
|
|
- List<IdPk> idPks = await _azureCosmos.DeleteAll<ClassStudent>(request.Select(x=> new IdPk { id=x.id,pk=x.code}).ToList());
|
|
|
- builder.Data(idPks);
|
|
|
- }
|
|
|
- return builder.build();
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 查询教室学生关联 根据id
|
|
|
- /// </summary>
|
|
|
- /// <param name="request"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [ProducesDefaultResponseType]
|
|
|
- //[AuthToken(Roles = "Teacher")]
|
|
|
- [HttpPost("find-by-id")]
|
|
|
- public async Task<BaseResponse> FindById(IdPk request)
|
|
|
- {
|
|
|
- ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- if (ValidateHelper.IsValid(request))
|
|
|
- {
|
|
|
- ClassStudent classStudent = await _azureCosmos.FindByIdPk<ClassStudent>(request.id,request.pk);
|
|
|
- List<OldStudent> students = await _azureCosmos.FindByDict<OldStudent>(new Dictionary<string, object> { { "studentId",request.pk } }, new List<string> { "id", "name", "code", "seatNo", "studentId", "classroomCode" });
|
|
|
- builder.Data(classStudent).Extend(new Dictionary<string, object> { { "student",students.IsNotEmpty()&& classStudent !=null? new { students[0].id, students[0].name, students[0].code, students[0].seatNo, students[0].studentId, students[0].classroomCode }:null } });
|
|
|
- }
|
|
|
- return builder.build();
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// 查询学生在哪些教室
|
|
|
- /// </summary>
|
|
|
- /// <param name="studentId"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [ProducesDefaultResponseType]
|
|
|
- //[AuthToken(Roles = "Teacher")]
|
|
|
- [HttpPost("find-by-stu")]
|
|
|
- public async Task<BaseResponse> FindByStu(JsonElement request)
|
|
|
- {
|
|
|
- ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- if (request.TryGetProperty("code", out JsonElement studentId))
|
|
|
- {
|
|
|
- List<ClassStudent> sc = await _azureCosmos.FindByDict<ClassStudent>(new Dictionary<string, object> { { "code",studentId.ToString()} });
|
|
|
- List<Class> classrooms = await _azureCosmos.FindByDict<Class>(new Dictionary<string, object> { { "classroomCode", sc.GroupBy(x => x.id).Select(x => x.Key).ToArray() } });
|
|
|
- builder.Data(classrooms).Extend(new Dictionary<string, object> { { "count", classrooms.Count } });
|
|
|
- }
|
|
|
- return builder.build();
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-}
|