using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AutoMapper; using GxPress.Common.AppOptions; using GxPress.Common.Exceptions; using GxPress.Common.Page; using GxPress.Common.Tools; using GxPress.Entity; using GxPress.Repository.Interface; using GxPress.Request.Comment; using GxPress.Result.Comment; using Microsoft.Extensions.Options; using Datory; using SqlKata; namespace GxPress.Repository.Implement { public class CommentRepository : ICommentRepository { private readonly Repository _repository; private readonly Repository _analyzeRepository; private readonly Repository
_articleRepository; private readonly Repository _userRepository; private readonly IMapper _mapper; public CommentRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _articleRepository = new Repository
(database); _userRepository = new Repository(database); _analyzeRepository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; /// /// 添加评论 /// /// /// public async Task CommentInAsync(CommentInRequest request) { //查询文章是否存在 // var article = _articleRepository.ExistsAsync(Q.Where(nameof(Article.Id), request.ArticleId)); // if (await article == false) // throw new BusinessException("该文章不存在"); //查询用户 var user = _userRepository.ExistsAsync(Q.Where(nameof(User.Id), request.UserId)); if (await user == false) throw new BusinessException("该用户不存在"); var comment = new Comment { ArticleId = request.ArticleId, Content = request.Content, UserId = request.UserId, Pid = request.Pid, TypeValue = request.TypeValue }; //是否被回复 if (request.Pid > 0) { //查询他的上级pid=0 var commentEntity = await _repository.GetAsync(request.Pid); if (commentEntity.Pid > 0) { comment.IsUnderstand = true; } } return await _repository.InsertAsync(comment); } /// /// 分页列表 /// /// /// public async Task> GetPagedList(CommentSearchPageRequest request) { var pagedList = new PagedList { Total = await GetCountAsync(request) }; request.Total = pagedList.Total; var list = await GetPageListAsync(request); pagedList.Items = list; return pagedList; } /// /// 获取总条数 /// /// /// public async Task GetCountAsync(CommentSearchPageRequest request) { return await _repository.CountAsync(Q.Where(nameof(Comment.ArticleId), request.ArticleId) .Where(nameof(Comment.Pid), 0).Where(nameof(Comment.TypeValue), request.TypeValue)); } /// /// 获取分页数据 /// /// /// public async Task> GetPageListAsync(CommentSearchPageRequest request) { List commentPageResults = new List(); var query = new SqlKata.Query(); query.Where(nameof(Comment.ArticleId), request.ArticleId) .Where(nameof(Comment.Pid), 0).Where(nameof(Comment.TypeValue), request.TypeValue); if (string.IsNullOrEmpty(request.Sort)) query.OrderByDesc(nameof(Comment.CreatedDate)); if (!string.IsNullOrEmpty(request.Sort) && request.Sort == "asc") query.OrderBy(nameof(Comment.CreatedDate)); if (!string.IsNullOrEmpty(request.Sort) && request.Sort == "desc") query.OrderByDesc(nameof(Comment.CreatedDate)); var commentList = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage)); var i = 1; foreach (var item in commentList) { //获取当前用户信息 var user = await _userRepository.GetAsync(item.UserId); if (user == null) continue; CommentPageResult commentPageResult = new CommentPageResult { ArticleId = item.ArticleId, AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl), Name = user.Name, Content = item.Content, CreatedTime = item.CreatedDate, LaudCount =await LaudCommentAsync(item.Id), UserId = item.UserId, //是否点赞 IsLaud = await IsLaudCommentAsync(item.Id, request.UserId) }; if (request.Sort == "asc") commentPageResult.FloorCount = $"第{((request.Page - 1) * request.PerPage) + i}楼"; else commentPageResult.FloorCount = $"第{((request.Total - (request.Page - 1) * request.PerPage) - (i - 1))}楼"; //计算点赞数量 //计算被回复数据 List commentReplyResults = new List(); commentPageResult.CommentReplyResults = await GetCommentReplyResultByPid(item.Id, commentReplyResults); commentPageResult.Id = item.Id; commentPageResults.Add(commentPageResult); i++; } return commentPageResults; } /// /// 当前用户是否点赞该评论 /// /// /// /// public async Task IsLaudCommentAsync(int commentId, int userId) { return await _analyzeRepository.CountAsync(Q.Where(nameof(Entity.Analyze.Analyze.CommentId), commentId) .Where(nameof(Entity.Analyze.Analyze.UserId), userId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 2)) > 0; } /// /// 当前用户是否点赞该评论 /// /// /// public async Task LaudCommentAsync(int commentId) { return await _analyzeRepository.CountAsync(Q.Where(nameof(Entity.Analyze.Analyze.CommentId), commentId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 2)); } /// /// 递归查询父级下面的所有回复 /// public async Task> GetCommentReplyResultByPid(int pid, List commentReplyResults) { var result = _repository.GetAllAsync(Q.Where(nameof(Comment.Pid), pid)).Result.ToList(); if (result.Count == 0) return commentReplyResults; foreach (var item in result) { CommentReplyResult commentReplyResult = new CommentReplyResult { Id = item.Id, Content = item.Content, CreatedTime = item.CreatedDate }; var user = await _userRepository.GetAsync(item.UserId); if (user == null) continue; commentReplyResult.Name = user.Name; if (item.IsUnderstand) { var comment = await _repository.GetAsync(Q.Where(nameof(Comment.Id), item.Pid)); if (comment == null) continue; user = _userRepository.GetAsync(comment.UserId).Result; commentReplyResult.ReplyName = user.Name; commentReplyResult.ReplyUserId = user.Id; } commentReplyResult.IsUnderstand = item.IsUnderstand; commentReplyResult.UserId = item.UserId; commentReplyResults.Add(commentReplyResult); commentReplyResults = await GetCommentReplyResultByPid(item.Id, commentReplyResults); } return commentReplyResults; } /// /// 删除评论 /// /// /// public async Task DeleteCommentAsync(CommentDeleteRequest request) { //查询评论数据 var commentEntity = await _repository.GetAsync(Q.Where(nameof(Comment.ArticleId), request.ArticleId) .Where(nameof(Comment.Id), request.CommentId)); if (commentEntity.Id == 0) throw new BusinessException("该评论不存在!"); //递归删除 var commentList = new List { commentEntity }; commentList = await GetCommentByParentId(commentEntity.Id, commentList); //删除 foreach (var item in commentList) { await _repository.DeleteAsync(item.Id); } return true; } /// /// 递归删除 /// /// /// /// public async Task> GetCommentByParentId(int id, List lists) { var commentList = await _repository.GetAllAsync(Q.Where(nameof(Comment.Pid), id)); if (!commentList.Any()) return lists; foreach (var comment in commentList) { lists.Add(comment); await GetCommentByParentId(comment.Id, lists); } return lists; } /// /// 获取评论数量 /// /// /// public async Task GetCommentCountAsync(int articleId) { return await _repository.CountAsync(Q.Where(nameof(Comment.ArticleId), articleId)); } public async Task GetAsync(Query query) { return await _repository.GetAsync(query); } public async Task UpdateAsync(Comment comment) { return await _repository.UpdateAsync(comment); } public async Task CountAsync(Query query) { return await _repository.CountAsync(query); } } }