ArticleCommentService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using Infrastructure;
  2. using Infrastructure.Attribute;
  3. using Infrastructure.Extensions;
  4. using ZR.Model.Content;
  5. using ZR.Model.Content.Dto;
  6. using ZR.Model.System;
  7. using ZR.Repository;
  8. using ZR.Service.Content.IService;
  9. using ZR.ServiceCore.Services;
  10. namespace ZR.Service.Content
  11. {
  12. [AppService(ServiceType = typeof(IArticleCommentService), ServiceLifetime = LifeTime.Transient)]
  13. public class ArticleCommentService : BaseService<ArticleComment>, IArticleCommentService
  14. {
  15. private ISysUserService UserService;
  16. private IEmailLogService EmailLogService;
  17. private IArticleService ArticleService;
  18. private ISysConfigService SysConfigService;
  19. private ISysUserMsgService UserMsgService;
  20. public ArticleCommentService(
  21. ISysUserService userService,
  22. IEmailLogService emailLogService,
  23. IArticleService articleService,
  24. ISysConfigService sysConfigService,
  25. ISysUserMsgService sysUserMsgService)
  26. {
  27. this.UserService = userService;
  28. EmailLogService = emailLogService;
  29. ArticleService = articleService;
  30. SysConfigService = sysConfigService;
  31. UserMsgService = sysUserMsgService;
  32. }
  33. /// <summary>
  34. /// 获取评论列表
  35. /// </summary>
  36. /// <param name="dto"></param>
  37. /// <returns></returns>
  38. public PagedInfo<ArticleCommentDto> GetMessageList(MessageQueryDto dto)
  39. {
  40. var predicate = Expressionable.Create<ArticleComment>();
  41. predicate.And(it => it.IsDelete == 0);
  42. predicate.And(it => it.ParentId == dto.CommentId);
  43. predicate.AndIF(dto.UserId != null, it => it.UserId == dto.UserId);
  44. predicate.AndIF(dto.CommentId > 0, it => it.CommentId > dto.CommentId);//分页使用
  45. //predicate.AndIF(dto.ClassifyId > 0, it => it.ClassifyId == dto.ClassifyId);
  46. //predicate.AndIF(dto.ClassifyId == 0, it => it.ClassifyId == 0);
  47. predicate.AndIF(dto.TargetId > 0, it => it.TargetId == dto.TargetId);
  48. var list = Queryable()
  49. .LeftJoin<SysUser>((it, u) => it.UserId == u.UserId)
  50. .OrderByDescending(it => it.Top)
  51. .OrderByIF(dto.OrderBy == 1, it => it.PraiseNum, OrderByType.Desc)
  52. .OrderByIF(dto.OrderBy == 2, it => it.CommentId, OrderByType.Desc)
  53. .Where(predicate.ToExpression())
  54. .WithCache(60 * 30)
  55. .Select((it, u) => new ArticleCommentDto()
  56. {
  57. NickName = u.NickName,
  58. Avatar = u.Avatar
  59. }, true)
  60. .ToPage(dto);
  61. foreach (var item in list.Result)
  62. {
  63. int take = 1;
  64. if (item.ReplyNum > 0)
  65. {
  66. item.ReplyList = GetCommentQueryable(item.CommentId).Take(take).ToList();
  67. }
  68. if (item.ReplyNum > take)
  69. {
  70. item.HasMore = true;
  71. }
  72. }
  73. return list;
  74. }
  75. /// <summary>
  76. /// 获取评论回复列表
  77. /// </summary>
  78. /// <param name="pager">分页</param>
  79. /// <param name="cid"></param>
  80. /// <returns></returns>
  81. public PagedInfo<ArticleCommentDto> GetReplyComments(long cid, MessageQueryDto pager)
  82. {
  83. PagedInfo<ArticleCommentDto> list = GetCommentQueryable(cid).ToPage(pager);
  84. //Context.ThenMapper(list.Result, f =>
  85. //{
  86. // if (f.ReplyId > 0 && f.ReplyUserId > 0)
  87. // {
  88. // f.ReplyNickName = UserService.GetFirst(x => x.UserId == f.ReplyUserId).NickName;
  89. // }
  90. //});
  91. return list;
  92. }
  93. private ISugarQueryable<ArticleCommentDto> GetCommentQueryable(long cid)
  94. {
  95. return Queryable()
  96. .LeftJoin<SysUser>((f, u) => f.UserId == u.UserId)
  97. .Where(f => f.ParentId == cid && f.IsDelete == 0)
  98. //.WhereIF(cid > 0, f => f.MId > cid)
  99. //.Includes(f => f.User.MappingField(z => z.Useridx, () => f.Useridx))
  100. .OrderBy(f => f.CommentId)
  101. .Select((f, u) => new ArticleCommentDto()
  102. {
  103. NickName = u.NickName,
  104. Avatar = u.Avatar
  105. }, true);
  106. }
  107. /// <summary>
  108. /// 评论
  109. /// </summary>
  110. /// <param name="message"></param>
  111. /// <returns></returns>
  112. public ArticleComment AddMessage(ArticleComment message)
  113. {
  114. var configInfo = SysConfigService.GetFirst(f => f.ConfigKey == "article.comment") ?? throw new CustomException(ResultCode.CUSTOM_ERROR, "评论失败,请联系管理员");
  115. var userInfo = UserService.GetById(message.UserId);
  116. if (configInfo.ConfigValue == "1" && userInfo.Phonenumber.IsEmpty())
  117. {
  118. throw new CustomException(ResultCode.PHONE_BIND, "请绑定手机号");
  119. }
  120. var contentInfo = ArticleService.GetById(message.TargetId);
  121. switch (contentInfo.CommentSwitch)
  122. {
  123. case Model.Enum.CommentSwitchEnum.ALL:
  124. break;
  125. case Model.Enum.CommentSwitchEnum.FANS:
  126. break;
  127. case Model.Enum.CommentSwitchEnum.SELF:
  128. if (message.UserId != contentInfo.UserId)
  129. {
  130. throw new CustomException("仅作者才能评论");
  131. }
  132. break;
  133. default:
  134. break;
  135. }
  136. var ipInfo = HttpContextExtension.GetIpInfo(message.UserIP);
  137. message.Location = ipInfo;
  138. message.AddTime = DateTime.Now;
  139. ArticleComment result = null;
  140. var r = UseTran(() =>
  141. {
  142. result = InsertReturnEntity(message);
  143. if (result != null && result.CommentId > 0)
  144. {
  145. if (message.ParentId > 0)
  146. {
  147. //评论表 评论数 + 1
  148. Update(it => it.CommentId == message.ParentId, it => new ArticleComment() { ReplyNum = it.ReplyNum + 1 });
  149. }
  150. //内容表评论数加1
  151. if (message.TargetId > 0)
  152. {
  153. ArticleService.Update(it => it.Cid == (int)message.TargetId, it => new Article() { CommentNum = it.CommentNum + 1 });
  154. }
  155. }
  156. });
  157. //查询出评论的内容信息找出作者
  158. var targetInfo = GetFirst(f => f.CommentId == message.TargetId);
  159. if (targetInfo != null && targetInfo.UserId != message.UserId)
  160. {
  161. //IM消息
  162. UserMsgService.AddSysUserMsg(targetInfo.UserId, message.Content, UserMsgType.COMMENT);
  163. }
  164. if (message.UserId != contentInfo?.UserId)
  165. {
  166. //给作者发送IM消息
  167. UserMsgService.AddSysUserMsg(contentInfo.UserId, message.Content, UserMsgType.COMMENT);
  168. }
  169. return result;
  170. }
  171. /// <summary>
  172. /// 评论点赞
  173. /// </summary>
  174. /// <param name="mid"></param>
  175. /// <returns></returns>
  176. public int PraiseMessage(long mid)
  177. {
  178. return Context.Updateable<ArticleComment>()
  179. .SetColumns(it => it.PraiseNum == it.PraiseNum + 1)
  180. .Where(it => it.CommentId == mid)
  181. .ExecuteCommand();
  182. }
  183. /// <summary>
  184. /// 删除评论
  185. /// </summary>
  186. /// <param name="commentId">评论ID</param>
  187. /// <param name="userId">当前登录用户</param>
  188. /// <returns></returns>
  189. public int DeleteMessage(long commentId, long userId)
  190. {
  191. var info = GetById(commentId) ?? throw new CustomException("评论不存在");
  192. var postInfo = ArticleService.GetById(info.TargetId);
  193. if (userId != info.UserId && userId != postInfo.UserId)
  194. {
  195. return 0;
  196. }
  197. var deleteNum = 0;
  198. var result = UseTran(() =>
  199. {
  200. Update(it => it.CommentId == commentId, it => new ArticleComment() { IsDelete = 1 });
  201. if (info.ParentId > 0)
  202. {
  203. //评论表 评论数 - 1
  204. Update(it => it.CommentId == info.ParentId, it => new ArticleComment() { ReplyNum = it.ReplyNum - 1 });
  205. }
  206. //减少文章评论次数
  207. if (info.TargetId > 0)
  208. {
  209. deleteNum = info.ReplyNum > 0 ? info.ReplyNum + 1 : 1;
  210. ArticleService.Update(it => it.Cid == (int)info.TargetId, it => new Article()
  211. {
  212. CommentNum = it.CommentNum - deleteNum
  213. });
  214. }
  215. });
  216. return result.IsSuccess ? deleteNum : 0;
  217. }
  218. /// <summary>
  219. /// 获取评论列表
  220. /// </summary>
  221. /// <param name="dto"></param>
  222. /// <returns></returns>
  223. public PagedInfo<ArticleCommentDto> GetMyMessageList(MessageQueryDto dto)
  224. {
  225. var predicate = Expressionable.Create<ArticleComment>();
  226. predicate.And(it => it.IsDelete == 0);
  227. //predicate.And(it => it.ParentId == dto.MId);
  228. predicate.AndIF(dto.UserId != null, it => it.UserId == dto.UserId);
  229. predicate.AndIF(dto.CommentId > 0, it => it.CommentId > dto.CommentId);//分页使用
  230. predicate.AndIF(dto.BeginAddTime != null, it => it.AddTime >= dto.BeginAddTime && it.AddTime <= dto.EndAddTime);//分页使用
  231. return Queryable()
  232. .WithCache(60)
  233. .OrderByIF(dto.OrderBy == 1, it => it.PraiseNum, OrderByType.Desc)
  234. .OrderByIF(dto.OrderBy == 2, it => it.CommentId, OrderByType.Desc)
  235. .Where(predicate.ToExpression())
  236. //.Select((it, u) => new MessageDto()
  237. //{
  238. // MId = it.MId.SelectAll(),
  239. // //NickName = u.NickName,
  240. // //Avatar = u.Avatar
  241. //}, true)
  242. .ToPage<ArticleComment, ArticleCommentDto>(dto);
  243. }
  244. /// <summary>
  245. /// 置顶评论
  246. /// </summary>
  247. /// <param name="commentId"></param>
  248. /// <param name="top"></param>
  249. /// <returns></returns>
  250. public long TopMessage(long commentId, long top)
  251. {
  252. long time = 0;
  253. if (top <= 0)
  254. {
  255. time = DateTimeHelper.GetUnixTimeStamp(DateTime.Now);
  256. }
  257. Update(it => it.CommentId == commentId, it => new ArticleComment() { Top = time });
  258. return time;
  259. }
  260. }
  261. }