SocialFansService.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using Infrastructure;
  2. using Infrastructure.Attribute;
  3. using Infrastructure.Extensions;
  4. using Infrastructure.Model;
  5. using Microsoft.AspNetCore.Mvc;
  6. using ZR.Model.Content.Dto;
  7. using ZR.Model.social;
  8. using ZR.Model.System;
  9. using ZR.Repository;
  10. using ZR.Service.Social.IService;
  11. namespace ZR.Service.Social
  12. {
  13. /// <summary>
  14. /// 粉丝
  15. /// </summary>
  16. [AppService(ServiceType = typeof(ISocialFansService))]
  17. public class SocialFansService : BaseService<SocialFans>, ISocialFansService, IDynamicApi
  18. {
  19. /// <summary>
  20. /// 查询关注/粉丝列表
  21. /// </summary>
  22. /// <param name="dto"></param>
  23. /// <returns></returns>
  24. [HttpGet]
  25. public ApiResult FollowList([FromQuery] FansQueryDto dto)
  26. {
  27. var userid = (int)HttpContextExtension.GetUId(App.HttpContext);
  28. PagedInfo<SocialFansDto> list = dto.SelectType == 1 ? GetFollow(dto, userid) : GetFans(dto, userid);
  29. return ApiResult.Success(list);
  30. }
  31. /// <summary>
  32. /// 是否关注
  33. /// </summary>
  34. /// <param name="toUserid"></param>
  35. /// <returns></returns>
  36. public ApiResult IsFollow([FromQuery] int toUserid)
  37. {
  38. var userid = (int)HttpContextExtension.GetUId(App.HttpContext);
  39. if (userid == toUserid || toUserid <= 0)
  40. {
  41. return ApiResult.Success("");
  42. }
  43. var isFollow = Any(f => f.Userid == userid && f.ToUserid == toUserid);
  44. return ApiResult.Success(isFollow);
  45. }
  46. /// <summary>
  47. /// 关注
  48. /// </summary>
  49. /// <param name="dto"></param>
  50. /// <returns></returns>
  51. [HttpPost]
  52. public ApiResult Follow([FromBody] SocialFansDto dto)
  53. {
  54. dto.Userid = (int)HttpContextExtension.GetUId(App.HttpContext);
  55. if (dto.Userid == dto.ToUserid)
  56. {
  57. throw new CustomException("不能关注自己");
  58. };
  59. if (dto.ToUserid <= 0)
  60. {
  61. return ApiResult.Error("关注失败");
  62. }
  63. //TODO 判断用户是否存在
  64. var isFollow = GetFirst(x => x.Userid == dto.Userid && x.ToUserid == dto.ToUserid);
  65. if (isFollow != null)
  66. {
  67. throw new CustomException("已关注");
  68. }
  69. var fans = new SocialFans
  70. {
  71. Userid = dto.Userid,
  72. ToUserid = dto.ToUserid,
  73. FollowTime = DateTime.Now,
  74. UserIP = HttpContextExtension.GetClientUserIp(App.HttpContext)
  75. };
  76. var result = UseTran2(() =>
  77. {
  78. //添加粉丝
  79. InsertReturnSnowflakeId(fans);
  80. UpdateFollowInfo(dto.Userid, isFollowNum: true);
  81. UpdateFollowInfo(dto.ToUserid, isFollowNum: false);
  82. });
  83. if (!result)
  84. {
  85. return ApiResult.Error("关注失败");
  86. }
  87. var data = Context.Queryable<SocialFansInfo>()
  88. .Where(x => x.Userid == dto.Userid)
  89. .First();
  90. return ApiResult.Success("关注成功", data);
  91. }
  92. /// <summary>
  93. /// 取消关注
  94. /// </summary>
  95. /// <param name="dto"></param>
  96. /// <returns></returns>
  97. [HttpPost]
  98. public ApiResult CancelFollow([FromBody] SocialFansDto dto)
  99. {
  100. dto.Userid = (int)HttpContextExtension.GetUId(App.HttpContext);
  101. var result = UseTran(() =>
  102. {
  103. //删除粉丝
  104. Delete(f => f.Userid == dto.Userid && f.ToUserid == dto.ToUserid);
  105. Context.Updateable<SocialFansInfo>()
  106. .SetColumns(x => x.FollowNum == x.FollowNum - 1)
  107. .Where(x => x.Userid == dto.Userid)
  108. .ExecuteCommand();
  109. Context.Updateable<SocialFansInfo>()
  110. .SetColumns(x => x.FansNum == x.FansNum - 1)
  111. .Where(x => x.Userid == dto.ToUserid)
  112. .ExecuteCommand();
  113. });
  114. if (!result.IsSuccess)
  115. {
  116. return ApiResult.Error("取消关注失败");
  117. }
  118. var data = Context.Queryable<SocialFansInfo>()
  119. .Where(x => x.Userid == dto.Userid)
  120. .First();
  121. return ApiResult.Success("取消关注成功", data);
  122. }
  123. private void UpdateFollowInfo(long userId, bool isFollowNum)
  124. {
  125. var count = Context.Updateable<SocialFansInfo>()
  126. .SetColumnsIF(isFollowNum, x => x.FollowNum == x.FollowNum + 1)
  127. .SetColumnsIF(!isFollowNum, x => x.FansNum == x.FansNum + 1)
  128. .Where(x => x.Userid == userId)
  129. .ExecuteCommand();
  130. if (count > 0) return;
  131. Context.Insertable(new SocialFansInfo
  132. {
  133. Userid = userId,
  134. FollowNum = isFollowNum ? 1 : 0,
  135. FansNum = isFollowNum ? 0 : 1,
  136. UpdateTime = DateTime.Now
  137. }).ExecuteCommand();
  138. }
  139. private PagedInfo<SocialFansDto> GetFollow(FansQueryDto dto, int userid)
  140. {
  141. return Queryable()
  142. .LeftJoin<SysUser>((it, u) => it.ToUserid == u.UserId)
  143. .Where(it => it.Userid == userid)
  144. .Select((it, u) => new SocialFansDto()
  145. {
  146. User = new ArticleUser()
  147. {
  148. Avatar = u.Avatar,
  149. NickName = u.NickName,
  150. Sex = u.Sex,
  151. },
  152. Status = 1,
  153. ToUserid = it.ToUserid,
  154. })
  155. .ToPage(dto);
  156. }
  157. private PagedInfo<SocialFansDto> GetFans(FansQueryDto dto, int userid)
  158. {
  159. return Queryable()
  160. .LeftJoin<SysUser>((it, u) => it.Userid == u.UserId)
  161. .Where(it => it.ToUserid == userid)
  162. .Select((it, u) => new SocialFansDto()
  163. {
  164. User = new ArticleUser()
  165. {
  166. Avatar = u.Avatar,
  167. NickName = u.NickName,
  168. Sex = u.Sex,
  169. },
  170. Userid = it.Userid,
  171. })
  172. .ToPage(dto);
  173. }
  174. }
  175. }