diff --git a/src/main/java/com/share/help/Constants.java b/src/main/java/com/share/help/Constants.java index ee8d627..b5e1261 100644 --- a/src/main/java/com/share/help/Constants.java +++ b/src/main/java/com/share/help/Constants.java @@ -35,7 +35,7 @@ public class Constants { public static final String ACTIVITY_INTERFACE_APPLY = "/apply"; //用户查询信息接口 - public static final String USER_INTERFACE_FIND_ID = USER_INTERFACE_FIND+"/{userId}"; + public static final String USER_INTERFACE_FIND_ID = USER_INTERFACE_FIND+"/{targetUserId}"; //用户信息更新接口 public static final String USER_INTERFACE_UPDATE = "/update"; @@ -59,4 +59,9 @@ public class Constants { public static final String USER_INTERFACE_REFUSE_FRIEND = "/refuse/{id}"; //同意好友邀请 public static final String USER_INTERFACE_AGREE_FRIEND = "/agree"; + +// 好友列表 + public static final String USER_INTERFACE_FRIEND_LIST = "/friend/list"; +// 刷新聊天室状态 + public static final String USER_INTERFACE_CHAT_STATUS = "/chat/status"; } diff --git a/src/main/java/com/share/help/controller/UserController.java b/src/main/java/com/share/help/controller/UserController.java index 7485ed7..04391a2 100644 --- a/src/main/java/com/share/help/controller/UserController.java +++ b/src/main/java/com/share/help/controller/UserController.java @@ -2,6 +2,7 @@ package com.share.help.controller; import com.share.help.Constants; import com.share.help.entity.custorm.FindUserRes; +import com.share.help.entity.custorm.Friend; import com.share.help.entity.custorm.LeaveWordRes; import com.share.help.entity.custorm.UserWithName; import com.share.help.form.*; @@ -88,9 +89,9 @@ public class UserController { * @return 返回用户信息 */ @GetMapping(Constants.USER_INTERFACE_FIND_ID) - public JSONResponse find(@PathVariable String userId){ - if(userId!=null){ - return userService.find(userId); + public JSONResponse find(@PathVariable String targetUserId,@CookieValue(Constants.USER_COOKIE) String userId){ + if(!StringUtils.isAllEmpty(targetUserId,userId)){ + return userService.find(targetUserId,userId); }else{ return new JSONResponse<>(DefaultRes.fail,Result.FAIL); } @@ -136,8 +137,9 @@ public class UserController { * @return 返回查询结果 */ @GetMapping(Constants.USER_INTERFACE_FIND_NAME) - public JSONResponse> queryUser(QueryUserForm queryUserForm){ - if(StringUtils.isNoneEmpty(queryUserForm.getUserName(),queryUserForm.getInfo())){ + public JSONResponse>> queryUser(QueryUserForm queryUserForm,@CookieValue(Constants.USER_COOKIE) String userId){ + if(userId!=null){ + queryUserForm.setUserId(userId); return userService.queryUser(queryUserForm); }else{ return new JSONResponse<>(DefaultRes.fail, Result.FAIL); @@ -186,6 +188,12 @@ public class UserController { } } + /** + * 同意好友邀请 + * @param agreeFriendForm 邀请表单 + * @param userId 邀请用户 + * @return 返回同意结果 + */ @PostMapping(Constants.USER_INTERFACE_AGREE_FRIEND) public JSONResponse agreeFriend(AgreeFriendForm agreeFriendForm,@CookieValue(Constants.USER_COOKIE) String userId){ if(ObjectUtils.allNotNull(agreeFriendForm.getSourceUserId(),userId,agreeFriendForm.getLeaveWordId())){ @@ -196,4 +204,34 @@ public class UserController { } } + /** + * 检索好友 + * @param searchFrom 检索好友表单 + * @return 返回检索结果 + */ + @GetMapping(Constants.USER_INTERFACE_FRIEND_LIST) + public JSONResponse>> searchFriend(SearchFrom searchFrom, @CookieValue(Constants.USER_COOKIE) String userId){ + if(ObjectUtils.isNotEmpty(searchFrom.getCurrentPage())&&StringUtils.isNotEmpty(userId)){ + searchFrom.setUserId(userId); + return userService.searchFriend(searchFrom); + }else{ + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + } + + /** + * 刷新用户的聊天状态 + * @param chatForm 刷新表单 + * @param userId 用户id + * @return 返回刷新结果 + */ + @PostMapping(Constants.USER_INTERFACE_CHAT_STATUS) + public JSONResponse changeChat(ChatForm chatForm,@CookieValue(Constants.USER_COOKIE) String userId){ + if(ObjectUtils.allNotNull(chatForm.getChatStatus(),userId)){ + chatForm.setUserId(userId); + return userService.changeChat(chatForm); + }else{ + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + } } diff --git a/src/main/java/com/share/help/dao/FriendMapper.java b/src/main/java/com/share/help/dao/FriendMapper.java index de758ab..2479d2a 100644 --- a/src/main/java/com/share/help/dao/FriendMapper.java +++ b/src/main/java/com/share/help/dao/FriendMapper.java @@ -1,13 +1,37 @@ package com.share.help.dao; import com.share.help.entity.FriendEntity; +import com.share.help.entity.custorm.Friend; +import com.share.help.res.Page; import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Param; + +import java.util.List; /** * 添加好友 */ public interface FriendMapper { + /** + * 添加好友邀请 + * @param friendEntity 好友邀请 + * @return 返回添加结果 + */ @Insert("insert into friend(source_user_id,target_user_id) value(#{sourceUserId},#{targetUserId})") boolean addFriend(FriendEntity friendEntity); + + /** + * 获取好友邀请 + * @param userId 目标用户 + * @return 返回好友id + */ + List getFriend(@Param("userId") String userId, @Param("start") long start, @Param("count") long count); + + /** + * 统计好友分页数 + * @param userId 目标用户 + * @return 返回分页数 + */ + Page> count(@Param("userId") String userId,@Param("pageSize") Integer pageSize); } diff --git a/src/main/java/com/share/help/dao/LeaveWordMapper.java b/src/main/java/com/share/help/dao/LeaveWordMapper.java index ef5502b..15c0a03 100644 --- a/src/main/java/com/share/help/dao/LeaveWordMapper.java +++ b/src/main/java/com/share/help/dao/LeaveWordMapper.java @@ -21,9 +21,7 @@ public interface LeaveWordMapper { boolean insert(LeaveWordEntity leaveWordEntity); // 查询留言列表 - @Select("select leave_word.source_user_id as userId,user.name,user.head_img as headImg,user.info,leave_word.type,activity_id as activityId,leave_word_id as leaveWordId from leave_word,user\n" + - "where leave_word.target_user_id=#{userId} and leave_word.target_user_id=user.user_id order by create_time desc limit #{start},#{count}") - List select(@Param("start") long start,@Param("count") long count,@Param("userId") String userId); + List select(@Param("start") long start,@Param("count") long count,@Param("userId") String userId); // 统计留言列表分页数 @Select("select count(*) as count,ceil(count(*)/#{pageSize}) as totalPage from leave_word where target_user_id=#{userId}") diff --git a/src/main/java/com/share/help/dao/UserMapper.java b/src/main/java/com/share/help/dao/UserMapper.java index 0ade9a9..092519b 100644 --- a/src/main/java/com/share/help/dao/UserMapper.java +++ b/src/main/java/com/share/help/dao/UserMapper.java @@ -3,6 +3,8 @@ package com.share.help.dao; import com.share.help.entity.UserEntity; import com.share.help.entity.custorm.FindUserRes; import com.share.help.entity.custorm.UserWithName; +import com.share.help.res.Page; +import com.share.help.res.account.QueryRes; import org.apache.ibatis.annotations.*; import java.util.List; @@ -89,5 +91,15 @@ public interface UserMapper { /** * 查找用户 */ - List queryUser(@Param("name") String name,@Param("info") String info); + List queryUser(@Param("name") String name, @Param("info") String info,@Param("userId") String userId); + + Page> countUser(@Param("name") String name, @Param("info") String info,@Param("userId") String userId,@Param("pageSize") Integer pageSize); + + /** + * 查看用户信息 + * @param targetUserId 被查看用户id + * @param userId 查看用户id + * @return 返回用户信息 + */ + QueryRes findInfo(@Param("targetUserId") String targetUserId,@Param("userId") String userId); } diff --git a/src/main/java/com/share/help/entity/custorm/Friend.java b/src/main/java/com/share/help/entity/custorm/Friend.java new file mode 100644 index 0000000..29a6d6e --- /dev/null +++ b/src/main/java/com/share/help/entity/custorm/Friend.java @@ -0,0 +1,16 @@ +package com.share.help.entity.custorm; + +/** + * 我的好友信息 + */ +public class Friend extends UserWithName{ + private Boolean chatStatus; + + public Boolean getChatStatus() { + return chatStatus; + } + + public void setChatStatus(Boolean chatStatus) { + this.chatStatus = chatStatus; + } +} diff --git a/src/main/java/com/share/help/form/ChatForm.java b/src/main/java/com/share/help/form/ChatForm.java new file mode 100644 index 0000000..3ffe7f3 --- /dev/null +++ b/src/main/java/com/share/help/form/ChatForm.java @@ -0,0 +1,27 @@ +package com.share.help.form; + +/** + * 刷新我在聊天室的状态 + */ +public class ChatForm { + + private String userId; + + private Boolean chatStatus; + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public Boolean getChatStatus() { + return chatStatus; + } + + public void setChatStatus(Boolean chatStatus) { + this.chatStatus = chatStatus; + } +} diff --git a/src/main/java/com/share/help/form/QueryUserForm.java b/src/main/java/com/share/help/form/QueryUserForm.java index 6f7b0d6..5a28d4a 100644 --- a/src/main/java/com/share/help/form/QueryUserForm.java +++ b/src/main/java/com/share/help/form/QueryUserForm.java @@ -7,7 +7,7 @@ public class QueryUserForm { //用户id private String userId; //用户名 - private String userName; + private String name; //简介信息 private String info; @@ -19,12 +19,12 @@ public class QueryUserForm { this.userId = userId; } - public String getUserName() { - return userName; + public String getName() { + return name; } - public void setUserName(String userName) { - this.userName = userName; + public void setName(String name) { + this.name = name; } public String getInfo() { diff --git a/src/main/java/com/share/help/form/SearchFrom.java b/src/main/java/com/share/help/form/SearchFrom.java new file mode 100644 index 0000000..7c8e539 --- /dev/null +++ b/src/main/java/com/share/help/form/SearchFrom.java @@ -0,0 +1,27 @@ +package com.share.help.form; + +/** + * 检索好友表单 + */ +public class SearchFrom { +// 用户id + private String userId; + //分页数 + private Integer currentPage; + + public Integer getCurrentPage() { + return currentPage; + } + + public void setCurrentPage(Integer currentPage) { + this.currentPage = currentPage; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } +} diff --git a/src/main/java/com/share/help/form/UserLoginForm.java b/src/main/java/com/share/help/form/UserLoginForm.java index e1a5807..9e48aa2 100644 --- a/src/main/java/com/share/help/form/UserLoginForm.java +++ b/src/main/java/com/share/help/form/UserLoginForm.java @@ -1,7 +1,25 @@ package com.share.help.form; -import com.share.help.entity.UserEntity; +/** + * 登陆表单 + */ +public class UserLoginForm{ + private String userId; + private String password; -public class UserLoginForm extends UserEntity { + public String getUserId() { + return userId; + } + public void setUserId(String userId) { + this.userId = userId; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } } diff --git a/src/main/java/com/share/help/res/account/QueryRes.java b/src/main/java/com/share/help/res/account/QueryRes.java index a85591a..a622699 100644 --- a/src/main/java/com/share/help/res/account/QueryRes.java +++ b/src/main/java/com/share/help/res/account/QueryRes.java @@ -1,5 +1,7 @@ package com.share.help.res.account; +import com.fasterxml.jackson.annotation.JsonProperty; + public class QueryRes { private String userId; private String name; @@ -12,6 +14,16 @@ public class QueryRes { private String email; private String info; private Integer age; + private boolean isMyFriend; + + @JsonProperty("isMyFriend") + public boolean isMyFriend() { + return isMyFriend; + } + + public void setMyFriend(boolean myFriend) { + isMyFriend = myFriend; + } public String getUserId() { return userId; diff --git a/src/main/java/com/share/help/service/UserService.java b/src/main/java/com/share/help/service/UserService.java index 71c5b16..e393bbd 100644 --- a/src/main/java/com/share/help/service/UserService.java +++ b/src/main/java/com/share/help/service/UserService.java @@ -1,18 +1,23 @@ package com.share.help.service; import com.share.help.Util; +import com.share.help.dao.FriendMapper; import com.share.help.dao.UserMapper; import com.share.help.entity.UserEntity; import com.share.help.entity.custorm.FindUserRes; +import com.share.help.entity.custorm.Friend; import com.share.help.entity.custorm.UserWithName; import com.share.help.form.*; import com.share.help.res.DefaultRes; import com.share.help.res.JSONResponse; +import com.share.help.res.Page; import com.share.help.res.Result; import com.share.help.res.account.LoginRes; import com.share.help.res.account.ModifyPwdRes; import com.share.help.res.account.QueryRes; import com.share.help.res.account.RegisterRes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -34,14 +39,22 @@ public class UserService{ //用户分页配置 @Value("${page-size.user.page-size}") - private int pageSize; + private int userPageSize; @Value("${page-size.user.row-size}") - private int rowSize; + private int userRowSize; @Autowired private ImageService imageService; + @Autowired + private FriendMapper friendMapper; + + @Value("${page-size.friend}") + private int friendSize; + + private Logger logger= LoggerFactory.getLogger(UserService.class); + /** * 注册 * @param userRegisterForm 注册表单 @@ -64,6 +77,7 @@ public class UserService{ //保存头像 String imageName=imageService.saveImg(userRegisterForm.getImgFile()); if(imageName==null){ + logger.error("保存头像失败"); return new JSONResponse<>(RegisterRes.fail, Result.FAIL); } userEntity.setHeadImg(imageName); @@ -96,13 +110,13 @@ public class UserService{ * @return 返回志愿者列表 */ public JSONResponse find(FindUserForm findUserForm){ - FindUserRes findUserRes=userMapper.count(pageSize,findUserForm.getUserTypes(),findUserForm.getInfo(),findUserForm.getServiceAddress()); - findUserRes.setPageSize(pageSize); + FindUserRes findUserRes=userMapper.count(userPageSize,findUserForm.getUserTypes(),findUserForm.getInfo(),findUserForm.getServiceAddress()); + findUserRes.setPageSize(userPageSize); findUserRes.setCurrentPage(findUserForm.getCurrentPage()); - List userEntityList=userMapper.getPage((findUserForm.getCurrentPage()-1)*pageSize,pageSize,findUserForm.getUserTypes(), + List userEntityList=userMapper.getPage((findUserForm.getCurrentPage()-1)* userPageSize, userPageSize,findUserForm.getUserTypes(), findUserForm.getInfo(),findUserForm.getServiceAddress()); if(!userEntityList.isEmpty()){ - findUserRes.setBody(Util.splitList(UserEntity.class,userEntityList,rowSize)); + findUserRes.setBody(Util.splitList(UserEntity.class,userEntityList, userRowSize)); return new JSONResponse(FindUserRes.Res.ok,Result.OK).setBody(findUserRes); }else{ return new JSONResponse<>(FindUserRes.Res.empty, Result.OK); @@ -111,23 +125,14 @@ public class UserService{ /** * 获取用户个人信息 + * + * @param targetUserId 目标用户 * @param userId 用户id * @return 返回用户信息 */ - public JSONResponse find(String userId){ - UserEntity userEntity= userMapper.findOne(userId); - if(userEntity!=null){ - QueryRes queryRes=new QueryRes(); - queryRes.setUserId(userEntity.getUserId()); - queryRes.setHeadImg(userEntity.getHeadImg()); - queryRes.setName(userEntity.getName()); - queryRes.setAge(userEntity.getAge()); - queryRes.setMobile(userEntity.getMobile()); - queryRes.setEmail(userEntity.getEmail()); - queryRes.setServiceAddress(userEntity.getServiceAddress()); - queryRes.setInfo(userEntity.getInfo()); - queryRes.setUserType(userEntity.getUserType()); - queryRes.setSex(userEntity.getSex()); + public JSONResponse find(String targetUserId, String userId){ + QueryRes queryRes= userMapper.findInfo(targetUserId,userId); + if(queryRes!=null){ return new JSONResponse(DefaultRes.ok,Result.OK).setBody(queryRes); }else{ return new JSONResponse<>(DefaultRes.fail,Result.FAIL); @@ -187,9 +192,40 @@ public class UserService{ * @param queryUserForm 搜索条件 * @return 返回查询结果 */ - public JSONResponse> queryUser(QueryUserForm queryUserForm){ - List userWithNameList=userMapper.queryUser(queryUserForm.getUserName(),queryUserForm.getInfo()); - return new JSONResponse>(DefaultRes.ok,Result.OK).setBody(userWithNameList); + public JSONResponse>> queryUser(QueryUserForm queryUserForm){ + Page> page=userMapper.countUser(queryUserForm.getName(),queryUserForm.getInfo(),queryUserForm.getUserId(),userPageSize); + List userWithNameList=userMapper.queryUser(queryUserForm.getName(),queryUserForm.getInfo(),queryUserForm.getUserId()); + page.setBody(userWithNameList); + return new JSONResponse>>(DefaultRes.ok,Result.OK).setBody(page); } + /** + * 检索好友表单 + * @param searchFrom 检索条件 + * @return 返回好友列表 + */ + public JSONResponse>> searchFriend(SearchFrom searchFrom) { + Page> page=friendMapper.count(searchFrom.getUserId(),friendSize); + List list=friendMapper.getFriend(searchFrom.getUserId(),(searchFrom.getCurrentPage()-1)*friendSize,friendSize); + page.setBody(list); + return new JSONResponse>>(DefaultRes.ok,Result.OK).setBody(page); + } + + /** + * 刷新聊天状态 + * @param chatForm 表单 + * @return 返回刷新结果 + */ + public JSONResponse changeChat(ChatForm chatForm) { + UserEntity userEntity=userMapper.findOne(chatForm.getUserId()); + if(userEntity==null){ + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + userEntity.setChatStatus(chatForm.getChatStatus()); + if(userMapper.update(userEntity)){ + return new JSONResponse<>(DefaultRes.ok,Result.OK); + }else{ + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + } } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 4f31cf0..3ed6af9 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -44,4 +44,6 @@ page-size: page-size: 6 # 前端一行显示数量 row-size: 3 + #好友列表 + friend: 3 diff --git a/src/main/resources/mapping/FriendMapper.xml b/src/main/resources/mapping/FriendMapper.xml new file mode 100644 index 0000000..1321ffc --- /dev/null +++ b/src/main/resources/mapping/FriendMapper.xml @@ -0,0 +1,21 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/mapping/LeaveWordMapper.xml b/src/main/resources/mapping/LeaveWordMapper.xml new file mode 100644 index 0000000..f7e7f63 --- /dev/null +++ b/src/main/resources/mapping/LeaveWordMapper.xml @@ -0,0 +1,21 @@ + + + + + \ No newline at end of file diff --git a/src/main/resources/mapping/UserMapper.xml b/src/main/resources/mapping/UserMapper.xml index 12ff86e..d241209 100644 --- a/src/main/resources/mapping/UserMapper.xml +++ b/src/main/resources/mapping/UserMapper.xml @@ -5,7 +5,6 @@ - @@ -13,6 +12,21 @@ + + + + + + + + + - select user_id as userId,name from user + + + + user_id not in( + select user_id from user where user_id in ( + select source_user_id as userId from friend where source_user_id!=#{userId} and target_user_id=#{userId} + union all + select target_user_id as userId from friend where target_user_id!=#{userId} and source_user_id=#{userId}) + ) - name like '%${name}%' + and name like '%${name}%' and info like '%${info}%' + + + + + + \ No newline at end of file diff --git a/src/test/java/com/share/help/HelpApplicationTests.java b/src/test/java/com/share/help/HelpApplicationTests.java index 9e3067f..67555d1 100644 --- a/src/test/java/com/share/help/HelpApplicationTests.java +++ b/src/test/java/com/share/help/HelpApplicationTests.java @@ -1,14 +1,12 @@ package com.share.help; -import com.share.help.dao.ActivityHistoryMapper; -import com.share.help.dao.ActivityMapper; -import com.share.help.dao.LeaveWordMapper; -import com.share.help.dao.UserMapper; +import com.share.help.dao.*; import com.share.help.entity.ActivityEntity; import com.share.help.entity.UserEntity; import com.share.help.entity.custorm.*; import com.share.help.form.ActivityStatus; import com.share.help.res.Page; +import com.share.help.res.account.QueryRes; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -26,6 +24,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.context.WebApplicationContext; +import javax.servlet.http.Cookie; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; @@ -53,6 +52,9 @@ class HelpApplicationTests { @Autowired private LeaveWordMapper leaveWordMapper; + @Autowired + private FriendMapper friendMapper; + private Logger logger = LoggerFactory.getLogger(HelpApplicationTests.class); @BeforeEach @@ -190,19 +192,22 @@ class HelpApplicationTests { //查询用户列表 @Test public void queryUser(){ - List userWithNames =userMapper.queryUser("6",null); + Page> count=userMapper.countUser("6",null,"123",10); + Assertions.assertTrue(count.getCount()>0); + + List userWithNames =userMapper.queryUser("2",null,"123"); Assertions.assertFalse(userWithNames.isEmpty()); - userWithNames =userMapper.queryUser("6","2"); + userWithNames =userMapper.queryUser("2","2", "123"); Assertions.assertFalse(userWithNames.isEmpty()); } //留言列表 @Test public void queryLeaveWord(){ - Page> listPage=leaveWordMapper.count(10,"222"); + Page> listPage=leaveWordMapper.count(10,"777"); Assertions.assertNotNull(listPage); - List leaveWordRes=leaveWordMapper.select(0,3,"222"); + List leaveWordRes=leaveWordMapper.select(0,3,"777"); Assertions.assertFalse(leaveWordRes.isEmpty()); } @@ -214,4 +219,36 @@ class HelpApplicationTests { List list= activityHistoryMapper.queryMyHelp(0,2,"123"); Assertions.assertFalse(list.isEmpty()); } + + /** + * 查找好友 + */ + @Test + public void queryFriend(){ + Page> page=friendMapper.count("123",3); + Assertions.assertEquals(1, (long) page.getCount()); + List user=friendMapper.getFriend("123",0,3); + Assertions.assertTrue(!user.isEmpty()&&"222".equals(user.get(0).getUserId())); + } + + /** + * 查找用户信息 + */ + @Test + public void queryUserInfo(){ + QueryRes queryRes= userMapper.findInfo("123","222"); + Assertions.assertTrue(queryRes.isMyFriend()); + + queryRes= userMapper.findInfo("123","777"); + Assertions.assertFalse(queryRes.isMyFriend()); + } + + @Test + public void testQueryUserCtrl() throws Exception { + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.USER_INTERFACE+ + Constants.USER_INTERFACE_FIND_ID.replace("{targetUserId}","123")).cookie(new Cookie("user_cookie","777")); + mvc.perform(builder).andDo(MockMvcResultHandlers.print()); + } + + }