fix(follow):将通过用户id查找,改为通过用户名查找,与前端接口相匹配

Change-Id: I061e8e613aa9280d019810fc31226d9fde776e4f
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserFollowController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserFollowController.java
index 17d7ca1..2e791c1 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserFollowController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserFollowController.java
@@ -1,4 +1,3 @@
-// 作者关注控制器
 package com.ruoyi.web.controller.system;
 
 import com.ruoyi.common.core.controller.BaseController;
@@ -19,14 +18,24 @@
     @PostMapping
     public AjaxResult follow(@RequestBody SysUserFollow follow) {
         follow.setUserId(getUserId());
-        return toAjax(followService.followAuthor(follow));
+        int result = followService.followAuthor(follow);
+        if (result == -1) {
+            return AjaxResult.error("作者不存在");
+        } else if (result == 0) {
+            return AjaxResult.error("已关注该作者");
+        }
+        return toAjax(result);
     }
 
     @PreAuthorize("@ss.hasPermi('system:user:follow:remove')")
     @DeleteMapping
     public AjaxResult unfollow(@RequestBody SysUserFollow follow) {
         follow.setUserId(getUserId());
-        return toAjax(followService.unfollowAuthor(follow));
+        int result = followService.unfollowAuthor(follow);
+        if (result == -1) {
+            return AjaxResult.error("作者不存在");
+        }
+        return toAjax(result);
     }
 
     @GetMapping("/list")
@@ -34,11 +43,12 @@
         return AjaxResult.success(followService.getFollowList(getUserId()));
     }
 
-    @GetMapping("/isFollowing/{authorId}")
-    public AjaxResult isFollowing(@PathVariable Long authorId) {
+    @PreAuthorize("@ss.hasPermi('system:user:follow:query')")
+    @GetMapping("/isFollowing/{authorUsername}")
+    public AjaxResult isFollowing(@PathVariable String authorUsername) {
         SysUserFollow follow = new SysUserFollow();
         follow.setUserId(getUserId());
-        follow.setAuthorId(authorId);
+        follow.setAuthorUsername(authorUsername);
         return AjaxResult.success(followService.isFollowing(follow));
     }
 }
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserFollow.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserFollow.java
index a5878b1..4831e79 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserFollow.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserFollow.java
@@ -1,4 +1,3 @@
-// 作者关注
 package com.ruoyi.system.domain;
 
 import com.ruoyi.common.annotation.Excel;
@@ -20,4 +19,7 @@
     @Excel(name = "作者ID")
     @NotNull(message = "作者ID不能为空")
     private Long authorId;
+
+    // 用于接收前端传入的作者用户名
+    private String authorUsername;
 }
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserFollowMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserFollowMapper.java
index 77bca7e..521f26b 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserFollowMapper.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserFollowMapper.java
@@ -1,4 +1,3 @@
-// 作者关注 Mapper
 package com.ruoyi.system.mapper;
 
 import com.ruoyi.system.domain.SysUserFollow;
@@ -9,4 +8,5 @@
     int deleteFollow(SysUserFollow follow);
     List<SysUserFollow> selectFollowListByUserId(Long userId);
     SysUserFollow selectFollow(SysUserFollow follow);
+    Long selectUserIdByUsername(String username);
 }
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserFollowServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserFollowServiceImpl.java
index 9cec124..a12f985 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserFollowServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserFollowServiceImpl.java
@@ -1,9 +1,10 @@
-// 作者关注服务实现
 package com.ruoyi.system.service.impl;
 
 import com.ruoyi.system.domain.SysUserFollow;
+import com.ruoyi.system.domain.SysUserMessage;
 import com.ruoyi.system.mapper.SysUserFollowMapper;
 import com.ruoyi.system.service.ISysUserFollowService;
+import com.ruoyi.system.service.ISysUserMessageService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import java.util.List;
@@ -13,17 +14,46 @@
     @Autowired
     private SysUserFollowMapper followMapper;
 
+    @Autowired
+    private ISysUserMessageService messageService;
+
     @Override
     public int followAuthor(SysUserFollow follow) {
+        // 根据 authorUsername 查询 authorId
+        Long authorId = followMapper.selectUserIdByUsername(follow.getAuthorUsername());
+        if (authorId == null) {
+            return -1; // 作者不存在
+        }
+        follow.setAuthorId(authorId);
+
+        // 检查是否已关注
         if (followMapper.selectFollow(follow) != null) {
             return 0; // 已经关注
         }
-        return followMapper.insertFollow(follow);
+
+        // 插入关注记录
+        int result = followMapper.insertFollow(follow);
+        if (result > 0) {
+            // 发送 "hello" 消息
+            SysUserMessage message = new SysUserMessage();
+            message.setSenderId(follow.getUserId());
+            message.setReceiverId(authorId);
+            message.setContent("hello");
+            messageService.sendMessage(message);
+        }
+        return result;
     }
 
     @Override
     public int unfollowAuthor(SysUserFollow follow) {
-        return followMapper.deleteFollow(follow);
+        // 根据 authorUsername 查询 authorId
+        Long authorId = followMapper.selectUserIdByUsername(follow.getAuthorUsername());
+        if (authorId == null) {
+            return -1; // 作者不存在
+        }
+        follow.setAuthorId(authorId);
+
+        return followMapper.deleteFollow(follow); // 返回删除的行数(0 或 1)
     }
 
     @Override
@@ -33,6 +63,13 @@
 
     @Override
     public boolean isFollowing(SysUserFollow follow) {
+        // 根据 authorUsername 查询 authorId
+        Long authorId = followMapper.selectUserIdByUsername(follow.getAuthorUsername());
+        if (authorId == null) {
+            return false; // 作者不存在
+        }
+        follow.setAuthorId(authorId);
+
         return followMapper.selectFollow(follow) != null;
     }
 }
\ No newline at end of file
diff --git a/ruoyi-system/src/main/resources/mapper/system/SysUserFollowMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysUserFollowMapper.xml
index 6fb9169..bea8888 100644
--- a/ruoyi-system/src/main/resources/mapper/system/SysUserFollowMapper.xml
+++ b/ruoyi-system/src/main/resources/mapper/system/SysUserFollowMapper.xml
@@ -28,4 +28,10 @@
         from sys_user_follow
         where user_id = #{userId} and author_id = #{authorId}
     </select>
+
+    <select id="selectUserIdByUsername" resultType="java.lang.Long">
+        select user_id
+        from sys_user
+        where user_name = #{username}
+    </select>
 </mapper>
\ No newline at end of file