From 55b7f360484d171df5eb0101af7334ce1fc25827 Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Wed, 27 May 2020 08:47:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9F=A5=E8=AF=A2=E6=B4=BB?= =?UTF-8?q?=E5=8A=A8=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/share/help/Constants.java | 9 ++ .../help/controller/ActivityController.java | 55 ++++++++++++ .../com/share/help/dao/ActivityMapper.java | 52 +++++++++++ .../java/com/share/help/dao/UserMapper.java | 3 +- .../com/share/help/form/FindActivityForm.java | 28 ++++++ .../com/share/help/form/SendHelpForm.java | 23 +++++ .../com/share/help/res/ActivitySimple.java | 43 ++++++++++ .../com/share/help/res/FindActivityRes.java | 16 ++++ src/main/java/com/share/help/res/Page.java | 53 ++++++++++++ .../java/com/share/help/res/SendHelpRes.java | 11 +++ .../share/help/service/ActivityService.java | 86 +++++++++++++++++++ .../com/share/help/service/ImageService.java | 11 +-- .../com/share/help/service/UserService.java | 3 + src/main/resources/application.yaml | 7 ++ src/main/resources/mapping/ActivityMapper.xml | 23 +++++ src/test/java/com/share/help/DefaultTest.java | 18 +++- .../com/share/help/HelpApplicationTests.java | 29 ++++++- 17 files changed, 460 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/share/help/controller/ActivityController.java create mode 100644 src/main/java/com/share/help/dao/ActivityMapper.java create mode 100644 src/main/java/com/share/help/form/FindActivityForm.java create mode 100644 src/main/java/com/share/help/form/SendHelpForm.java create mode 100644 src/main/java/com/share/help/res/ActivitySimple.java create mode 100644 src/main/java/com/share/help/res/FindActivityRes.java create mode 100644 src/main/java/com/share/help/res/Page.java create mode 100644 src/main/java/com/share/help/res/SendHelpRes.java create mode 100644 src/main/java/com/share/help/service/ActivityService.java create mode 100644 src/main/resources/mapping/ActivityMapper.xml diff --git a/src/main/java/com/share/help/Constants.java b/src/main/java/com/share/help/Constants.java index 1826363..3dcfa02 100644 --- a/src/main/java/com/share/help/Constants.java +++ b/src/main/java/com/share/help/Constants.java @@ -13,4 +13,13 @@ public class Constants { //用户登陆接口 public static final String USER_INTERFACE_LOGIN = "/login"; + + //求助接口前缀 + public static final String ACTIVITY_INTERFACE = "/api/activity"; + + //发布求助信息 + public static final String ACTIVITY_INTERFACE_SEND_HELP = "/sendHelp"; + + //查找活动列表 + public static final String ACTIVITY_INTERFACE_FIND = "/find"; } diff --git a/src/main/java/com/share/help/controller/ActivityController.java b/src/main/java/com/share/help/controller/ActivityController.java new file mode 100644 index 0000000..29e59c2 --- /dev/null +++ b/src/main/java/com/share/help/controller/ActivityController.java @@ -0,0 +1,55 @@ +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.service.ActivityService; +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.GetMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(Constants.ACTIVITY_INTERFACE) +public class ActivityController { + + @Autowired + private ActivityService activityService; + + + /** + * 发布求助信息 + * @return 返回发布结果 + */ + @PutMapping(Constants.ACTIVITY_INTERFACE_SEND_HELP) + public JSONResponse send(SendHelpForm sendHelpForm){ + if(StringUtils.isNoneEmpty(sendHelpForm.getTitle(),sendHelpForm.getContent(),sendHelpForm.getServiceAddress())&& + ObjectUtils.allNotNull(sendHelpForm.getActivityStartTime(),sendHelpForm.getActivityEndTime() + ,sendHelpForm.getActivityImgFile())){ + return activityService.sendHelp(sendHelpForm); + }else{ + return new JSONResponse<>(SendHelpRes.fail,Result.FAIL); + } + } + + /** + * 返回活动列表数据 + * @param findActivityForm 查询表单 + * @return 返回活动列表数据 + */ + @GetMapping(Constants.ACTIVITY_INTERFACE_FIND) + public JSONResponse find(FindActivityForm findActivityForm){ + if(findActivityForm.getCurrentPage()==null){ + return new JSONResponse<>(FindActivityRes.Res.fail,Result.FAIL); + }else{ + return activityService.find(findActivityForm); + } + } +} diff --git a/src/main/java/com/share/help/dao/ActivityMapper.java b/src/main/java/com/share/help/dao/ActivityMapper.java new file mode 100644 index 0000000..9397517 --- /dev/null +++ b/src/main/java/com/share/help/dao/ActivityMapper.java @@ -0,0 +1,52 @@ +package com.share.help.dao; + +import com.share.help.entity.ActivityEntity; +import com.share.help.res.ActivitySimple; +import com.share.help.res.FindActivityRes; +import org.apache.ibatis.annotations.*; + +import java.util.List; + +/** + * 活动数据接口 + */ +public interface ActivityMapper { + + /** + * 保存活动信息 + * @param activityEntity 活动实体 + * @return 返回保存结果 + */ + @Insert("insert into activity(activity_id,title,content,seek_help_time,activity_start_time,activity_end_time,activity_img,service_address)value(#{activityId},#{title},#{content},#{seekHelpTime},#{activityStartTime},#{activityEndTime},#{activityImg},#{serviceAddress})") + boolean insert(ActivityEntity activityEntity); + + /** + * 根据活动id删除活动信息 + * @param activityId 活动id + * @return 返回删除结果 + */ + @Delete("delete from activity where activity_id=#{activityId}") + boolean delete(@Param("activityId") Long activityId); + + /** + * 根据活动id查找活动信息 + * @param activityId 活动id + * @return 返回活动信息 + */ + @Select("select * from activity where activity_id=#{activityId}") + @ResultMap("activityMap") + ActivityEntity findOne(@Param("activityId") Long activityId); + + /** + * 分页查询活动 + * @param start 起始索引 + * @param count 查询个数 + * @return 返回活动列表 + */ + @Select("select activity_id,title,content,activity_img from activity limit #{start},#{count}") + @ResultMap("activitySimpleMap") + List getPage(@Param("start") Integer start, @Param("count") Integer count); + + @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 14f01a6..3aaa3ad 100644 --- a/src/main/java/com/share/help/dao/UserMapper.java +++ b/src/main/java/com/share/help/dao/UserMapper.java @@ -45,11 +45,10 @@ public interface UserMapper { /** * - * @param userEntity 更新用户 * @return 返回更新结果 */ @Update("update user set password=#{password} where user_id=#{userId}") - boolean update(UserEntity userEntity); + boolean updatePassword(@Param("userId") String userId, @Param("password") String password); /** * 根据用户id判断用户是否存在 diff --git a/src/main/java/com/share/help/form/FindActivityForm.java b/src/main/java/com/share/help/form/FindActivityForm.java new file mode 100644 index 0000000..bbc3183 --- /dev/null +++ b/src/main/java/com/share/help/form/FindActivityForm.java @@ -0,0 +1,28 @@ +package com.share.help.form; + +/** + * 查找活动列表表单 + */ +public class FindActivityForm { + //查找分页 + private Integer currentPage; + + //标题信息 + private String title; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Integer getCurrentPage() { + return currentPage; + } + + public void setCurrentPage(Integer currentPage) { + this.currentPage = currentPage; + } +} diff --git a/src/main/java/com/share/help/form/SendHelpForm.java b/src/main/java/com/share/help/form/SendHelpForm.java new file mode 100644 index 0000000..dd50d86 --- /dev/null +++ b/src/main/java/com/share/help/form/SendHelpForm.java @@ -0,0 +1,23 @@ +package com.share.help.form; + +import com.share.help.entity.ActivityEntity; +import org.springframework.web.multipart.MultipartFile; + +/** + * 发布求助信息表单 + */ +public class SendHelpForm extends ActivityEntity { + + /** + * 活动背景图 + */ + private MultipartFile activityImgFile; + + public MultipartFile getActivityImgFile() { + return activityImgFile; + } + + public void setActivityImgFile(MultipartFile activityImgFile) { + this.activityImgFile = activityImgFile; + } +} diff --git a/src/main/java/com/share/help/res/ActivitySimple.java b/src/main/java/com/share/help/res/ActivitySimple.java new file mode 100644 index 0000000..d6a4590 --- /dev/null +++ b/src/main/java/com/share/help/res/ActivitySimple.java @@ -0,0 +1,43 @@ +package com.share.help.res; + +/** + * 活动列表实体 + */ +public class ActivitySimple { + private Long activityId; + private String title; + private String content; + private String activityImg; + + public Long getActivityId() { + return activityId; + } + + public void setActivityId(Long activityId) { + this.activityId = activityId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getActivityImg() { + return activityImg; + } + + public void setActivityImg(String activityImg) { + this.activityImg = activityImg; + } +} diff --git a/src/main/java/com/share/help/res/FindActivityRes.java b/src/main/java/com/share/help/res/FindActivityRes.java new file mode 100644 index 0000000..663691b --- /dev/null +++ b/src/main/java/com/share/help/res/FindActivityRes.java @@ -0,0 +1,16 @@ +package com.share.help.res; + +import java.util.List; + +/** + * 查找活动列表结果 + */ +public class FindActivityRes extends Page>>{ + + public enum Res{ + //成功 + ok, + //失败 + fail + } +} diff --git a/src/main/java/com/share/help/res/Page.java b/src/main/java/com/share/help/res/Page.java new file mode 100644 index 0000000..664ae82 --- /dev/null +++ b/src/main/java/com/share/help/res/Page.java @@ -0,0 +1,53 @@ +package com.share.help.res; + +public class Page { + private Integer currentPage; + + private Integer totalPage; + + private Long count; + + private Integer pageSize; + + private T body; + + public Integer getCurrentPage() { + return currentPage; + } + + public void setCurrentPage(Integer currentPage) { + this.currentPage = currentPage; + } + + public Integer getTotalPage() { + return totalPage; + } + + public void setTotalPage(Integer totalPage) { + this.totalPage = totalPage; + } + + public Integer getPageSize() { + return pageSize; + } + + public void setPageSize(Integer pageSize) { + this.pageSize = pageSize; + } + + public Long getCount() { + return count; + } + + public void setCount(Long count) { + this.count = count; + } + + public T getBody() { + return body; + } + + public void setBody(T body) { + this.body = body; + } +} diff --git a/src/main/java/com/share/help/res/SendHelpRes.java b/src/main/java/com/share/help/res/SendHelpRes.java new file mode 100644 index 0000000..63d1aad --- /dev/null +++ b/src/main/java/com/share/help/res/SendHelpRes.java @@ -0,0 +1,11 @@ +package com.share.help.res; + +/** + * 发布求助信息响应 + */ +public enum SendHelpRes { + //发布成功 + ok, + //发布失败请联系管理员 + fail +} diff --git a/src/main/java/com/share/help/service/ActivityService.java b/src/main/java/com/share/help/service/ActivityService.java new file mode 100644 index 0000000..f36c366 --- /dev/null +++ b/src/main/java/com/share/help/service/ActivityService.java @@ -0,0 +1,86 @@ +package com.share.help.service; + +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 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{ + + @Autowired + private ActivityMapper activityMapper; + + //活动分页配置 + @Value("${activity.page-size}") + private int pageSize; + + @Value("${activity.row-size}") + private int rowSize; + + + + + /** + * 发布求助信息 + * @param sendHelpForm 求助信息表单 + * @return 发布结果 + */ + public JSONResponse sendHelp(SendHelpForm sendHelpForm){ + ActivityEntity activityEntity=new ActivityEntity(); + activityEntity.setTitle(sendHelpForm.getTitle()); + activityEntity.setContent(sendHelpForm.getContent()); + activityEntity.setSeekHelpTime(new Timestamp(System.currentTimeMillis())); + activityEntity.setActivityStartTime(sendHelpForm.getActivityStartTime()); + activityEntity.setActivityEndTime(sendHelpForm.getActivityEndTime()); + //保存活动背景图 + String imageName=saveImg(sendHelpForm.getActivityImgFile()); + if(imageName==null){ + return new JSONResponse<>(SendHelpRes.fail, Result.FAIL); + } + activityEntity.setActivityImg(imageName); + activityEntity.setServiceAddress(sendHelpForm.getServiceAddress()); + return activityMapper.insert(activityEntity)?new JSONResponse<>(SendHelpRes.ok,Result.OK):new JSONResponse<>(SendHelpRes.fail,Result.FAIL); + } + + /** + * 分页查询活动信息 + * @param findActivityForm 查询活动信息 + * @return 返回活动信息 + */ + public JSONResponse find(FindActivityForm findActivityForm){ + FindActivityRes findActivityRes=activityMapper.count(pageSize); + 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); + return new JSONResponse(FindActivityRes.Res.ok,Result.OK).setBody(findActivityRes); + }else{ + return new JSONResponse<>(FindActivityRes.Res.fail, 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 7f09d90..57cd4e3 100644 --- a/src/main/java/com/share/help/service/ImageService.java +++ b/src/main/java/com/share/help/service/ImageService.java @@ -9,13 +9,14 @@ import java.io.File; import java.io.IOException; import java.util.UUID; -enum ImageType{ - jpg, - jpeg, - png -} public abstract class ImageService { + private enum ImageType{ + jpg, + jpeg, + png + } + private Logger logger = LoggerFactory.getLogger(ImageService.class); @Value("${spring.resources.static-locations}") diff --git a/src/main/java/com/share/help/service/UserService.java b/src/main/java/com/share/help/service/UserService.java index cce15bc..48e7b9e 100644 --- a/src/main/java/com/share/help/service/UserService.java +++ b/src/main/java/com/share/help/service/UserService.java @@ -13,6 +13,9 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.DigestUtils; +/** + * 用户服务层 + */ @Service public class UserService extends ImageService{ diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 182824c..6d6fa58 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -25,3 +25,10 @@ user: register: #初始时间币 time-score: 20 +#活动 +activity: + # 分页大小 + page-size: 6 + # 前端一行显示数量 + row-size: 3 + diff --git a/src/main/resources/mapping/ActivityMapper.xml b/src/main/resources/mapping/ActivityMapper.xml new file mode 100644 index 0000000..0d30eec --- /dev/null +++ b/src/main/resources/mapping/ActivityMapper.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/com/share/help/DefaultTest.java b/src/test/java/com/share/help/DefaultTest.java index a71ad70..6f47faf 100644 --- a/src/test/java/com/share/help/DefaultTest.java +++ b/src/test/java/com/share/help/DefaultTest.java @@ -1,10 +1,12 @@ package com.share.help; +import org.apache.commons.lang3.ArrayUtils; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.UUID; +import java.util.ArrayList; +import java.util.List; public class DefaultTest { @@ -12,6 +14,18 @@ public class DefaultTest { @Test public void test() { - logger.info(UUID.randomUUID().toString().length()+""); + + List s=new ArrayList(){{ + for(int i=0;i<100;i++){ + add(i); + } + }}; + + for(int i=3,j=0;i=s.size()){ + logger.info( ArrayUtils.toString(s.subList(i,s.size()))); + } + } } } diff --git a/src/test/java/com/share/help/HelpApplicationTests.java b/src/test/java/com/share/help/HelpApplicationTests.java index 9192af8..8e1446d 100644 --- a/src/test/java/com/share/help/HelpApplicationTests.java +++ b/src/test/java/com/share/help/HelpApplicationTests.java @@ -1,6 +1,8 @@ package com.share.help; +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 org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -33,6 +35,9 @@ class HelpApplicationTests { @Autowired private UserMapper userMapper; + @Autowired + private ActivityMapper activityMapper; + private Logger logger = LoggerFactory.getLogger(HelpApplicationTests.class); @BeforeEach @@ -83,7 +88,7 @@ class HelpApplicationTests { userEntity.setPassword(newPassword); - Assertions.assertTrue(userMapper.update(userEntity),"更新成功"); + Assertions.assertTrue(userMapper.updatePassword(userId,newPassword),"更新成功"); userEntity=userMapper.findOne(userId); @@ -110,4 +115,26 @@ class HelpApplicationTests { MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post(Constants.USER_INTERFACE+Constants.USER_INTERFACE_REGISTER); mvc.perform(builder).andDo(MockMvcResultHandlers.print()); } + + /** + * 测试活动分页查询 + */ + @Test + public void testActivity(){ +// FindActivityRes findActivityRes=activityMapper.count(10L); + +// List list=activityMapper.getPage(0,10); + + + } + + + @Test + public void testAddActivity(){ + ActivityEntity activityEntity=activityMapper.findOne(1L); + for(int i=0;i<20;i++){ + activityEntity.setActivityId(null); + activityMapper.insert(activityEntity); + } + } }