123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- 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.Extension.DataResult.JsonRpcRequest;
- using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- using TEAMModelOS.SDK.Helper.Common.ValidateHelper;
- using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
- using TEAMModelOS.Service.Models;
-
- namespace TEAMModelOS.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class ClassStudentController : BaseController
- {
- private IAzureCosmosDBV3Repository _cosmos;
- public ClassStudentController(IAzureCosmosDBV3Repository cosmos)
- {
- _cosmos = cosmos;
- }
- /// <summary>
- /// 获取实时的学生-班级关联信息
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("find")]
- public async Task<BaseResponse> find(JosnRequest<Dictionary<string,object>> request) {
- ResponseBuilder builder = ResponseBuilder.custom();
- // 班级编码
- if (request.@params.TryGetValue("classroomCode", out object classroomCode)
- ///学校编码
- && request.@params.TryGetValue("schoolCode", out object schoolCode)
- )
- {
- List<ClassStudent> classroomStudents = await _cosmos.FindByDict<ClassStudent>(new Dictionary<string, object> { { "id", classroomCode } });
- List<Student> students = await _cosmos.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>();
- classroomStudents.ForEach(x =>
- {
- if (!students.Select(m => m.studentId).Contains(x.code))
- {
- ids.Add(x.code);
- }
- });
- var sts = await _cosmos.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 _cosmos.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>
- [HttpPost("upsert")]
- public async Task<BaseResponse> Upsert(JosnRequest<List<ClassStudent>> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- List<ClassStudent> students = await _cosmos.SaveOrUpdateAll(request.@params);
- builder.Data(students);
- return builder.build();
- }
- ///// <summary>
- ///// 学生加入教室
- ///// </summary>
- ///// <param name="request"></param>
- ///// <returns></returns>
- //[HttpPost("addStudent")]
- //public async Task<BaseJosnRPCResponse> 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>
- [HttpPost("exit")]
- public async Task<BaseResponse> Exit(JosnRequest<List<ClassStudent>> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- if (ValidateHelper.IsValid(request.@params) && request.@params.Count>0)
- {
- List<ClassStudent> rm = new List<ClassStudent>();
- List<Student> students = await _cosmos.FindByDict<Student>(new Dictionary<string, object> { { "studentId", request.@params.GroupBy(x => x.code).ToList().Select(x => x.Key).ToArray() } });
- students.ForEach(x => {
- if (!string.IsNullOrEmpty(x.classroomCode)) {
- request.@params.ForEach(m =>
- {
- if (x.classroomCode.Equals(m.id) && x.studentId.Equals(m.code)) {
- rm.Add(m);
- }
- });
- }
- });
- rm.ForEach(x=> { request.@params.Remove(x); });
- List<IdPk> idPks = await _cosmos.DeleteAll<ClassStudent>(request.@params.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>
- [HttpPost("findById")]
- public async Task<BaseResponse> FindById(JosnRequest<IdPk> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- if (ValidateHelper.IsValid(request.@params))
- {
- ClassStudent classStudent = await _cosmos.FindByIdPk<ClassStudent>(request.@params.id,request.@params.pk);
- List<Student> students = await _cosmos.FindByDict<Student>(new Dictionary<string, object> { { "studentId",request.@params.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>
- [HttpPost("findByStu")]
- public async Task<BaseResponse> FindByStu(JosnRequest<string> studentId)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- if (!string.IsNullOrEmpty(studentId.@params))
- {
- List<ClassStudent> sc = await _cosmos.FindByDict<ClassStudent>(new Dictionary<string, object> { { "code",studentId.@params} });
- List<Classroom> classrooms = await _cosmos.FindByDict<Classroom>(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();
- }
- }
- }
|