業務需求:A、B兩個用戶的關注/取消關注接口
1、建表
由數據庫設計文檔和數據庫存放關系可知,
數據 用戶的userId 既可以在數據庫的user_id位置,也可以在數據庫的follower_id位置上。
2、該接口的實現思路如下
2.1、關注/取消關注 用戶的設計思路
參數:用戶A、用戶B、關注/取消關注flag
A和B的關系,假設A在數據庫的A位置
00 A關注B
01 B關注A
02 A、B相互關注
備注:A在數據庫的B位置時,如上關系為01、00、02
2.2、實現思路如下:
0.去數據庫查詢A、B這兩個用戶有沒有關系(00/01/02)
1.點擊關注 --說明A和B的關系:1>沒有關系;2>B關注A
1-1.判斷A和B的關系
1-2.如果沒有關系,則添加一條數據
1-3.如果有關系,則將關系改為相互關注(這個要結合實際,如果A已經關注了B,則A不能在關注B了,即進來
的肯定是B點擊了關注A)
2.3、點擊取消關注 --說明A和B的關系:
1>A關注B;2>A、B相互關注
2.4、判斷A和B的關系
2.4.1.如果是關注(結合實際,只有相互關注和關注兩種關系),則取消關注,即刪除這條數據
2.4.2.如果是相互關注,則需要判斷A和B的位置
2.4.3 如果A在數據庫的A位置,則修改A和B的關系為被關注--即關系為01
2.4.4 如果A在數據庫的B位置,則修改A和B的關系為被關注--即關系為00
3、Java代碼實現
/** * 添加用戶關注(關注、取消關注) * @param json * @return */ @PostMapping("/addUserRelationShip") public Map<String,Object> addUserRelationShip(@RequestBody String json) { Map<String, Object> resultMap = new HashMap<>(); UserRelationShip relationShip = JSON.parseObject(json, UserRelationShip.class); relationShip.setRelationId(UUID.randomUUID().toString().replace("-", "")); System.out.println(relationShip); JSONObject jsonObject = JSONObject.parseObject(json); Integer status = jsonObject.getInteger("status"); String userId = jsonObject.getString("userId"); String followerId = jsonObject.getString("followerId"); //獲取A和B的關系 List<UserRelationShip> userRelationShips = relationShipService.checkRelation(userId,followerId); if (userRelationShips.size() > 0 ) { //已經有關系了 System.out.println("有關系了"); UserRelationShip userRelationShip = userRelationShips.get(0); System.out.println(userRelationShip.getRelation()); String relation = userRelationShip.getRelation(); String relationUserId = userRelationShip.getUserId(); String relationFollowerId = userRelationShip.getFollowerId(); String position = ""; if (userId.equals(relationUserId)) { position = "left"; } else if (userId.equals(relationFollowerId)) { position = "right"; } if (status == 1) { //執行關注操作 if (position.equals("left")) { if (relation.equals("00") || relation.equals("02")) { //自己在左邊:00:主動關注過對方,02:兩人互關着呢,不能再執行關注操作了 resultMap.put("message", "關注失敗,不能重復關注"); resultMap.put("state", JsonResult.failing); } else {//對方在關注你,但是你沒有關注對方,所有執行操作,更改狀態為02,變成互關的狀態 int updateCount = relationShipService.updateRelationStatus(userId,followerId,"02"); if (updateCount > 0) { resultMap.put("message", "關注成功"); resultMap.put("state", JsonResult.SUCCESS); } else { resultMap.put("message", "關注失敗"); resultMap.put("state", JsonResult.failing); } } } else { //說明自己在右邊,關注是自己被對方主動關注過, if (relation.equals("01") || relation.equals("02")) { //自己在右邊:01:自己關注別人了,02:兩人互關呢,不能再執行關注操作了 resultMap.put("message", "關注失敗,不能重復關注"); resultMap.put("state", JsonResult.failing); } else { //00:對方在關注你,但是你沒有關注對方,所有執行操作,更改狀態為02,變成互關的狀態 int updateCount = relationShipService.updateRelationStatus(userId,followerId,"02"); if (updateCount > 0) { resultMap.put("message", "關注成功"); resultMap.put("state", JsonResult.SUCCESS); } else { resultMap.put("message", "關注失敗"); resultMap.put("state", JsonResult.failing); } } } } else {//執行取關操作 if (position.equals("left")) { if (relation.equals("00")) { //此時只有自己關注了對方,但是對方,卻沒有關注自己,所以執行取消操作的話,直接刪除就行了 int deleteCount = relationShipService.deleteUserRelationShip(userId,followerId); if (deleteCount > 0) { resultMap.put("message", "取消關注成功"); resultMap.put("state", JsonResult.SUCCESS); } else { resultMap.put("message", "取消關注失敗"); resultMap.put("state", JsonResult.failing); } } else if (relation.equals("02")) { //兩人互關呢,自己執行取關操作,要更改狀態為01 int updateCount = relationShipService.updateRelationStatus(userId,followerId,"01"); if (updateCount > 0) { resultMap.put("message", "取消關注成功"); resultMap.put("state", JsonResult.SUCCESS); } else { resultMap.put("message", "取消關注失敗"); resultMap.put("state", JsonResult.failing); } } else { //01時候,對方在關注者自己,就沒關注別人,取關個毛線啊 resultMap.put("message", "取消關注失敗"); resultMap.put("state", JsonResult.failing); } } else { //說明自己在右邊,關注是自己被對方主動關注過, if (relation.equals("01")) { //自己關注了對方,但是對方卻沒有關注自己,直接刪除就行了 int deleteCount = relationShipService.deleteUserRelationShip(userId,followerId); if (deleteCount > 0) { resultMap.put("message", "取消關注成功"); resultMap.put("state", JsonResult.SUCCESS); } else { resultMap.put("message", "取消關注失敗"); resultMap.put("state", JsonResult.failing); } } else if (relation.equals("02")) { int updateCount = relationShipService.updateRelationStatus(userId,followerId,"00"); if (updateCount > 0) { resultMap.put("message", "取消關注成功"); resultMap.put("state", JsonResult.SUCCESS); } else { resultMap.put("message", "取消關注失敗"); resultMap.put("state", JsonResult.failing); } } else { //你都沒關注別人,取關個毛線啊 resultMap.put("message", "取消關注失敗"); resultMap.put("state", JsonResult.failing); } } } } else { //沒有關系 System.out.println("沒有關系"); relationShip.setRelation("00"); //既然沒有關系,就新增一條,記錄狀態標記為00 int addCount = relationShipService.addUserRelationShip(relationShip); if (addCount > 0) { System.out.println("新增關系成功"); resultMap.put("message", "新增關注成功"); resultMap.put("state", JsonResult.SUCCESS); } else { resultMap.put("message", "新增關注失敗"); resultMap.put("state", JsonResult.failing); } } return resultMap; }
備注:上述的sql語句:addUserRelationShip / deleteUserRelationShip / updateRelationStatus
<!-- 檢查好友關系 --> <select id="checkRelation" resultMap="BasePlusResultMap"> select * from follower where (user_id = #{userId} and follower_id = #{followerId}) or (user_id = #{followerId} and follower_id = #{userId}) </select> <!-- 關注某人 --> <insert id="addUserRelationShip" > insert into follower(relation_id,user_id,follower_id,status,relation) values (#{relationId},#{userId},#{followerId},#{status},#{relation}) </insert> <!-- 取消關注某人 --> <delete id="deleteUserRelationShip"> delete from follower where user_id=#{userId} and follower_id = #{followerId} </delete> <!-- 更新還有關系狀態 --> <update id="updateRelationStatus"> update follower set relation = #{relation} where (user_id = #{userId} and follower_id = #{followerId}) or (user_id = #{followerId} and follower_id = #{userId}) </update>
封裝檢查好友關系的邏輯代碼,可以貼過去直接用的
public static int checkRelationStatus(String userId,String targetId,List<Fans> userRelationShips) { int relationStatus = 0; for (int index = 0;index < userRelationShips.size();index++) { Fans userRelationShip = userRelationShips.get(index); String relationShipUserId = userRelationShip.getUserId(); String relationShipFollowerId = userRelationShip.getFollowerId(); String relation = userRelationShip.getRelation(); if (relationShipUserId.equals(userId) && relationShipFollowerId.equals(targetId) || (relationShipUserId.equals(targetId) && relationShipFollowerId.equals(userId))) { String position = ""; if (userId.equals(relationShipUserId)) { position = "left"; } else if (userId.equals(relationShipFollowerId)) { position = "right"; } if (position.equals("left")) { if (relation.equals("00") || relation.equals("02")) { //自己在左邊:00:主動關注過對方,02:兩人互關着呢,不能再執行關注操作了 relationStatus = 1; } } else { //說明自己在右邊,關注是自己被對方主動關注過, if (relation.equals("01") || relation.equals("02")) { //自己在右邊:01:自己關注別人了,02:兩人互關呢,不能再執行關注操作了 relationStatus = 1; } } break; } } return relationStatus; }
4、獲取我的關注列表
/* *//** * 獲取我的關注列表 * @param userId * @param page * @param pageSize * @return */ @GetMapping("/selectUserAttention") public Map<String,Object> selectUserAttention(String userId,int page,int pageSize){ Map<String,Object> resultMap = new HashMap<>(); List<RelationShipUser> relationUsers = new ArrayList<>(); Map<String,Object> paramMap = new HashMap<>(); paramMap.put("userId",userId); paramMap.put("followerId",userId); int pageCount = (page)* pageSize; paramMap.put("page", pageCount); paramMap.put("size",pageSize); List<Fans> userRelationShips = fansUserService.selectUserAttention(paramMap); if(userRelationShips.size() > 0) { List<String> userIds = new ArrayList<>(); for (int index = 0;index < userRelationShips.size();index++) { Fans userRelationShip = userRelationShips.get(index); String relationShipUserId = userRelationShip.getUserId(); String relationShipFollowerId = userRelationShip.getFollowerId(); if (relationShipFollowerId.equals(userId)) { userIds.add(relationShipUserId); } else { userIds.add(relationShipFollowerId); } } System.out.println(userIds); //獲取用戶 List<UserClone> users = relationShipService.queryUserWithIds(userIds); if (users.size() > 0) { for (int index = 0;index < users.size(); index++) { RelationShipUser relationShipUser = new RelationShipUser(); relationShipUser.setUser(users.get(index)); relationUsers.add(relationShipUser); } } for (int idx = 0;idx < relationUsers.size();idx++) { RelationShipUser relationShipUser = relationUsers.get(idx); String targetId = relationShipUser.getUser().getUserId(); int relationStatus = checkRelationStatus(userId,targetId,userRelationShips); relationShipUser.setStatus(relationStatus); relationUsers.set(idx,relationShipUser); } resultMap.put("data",relationUsers); resultMap.put("message", "查詢關注人成功"); resultMap.put("state", JsonResult.SUCCESS); }else if(userRelationShips.size() == 0){ resultMap.put("data",relationUsers); resultMap.put("message", "你還沒關注任何人"); resultMap.put("state", JsonResult.SUCCESS); }else { resultMap.put("message", "查詢關注人失敗"); resultMap.put("state", JsonResult.failing); } return resultMap; }
以上代碼所用的Sql語句:selectUserAttention
<!--我的關注--> <select id="selectUserAttention" parameterType="Map" resultMap="BasePlusResultMap"> select * from follower where (follower.user_id = #{userId} and follower.relation in ('00','02')) or (follower.follower_id = #{followerId} and follower.relation in ('01','02')) LIMIT #{page}, #{size} </select>
5、獲取我的粉絲
/** * 獲取我的粉絲列表 * @param userId * @param page * @param pageSize * @return */ @GetMapping("/selectUserVermicelli") public Map<String,Object> selectUserVermicelli(String userId,int page,int pageSize){ Map<String,Object> resultMap = new HashMap<>(); List<RelationShipUser> relationUsers = new ArrayList<>(); Map<String,Object> paramMap = new HashMap<>(); paramMap.put("userId",userId); paramMap.put("followerId",userId); int pageCount = (page)* pageSize; paramMap.put("page", pageCount); paramMap.put("size",pageSize); List<Fans> userRelationShips = fansUserService.selectUserVermicelli(paramMap); if(userRelationShips.size() > 0){ List<String> userIds = new ArrayList<>(); for (int index = 0;index < userRelationShips.size();index++) { Fans userRelationShip = userRelationShips.get(index); String relationShipUserId = userRelationShip.getUserId(); String relationShipFollowerId = userRelationShip.getFollowerId(); if (relationShipFollowerId.equals(userId)) { userIds.add(relationShipUserId); } else { userIds.add(relationShipFollowerId); } } System.out.println(userIds); //獲取用戶 List<UserClone> users = relationShipService.queryUserWithIds(userIds); if (users.size() > 0) { for (int index = 0;index < users.size(); index++) { RelationShipUser relationShipUser = new RelationShipUser(); relationShipUser.setUser(users.get(index)); relationUsers.add(relationShipUser); } } for (int idx = 0;idx < relationUsers.size();idx++) { RelationShipUser relationShipUser = relationUsers.get(idx); String targetId = relationShipUser.getUser().getUserId(); int relationStatus = checkRelationStatus(userId,targetId,userRelationShips); relationShipUser.setStatus(relationStatus); relationUsers.set(idx,relationShipUser); } resultMap.put("data",relationUsers); resultMap.put("message", "查詢成功"); resultMap.put("state", JsonResult.SUCCESS); }else if(userRelationShips.size() == 0){ resultMap.put("data",relationUsers); resultMap.put("message", ""); resultMap.put("state", JsonResult.SUCCESS); }else { resultMap.put("message", "查詢失敗"); resultMap.put("state", JsonResult.failing); } return resultMap; }
以上代碼所用到的Sql:selectUserVermicelli
<!-- <!–我的粉絲--> <select id="selectUserVermicelli" resultMap="BasePlusResultMap" parameterType="Map"> select u2.nickname, u2.phone,u2.avatar,follower.follower_id ,follower.relation_id,u2.user_id,follower.relation,u2.description,follower.user_id from follower inner join dnx_app_user u1 on follower.follower_id = u1.user_id inner join dnx_app_user u2 on follower.user_id = u2.user_id where (follower.user_id = #{userId} and follower.relation in ('01','02')) or (follower.follower_id = #{followerId} and follower.relation in ('00','02')) LIMIT #{page}, #{size} </select>