完善搜索好友接口

master
pan 5 years ago
parent 3cd574dea8
commit 3b788177d8
  1. 4
      src/main/java/com/share/help/dao/FriendMapper.java
  2. 10
      src/main/java/com/share/help/form/SearchFrom.java
  3. 4
      src/main/java/com/share/help/service/UserService.java
  4. 22
      src/main/resources/mapping/FriendMapper.xml
  5. 6
      src/test/java/com/share/help/HelpApplicationTests.java

@ -26,12 +26,12 @@ public interface FriendMapper {
* @param userId 目标用户 * @param userId 目标用户
* @return 返回好友id * @return 返回好友id
*/ */
List<Friend> getFriend(@Param("userId") String userId, @Param("start") long start, @Param("count") long count); List<Friend> getFriend(@Param("userId") String userId, @Param("name") String name,@Param("start") long start, @Param("count") long count);
/** /**
* 统计好友分页数 * 统计好友分页数
* @param userId 目标用户 * @param userId 目标用户
* @return 返回分页数 * @return 返回分页数
*/ */
Page<List<Friend>> count(@Param("userId") String userId,@Param("pageSize") Integer pageSize); Page<List<Friend>> count(@Param("userId") String userId, @Param("name") String name,@Param("pageSize") Integer pageSize);
} }

@ -6,9 +6,19 @@ package com.share.help.form;
public class SearchFrom { public class SearchFrom {
// 用户id // 用户id
private String userId; private String userId;
//用户名
private String name;
//分页数 //分页数
private Integer currentPage; private Integer currentPage;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCurrentPage() { public Integer getCurrentPage() {
return currentPage; return currentPage;
} }

@ -205,8 +205,8 @@ public class UserService{
* @return 返回好友列表 * @return 返回好友列表
*/ */
public JSONResponse<DefaultRes, Page<List<Friend>>> searchFriend(SearchFrom searchFrom) { public JSONResponse<DefaultRes, Page<List<Friend>>> searchFriend(SearchFrom searchFrom) {
Page<List<Friend>> page=friendMapper.count(searchFrom.getUserId(),friendSize); Page<List<Friend>> page=friendMapper.count(searchFrom.getUserId(),searchFrom.getName(),friendSize);
List<Friend> list=friendMapper.getFriend(searchFrom.getUserId(),(searchFrom.getCurrentPage()-1)*friendSize,friendSize); List<Friend> list=friendMapper.getFriend(searchFrom.getUserId(),searchFrom.getName(),(searchFrom.getCurrentPage()-1)*friendSize,friendSize);
page.setBody(list); page.setBody(list);
return new JSONResponse<DefaultRes,Page<List<Friend>>>(DefaultRes.ok,Result.OK).setBody(page); return new JSONResponse<DefaultRes,Page<List<Friend>>>(DefaultRes.ok,Result.OK).setBody(page);
} }

@ -10,12 +10,28 @@
select source_user_id as userId from friend where source_user_id!=#{userId} and target_user_id=#{userId} select source_user_id as userId from friend where source_user_id!=#{userId} and target_user_id=#{userId}
union all union all
select target_user_id as userId from friend where target_user_id!=#{userId} and source_user_id=#{userId}) select target_user_id as userId from friend where target_user_id!=#{userId} and source_user_id=#{userId})
<if test="name!=null and name!=''">
and name like '%${name}%'
</if>
limit #{start},#{count} limit #{start},#{count}
</select> </select>
<select id="count" resultType="com.share.help.res.Page"> <select id="count" resultType="com.share.help.res.Page">
select count,ceil(count/#{pageSize}) as totalPage from( select count(*) as count,ceil(count(*)/#{pageSize}) as totalPage
(select (select count(*) from friend where source_user_id!=#{userId})+ from user
(select count(*) from friend where target_user_id!=#{userId}) as count)) as c 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}
)
<if test="name!=null and name!=''">
and name like '%${name}%'
</if>
</select> </select>
</mapper> </mapper>

@ -226,10 +226,10 @@ class HelpApplicationTests {
*/ */
@Test @Test
public void queryFriend(){ public void queryFriend(){
Page<List<Friend>> page=friendMapper.count("123",3); Page<List<Friend>> page=friendMapper.count("123","6",3);
Assertions.assertEquals(1, (long) page.getCount()); Assertions.assertEquals(1, (long) page.getCount());
List<Friend> user=friendMapper.getFriend("123",0,3); List<Friend> user=friendMapper.getFriend("123","6",0,3);
Assertions.assertTrue(!user.isEmpty()&&"222".equals(user.get(0).getUserId())); Assertions.assertEquals(1,user.size());
} }
/** /**

Loading…
Cancel
Save