123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- using TEAMModelOS.SDK.Helper.Security.TmdCrypt;
- using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
- using TEAMModelOS.SDK.Helper.Common.ValidateHelper;
- using TEAMModelOS.SDK.Context.Exception;
- using TEAMModelOS.Service.Models;
- namespace TEAMModelOS.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- // [Authorize]
- public class StudentController : BaseController
- {
- private readonly IAzureCosmosDBV3Repository azureCosmosDBRepository;
- public StudentController(IAzureCosmosDBV3Repository _azureCosmosDBRepository)
- {
- azureCosmosDBRepository = _azureCosmosDBRepository;
- }
- /// <summary>
- /// 保存或更新学生,并维护学生关系表
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("upsert")]
- public async Task<BaseResponse> Upsert(JosnRequest<Student> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- //设置密码 isSet 是否加密 如果加密则不会再次加密
- if (!request.@params.password.isSet)
- {
- request.@params.password.value = TmdCrypt.Encrypt(request.@params.password.value);
- request.@params.password.isSet = true;
- }
- request.@params.id = request.@params.studentId.Replace("#", "-");
- ///假如更新了班级则先获取更新之前的班级
- var olStudent= await azureCosmosDBRepository.FindByIdPk<Student>(request.@params.id,request.@params.code);
- if (olStudent!=null && !string.IsNullOrEmpty(olStudent.classroomCode) && ! olStudent.classroomCode.Equals(request.@params.classroomCode) ) {
- //移除之前的原生班级
- IdPk idPk=await azureCosmosDBRepository.DeleteAsync<ClassStudent>( olStudent.classroomCode ,olStudent.studentId);
-
- }
- ///新建最新的班级关系表
- ClassStudent classroomStudent = new ClassStudent { id = request.@params.classroomCode, code = request.@params.studentId };
- await azureCosmosDBRepository.SaveOrUpdate(classroomStudent);
- Student data = await azureCosmosDBRepository.SaveOrUpdate<Student>(request.@params);
- return builder.Data(data).build();
- }
- /// <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("code", out object _))
- {
- List<Student> data = await azureCosmosDBRepository.FindByDict<Student>(request.@params);
- return builder.Data(data).build();
- }
- else
- {
- return builder.Error(ResponseCode.PARAMS_ERROR, "code is null !").build();
-
- }
- }
- [HttpPost("upsertAll")]
- public async Task<BaseResponse> UpsertAll(JosnRequest<List<Student>> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- if (ValidateHelper.IsValid(request.@params) && request.@params.IsNotEmpty()) {
- ///假如更新了班级则先获取更新之前的班级
- string[] ids = request.@params.Select(x=>x.studentId).ToArray();
- List<Student> oldStudent = await azureCosmosDBRepository.FindByDict<Student>(new Dictionary<string, object>() { { "studentId", ids } });
- List<IdPk> idPks = new List<IdPk>();
- ///处理未变动的班级关系
- List<IdPk> unpk = new List<IdPk>();
- oldStudent.ForEach(x=> {
- request.@params.ForEach(m => {
- if (x.studentId.Equals(m.studentId)) {
- if (!x.classroomCode.Equals(m.classroomCode))
- {
- idPks.Add(new IdPk { id = x.classroomCode, pk = x.studentId });
- }
- else {
- unpk.Add(new IdPk { id = x.classroomCode, pk = x.studentId });
- }
- }
- });
- });
- if (idPks.IsNotEmpty()) {
- await azureCosmosDBRepository.DeleteAll<ClassStudent>(idPks);
- }
- long createDate = DateTimeOffset.UtcNow.Ticks;
- request.@params.ForEach(
- x => {
- x.createDate = createDate;
- x.id = x.studentId.Replace("#", "-");
- //设置密码 isSet 是否加密 如果加密则不会再次加密
- if (!x.password.isSet)
- {
- x.password.value = TmdCrypt.Encrypt(x.password.value);
- x.password.isSet = true;
- }
- });
- List<Student> students = await azureCosmosDBRepository.SaveOrUpdateAll(request.@params);
- ///更新学生关系表
- List<ClassStudent> classroomStudents = new List<ClassStudent>();
- foreach (var student in students)
- {
- // 处理未变更原生班级的学生
- bool has = false;
- foreach (IdPk idPk in unpk) {
- if (idPk.id.Equals(student.classroomCode) && idPk.pk.Equals(student.studentId)) {
- has = true;
- }
- }
- if (has)
- {
- continue;
- }
- else {
- classroomStudents.Add(new ClassStudent { id = student.classroomCode, code = student.studentId });
- }
- }
- await azureCosmosDBRepository.SaveOrUpdateAll(classroomStudents);
- builder.Data(students);
- }
- return builder.build();
- }
-
-
- /// <summary>
- /// 删除单个学生
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("delete")]
- public async Task<BaseResponse> Delete(JosnRequest<Student> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- IdPk data = await azureCosmosDBRepository.DeleteAsync<Student>(request.@params.id, request.@params.code);
- ///更新学生关系表
- await azureCosmosDBRepository.DeleteAll<ClassStudent>(new Dictionary<string, object> { { "code", request.@params.studentId } });
- return builder.Data(data).build();
- }
- /// <summary>
- /// 批量删除并维护关联关系
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("bulkDelete")]
- public async Task<BaseResponse> BulkDelete(JosnRequest<Dictionary<string,object>> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- if (request.@params.Keys.Count > 0&& request.@params.ContainsKey("code")) {
- List<Student> students = await azureCosmosDBRepository.FindByDict<Student>(request.@params);
- await azureCosmosDBRepository.DeleteAll<Student>(students);
- ///更新学生关系表
- await azureCosmosDBRepository.DeleteAll<ClassStudent>(new Dictionary<string, object> { {"code",students.Select(x=>x.studentId).ToArray() } });
- builder.Data(students);
- }
- return builder.build();
- }
- }
- }
|