StudentController.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using TEAMModelOS.SDK;
  7. using TEAMModelOS.SDK;
  8. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  9. using TEAMModelOS.SDK.Helper.Security.TmdCrypt;
  10. using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
  11. using TEAMModelOS.SDK.Helper.Common.ValidateHelper;
  12. using TEAMModelOS.SDK.Context.Exception;
  13. using TEAMModelOS.Service.Models;
  14. namespace TEAMModelOS.Controllers
  15. {
  16. [Route("api/[controller]")]
  17. [ApiController]
  18. // [Authorize]
  19. public class StudentController : BaseController
  20. {
  21. private readonly IAzureCosmosDBV3Repository azureCosmosDBRepository;
  22. public StudentController(IAzureCosmosDBV3Repository _azureCosmosDBRepository)
  23. {
  24. azureCosmosDBRepository = _azureCosmosDBRepository;
  25. }
  26. /// <summary>
  27. /// 保存或更新学生,并维护学生关系表
  28. /// </summary>
  29. /// <param name="request"></param>
  30. /// <returns></returns>
  31. [HttpPost("upsert")]
  32. public async Task<BaseResponse> Upsert(JosnRequest<Student> request)
  33. {
  34. ResponseBuilder builder = ResponseBuilder.custom();
  35. //设置密码 isSet 是否加密 如果加密则不会再次加密
  36. if (!request.@params.password.isSet)
  37. {
  38. request.@params.password.value = TmdCrypt.Encrypt(request.@params.password.value);
  39. request.@params.password.isSet = true;
  40. }
  41. request.@params.id = request.@params.studentId.Replace("#", "-");
  42. ///假如更新了班级则先获取更新之前的班级
  43. var olStudent= await azureCosmosDBRepository.FindByIdPk<Student>(request.@params.id,request.@params.code);
  44. if (olStudent!=null && !string.IsNullOrEmpty(olStudent.classroomCode) && ! olStudent.classroomCode.Equals(request.@params.classroomCode) ) {
  45. //移除之前的原生班级
  46. IdPk idPk=await azureCosmosDBRepository.DeleteAsync<ClassStudent>( olStudent.classroomCode ,olStudent.studentId);
  47. }
  48. ///新建最新的班级关系表
  49. ClassStudent classroomStudent = new ClassStudent { id = request.@params.classroomCode, code = request.@params.studentId };
  50. await azureCosmosDBRepository.SaveOrUpdate(classroomStudent);
  51. Student data = await azureCosmosDBRepository.SaveOrUpdate<Student>(request.@params);
  52. return builder.Data(data).build();
  53. }
  54. /// <summary>
  55. /// 查找学生
  56. /// </summary>
  57. /// <param name="request"></param>
  58. /// <returns></returns>
  59. [HttpPost("find")]
  60. public async Task<BaseResponse> Find(JosnRequest<Dictionary<string, object>> request)
  61. {
  62. ResponseBuilder builder = ResponseBuilder.custom();
  63. if (request.@params.TryGetValue("code", out object _))
  64. {
  65. List<Student> data = await azureCosmosDBRepository.FindByDict<Student>(request.@params);
  66. return builder.Data(data).build();
  67. }
  68. else
  69. {
  70. return builder.Error(ResponseCode.PARAMS_ERROR, "code is null !").build();
  71. }
  72. }
  73. [HttpPost("upsertAll")]
  74. public async Task<BaseResponse> UpsertAll(JosnRequest<List<Student>> request)
  75. {
  76. ResponseBuilder builder = ResponseBuilder.custom();
  77. if (ValidateHelper.IsValid(request.@params) && request.@params.IsNotEmpty()) {
  78. ///假如更新了班级则先获取更新之前的班级
  79. string[] ids = request.@params.Select(x=>x.studentId).ToArray();
  80. List<Student> oldStudent = await azureCosmosDBRepository.FindByDict<Student>(new Dictionary<string, object>() { { "studentId", ids } });
  81. List<IdPk> idPks = new List<IdPk>();
  82. ///处理未变动的班级关系
  83. List<IdPk> unpk = new List<IdPk>();
  84. oldStudent.ForEach(x=> {
  85. request.@params.ForEach(m => {
  86. if (x.studentId.Equals(m.studentId)) {
  87. if (!x.classroomCode.Equals(m.classroomCode))
  88. {
  89. idPks.Add(new IdPk { id = x.classroomCode, pk = x.studentId });
  90. }
  91. else {
  92. unpk.Add(new IdPk { id = x.classroomCode, pk = x.studentId });
  93. }
  94. }
  95. });
  96. });
  97. if (idPks.IsNotEmpty()) {
  98. await azureCosmosDBRepository.DeleteAll<ClassStudent>(idPks);
  99. }
  100. long createDate = DateTimeOffset.UtcNow.Ticks;
  101. request.@params.ForEach(
  102. x => {
  103. x.createDate = createDate;
  104. x.id = x.studentId.Replace("#", "-");
  105. //设置密码 isSet 是否加密 如果加密则不会再次加密
  106. if (!x.password.isSet)
  107. {
  108. x.password.value = TmdCrypt.Encrypt(x.password.value);
  109. x.password.isSet = true;
  110. }
  111. });
  112. List<Student> students = await azureCosmosDBRepository.SaveOrUpdateAll(request.@params);
  113. ///更新学生关系表
  114. List<ClassStudent> classroomStudents = new List<ClassStudent>();
  115. foreach (var student in students)
  116. {
  117. // 处理未变更原生班级的学生
  118. bool has = false;
  119. foreach (IdPk idPk in unpk) {
  120. if (idPk.id.Equals(student.classroomCode) && idPk.pk.Equals(student.studentId)) {
  121. has = true;
  122. }
  123. }
  124. if (has)
  125. {
  126. continue;
  127. }
  128. else {
  129. classroomStudents.Add(new ClassStudent { id = student.classroomCode, code = student.studentId });
  130. }
  131. }
  132. await azureCosmosDBRepository.SaveOrUpdateAll(classroomStudents);
  133. builder.Data(students);
  134. }
  135. return builder.build();
  136. }
  137. /// <summary>
  138. /// 删除单个学生
  139. /// </summary>
  140. /// <param name="request"></param>
  141. /// <returns></returns>
  142. [HttpPost("delete")]
  143. public async Task<BaseResponse> Delete(JosnRequest<Student> request)
  144. {
  145. ResponseBuilder builder = ResponseBuilder.custom();
  146. IdPk data = await azureCosmosDBRepository.DeleteAsync<Student>(request.@params.id, request.@params.code);
  147. ///更新学生关系表
  148. await azureCosmosDBRepository.DeleteAll<ClassStudent>(new Dictionary<string, object> { { "code", request.@params.studentId } });
  149. return builder.Data(data).build();
  150. }
  151. /// <summary>
  152. /// 批量删除并维护关联关系
  153. /// </summary>
  154. /// <param name="request"></param>
  155. /// <returns></returns>
  156. [HttpPost("bulkDelete")]
  157. public async Task<BaseResponse> BulkDelete(JosnRequest<Dictionary<string,object>> request)
  158. {
  159. ResponseBuilder builder = ResponseBuilder.custom();
  160. if (request.@params.Keys.Count > 0&& request.@params.ContainsKey("code")) {
  161. List<Student> students = await azureCosmosDBRepository.FindByDict<Student>(request.@params);
  162. await azureCosmosDBRepository.DeleteAll<Student>(students);
  163. ///更新学生关系表
  164. await azureCosmosDBRepository.DeleteAll<ClassStudent>(new Dictionary<string, object> { {"code",students.Select(x=>x.studentId).ToArray() } });
  165. builder.Data(students);
  166. }
  167. return builder.build();
  168. }
  169. }
  170. }