ArticlePraiseService.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Infrastructure.Attribute;
  2. using ZR.Model.Content;
  3. using ZR.Service.Content.IService;
  4. using ZR.ServiceCore.Services;
  5. namespace ZR.Service.Content
  6. {
  7. [AppService(ServiceType = typeof(IArticlePraiseService), ServiceLifetime = LifeTime.Transient)]
  8. public class ArticlePraiseService : BaseService<ArticlePraise>, IArticlePraiseService
  9. {
  10. private IArticleService _articleService;
  11. private ISysUserMsgService _sysUserMsgService;
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. /// <param name="articleService"></param>
  16. /// <param name="userMsgService"></param>
  17. public ArticlePraiseService(IArticleService articleService, ISysUserMsgService userMsgService)
  18. {
  19. _articleService = articleService;
  20. _sysUserMsgService = userMsgService;
  21. }
  22. /// <summary>
  23. /// 点赞
  24. /// </summary>
  25. /// <param name="dto"></param>
  26. /// <returns></returns>
  27. public int Praise(ArticlePraise dto)
  28. {
  29. int result = 0;
  30. var praiseInfo = GetFirst(f => f.UserId == dto.UserId && f.ArticleId == dto.ArticleId);
  31. var isPraise = 0;
  32. var r = UseTran(() =>
  33. {
  34. //第一次点赞插入数据
  35. if (praiseInfo == null)
  36. {
  37. Insertable(dto).ExecuteReturnSnowflakeId();
  38. result = _articleService.PraiseArticle(dto.ArticleId);
  39. isPraise = 1;
  40. if (dto.UserId != dto.ToUserId)
  41. {
  42. _sysUserMsgService.AddSysUserMsg(new SysUserMsg()
  43. {
  44. FromUserid = dto.UserId,
  45. UserId = dto.ToUserId,
  46. Content = "赞了你的内容",
  47. MsgType = UserMsgType.PRAISE,
  48. TargetId = dto.ArticleId,
  49. });
  50. }
  51. }
  52. else
  53. {
  54. if (praiseInfo.IsDelete == 0)
  55. {
  56. result = CanclePraise(dto.UserId, dto.ArticleId);
  57. //文章点赞-1
  58. _articleService.CancelPraise(dto.ArticleId);
  59. }
  60. else
  61. {
  62. //文章点赞+1
  63. _articleService.PraiseArticle(dto.ArticleId);
  64. //恢复点赞为未删除状态
  65. PlusPraise(praiseInfo.PId);
  66. isPraise = 1;
  67. }
  68. }
  69. });
  70. return r.IsSuccess && isPraise == 1 ? 1 : 0;
  71. }
  72. /// <summary>
  73. /// 取消点赞点赞
  74. /// </summary>
  75. /// <param name="userid"></param>
  76. /// <param name="articleId"></param>
  77. /// <returns></returns>
  78. public int CanclePraise(long userid, long articleId)
  79. {
  80. return Deleteable()
  81. .Where(f => f.ArticleId == articleId && f.UserId == userid)
  82. .IsLogic()
  83. .ExecuteCommand(deleteValue: 1);
  84. }
  85. /// <summary>
  86. /// 恢复点赞
  87. /// </summary>
  88. /// <param name="pid"></param>
  89. /// <returns></returns>
  90. public int PlusPraise(long pid)
  91. {
  92. return Deleteable()
  93. .Where(f => f.PId == pid)
  94. .IsLogic()
  95. .ExecuteCommand(deleteValue: 0);
  96. }
  97. }
  98. }