From 1fc0b98ae95b8cb6c696d8a79fa3a7a52bd1e306 Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Mon, 1 Jun 2020 05:40:46 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=AE=8C=E5=96=84=E7=B3=BB=E7=BB=9F=E6=8E=A8?= =?UTF-8?q?=E8=8D=90=202.=E5=A2=9E=E5=8A=A0=E4=BF=AE=E6=94=B9=E6=B4=BB?= =?UTF-8?q?=E5=8A=A8=E4=BF=A1=E6=81=AF=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 | 2 + .../com/share/help/config/CrosConfig.java | 41 ++++++++++++++ .../share/help/config/CrosConfiguration.java | 33 ++++------- .../help/controller/ManagerController.java | 24 +++++--- .../com/share/help/dao/ActivityMapper.java | 13 ++++- .../java/com/share/help/dao/FriendMapper.java | 6 +- .../java/com/share/help/form/SearchFrom.java | 10 ++++ .../com/share/help/form/UpdateUserForm.java | 5 +- .../help/form/manager/ActiveUserRes.java | 22 ++++++++ .../help/form/manager/UpdateActivityForm.java | 36 ++++++++++++ .../share/help/service/ActivityService.java | 28 +++++++--- .../share/help/service/ManagerService.java | 20 ++++--- .../com/share/help/service/UserService.java | 4 +- src/main/resources/application.yaml | 9 ++- src/main/resources/mapping/ActivityMapper.xml | 37 +++++++++++- src/main/resources/mapping/FriendMapper.xml | 56 ++++++++++++------- .../com/share/help/HelpApplicationTests.java | 26 +++++++-- 17 files changed, 290 insertions(+), 82 deletions(-) create mode 100644 src/main/java/com/share/help/config/CrosConfig.java create mode 100644 src/main/java/com/share/help/form/manager/ActiveUserRes.java create mode 100644 src/main/java/com/share/help/form/manager/UpdateActivityForm.java diff --git a/src/main/java/com/share/help/Constants.java b/src/main/java/com/share/help/Constants.java index 6d2cedc..915220a 100644 --- a/src/main/java/com/share/help/Constants.java +++ b/src/main/java/com/share/help/Constants.java @@ -88,4 +88,6 @@ public class Constants { public static final String MANAGER_INTERFACE_FIND_ACTIVITY ="/find/activity" ; //删除活动接口 public static final String MANAGER_INTERFACE_DELTE_ACTIVITY = "/delete/{activityId}"; + //更新活动接口 + public static final String MANAGER_INTERFACE_UPDATE_ACTIVITY = "/update/{activityId}"; } diff --git a/src/main/java/com/share/help/config/CrosConfig.java b/src/main/java/com/share/help/config/CrosConfig.java new file mode 100644 index 0000000..1ce7793 --- /dev/null +++ b/src/main/java/com/share/help/config/CrosConfig.java @@ -0,0 +1,41 @@ +package com.share.help.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +@Component +@PropertySource("classpath:application.yaml") +@ConfigurationProperties(prefix = "cros") +public class CrosConfig { + + private String pathPattern; + + private String allowedOrigins[]; + + private String allowedMethods[]; + + public String[] getAllowedOrigins() { + return allowedOrigins; + } + + public void setAllowedOrigins(String[] allowedOrigins) { + this.allowedOrigins = allowedOrigins; + } + + public String[] getAllowedMethods() { + return allowedMethods; + } + + public void setAllowedMethods(String[] allowedMethods) { + this.allowedMethods = allowedMethods; + } + + public String getPathPattern() { + return pathPattern; + } + + public void setPathPattern(String pathPattern) { + this.pathPattern = pathPattern; + } +} diff --git a/src/main/java/com/share/help/config/CrosConfiguration.java b/src/main/java/com/share/help/config/CrosConfiguration.java index 8801bb7..1c9ebf3 100644 --- a/src/main/java/com/share/help/config/CrosConfiguration.java +++ b/src/main/java/com/share/help/config/CrosConfiguration.java @@ -1,30 +1,21 @@ package com.share.help.config; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.web.servlet.FilterRegistrationBean; -import org.springframework.context.annotation.Bean; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; -import org.springframework.web.cors.CorsConfiguration; -import org.springframework.web.cors.UrlBasedCorsConfigurationSource; -import org.springframework.web.filter.CorsFilter; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration -public class CrosConfiguration { +public class CrosConfiguration implements WebMvcConfigurer { - @Value("${cros.allow-origin}") - private String allowOrigin; + @Autowired + private CrosConfig crosConfig; - @Bean - public FilterRegistrationBean corsFilter() { - UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); - CorsConfiguration config = new CorsConfiguration(); - config.setAllowCredentials(true); - config.addAllowedOrigin(allowOrigin); - config.addAllowedHeader("*"); - config.addAllowedMethod("*"); - source.registerCorsConfiguration("/api/**", config); - FilterRegistrationBean bean = new FilterRegistrationBean<>(new CorsFilter(source)); - bean.setOrder(0); - return bean; + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping(crosConfig.getPathPattern()). + allowedOrigins(crosConfig.getAllowedOrigins()). + allowedMethods(crosConfig.getAllowedMethods()). + allowCredentials(true); } } diff --git a/src/main/java/com/share/help/controller/ManagerController.java b/src/main/java/com/share/help/controller/ManagerController.java index acd8d01..ee7cf69 100644 --- a/src/main/java/com/share/help/controller/ManagerController.java +++ b/src/main/java/com/share/help/controller/ManagerController.java @@ -4,6 +4,7 @@ import com.share.help.Constants; import com.share.help.entity.UserEntity; import com.share.help.form.*; import com.share.help.form.manager.FindActivityForm; +import com.share.help.form.manager.UpdateActivityForm; import com.share.help.res.*; import com.share.help.res.account.ManagerLoginRes; import com.share.help.res.account.RegisterRes; @@ -136,12 +137,19 @@ public class ManagerController { } } -// @GetMapping(Constants.MANAGER_INTERFACE_DELTE_ACTIVITY) -// public JSONResponse deleteActivity(@PathVariable Long activityId){ -// if(activityId!=null){ -// return managerService.deleteActivity(activityId); -// }else{ -// return new JSONResponse<>(DefaultRes.fail,Result.FAIL); -// } -// } + /** + * 更新活动信息 + * @param updateActivityForm 活动表单 + * @param activityId 活动id + * @return 返回更新结果 + */ + @PostMapping(Constants.MANAGER_INTERFACE_UPDATE_ACTIVITY) + public JSONResponse updateActivity(UpdateActivityForm updateActivityForm,@PathVariable Long activityId){ + if(activityId!=null){ + updateActivityForm.setActivityId(activityId); + return managerService.updateActivity(updateActivityForm); + }else{ + return new JSONResponse<>(DefaultRes.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 4483378..bb25eac 100644 --- a/src/main/java/com/share/help/dao/ActivityMapper.java +++ b/src/main/java/com/share/help/dao/ActivityMapper.java @@ -5,6 +5,7 @@ import com.share.help.entity.custorm.ActivityDetail; import com.share.help.entity.custorm.ActivitySimple; import com.share.help.entity.custorm.FindActivityRes; import com.share.help.entity.custorm.SeekHelpSum; +import com.share.help.form.manager.ActiveUserRes; import com.share.help.res.ManagerActivityRes; import com.share.help.res.Page; import org.apache.ibatis.annotations.Delete; @@ -48,16 +49,18 @@ public interface ActivityMapper { * 分页查询活动 * @param start 起始索引 * @param count 查询个数 + * @param userId 用户id,判断该用户是否报名活动 * @return 返回活动列表 */ - List getPage(@Param("start") Integer start, @Param("count") Integer count,@Param("userId") String userId); + List getPage(@Param("start") Integer start, @Param("count") Integer count,@Param("userId") String userId,@Param("title") String title); /** * 获取活动列表分页数 * @param pageSize 分页大小 + * @param userId 用户id,判断该用户是否报名活动 * @return 返回分页信息 */ - FindActivityRes count(@Param("pageSize") Integer pageSize,@Param("userId") String userId); + FindActivityRes count(@Param("pageSize") Integer pageSize,@Param("userId") String userId,@Param("title") String title); //求助信息 List seekHelp(@Param("userId") String userId); @@ -67,4 +70,10 @@ public interface ActivityMapper { //分页信息 Page> countActivity(@Param("pageSize") Integer pageSize); + + //修改活动信息 + boolean updateActivity(@Param("activityId") Long activityId,@Param("title") String title,@Param("content") String content); + + //查询活跃用户 + List queryActiveUser(@Param("userId") String userId,@Param("serviceAddress") String serviceAddress); } diff --git a/src/main/java/com/share/help/dao/FriendMapper.java b/src/main/java/com/share/help/dao/FriendMapper.java index 560ed4c..ff6b07b 100644 --- a/src/main/java/com/share/help/dao/FriendMapper.java +++ b/src/main/java/com/share/help/dao/FriendMapper.java @@ -24,14 +24,16 @@ public interface FriendMapper { /** * 获取好友邀请 * @param userId 目标用户 + * @param serviceAddress 服务地点 * @return 返回好友id */ - List getFriend(@Param("userId") String userId, @Param("name") String name,@Param("start") long start, @Param("count") long count); + List getFriend(@Param("userId") String userId, @Param("name") String name, String serviceAddress, @Param("start") long start, @Param("count") long count); /** * 统计好友分页数 * @param userId 目标用户 + * @param serviceAddress 服务地点 * @return 返回分页数 */ - Page> count(@Param("userId") String userId, @Param("name") String name,@Param("pageSize") Integer pageSize); + Page> count(@Param("userId") String userId, @Param("name") String name, String serviceAddress, @Param("pageSize") Integer pageSize); } diff --git a/src/main/java/com/share/help/form/SearchFrom.java b/src/main/java/com/share/help/form/SearchFrom.java index 3f03bd5..6306aa3 100644 --- a/src/main/java/com/share/help/form/SearchFrom.java +++ b/src/main/java/com/share/help/form/SearchFrom.java @@ -8,6 +8,8 @@ public class SearchFrom { private String userId; //用户名 private String name; + //服务地点 + private String serviceAddress; //分页数 private Integer currentPage; @@ -34,4 +36,12 @@ public class SearchFrom { public void setUserId(String userId) { this.userId = userId; } + + public String getServiceAddress() { + return serviceAddress; + } + + public void setServiceAddress(String serviceAddress) { + this.serviceAddress = serviceAddress; + } } diff --git a/src/main/java/com/share/help/form/UpdateUserForm.java b/src/main/java/com/share/help/form/UpdateUserForm.java index c9ce997..0dd4941 100644 --- a/src/main/java/com/share/help/form/UpdateUserForm.java +++ b/src/main/java/com/share/help/form/UpdateUserForm.java @@ -3,6 +3,8 @@ package com.share.help.form; public class UpdateUserForm { private String userId; private String name; + private Integer age; + private String sex; private String headImg; private String address; private String serviceAddress; @@ -10,8 +12,7 @@ public class UpdateUserForm { private Long mobile; private String email; private String info; - private Integer age; - private String sex; + public String getUserId() { return userId; diff --git a/src/main/java/com/share/help/form/manager/ActiveUserRes.java b/src/main/java/com/share/help/form/manager/ActiveUserRes.java new file mode 100644 index 0000000..4c01e9f --- /dev/null +++ b/src/main/java/com/share/help/form/manager/ActiveUserRes.java @@ -0,0 +1,22 @@ +package com.share.help.form.manager; + +public class ActiveUserRes { + private String userId; + private Integer count; + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public Integer getCount() { + return count; + } + + public void setCount(Integer count) { + this.count = count; + } +} diff --git a/src/main/java/com/share/help/form/manager/UpdateActivityForm.java b/src/main/java/com/share/help/form/manager/UpdateActivityForm.java new file mode 100644 index 0000000..ebd2645 --- /dev/null +++ b/src/main/java/com/share/help/form/manager/UpdateActivityForm.java @@ -0,0 +1,36 @@ +package com.share.help.form.manager; + +/** + * 管理员修改活动信息表单 + */ +public class UpdateActivityForm { + private Long activityId; + + private String title; + + private String content; + + 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; + } +} diff --git a/src/main/java/com/share/help/service/ActivityService.java b/src/main/java/com/share/help/service/ActivityService.java index 4e2d067..14c3992 100644 --- a/src/main/java/com/share/help/service/ActivityService.java +++ b/src/main/java/com/share/help/service/ActivityService.java @@ -11,6 +11,7 @@ import com.share.help.entity.LeaveWordEntity; import com.share.help.entity.UserEntity; import com.share.help.entity.custorm.*; import com.share.help.form.*; +import com.share.help.form.manager.ActiveUserRes; import com.share.help.res.DefaultRes; import com.share.help.res.JSONResponse; import com.share.help.res.Page; @@ -85,7 +86,7 @@ public class ActivityService{ if(activityMapper.insert(activityEntity)) { switch (sendHelpForm.getRecommendType()) { case auto: - autoRecommend(sendHelpForm); + autoRecommend(activityEntity); break; case choose: if (ArrayUtils.isNotEmpty(friendUserId)) { @@ -107,13 +108,26 @@ public class ActivityService{ } } + + /** * 系统推荐 - * @param sendHelpForm 推荐表单 + * @param activityEntity 推荐表单 */ - private void autoRecommend(SendHelpForm sendHelpForm) { - //查找参加过我活动的好友 - + private void autoRecommend(ActivityEntity activityEntity) { + //查找参加过我活动的活跃好友 + List activeUserRes=activityMapper.queryActiveUser(activityEntity.getUserId(),activityEntity.getServiceAddress()); + for(ActiveUserRes res:activeUserRes){ + LeaveWordEntity leaveWordEntity = new LeaveWordEntity(); + leaveWordEntity.setType(LeaveWordType.recommend.name()); + leaveWordEntity.setSourceUserId(activityEntity.getUserId()); + leaveWordEntity.setTargetUserId(res.getUserId()); + leaveWordEntity.setCreateTime(new Timestamp(System.currentTimeMillis())); + leaveWordEntity.setActivityId(activityEntity.getActivityId()); + if(leaveWordMapper.insert(leaveWordEntity)){ + logger.info("成功把活动id="+activityEntity.getActivityId()+"推荐给好友id="+res.getUserId()); + } + } } @@ -123,10 +137,10 @@ public class ActivityService{ * @return 返回活动信息 */ public JSONResponse find(FindActivityForm findActivityForm){ - FindActivityRes findActivityRes=activityMapper.count(activityPageSize,findActivityForm.getUserId()); + FindActivityRes findActivityRes=activityMapper.count(activityPageSize,findActivityForm.getUserId(),findActivityForm.getTitle()); findActivityRes.setPageSize(activityPageSize); findActivityRes.setCurrentPage(findActivityForm.getCurrentPage()); - List activitySimpleList=activityMapper.getPage((findActivityForm.getCurrentPage()-1)* activityPageSize, activityPageSize,findActivityForm.getUserId()); + List activitySimpleList=activityMapper.getPage((findActivityForm.getCurrentPage()-1)* activityPageSize, activityPageSize,findActivityForm.getUserId(),findActivityForm.getTitle()); if(!activitySimpleList.isEmpty()){ findActivityRes.setBody(Util.splitList(ActivitySimple.class,activitySimpleList, activityRowSize)); return new JSONResponse(FindActivityRes.Res.ok,Result.OK).setBody(findActivityRes); diff --git a/src/main/java/com/share/help/service/ManagerService.java b/src/main/java/com/share/help/service/ManagerService.java index d4e33c5..5966197 100644 --- a/src/main/java/com/share/help/service/ManagerService.java +++ b/src/main/java/com/share/help/service/ManagerService.java @@ -7,6 +7,7 @@ import com.share.help.entity.ManagerEntity; import com.share.help.entity.UserEntity; import com.share.help.form.*; import com.share.help.form.manager.FindActivityForm; +import com.share.help.form.manager.UpdateActivityForm; import com.share.help.res.*; import com.share.help.res.account.ManagerLoginRes; import com.share.help.res.account.RegisterRes; @@ -146,11 +147,16 @@ public class ManagerService { return new JSONResponse>>(DefaultRes.ok,Result.OK).setBody(page); } -// public JSONResponse deleteActivity(Long activityId) { -// if(activityMapper.delete(activityId)){ -// -// }else{ -// -// } -// } + /** + * 更新活动信息 + * @param updateActivityForm 更新表单 + * @return 返回更新结果 + */ + public JSONResponse updateActivity(UpdateActivityForm updateActivityForm) { + if(activityMapper.updateActivity(updateActivityForm.getActivityId(),updateActivityForm.getTitle(),updateActivityForm.getContent())){ + return new JSONResponse<>(DefaultRes.ok,Result.OK); + }else{ + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + } } diff --git a/src/main/java/com/share/help/service/UserService.java b/src/main/java/com/share/help/service/UserService.java index 9ccee7d..6901682 100644 --- a/src/main/java/com/share/help/service/UserService.java +++ b/src/main/java/com/share/help/service/UserService.java @@ -206,8 +206,8 @@ public class UserService{ * @return 返回好友列表 */ public JSONResponse>> searchFriend(SearchFrom searchFrom) { - Page> page=friendMapper.count(searchFrom.getUserId(),searchFrom.getName(),friendSize); - List list=friendMapper.getFriend(searchFrom.getUserId(),searchFrom.getName(),(searchFrom.getCurrentPage()-1)*friendSize,friendSize); + Page> page=friendMapper.count(searchFrom.getUserId(),searchFrom.getName(),searchFrom.getServiceAddress(),friendSize); + List list=friendMapper.getFriend(searchFrom.getUserId(),searchFrom.getName(),searchFrom.getServiceAddress(),(searchFrom.getCurrentPage()-1)*friendSize,friendSize); page.setBody(list); return new JSONResponse>>(DefaultRes.ok,Result.OK).setBody(page); } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index c5197ed..bbdd507 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -22,7 +22,14 @@ logging: root: info #跨域配置 cros: - allow-origin: 'http://localhost:3000' + allowedOrigins: + - http://localhost:3000 + - http://localhost:3001 + pathPattern: /api/** + allowedMethods: + - PUT + - GET + - POST #用户 user: #注册 diff --git a/src/main/resources/mapping/ActivityMapper.xml b/src/main/resources/mapping/ActivityMapper.xml index a499530..fee2066 100644 --- a/src/main/resources/mapping/ActivityMapper.xml +++ b/src/main/resources/mapping/ActivityMapper.xml @@ -28,7 +28,13 @@ @@ -110,7 +116,32 @@ and activity_id not in(select activity_id from activity_history) order by seek_ select user_id as userId,name from user where user_id in (select user_id from activity_history where activity_id=#{activityId} and activity_status=#{activityStatus}; - - + + + update activity + + + title=#{title}, + + + content=#{content} + + + where activity_id=#{activityId} + + + + \ No newline at end of file diff --git a/src/main/resources/mapping/FriendMapper.xml b/src/main/resources/mapping/FriendMapper.xml index 8dfb68e..901fc34 100644 --- a/src/main/resources/mapping/FriendMapper.xml +++ b/src/main/resources/mapping/FriendMapper.xml @@ -6,32 +6,46 @@ \ 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 a9607af..67010da 100644 --- a/src/test/java/com/share/help/HelpApplicationTests.java +++ b/src/test/java/com/share/help/HelpApplicationTests.java @@ -6,6 +6,7 @@ import com.share.help.entity.ManagerEntity; import com.share.help.entity.UserEntity; import com.share.help.entity.custorm.*; import com.share.help.form.ActivityStatus; +import com.share.help.form.manager.ActiveUserRes; import com.share.help.res.ManagerActivityRes; import com.share.help.res.Page; import com.share.help.res.account.QueryRes; @@ -164,12 +165,11 @@ class HelpApplicationTests { */ @Test public void testActivity(){ - List activityEntityList=activityMapper.getPage(0,10,"123"); - Assertions.assertTrue(activityEntityList.isEmpty()); - activityEntityList=activityMapper.getPage(0,10,"666"); + List activityEntityList=activityMapper.getPage(0,10,"123",""); + Assertions.assertFalse(activityEntityList.isEmpty()); + activityEntityList=activityMapper.getPage(0,10,"123","赛亚人"); Assertions.assertFalse(activityEntityList.isEmpty()); - Assertions.assertNotNull(activityMapper.count(10,"123")); } @@ -229,10 +229,12 @@ class HelpApplicationTests { */ @Test public void queryFriend(){ - Page> page=friendMapper.count("123","6",3); + Page> page=friendMapper.count("123","6", "", 3); Assertions.assertEquals(1, (long) page.getCount()); - List user=friendMapper.getFriend("123","6",0,3); + List user=friendMapper.getFriend("123","6", "上海", 0,3); Assertions.assertEquals(1,user.size()); + user=friendMapper.getFriend("123","6", "广州", 0,3); + Assertions.assertTrue(user.isEmpty()); } /** @@ -306,4 +308,16 @@ class HelpApplicationTests { Page> page=activityMapper.countActivity(10); Assertions.assertTrue(page.getCount()>0,"管理员分页查询活动失败"); } + + //更新活动信息 + @Test + public void testUpdateActivity(){ + Assertions.assertTrue(activityMapper.updateActivity(66L,"44","")); + } + + @Test + public void testQueryActiveUser(){ + List activeUserRes= activityMapper.queryActiveUser("123","广州"); + Assertions.assertEquals(2, activeUserRes.get(0).getCount()); + } }