ClassroomService.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Grpc.Core;
  2. using Grpc.Extension.Abstract;
  3. using Microsoft.AspNetCore.Authorization;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using TEAMModelGrpc.Models;
  9. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.Service.Models;
  12. namespace TEAMModelGrpc.Services
  13. {
  14. public class ClassroomService : IGrpcService
  15. {
  16. public readonly AzureCosmosFactory cosmosrepository;
  17. public ClassroomService(AzureCosmosFactory cosmosrepository)
  18. {
  19. this.cosmosrepository = cosmosrepository;
  20. }
  21. /// <summary>
  22. /// 保存教室
  23. /// </summary>
  24. /// <param name="requestStream"></param>
  25. /// <param name="responseStream"></param>
  26. /// <param name="context"></param>
  27. /// <returns></returns>
  28. [Authorize]
  29. public async Task SaveOrUpdateVolume(IAsyncStreamReader<Classroom> requestStream, IServerStreamWriter<Classroom> responseStream, ServerCallContext context)
  30. {
  31. List<Classroom> classrooms = new List<Classroom>();
  32. await foreach (Classroom classroom in requestStream.ReadAllAsync())
  33. {
  34. if (classroom.id != null)
  35. {
  36. classrooms.Add( await cosmosrepository.SaveOrUpdate(classroom));
  37. }
  38. else
  39. {
  40. ClassStudent students = new ClassStudent();
  41. students.code = classroom.code;
  42. students.id = classroom.classroomCode;
  43. await cosmosrepository.SaveOrUpdate(students);
  44. classrooms.Add(await cosmosrepository.SaveOrUpdate(classroom));
  45. }
  46. }
  47. classrooms.ForEach(x => {
  48. responseStream.WriteAsync(x);
  49. });
  50. }
  51. /// <summary>
  52. /// 查询教室
  53. /// </summary>
  54. /// <param name="request"></param>
  55. /// <param name="responseStream"></param>
  56. /// <param name="context"></param>
  57. /// <returns></returns>
  58. [Authorize]
  59. public async Task FindVolume(Dict request, IServerStreamWriter<Classroom> responseStream, ServerCallContext context)
  60. {
  61. Dictionary<string, object> dict = request.ToDict();
  62. List<Classroom> syllabusVolumes = await cosmosrepository.FindByDict<Classroom>(dict);
  63. syllabusVolumes.ForEach(x => {
  64. responseStream.WriteAsync(x);
  65. });
  66. }
  67. /// <summary>
  68. /// 删除教室
  69. /// </summary>
  70. /// <param name="listPid"></param>
  71. /// <param name="context"></param>
  72. /// <returns></returns>
  73. [Authorize]
  74. public async Task<ListPid> Delete(ListPid listPid, ServerCallContext context)
  75. {
  76. await cosmosrepository.DeleteAll<Classroom>(listPid.idPks);
  77. if (listPid.idPks.IsNotEmpty())
  78. {
  79. List<ClassStudent> students = new List<ClassStudent>();
  80. foreach (IdPk classroom in listPid.idPks)
  81. {
  82. List<ClassStudent> ClassStudents = await cosmosrepository.FindByDict<ClassStudent>(new Dictionary<string, object> { { "id", classroom.id }, { "code", classroom.pk } });
  83. students.AddRange(ClassStudents);
  84. }
  85. if (students.Count > 0)
  86. {
  87. await cosmosrepository.DeleteAll<ClassStudent>(students);
  88. }
  89. listPid.idPks = await cosmosrepository.DeleteAll<Classroom>(listPid.idPks);
  90. }
  91. return listPid;
  92. }
  93. }
  94. }