From 0a00898ce01c72eba21ff554e46690942346a47a Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Fri, 29 May 2020 17:35:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=8C=E6=84=8F=E3=80=81?= =?UTF-8?q?=E6=8B=92=E7=BB=9D=E5=A5=BD=E5=8F=8B=E9=82=80=E8=AF=B7=E6=8E=A5?= =?UTF-8?q?=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 | 6 ++ .../share/help/controller/UserController.java | 37 +++++++++++ .../java/com/share/help/dao/FriendMapper.java | 13 ++++ .../com/share/help/dao/LeaveWordMapper.java | 11 +++- .../help/entity/custorm/LeaveWordRes.java | 10 +++ .../com/share/help/form/ActivityStatus.java | 1 + .../com/share/help/form/AgreeFriendForm.java | 34 ++++++++++ .../share/help/form/NotActivityStatus.java | 8 +++ .../com/share/help/form/SendFriendForm.java | 27 ++++++++ .../share/help/service/ActivityService.java | 2 +- .../share/help/service/LeaveWordService.java | 66 ++++++++++++++++++- 11 files changed, 211 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/share/help/dao/FriendMapper.java create mode 100644 src/main/java/com/share/help/form/AgreeFriendForm.java create mode 100644 src/main/java/com/share/help/form/NotActivityStatus.java create mode 100644 src/main/java/com/share/help/form/SendFriendForm.java diff --git a/src/main/java/com/share/help/Constants.java b/src/main/java/com/share/help/Constants.java index 8efbf99..ee8d627 100644 --- a/src/main/java/com/share/help/Constants.java +++ b/src/main/java/com/share/help/Constants.java @@ -53,4 +53,10 @@ public class Constants { public static final String ACTIVITY_INTERFACE_HELP = "/help"; //修改分数 public static final String ACTIVITY_INTERFACE_SCORE = "/score"; + //发送好友邀请 + public static final String USER_INTERFACE_SEND_FRIEND = "/friend"; + //拒绝好友邀请 + public static final String USER_INTERFACE_REFUSE_FRIEND = "/refuse/{id}"; + //同意好友邀请 + public static final String USER_INTERFACE_AGREE_FRIEND = "/agree"; } diff --git a/src/main/java/com/share/help/controller/UserController.java b/src/main/java/com/share/help/controller/UserController.java index 8df442f..7485ed7 100644 --- a/src/main/java/com/share/help/controller/UserController.java +++ b/src/main/java/com/share/help/controller/UserController.java @@ -159,4 +159,41 @@ public class UserController { } + /** + *发送好友邀请 + */ + @PostMapping(Constants.USER_INTERFACE_SEND_FRIEND) + public JSONResponse sendFriend(SendFriendForm sendFriendForm,@CookieValue(Constants.USER_COOKIE) String userId){ + if(ObjectUtils.allNotNull(sendFriendForm.getTargetUserId(),userId)){ + sendFriendForm.setSourceUserId(userId); + return leaveWordService.sendFriend(sendFriendForm); + }else { + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + } + + /** + * 拒绝好友邀请 + * @param id 留言id + * @return 返回拒绝结果 + */ + @PostMapping(Constants.USER_INTERFACE_REFUSE_FRIEND) + public JSONResponse refuseFriend(@PathVariable Long id){ + if(id!=null){ + return leaveWordService.refuseFriend(id); + }else{ + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + } + + @PostMapping(Constants.USER_INTERFACE_AGREE_FRIEND) + public JSONResponse agreeFriend(AgreeFriendForm agreeFriendForm,@CookieValue(Constants.USER_COOKIE) String userId){ + if(ObjectUtils.allNotNull(agreeFriendForm.getSourceUserId(),userId,agreeFriendForm.getLeaveWordId())){ + agreeFriendForm.setTargetUserId(userId); + return leaveWordService.agreeFriend(agreeFriendForm); + }else{ + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + } + } diff --git a/src/main/java/com/share/help/dao/FriendMapper.java b/src/main/java/com/share/help/dao/FriendMapper.java new file mode 100644 index 0000000..de758ab --- /dev/null +++ b/src/main/java/com/share/help/dao/FriendMapper.java @@ -0,0 +1,13 @@ +package com.share.help.dao; + +import com.share.help.entity.FriendEntity; +import org.apache.ibatis.annotations.Insert; + +/** + * 添加好友 + */ +public interface FriendMapper { + + @Insert("insert into friend(source_user_id,target_user_id) value(#{sourceUserId},#{targetUserId})") + boolean addFriend(FriendEntity friendEntity); +} diff --git a/src/main/java/com/share/help/dao/LeaveWordMapper.java b/src/main/java/com/share/help/dao/LeaveWordMapper.java index 540aeee..ef5502b 100644 --- a/src/main/java/com/share/help/dao/LeaveWordMapper.java +++ b/src/main/java/com/share/help/dao/LeaveWordMapper.java @@ -3,6 +3,7 @@ package com.share.help.dao; import com.share.help.entity.LeaveWordEntity; import com.share.help.entity.custorm.LeaveWordRes; import com.share.help.res.Page; +import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; @@ -20,11 +21,19 @@ public interface LeaveWordMapper { boolean insert(LeaveWordEntity leaveWordEntity); // 查询留言列表 - @Select("select user.user_id as userId,user.name,user.head_img as headImg,user.info,leave_word.type,activity_id as activityId from leave_word,user\n" + + @Select("select leave_word.source_user_id as userId,user.name,user.head_img as headImg,user.info,leave_word.type,activity_id as activityId,leave_word_id as leaveWordId from leave_word,user\n" + "where leave_word.target_user_id=#{userId} and leave_word.target_user_id=user.user_id order by create_time desc limit #{start},#{count}") List select(@Param("start") long start,@Param("count") long count,@Param("userId") String userId); // 统计留言列表分页数 @Select("select count(*) as count,ceil(count(*)/#{pageSize}) as totalPage from leave_word where target_user_id=#{userId}") Page> count(@Param("pageSize") Integer pageSize,@Param("userId") String userId); + + /** + * 删除留言 + * @param leaveWordId 留言id + * @return 返回删除结果 + */ + @Delete("delete from leave_word where leave_word_id=#{leaveWordId}") + boolean deleteOne(@Param("leaveWordId") Long leaveWordId); } diff --git a/src/main/java/com/share/help/entity/custorm/LeaveWordRes.java b/src/main/java/com/share/help/entity/custorm/LeaveWordRes.java index ef4961e..508bb20 100644 --- a/src/main/java/com/share/help/entity/custorm/LeaveWordRes.java +++ b/src/main/java/com/share/help/entity/custorm/LeaveWordRes.java @@ -8,6 +8,8 @@ import com.share.help.form.LeaveWordType; public class LeaveWordRes { // 留言用户id private String userId; + // 留言id + private Long leaveWordId; // 留言用户姓名 private String name; // 留言用户头像 @@ -23,6 +25,14 @@ public class LeaveWordRes { //活动内容 private String content; + public Long getLeaveWordId() { + return leaveWordId; + } + + public void setLeaveWordId(Long leaveWordId) { + this.leaveWordId = leaveWordId; + } + public String getTitle() { return title; } diff --git a/src/main/java/com/share/help/form/ActivityStatus.java b/src/main/java/com/share/help/form/ActivityStatus.java index 7cf4a40..92aacae 100644 --- a/src/main/java/com/share/help/form/ActivityStatus.java +++ b/src/main/java/com/share/help/form/ActivityStatus.java @@ -11,3 +11,4 @@ public enum ActivityStatus { // 完成 complete } + diff --git a/src/main/java/com/share/help/form/AgreeFriendForm.java b/src/main/java/com/share/help/form/AgreeFriendForm.java new file mode 100644 index 0000000..af76dad --- /dev/null +++ b/src/main/java/com/share/help/form/AgreeFriendForm.java @@ -0,0 +1,34 @@ +package com.share.help.form; + +/** + * 同意好友邀请表单 + */ +public class AgreeFriendForm { + private Long leaveWordId; + private String sourceUserId; + private String targetUserId; + + public Long getLeaveWordId() { + return leaveWordId; + } + + public void setLeaveWordId(Long leaveWordId) { + this.leaveWordId = leaveWordId; + } + + public String getSourceUserId() { + return sourceUserId; + } + + public void setSourceUserId(String sourceUserId) { + this.sourceUserId = sourceUserId; + } + + public String getTargetUserId() { + return targetUserId; + } + + public void setTargetUserId(String targetUserId) { + this.targetUserId = targetUserId; + } +} diff --git a/src/main/java/com/share/help/form/NotActivityStatus.java b/src/main/java/com/share/help/form/NotActivityStatus.java new file mode 100644 index 0000000..c73a580 --- /dev/null +++ b/src/main/java/com/share/help/form/NotActivityStatus.java @@ -0,0 +1,8 @@ +package com.share.help.form; + +public enum NotActivityStatus { + //推荐 + recommend, + //好友 + friend +} diff --git a/src/main/java/com/share/help/form/SendFriendForm.java b/src/main/java/com/share/help/form/SendFriendForm.java new file mode 100644 index 0000000..26634d5 --- /dev/null +++ b/src/main/java/com/share/help/form/SendFriendForm.java @@ -0,0 +1,27 @@ +package com.share.help.form; + +/** + * 发送好友邀请表单 + */ +public class SendFriendForm { + //发送邀请用户 + private String sourceUserId; + //添加用户 + private String targetUserId; + + public String getSourceUserId() { + return sourceUserId; + } + + public void setSourceUserId(String sourceUserId) { + this.sourceUserId = sourceUserId; + } + + public String getTargetUserId() { + return targetUserId; + } + + public void setTargetUserId(String targetUserId) { + this.targetUserId = targetUserId; + } +} diff --git a/src/main/java/com/share/help/service/ActivityService.java b/src/main/java/com/share/help/service/ActivityService.java index d1d3aa6..80e0c4d 100644 --- a/src/main/java/com/share/help/service/ActivityService.java +++ b/src/main/java/com/share/help/service/ActivityService.java @@ -127,7 +127,7 @@ public class ActivityService{ // 给求助用户发送留言 LeaveWordEntity leaveWordEntity=new LeaveWordEntity(); - leaveWordEntity.setType(activityForm.getActivityStatus().toString()); + leaveWordEntity.setType(activityForm.getActivityStatus().name()); leaveWordEntity.setSourceUserId(activityForm.getUserId()); ActivityEntity activity=activityMapper.findOne(activityForm.getActivityId()); if(activity==null){ diff --git a/src/main/java/com/share/help/service/LeaveWordService.java b/src/main/java/com/share/help/service/LeaveWordService.java index 7f8412c..e85a018 100644 --- a/src/main/java/com/share/help/service/LeaveWordService.java +++ b/src/main/java/com/share/help/service/LeaveWordService.java @@ -1,19 +1,24 @@ package com.share.help.service; import com.share.help.dao.ActivityMapper; +import com.share.help.dao.FriendMapper; import com.share.help.dao.LeaveWordMapper; import com.share.help.entity.ActivityEntity; +import com.share.help.entity.FriendEntity; +import com.share.help.entity.LeaveWordEntity; import com.share.help.entity.custorm.LeaveWordRes; -import com.share.help.form.FindLeaveWordForm; -import com.share.help.form.LeaveWordType; +import com.share.help.form.*; import com.share.help.res.DefaultRes; import com.share.help.res.JSONResponse; import com.share.help.res.Page; import com.share.help.res.Result; +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 java.sql.Timestamp; import java.util.List; @Service @@ -28,6 +33,11 @@ public class LeaveWordService { @Autowired private ActivityMapper activityMapper; + @Autowired + private FriendMapper friendMapper; + + private Logger logger= LoggerFactory.getLogger(LeaveWordService.class); + /** * 查询留言 * @param findLeaveWordForm 查询条件 @@ -46,4 +56,56 @@ public class LeaveWordService { listPage.setBody(leaveWordRes); return new JSONResponse>>(DefaultRes.ok, Result.OK).setBody(listPage); } + + /** + * 发送好友邀请 + * @param sendFriendForm 邀请表单 + * @return 返回发送结果 + */ + public JSONResponse sendFriend(SendFriendForm sendFriendForm){ + LeaveWordEntity leaveWordEntity=new LeaveWordEntity(); + leaveWordEntity.setCreateTime(new Timestamp(System.currentTimeMillis())); + leaveWordEntity.setSourceUserId(sendFriendForm.getSourceUserId()); + leaveWordEntity.setTargetUserId(sendFriendForm.getTargetUserId()); + leaveWordEntity.setType(NotActivityStatus.friend.name()); + if(leaveWordMapper.insert(leaveWordEntity)){ + return new JSONResponse<>(DefaultRes.ok,Result.OK); + }else{ + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + } + + /** + * 拒绝好友邀请 + * @param id 留言id + * @return 返回拒绝结果 + */ + public JSONResponse refuseFriend(Long id){ + if(leaveWordMapper.deleteOne(id)){ + return new JSONResponse<>(DefaultRes.ok,Result.OK); + }else{ + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + } + + /** + * 同意好友邀请 + * @param agreeFriendForm 邀请表单 + * @return 返回邀请结果 + */ + public JSONResponse agreeFriend(AgreeFriendForm agreeFriendForm) { + if(!leaveWordMapper.deleteOne(agreeFriendForm.getLeaveWordId())){ + logger.error("删除留言失败"); + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + + FriendEntity friendEntity=new FriendEntity(); + friendEntity.setSourceUserId(agreeFriendForm.getSourceUserId()); + friendEntity.setTargetUserId(agreeFriendForm.getTargetUserId()); + if(friendMapper.addFriend(friendEntity)){ + return new JSONResponse<>(DefaultRes.ok,Result.OK); + }else{ + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } + } }