using Grpc.Core; using Grpc.Extension.Abstract; using Microsoft.AspNetCore.Authorization; using System; using System.Collections.Generic; using System.Threading.Tasks; using TEAMModelGrpc.Models; using TEAMModelOS; using TEAMModelOS.Models; using TEAMModelOS.SDK.Context.Exception; using TEAMModelOS.SDK.Helper.Common.CollectionHelper; using TEAMModelOS.SDK.DI; using TEAMModelOS.Models.StudentInfo; using TEAMModelOS.Models.Dto; using TEAMModelOS.Models.TeacherInfo; namespace TEAMModelGrpc.Services { public class HomeWorkService : IGrpcService { private readonly AzureCosmosFactory _azureCosmos; public HomeWorkService(AzureCosmosFactory azureCosmos) { _azureCosmos = azureCosmos; } /// /// 学生作业打分评论 /// /// /// /// [Authorize] public async Task StudentScoring(HomeworkCommentDto homeWorkCommentDto, ServerCallContext context) { List homeWorkStudents = await _azureCosmos.FindByDict(new Dictionary { { "code", homeWorkCommentDto.homeWorkId }, { "id", homeWorkCommentDto.homeWorkId } }); List homeWorks = await _azureCosmos.FindByDict(new Dictionary { { "id", homeWorkCommentDto.homeWorkId } }); HomeworkRecord data = new HomeworkRecord(); if (homeWorks.IsNotEmpty() && homeWorks[0].other.Contains("comment")) { if (homeWorkStudents.IsNotEmpty()) { if (string.IsNullOrEmpty(homeWorkCommentDto.commentid)) { //评论 StudentComment homeWorkComment = new StudentComment { comment = homeWorkCommentDto.comment, createTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(), fromId = homeWorkCommentDto.fromId, score = homeWorkCommentDto.score }; homeWorkStudents[0].stuCmt.Add(homeWorkComment); } else { //回复评论 foreach (StudentComment comment in homeWorkStudents[0].stuCmt) { if (comment.commentid == homeWorkCommentDto.commentid) { Reply reply = new Reply(); reply.fromId = homeWorkCommentDto.fromId; reply.toId = homeWorkCommentDto.toId; reply.identity = homeWorkCommentDto.identity; reply.comment = homeWorkCommentDto.comment; reply.createTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds(); comment.reply.Add(reply); } } } //homeWorkStudents[0].comments[request.@params.TEAMModelId] = request.@params.comment; data = await _azureCosmos.Update(homeWorkStudents[0]); } return data; } else throw new BizException("未开放互评"); } /// /// 教师作业打分评论 /// /// /// /// public async Task TeacherScoring(HomeworkScoringDto homeWorkCommentDto, ServerCallContext context) { List homeWorkStudents = await _azureCosmos.FindByDict(new Dictionary { { "code", homeWorkCommentDto.studentId }, { "id", homeWorkCommentDto.homeWorkId } }); HomeworkRecord data = new HomeworkRecord(); if (homeWorkStudents.IsNotEmpty()) { homeWorkStudents[0].score = homeWorkCommentDto.score ?? homeWorkStudents[0].score; HomeWorkComment homeWorkComment = new HomeWorkComment { comment = homeWorkCommentDto.comments, createTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(), TEAMModelId = homeWorkCommentDto.TEAMModelId }; homeWorkStudents[0].tchCmt = homeWorkComment; data = await _azureCosmos.SaveOrUpdate(homeWorkStudents[0]); } return data; } /// /// 查询教师评语罐头 /// /// /// /// /// public async System.Threading.Tasks.Task FindTeacherComments(Dict dict, IServerStreamWriter responseStream, ServerCallContext context) { Dictionary keyValuePairs = dict.ToDict(); List teacherComments = new List(); if (keyValuePairs.IsNotEmpty()) { teacherComments = await _azureCosmos.FindByDict(keyValuePairs); if (teacherComments.IsNotEmpty()) { teacherComments.ForEach(x=> { responseStream.WriteAsync(x); }); } } } } }