From a099d3bd898a63a9f9507e5e7b39205df43074f4 Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Wed, 27 May 2020 22:04:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B4=BB=E5=8A=A8=E6=8A=A5?= =?UTF-8?q?=E5=90=8D=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 | 5 ++ .../java/com/share/help/HelpApplication.java | 2 + .../help/controller/ActivityController.java | 21 ++++++- .../share/help/dao/ActivityHistoryMapper.java | 21 +++++++ .../com/share/help/dao/ActivityMapper.java | 3 +- .../com/share/help/dao/LeaveWordMapper.java | 14 +++++ .../com/share/help/entity/ActivityEntity.java | 18 ++++-- .../com/share/help/form/ActivityStatus.java | 13 +++++ .../share/help/form/ApplyActivityForm.java | 10 ++++ .../com/share/help/form/LeaveWordType.java | 17 ++++++ .../com/share/help/form/SendHelpForm.java | 10 ++++ .../share/help/form/UpdateActivityForm.java | 37 ++++++++++++ .../help/res/activity/ActivitySimple.java | 9 +++ .../help/res/activity/ApplyActivityRes.java | 9 +++ .../share/help/service/ActivityService.java | 56 ++++++++++++++++++- .../com/share/help/service/ImageService.java | 5 +- src/main/resources/mapping/ActivityMapper.xml | 2 + 17 files changed, 243 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/share/help/dao/ActivityHistoryMapper.java create mode 100644 src/main/java/com/share/help/dao/LeaveWordMapper.java create mode 100644 src/main/java/com/share/help/form/ActivityStatus.java create mode 100644 src/main/java/com/share/help/form/ApplyActivityForm.java create mode 100644 src/main/java/com/share/help/form/LeaveWordType.java create mode 100644 src/main/java/com/share/help/form/UpdateActivityForm.java create mode 100644 src/main/java/com/share/help/res/activity/ApplyActivityRes.java diff --git a/src/main/java/com/share/help/Constants.java b/src/main/java/com/share/help/Constants.java index 5a01c88..4904b65 100644 --- a/src/main/java/com/share/help/Constants.java +++ b/src/main/java/com/share/help/Constants.java @@ -28,4 +28,9 @@ public class Constants { //查找活动接口 public static final String ACTIVITY_INTERFACE_FIND_ID =ACTIVITY_INTERFACE_FIND+"/{activityId}" ; + + public static final String USER_COOKIE="user_cookie"; + + //活动报名接口 + public static final String ACTIVITY_INTERFACE_APPLY = "/apply"; } diff --git a/src/main/java/com/share/help/HelpApplication.java b/src/main/java/com/share/help/HelpApplication.java index bcb8fdd..d90b660 100644 --- a/src/main/java/com/share/help/HelpApplication.java +++ b/src/main/java/com/share/help/HelpApplication.java @@ -4,8 +4,10 @@ import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScans; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication +@EnableTransactionManagement @MapperScans(@MapperScan("com.share.help.dao")) public class HelpApplication { diff --git a/src/main/java/com/share/help/controller/ActivityController.java b/src/main/java/com/share/help/controller/ActivityController.java index ca3ad49..7dc4ff9 100644 --- a/src/main/java/com/share/help/controller/ActivityController.java +++ b/src/main/java/com/share/help/controller/ActivityController.java @@ -2,12 +2,14 @@ package com.share.help.controller; import com.share.help.Constants; import com.share.help.entity.ActivityEntity; +import com.share.help.form.ApplyActivityForm; import com.share.help.form.FindActivityForm; import com.share.help.form.SendHelpForm; import com.share.help.res.JSONResponse; import com.share.help.res.Result; import com.share.help.res.SendHelpRes; import com.share.help.res.activity.ActivityDetailRes; +import com.share.help.res.activity.ApplyActivityRes; import com.share.help.res.activity.FindActivityRes; import com.share.help.service.ActivityService; import org.apache.commons.lang3.ObjectUtils; @@ -28,10 +30,11 @@ public class ActivityController { * @return 返回发布结果 */ @PutMapping(Constants.ACTIVITY_INTERFACE_SEND_HELP) - public JSONResponse send(SendHelpForm sendHelpForm){ - if(StringUtils.isNoneEmpty(sendHelpForm.getTitle(),sendHelpForm.getContent(),sendHelpForm.getServiceAddress())&& + public JSONResponse send(SendHelpForm sendHelpForm,@CookieValue(Constants.USER_COOKIE) String userId){ + if(StringUtils.isNoneEmpty(userId,sendHelpForm.getTitle(),sendHelpForm.getContent(),sendHelpForm.getServiceAddress())&& ObjectUtils.allNotNull(sendHelpForm.getActivityStartTime(),sendHelpForm.getActivityEndTime() ,sendHelpForm.getActivityImgFile())){ + sendHelpForm.setUserId(userId); return activityService.sendHelp(sendHelpForm); }else{ return new JSONResponse<>(SendHelpRes.fail,Result.FAIL); @@ -65,4 +68,18 @@ public class ActivityController { return activityService.find(activityId); } } + + /** + * 报名活动 + * @return 返回报名结果 + */ + @PostMapping(Constants.ACTIVITY_INTERFACE_APPLY) + public JSONResponse apply(ApplyActivityForm activityForm,@CookieValue(Constants.USER_COOKIE) String userId){ + if(StringUtils.isNotEmpty(userId)&&ObjectUtils.allNotNull(activityForm.getActivityId(),activityForm.getActivityStatus())){ + activityForm.setUserId(userId); + return activityService.apply(activityForm); + }else{ + return new JSONResponse<>(ApplyActivityRes.fail,Result.FAIL); + } + } } diff --git a/src/main/java/com/share/help/dao/ActivityHistoryMapper.java b/src/main/java/com/share/help/dao/ActivityHistoryMapper.java new file mode 100644 index 0000000..67f2ae6 --- /dev/null +++ b/src/main/java/com/share/help/dao/ActivityHistoryMapper.java @@ -0,0 +1,21 @@ +package com.share.help.dao; + +import com.share.help.entity.ActivityHistoryEntity; +import org.apache.ibatis.annotations.Insert; + +/** + * 活动历史记录 + */ +public interface ActivityHistoryMapper { + + /** + * 添加活动历史记录 + * @param activityHistoryEntity 活动历史实体 + * @return 返回添加结果 + */ + @Insert("insert into activity_history(activity_id,user_id,activity_status,create_time,activity_score,time_score)value" + + "(#{activityId},#{userId},#{activityStatus},#{createTime},#{activityScore},#{timeScore})") + boolean insert(ActivityHistoryEntity activityHistoryEntity); + + +} diff --git a/src/main/java/com/share/help/dao/ActivityMapper.java b/src/main/java/com/share/help/dao/ActivityMapper.java index 1e2cc2d..18d2f81 100644 --- a/src/main/java/com/share/help/dao/ActivityMapper.java +++ b/src/main/java/com/share/help/dao/ActivityMapper.java @@ -17,7 +17,8 @@ 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})") + @Insert("insert into activity(activity_id,title,content,seek_help_time,activity_start_time,activity_end_time,activity_img,service_address,user_id)" + + "value(#{activityId},#{title},#{content},#{seekHelpTime},#{activityStartTime},#{activityEndTime},#{activityImg},#{serviceAddress},#{userId})") boolean insert(ActivityEntity activityEntity); /** diff --git a/src/main/java/com/share/help/dao/LeaveWordMapper.java b/src/main/java/com/share/help/dao/LeaveWordMapper.java new file mode 100644 index 0000000..671b0ab --- /dev/null +++ b/src/main/java/com/share/help/dao/LeaveWordMapper.java @@ -0,0 +1,14 @@ +package com.share.help.dao; + +import com.share.help.entity.LeaveWordEntity; +import org.apache.ibatis.annotations.Insert; + +/** + * 留言信息数据接口 + */ +public interface LeaveWordMapper { + + @Insert("insert into leave_word(leave_word_id,type,source_user_id,target_user_id,activity_id,create_time)" + + "value(#{leaveWordId},#{type},#{sourceUserId},#{targetUserId},#{activityId},#{createTime})") + boolean insert(LeaveWordEntity leaveWordEntity); +} diff --git a/src/main/java/com/share/help/entity/ActivityEntity.java b/src/main/java/com/share/help/entity/ActivityEntity.java index ac06424..c6a5849 100644 --- a/src/main/java/com/share/help/entity/ActivityEntity.java +++ b/src/main/java/com/share/help/entity/ActivityEntity.java @@ -5,7 +5,6 @@ import java.util.Collection; import java.util.Objects; - public class ActivityEntity { private Long activityId; private String title; @@ -15,6 +14,7 @@ public class ActivityEntity { private Timestamp activityEndTime; private String activityImg; private String serviceAddress; + private String userId; private Collection activityHistoriesByActivityId; private Collection leaveWordsByActivityId; @@ -98,6 +98,14 @@ public class ActivityEntity { this.serviceAddress = serviceAddress; } + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -110,15 +118,17 @@ public class ActivityEntity { Objects.equals(activityStartTime, that.activityStartTime) && Objects.equals(activityEndTime, that.activityEndTime) && Objects.equals(activityImg, that.activityImg) && - Objects.equals(serviceAddress, that.serviceAddress); + Objects.equals(serviceAddress, that.serviceAddress) && + Objects.equals(userId, that.userId) && + Objects.equals(activityHistoriesByActivityId, that.activityHistoriesByActivityId) && + Objects.equals(leaveWordsByActivityId, that.leaveWordsByActivityId); } @Override public int hashCode() { - return Objects.hash(activityId, title, content, seekHelpTime, activityStartTime, activityEndTime, activityImg, serviceAddress); + return Objects.hash(activityId, title, content, seekHelpTime, activityStartTime, activityEndTime, activityImg, serviceAddress, userId, activityHistoriesByActivityId, leaveWordsByActivityId); } - public Collection getActivityHistoriesByActivityId() { return activityHistoriesByActivityId; } diff --git a/src/main/java/com/share/help/form/ActivityStatus.java b/src/main/java/com/share/help/form/ActivityStatus.java new file mode 100644 index 0000000..7cf4a40 --- /dev/null +++ b/src/main/java/com/share/help/form/ActivityStatus.java @@ -0,0 +1,13 @@ +package com.share.help.form; + +/** + * 活动状态 + */ +public enum ActivityStatus { +// 报名 + apply, +// 参与 + join, +// 完成 + complete +} diff --git a/src/main/java/com/share/help/form/ApplyActivityForm.java b/src/main/java/com/share/help/form/ApplyActivityForm.java new file mode 100644 index 0000000..b9e433b --- /dev/null +++ b/src/main/java/com/share/help/form/ApplyActivityForm.java @@ -0,0 +1,10 @@ +package com.share.help.form; + +/** + * 报名活动表单 + */ +public class ApplyActivityForm extends UpdateActivityForm{ + + +} + diff --git a/src/main/java/com/share/help/form/LeaveWordType.java b/src/main/java/com/share/help/form/LeaveWordType.java new file mode 100644 index 0000000..06c0cbe --- /dev/null +++ b/src/main/java/com/share/help/form/LeaveWordType.java @@ -0,0 +1,17 @@ +package com.share.help.form; + +/** + * 留言类型 + */ +public enum LeaveWordType { + //推荐 + recommend, + //报名 + apply, + //参与 + join, + //完成 + complete, + //好友验证 + friend +} diff --git a/src/main/java/com/share/help/form/SendHelpForm.java b/src/main/java/com/share/help/form/SendHelpForm.java index dd50d86..16c0be8 100644 --- a/src/main/java/com/share/help/form/SendHelpForm.java +++ b/src/main/java/com/share/help/form/SendHelpForm.java @@ -7,6 +7,16 @@ import org.springframework.web.multipart.MultipartFile; * 发布求助信息表单 */ public class SendHelpForm extends ActivityEntity { +// 求助用户 + private String userId; + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } /** * 活动背景图 diff --git a/src/main/java/com/share/help/form/UpdateActivityForm.java b/src/main/java/com/share/help/form/UpdateActivityForm.java new file mode 100644 index 0000000..43d7386 --- /dev/null +++ b/src/main/java/com/share/help/form/UpdateActivityForm.java @@ -0,0 +1,37 @@ +package com.share.help.form; + +/** + * 改变活动状态 + */ +public abstract class UpdateActivityForm { + //活动id + private Long activityId; + //用户id + private String userId; + //刷新状态 + private ActivityStatus activityStatus; + + public Long getActivityId() { + return activityId; + } + + public void setActivityId(Long activityId) { + this.activityId = activityId; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public ActivityStatus getActivityStatus() { + return activityStatus; + } + + public void setActivityStatus(ActivityStatus activityStatus) { + this.activityStatus = activityStatus; + } +} diff --git a/src/main/java/com/share/help/res/activity/ActivitySimple.java b/src/main/java/com/share/help/res/activity/ActivitySimple.java index 5694d3f..f7064f0 100644 --- a/src/main/java/com/share/help/res/activity/ActivitySimple.java +++ b/src/main/java/com/share/help/res/activity/ActivitySimple.java @@ -5,10 +5,19 @@ package com.share.help.res.activity; */ public class ActivitySimple { private Long activityId; + private String userId; private String title; private String content; private String activityImg; + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + public Long getActivityId() { return activityId; } diff --git a/src/main/java/com/share/help/res/activity/ApplyActivityRes.java b/src/main/java/com/share/help/res/activity/ApplyActivityRes.java new file mode 100644 index 0000000..20d80f1 --- /dev/null +++ b/src/main/java/com/share/help/res/activity/ApplyActivityRes.java @@ -0,0 +1,9 @@ +package com.share.help.res.activity; + +/** + * 报名活动操作反馈 + */ +public enum ApplyActivityRes { + ok, + fail +} diff --git a/src/main/java/com/share/help/service/ActivityService.java b/src/main/java/com/share/help/service/ActivityService.java index d9d5a0e..d1fe90c 100644 --- a/src/main/java/com/share/help/service/ActivityService.java +++ b/src/main/java/com/share/help/service/ActivityService.java @@ -1,19 +1,29 @@ package com.share.help.service; import com.share.help.Util; +import com.share.help.dao.ActivityHistoryMapper; import com.share.help.dao.ActivityMapper; +import com.share.help.dao.LeaveWordMapper; import com.share.help.entity.ActivityEntity; +import com.share.help.entity.ActivityHistoryEntity; +import com.share.help.entity.LeaveWordEntity; +import com.share.help.form.ApplyActivityForm; import com.share.help.form.FindActivityForm; +import com.share.help.form.LeaveWordType; import com.share.help.form.SendHelpForm; import com.share.help.res.JSONResponse; import com.share.help.res.Result; import com.share.help.res.SendHelpRes; import com.share.help.res.activity.ActivityDetailRes; import com.share.help.res.activity.ActivitySimple; +import com.share.help.res.activity.ApplyActivityRes; import com.share.help.res.activity.FindActivityRes; +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; +import org.springframework.transaction.annotation.Transactional; import java.sql.Timestamp; import java.util.List; @@ -27,6 +37,12 @@ public class ActivityService{ @Autowired private ActivityMapper activityMapper; + @Autowired + private ActivityHistoryMapper activityHistoryMapper; + + @Autowired + private LeaveWordMapper leaveWordMapper; + @Autowired private ImageService imageService; @@ -38,7 +54,7 @@ public class ActivityService{ private int rowSize; - + private Logger logger = LoggerFactory.getLogger(ActivityService.class); /** * 发布求助信息 @@ -47,6 +63,7 @@ public class ActivityService{ */ public JSONResponse sendHelp(SendHelpForm sendHelpForm){ ActivityEntity activityEntity=new ActivityEntity(); + activityEntity.setUserId(sendHelpForm.getUserId()); activityEntity.setTitle(sendHelpForm.getTitle()); activityEntity.setContent(sendHelpForm.getContent()); activityEntity.setSeekHelpTime(new Timestamp(System.currentTimeMillis())); @@ -55,6 +72,7 @@ public class ActivityService{ //保存活动背景图 String imageName=imageService.saveImg(sendHelpForm.getActivityImgFile()); if(imageName==null){ + logger.error("活动背景图保存失败"); return new JSONResponse<>(SendHelpRes.fail, Result.FAIL); } activityEntity.setActivityImg(imageName); @@ -94,4 +112,40 @@ public class ActivityService{ } } + /** + * 报名活动 + * @param activityForm 报名名单 + * @return 返回报名结果 + */ + @Transactional + public JSONResponse apply(ApplyActivityForm activityForm){ +// 保存到活动历史记录 + ActivityHistoryEntity activityEntity=new ActivityHistoryEntity(); + activityEntity.setActivityId(activityForm.getActivityId()); + activityEntity.setUserId(activityForm.getUserId()); + activityEntity.setCreateTime(new Timestamp(System.currentTimeMillis())); + activityEntity.setActivityStatus(activityForm.getActivityStatus().name()); + if(!activityHistoryMapper.insert(activityEntity)){ + logger.error("保存活动历史记录失败"); + return new JSONResponse<>(ApplyActivityRes.fail,Result.FAIL); + } + +// 给求助用户发送留言 + LeaveWordEntity leaveWordEntity=new LeaveWordEntity(); + leaveWordEntity.setType(LeaveWordType.apply.name()); + leaveWordEntity.setSourceUserId(activityForm.getUserId()); + ActivityEntity activity=activityMapper.findOne(activityForm.getActivityId()); + if(activity==null){ + logger.error("查询活动记录失败"); + return new JSONResponse<>(ApplyActivityRes.fail,Result.FAIL); + } + leaveWordEntity.setTargetUserId(activity.getUserId()); + leaveWordEntity.setActivityId(activityForm.getActivityId()); + leaveWordEntity.setCreateTime(new Timestamp(System.currentTimeMillis())); + if(leaveWordMapper.insert(leaveWordEntity)){ + logger.error("保存留言信息失败"); + } + return new JSONResponse<>(ApplyActivityRes.ok,Result.OK); + } + } diff --git a/src/main/java/com/share/help/service/ImageService.java b/src/main/java/com/share/help/service/ImageService.java index 0e738a6..6dbf16e 100644 --- a/src/main/java/com/share/help/service/ImageService.java +++ b/src/main/java/com/share/help/service/ImageService.java @@ -31,6 +31,7 @@ public class ImageService { String fileName; //判断图片类型 if(file.getContentType()==null){ + logger.error("获取不到图片的格式"); return null; }else if(file.getContentType().contains(ImageType.jpg.name())){ fileName= UUID.randomUUID()+"."+ImageType.jpg.name(); @@ -39,16 +40,18 @@ public class ImageService { }else if (file.getContentType().contains(ImageType.png.name())) { fileName= UUID.randomUUID()+"."+ImageType.png.name(); } else { + logger.error("图片文件格式不正确"); return null; } File loadFile = new File(getClass().getResource("/").getFile() + imgPath.replace("classpath:/", ""), fileName); try { file.transferTo(loadFile); + logger.debug("图片保存到"+loadFile.getAbsolutePath()); return fileName; } catch (IOException e) { e.printStackTrace(); - logger.error(String.valueOf(e)); + logger.error("保存图片文件异常信息:"+e); return null; } } diff --git a/src/main/resources/mapping/ActivityMapper.xml b/src/main/resources/mapping/ActivityMapper.xml index a69b06f..ac8be6c 100644 --- a/src/main/resources/mapping/ActivityMapper.xml +++ b/src/main/resources/mapping/ActivityMapper.xml @@ -12,10 +12,12 @@ + +