HomeWorkService.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.Threading.Tasks;
  7. using TEAMModelGrpc.Models;
  8. using TEAMModelOS;
  9. using TEAMModelOS.Models;
  10. using TEAMModelOS.SDK.Context.Exception;
  11. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  12. using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
  13. using TEAMModelOS.Service.Models.Learn;
  14. namespace TEAMModelGrpc.Services
  15. {
  16. public class HomeWorkService : IGrpcService
  17. {
  18. private readonly IAzureCosmosDBV3Repository _cosmos;
  19. public HomeWorkService(IAzureCosmosDBV3Repository cosmos)
  20. {
  21. _cosmos = cosmos;
  22. }
  23. /// <summary>
  24. /// 学生作业打分评论
  25. /// </summary>
  26. /// <param name="homeWorkCommentDto"></param>
  27. /// <param name="context"></param>
  28. /// <returns></returns>
  29. [Authorize]
  30. public async Task<HomeWorkStudent> StudentScoring(HomeWorkCommentDto homeWorkCommentDto, ServerCallContext context)
  31. {
  32. List<HomeWorkStudent> homeWorkStudents = await _cosmos.FindByDict<HomeWorkStudent>(new Dictionary<string, object> { { "id", homeWorkCommentDto.id }, { "homeWorkId", homeWorkCommentDto.homeWorkId } });
  33. List<HomeWork> homeWorks = await _cosmos.FindByDict<HomeWork>(new Dictionary<string, object> { { "id", homeWorkCommentDto.homeWorkId } });
  34. HomeWorkStudent data = new HomeWorkStudent();
  35. if (homeWorks.IsNotEmpty() && homeWorks[0].other.Contains("comment"))
  36. {
  37. if (homeWorkStudents.IsNotEmpty())
  38. {
  39. if (string.IsNullOrEmpty(homeWorkCommentDto.commentid))
  40. {
  41. //评论
  42. StudentComment homeWorkComment = new StudentComment
  43. {
  44. commentid = Guid.NewGuid().ToString(),
  45. comment = homeWorkCommentDto.comment,
  46. createTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(),
  47. fromId = homeWorkCommentDto.fromId,
  48. score = homeWorkCommentDto.score
  49. };
  50. homeWorkStudents[0].studentComments.Add(homeWorkComment);
  51. }
  52. else
  53. {
  54. //回复评论
  55. foreach (StudentComment comment in homeWorkStudents[0].studentComments)
  56. {
  57. if (comment.commentid == homeWorkCommentDto.commentid)
  58. {
  59. Reply reply = new Reply();
  60. reply.fromId = homeWorkCommentDto.fromId;
  61. reply.toId = homeWorkCommentDto.toId;
  62. reply.identity = homeWorkCommentDto.identity;
  63. reply.comment = homeWorkCommentDto.comment;
  64. reply.createTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
  65. comment.reply.Add(reply);
  66. }
  67. }
  68. }
  69. //homeWorkStudents[0].comments[request.@params.TEAMModelId] = request.@params.comment;
  70. data = await _cosmos.Update(homeWorkStudents[0]);
  71. }
  72. return data;
  73. }
  74. else throw new BizException("未开放互评");
  75. }
  76. /// <summary>
  77. /// 教师作业打分评论
  78. /// </summary>
  79. /// <param name="homeWorkCommentDto"></param>
  80. /// <param name="context"></param>
  81. /// <returns></returns>
  82. public async Task<HomeWorkStudent> TeacherScoring(HomeWorkScoringDto homeWorkCommentDto, ServerCallContext context)
  83. {
  84. List<HomeWorkStudent> homeWorkStudents = await _cosmos.FindByDict<HomeWorkStudent>(new Dictionary<string, object> { { "id", homeWorkCommentDto.id }, { "homeWorkId", homeWorkCommentDto.homeWorkId } });
  85. HomeWorkStudent data = new HomeWorkStudent();
  86. if (homeWorkStudents.IsNotEmpty())
  87. {
  88. homeWorkStudents[0].score = homeWorkCommentDto.score ?? homeWorkStudents[0].score;
  89. HomeWorkComment homeWorkComment = new HomeWorkComment
  90. {
  91. comment = homeWorkCommentDto.comments,
  92. createTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(),
  93. TEAMModelId = homeWorkCommentDto.TEAMModelId
  94. };
  95. homeWorkStudents[0].teacherComments = homeWorkComment;
  96. data = await _cosmos.SaveOrUpdate(homeWorkStudents[0]);
  97. }
  98. return data;
  99. }
  100. /// <summary>
  101. /// 查询教师评语罐头
  102. /// </summary>
  103. /// <param name="dict"></param>
  104. /// <param name="context"></param>
  105. /// <returns></returns>
  106. public async Task FindTeacherComments(Dict dict, IServerStreamWriter<TeacherComments> responseStream, ServerCallContext context)
  107. {
  108. Dictionary<string, object> keyValuePairs = dict.ToDict();
  109. List<TeacherComments> teacherComments = new List<TeacherComments>();
  110. if (keyValuePairs.IsNotEmpty()) {
  111. teacherComments = await _cosmos.FindByDict<TeacherComments>(keyValuePairs);
  112. if (teacherComments.IsNotEmpty()) {
  113. teacherComments.ForEach(x=> {
  114. responseStream.WriteAsync(x);
  115. });
  116. }
  117. }
  118. }
  119. }
  120. }