diff --git a/src/main/java/com/share/help/Constants.java b/src/main/java/com/share/help/Constants.java index 3dcfa02..4da6d42 100644 --- a/src/main/java/com/share/help/Constants.java +++ b/src/main/java/com/share/help/Constants.java @@ -22,4 +22,7 @@ public class Constants { //查找活动列表 public static final String ACTIVITY_INTERFACE_FIND = "/find"; + + //查找用户接口 + public static final String USER_INTERFACE_FIND = "/find"; } diff --git a/src/main/java/com/share/help/Util.java b/src/main/java/com/share/help/Util.java new file mode 100644 index 0000000..e3fe2a7 --- /dev/null +++ b/src/main/java/com/share/help/Util.java @@ -0,0 +1,29 @@ +package com.share.help; + +import java.util.ArrayList; +import java.util.List; + +public class Util { + + /** + * 按分割大小切割子集合 + * @param dataList 列表数据 + * @param splitSize 分割大小 + * @param 数据类型 + * @return 返回包含所有子集的集合 + */ + public static List> splitList(Class c,List dataList, int splitSize){ + List> splitList=new ArrayList<>(); + if(dataList.size()= dataList.size()) { + splitList.add(dataList.subList(i, dataList.size())); + } + } + } + return splitList; + } +} diff --git a/src/main/java/com/share/help/controller/ActivityController.java b/src/main/java/com/share/help/controller/ActivityController.java index 29e59c2..06e0810 100644 --- a/src/main/java/com/share/help/controller/ActivityController.java +++ b/src/main/java/com/share/help/controller/ActivityController.java @@ -3,10 +3,10 @@ package com.share.help.controller; import com.share.help.Constants; import com.share.help.form.FindActivityForm; import com.share.help.form.SendHelpForm; -import com.share.help.res.FindActivityRes; import com.share.help.res.JSONResponse; import com.share.help.res.Result; import com.share.help.res.SendHelpRes; +import com.share.help.res.activity.FindActivityRes; import com.share.help.service.ActivityService; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; diff --git a/src/main/java/com/share/help/controller/UserController.java b/src/main/java/com/share/help/controller/UserController.java index c9f6e3a..4ca6ed5 100644 --- a/src/main/java/com/share/help/controller/UserController.java +++ b/src/main/java/com/share/help/controller/UserController.java @@ -1,20 +1,19 @@ package com.share.help.controller; import com.share.help.Constants; +import com.share.help.form.FindUserForm; import com.share.help.form.UserLoginForm; import com.share.help.form.UserRegisterForm; import com.share.help.res.JSONResponse; import com.share.help.res.Result; +import com.share.help.res.account.FindUserRes; import com.share.help.res.account.LoginRes; import com.share.help.res.account.RegisterRes; import com.share.help.service.UserService; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; /** * 用户接口 @@ -45,6 +44,11 @@ public class UserController { } + /** + * 登录 + * @param userLoginForm 登陆表单 + * @return 返回登陆结果 + */ @PostMapping(Constants.USER_INTERFACE_LOGIN) public JSONResponse login(UserLoginForm userLoginForm){ if(StringUtils.isNoneEmpty(userLoginForm.getUserId(),userLoginForm.getPassword())){ @@ -53,4 +57,18 @@ public class UserController { return new JSONResponse<>(LoginRes.Res.form_error,Result.FAIL); } } + + /** + * 查找用户 + * @param findUserForm 查找表单 + * @return 返回用户列表 + */ + @GetMapping(Constants.USER_INTERFACE_FIND) + public JSONResponse find(FindUserForm findUserForm){ + if(findUserForm.getCurrentPage()!=null){ + return userService.find(findUserForm); + }else{ + return new JSONResponse<>(FindUserRes.Res.fail,Result.FAIL); + } + } } diff --git a/src/main/java/com/share/help/dao/ActivityMapper.java b/src/main/java/com/share/help/dao/ActivityMapper.java index 9397517..1e2cc2d 100644 --- a/src/main/java/com/share/help/dao/ActivityMapper.java +++ b/src/main/java/com/share/help/dao/ActivityMapper.java @@ -1,8 +1,8 @@ package com.share.help.dao; import com.share.help.entity.ActivityEntity; -import com.share.help.res.ActivitySimple; -import com.share.help.res.FindActivityRes; +import com.share.help.res.activity.ActivitySimple; +import com.share.help.res.activity.FindActivityRes; import org.apache.ibatis.annotations.*; import java.util.List; @@ -47,6 +47,11 @@ public interface ActivityMapper { @ResultMap("activitySimpleMap") List getPage(@Param("start") Integer start, @Param("count") Integer count); + /** + * 获取活动列表分页数 + * @param pageSize 分页大小 + * @return 返回分页信息 + */ @Select("select count(*) as count,ceil(count(*)/#{pageSize}) as totalPage from activity") FindActivityRes count(@Param("pageSize") Integer pageSize); } diff --git a/src/main/java/com/share/help/dao/UserMapper.java b/src/main/java/com/share/help/dao/UserMapper.java index 3aaa3ad..3aff563 100644 --- a/src/main/java/com/share/help/dao/UserMapper.java +++ b/src/main/java/com/share/help/dao/UserMapper.java @@ -1,6 +1,7 @@ package com.share.help.dao; import com.share.help.entity.UserEntity; +import com.share.help.res.account.FindUserRes; import org.apache.ibatis.annotations.*; import java.util.List; @@ -71,7 +72,28 @@ public interface UserMapper { * @param count 查询个数 * @return 返回用户列表 */ - @Select("select * from user limit #{start},#{count}") + @Select( " " ) @ResultMap("userMap") - List getPage(@Param("start") long start,@Param("count") long count); + List getPage(@Param("start") Integer start, @Param("count") Integer count, + @Param("userTypes") List userTypes,@Param("info") String info,@Param("serviceAddress") String serviceAddress); + + /** + * 获取志愿者分页数 + * @param pageSize 分页大小 + * @return 返回分页信息 + */ + @Select( " " ) + FindUserRes count(@Param("pageSize") Integer pageSize, @Param("userTypes") List userTypes,@Param("info") + String info,@Param("serviceAddress") String serviceAddress); } diff --git a/src/main/java/com/share/help/form/FindUserForm.java b/src/main/java/com/share/help/form/FindUserForm.java new file mode 100644 index 0000000..403211d --- /dev/null +++ b/src/main/java/com/share/help/form/FindUserForm.java @@ -0,0 +1,49 @@ +package com.share.help.form; + +import java.util.List; + +/** + * 查找用户列表 + */ +public class FindUserForm { + //查找分页 + private Integer currentPage; + //查找用户类型 + private List userTypes; + //描述信息 + private String info; + //服务地点 + private String serviceAddress; + + public String getInfo() { + return info; + } + + public void setInfo(String info) { + this.info = info; + } + + public String getServiceAddress() { + return serviceAddress; + } + + public void setServiceAddress(String serviceAddress) { + this.serviceAddress = serviceAddress; + } + + public Integer getCurrentPage() { + return currentPage; + } + + public void setCurrentPage(Integer currentPage) { + this.currentPage = currentPage; + } + + public List getUserTypes() { + return userTypes; + } + + public void setUserTypes(List userTypes) { + this.userTypes = userTypes; + } +} diff --git a/src/main/java/com/share/help/res/account/FindUserRes.java b/src/main/java/com/share/help/res/account/FindUserRes.java new file mode 100644 index 0000000..1560f80 --- /dev/null +++ b/src/main/java/com/share/help/res/account/FindUserRes.java @@ -0,0 +1,20 @@ +package com.share.help.res.account; + +import com.share.help.entity.UserEntity; +import com.share.help.res.Page; + +import java.util.List; + +/** + * 用户列表 + */ +public class FindUserRes extends Page>> { + + + public enum Res{ + ok, +// 空数据 + empty, + fail + } +} diff --git a/src/main/java/com/share/help/res/ActivitySimple.java b/src/main/java/com/share/help/res/activity/ActivitySimple.java similarity index 95% rename from src/main/java/com/share/help/res/ActivitySimple.java rename to src/main/java/com/share/help/res/activity/ActivitySimple.java index d6a4590..5694d3f 100644 --- a/src/main/java/com/share/help/res/ActivitySimple.java +++ b/src/main/java/com/share/help/res/activity/ActivitySimple.java @@ -1,4 +1,4 @@ -package com.share.help.res; +package com.share.help.res.activity; /** * 活动列表实体 diff --git a/src/main/java/com/share/help/res/FindActivityRes.java b/src/main/java/com/share/help/res/activity/FindActivityRes.java similarity index 63% rename from src/main/java/com/share/help/res/FindActivityRes.java rename to src/main/java/com/share/help/res/activity/FindActivityRes.java index 663691b..147d2fd 100644 --- a/src/main/java/com/share/help/res/FindActivityRes.java +++ b/src/main/java/com/share/help/res/activity/FindActivityRes.java @@ -1,15 +1,19 @@ -package com.share.help.res; +package com.share.help.res.activity; + +import com.share.help.res.Page; import java.util.List; /** * 查找活动列表结果 */ -public class FindActivityRes extends Page>>{ +public class FindActivityRes extends Page>> { public enum Res{ //成功 ok, + // 空数据 + empty, //失败 fail } diff --git a/src/main/java/com/share/help/service/ActivityService.java b/src/main/java/com/share/help/service/ActivityService.java index f36c366..ca6c90b 100644 --- a/src/main/java/com/share/help/service/ActivityService.java +++ b/src/main/java/com/share/help/service/ActivityService.java @@ -1,27 +1,34 @@ package com.share.help.service; +import com.share.help.Util; import com.share.help.dao.ActivityMapper; import com.share.help.entity.ActivityEntity; import com.share.help.form.FindActivityForm; import com.share.help.form.SendHelpForm; -import com.share.help.res.*; +import com.share.help.res.JSONResponse; +import com.share.help.res.Result; +import com.share.help.res.SendHelpRes; +import com.share.help.res.activity.ActivitySimple; +import com.share.help.res.activity.FindActivityRes; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.sql.Timestamp; -import java.util.ArrayList; import java.util.List; /** * 活动服务层 */ @Service -public class ActivityService extends ImageService{ +public class ActivityService{ @Autowired private ActivityMapper activityMapper; + @Autowired + private ImageService imageService; + //活动分页配置 @Value("${activity.page-size}") private int pageSize; @@ -45,7 +52,7 @@ public class ActivityService extends ImageService{ activityEntity.setActivityStartTime(sendHelpForm.getActivityStartTime()); activityEntity.setActivityEndTime(sendHelpForm.getActivityEndTime()); //保存活动背景图 - String imageName=saveImg(sendHelpForm.getActivityImgFile()); + String imageName=imageService.saveImg(sendHelpForm.getActivityImgFile()); if(imageName==null){ return new JSONResponse<>(SendHelpRes.fail, Result.FAIL); } @@ -64,22 +71,11 @@ public class ActivityService extends ImageService{ findActivityRes.setPageSize(pageSize); findActivityRes.setCurrentPage(findActivityForm.getCurrentPage()); List activitySimpleList=activityMapper.getPage((findActivityForm.getCurrentPage()-1)*pageSize,pageSize); - List> dataList=new ArrayList<>(); if(!activitySimpleList.isEmpty()){ - if(activitySimpleList.size()= activitySimpleList.size()) { - dataList.add(activitySimpleList.subList(i, activitySimpleList.size())); - } - } - } - findActivityRes.setBody(dataList); + findActivityRes.setBody(Util.splitList(ActivitySimple.class,activitySimpleList,rowSize)); return new JSONResponse(FindActivityRes.Res.ok,Result.OK).setBody(findActivityRes); }else{ - return new JSONResponse<>(FindActivityRes.Res.fail, Result.FAIL); + return new JSONResponse<>(FindActivityRes.Res.empty, Result.FAIL); } } diff --git a/src/main/java/com/share/help/service/ImageService.java b/src/main/java/com/share/help/service/ImageService.java index 57cd4e3..0e738a6 100644 --- a/src/main/java/com/share/help/service/ImageService.java +++ b/src/main/java/com/share/help/service/ImageService.java @@ -3,14 +3,15 @@ package com.share.help.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.UUID; - -public abstract class ImageService { +@Service +public class ImageService { private enum ImageType{ jpg, jpeg, diff --git a/src/main/java/com/share/help/service/UserService.java b/src/main/java/com/share/help/service/UserService.java index 48e7b9e..e9fef1d 100644 --- a/src/main/java/com/share/help/service/UserService.java +++ b/src/main/java/com/share/help/service/UserService.java @@ -1,11 +1,14 @@ package com.share.help.service; +import com.share.help.Util; import com.share.help.dao.UserMapper; import com.share.help.entity.UserEntity; +import com.share.help.form.FindUserForm; import com.share.help.form.UserLoginForm; import com.share.help.form.UserRegisterForm; import com.share.help.res.JSONResponse; import com.share.help.res.Result; +import com.share.help.res.account.FindUserRes; import com.share.help.res.account.LoginRes; import com.share.help.res.account.RegisterRes; import org.springframework.beans.factory.annotation.Autowired; @@ -13,11 +16,13 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.DigestUtils; +import java.util.List; + /** * 用户服务层 */ @Service -public class UserService extends ImageService{ +public class UserService{ @Autowired private UserMapper userMapper; @@ -25,6 +30,16 @@ public class UserService extends ImageService{ @Value("${user.register.time-score}") private int timeScore; + //用户分页配置 + @Value("${activity.page-size}") + private int pageSize; + + @Value("${activity.row-size}") + private int rowSize; + + @Autowired + private ImageService imageService; + /** * 注册 * @param userRegisterForm 注册表单 @@ -44,7 +59,7 @@ public class UserService extends ImageService{ userEntity.setName(userRegisterForm.getName()); userEntity.setSex(userRegisterForm.getSex()); //保存头像 - String imageName=saveImg(userRegisterForm.getImgFile()); + String imageName=imageService.saveImg(userRegisterForm.getImgFile()); if(imageName==null){ return new JSONResponse<>(RegisterRes.fail, Result.FAIL); } @@ -71,4 +86,18 @@ public class UserService extends ImageService{ return userEntity==null?new JSONResponse<>(LoginRes.Res.valid_error,Result.FAIL) :new JSONResponse(LoginRes.Res.ok,Result.OK).setBody(new LoginRes(userEntity)); } + + public JSONResponse find(FindUserForm findUserForm){ + FindUserRes findUserRes=userMapper.count(pageSize,findUserForm.getUserTypes(),findUserForm.getInfo(),findUserForm.getServiceAddress()); + findUserRes.setPageSize(pageSize); + findUserRes.setCurrentPage(findUserForm.getCurrentPage()); + List userEntityList=userMapper.getPage((findUserForm.getCurrentPage()-1)*pageSize,pageSize,findUserForm.getUserTypes(), + findUserForm.getInfo(),findUserForm.getServiceAddress()); + if(!userEntityList.isEmpty()){ + findUserRes.setBody(Util.splitList(UserEntity.class,userEntityList,rowSize)); + return new JSONResponse(FindUserRes.Res.ok,Result.OK).setBody(findUserRes); + }else{ + return new JSONResponse<>(FindUserRes.Res.empty, Result.FAIL); + } + } } diff --git a/src/main/resources/mapping/ActivityMapper.xml b/src/main/resources/mapping/ActivityMapper.xml index 0d30eec..a69b06f 100644 --- a/src/main/resources/mapping/ActivityMapper.xml +++ b/src/main/resources/mapping/ActivityMapper.xml @@ -14,7 +14,7 @@ - + diff --git a/src/test/java/com/share/help/HelpApplicationTests.java b/src/test/java/com/share/help/HelpApplicationTests.java index 8e1446d..de8a2d0 100644 --- a/src/test/java/com/share/help/HelpApplicationTests.java +++ b/src/test/java/com/share/help/HelpApplicationTests.java @@ -4,6 +4,7 @@ import com.share.help.dao.ActivityMapper; import com.share.help.dao.UserMapper; import com.share.help.entity.ActivityEntity; import com.share.help.entity.UserEntity; +import com.share.help.res.account.FindUserRes; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -18,13 +19,16 @@ import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilde import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.context.WebApplicationContext; +import java.util.ArrayList; import java.util.List; import java.util.UUID; @SpringBootTest @MapperScans(@MapperScan("com.share.help.dao")) +@EnableTransactionManagement class HelpApplicationTests { @Autowired @@ -107,6 +111,21 @@ class HelpApplicationTests { Assertions.assertTrue(userMapper.delete(userId),"删除成功"); } + /** + * 测试用户分页查询 + */ + @Test + public void testFindUser(){ + List list=new ArrayList(){{ + add("help"); + }}; + FindUserRes findUserRes=userMapper.count(10,list,null,null); + + List userEntities=userMapper.getPage(0,10,list,null,null); + + Assertions.assertFalse(userEntities.isEmpty()); + } + /** * 测试用户接口 */