From f8c9d17dc68e3af8126778ab2999dc22b4076eb5 Mon Sep 17 00:00:00 2001 From: chuyan <1047381936@qq.com> Date: Thu, 10 Sep 2020 20:58:43 +0800 Subject: [PATCH] =?UTF-8?q?ActivityService=E8=BF=9B=E4=B8=80=E6=AD=A5?= =?UTF-8?q?=E5=AE=8C=E5=96=84=EF=BC=9B=20=E4=BF=AE=E6=94=B9=E9=83=A8?= =?UTF-8?q?=E5=88=86Model=E5=B9=B6=E9=87=8D=E7=BD=AE=E5=88=86=E7=BB=84=20?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E6=9C=BA=E5=88=B6=E4=BB=8D?= =?UTF-8?q?=E5=BE=85=E8=A7=A3=E5=86=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ase/model/activity/Activity.java | 8 +- .../model/{parent => message}/Message.java | 15 +- .../{student => message}/Notification.java | 2 +- .../repository/message/MessageRepository.java | 18 +++ .../student/NotificationRepository.java | 2 +- .../ase/service/ActivityService.java | 133 ++++++++++++++++++ .../ase/service/MessageService.java | 59 ++++++++ .../service/{student => }/StudentService.java | 7 +- .../ase/service/activity/ActivityService.java | 41 ------ 9 files changed, 227 insertions(+), 58 deletions(-) rename src/main/java/com/codesdream/ase/model/{parent => message}/Message.java (55%) rename src/main/java/com/codesdream/ase/model/{student => message}/Notification.java (89%) create mode 100644 src/main/java/com/codesdream/ase/repository/message/MessageRepository.java create mode 100644 src/main/java/com/codesdream/ase/service/ActivityService.java create mode 100644 src/main/java/com/codesdream/ase/service/MessageService.java rename src/main/java/com/codesdream/ase/service/{student => }/StudentService.java (97%) delete mode 100644 src/main/java/com/codesdream/ase/service/activity/ActivityService.java diff --git a/src/main/java/com/codesdream/ase/model/activity/Activity.java b/src/main/java/com/codesdream/ase/model/activity/Activity.java index 09a628a..b3690a8 100644 --- a/src/main/java/com/codesdream/ase/model/activity/Activity.java +++ b/src/main/java/com/codesdream/ase/model/activity/Activity.java @@ -2,7 +2,6 @@ package com.codesdream.ase.model.activity; import com.codesdream.ase.model.file.File; import com.codesdream.ase.model.mark.Tag; -import com.codesdream.ase.model.permission.User; import lombok.Data; import javax.persistence.*; @@ -19,11 +18,10 @@ public class Activity { String title; - @ManyToOne - User creator; + int creatorId; - @ManyToMany - List manager; + @ElementCollection + List managerIds = new ArrayList<>(); @ElementCollection List participantIds = new ArrayList<>(); diff --git a/src/main/java/com/codesdream/ase/model/parent/Message.java b/src/main/java/com/codesdream/ase/model/message/Message.java similarity index 55% rename from src/main/java/com/codesdream/ase/model/parent/Message.java rename to src/main/java/com/codesdream/ase/model/message/Message.java index a0e9501..5daa945 100644 --- a/src/main/java/com/codesdream/ase/model/parent/Message.java +++ b/src/main/java/com/codesdream/ase/model/message/Message.java @@ -1,13 +1,12 @@ -package com.codesdream.ase.model.parent; +package com.codesdream.ase.model.message; -import com.codesdream.ase.model.file.File; -import com.codesdream.ase.model.mark.Tag; import com.codesdream.ase.model.permission.User; import lombok.Data; -import javax.persistence.*; -import java.util.ArrayList; -import java.util.Date; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; import java.util.List; @Entity @@ -18,8 +17,10 @@ public class Message { int id; String title; + String text; - Boolean have_read; + + boolean isRead = false; // 重要性 值为0-1 int type; diff --git a/src/main/java/com/codesdream/ase/model/student/Notification.java b/src/main/java/com/codesdream/ase/model/message/Notification.java similarity index 89% rename from src/main/java/com/codesdream/ase/model/student/Notification.java rename to src/main/java/com/codesdream/ase/model/message/Notification.java index f1fba39..33c2322 100644 --- a/src/main/java/com/codesdream/ase/model/student/Notification.java +++ b/src/main/java/com/codesdream/ase/model/message/Notification.java @@ -1,4 +1,4 @@ -package com.codesdream.ase.model.student; +package com.codesdream.ase.model.message; import com.codesdream.ase.model.file.File; import lombok.Data; diff --git a/src/main/java/com/codesdream/ase/repository/message/MessageRepository.java b/src/main/java/com/codesdream/ase/repository/message/MessageRepository.java new file mode 100644 index 0000000..6858e7f --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/message/MessageRepository.java @@ -0,0 +1,18 @@ +package com.codesdream.ase.repository.message; + +import com.codesdream.ase.model.message.Message; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface MessageRepository extends JpaRepository { + + List findByTitle(String title); + List findByTitle(String title, Sort sort); + + List findByTitleContaining(String title); + List findByTitleContaining(String title, Sort sort); +} diff --git a/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java b/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java index 780ed96..b8220b2 100644 --- a/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java +++ b/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java @@ -1,6 +1,6 @@ package com.codesdream.ase.repository.student; -import com.codesdream.ase.model.student.Notification; +import com.codesdream.ase.model.message.Notification; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; diff --git a/src/main/java/com/codesdream/ase/service/ActivityService.java b/src/main/java/com/codesdream/ase/service/ActivityService.java new file mode 100644 index 0000000..5266e6a --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/ActivityService.java @@ -0,0 +1,133 @@ +package com.codesdream.ase.service; + +import com.codesdream.ase.exception.notfound.NotFoundException; +import com.codesdream.ase.model.activity.Activity; +import com.codesdream.ase.model.message.Message; +import com.codesdream.ase.model.permission.User; +import com.codesdream.ase.repository.activity.ActivityRepository; +import com.codesdream.ase.repository.permission.UserRepository; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Service +public class ActivityService { + + @Resource + ActivityRepository activityRepository; + + @Resource + UserRepository userRepository; + + @Resource + MessageService messageService; + + /** + * 为指定活动添加指定成员 + * @param activityId 活动id + * @param memberId 成员id + * @param type 成员类型,false表示管理员,true表示普通参与者 + * @return 更新之后的活动 + */ + public Activity addMember(int activityId, int memberId, boolean type){ + + Optional optionalActivity = activityRepository.findById(activityId); + Optional optionalUser = userRepository.findById(memberId); + if(!optionalActivity.isPresent() || !optionalUser.isPresent()){ + return null; + } + Activity activity = optionalActivity.get(); + User user = optionalUser.get(); + if(!type){ + activity.getManagerIds().add(user.getId()); + } + else{ + activity.getParticipantIds().add(user.getId()); + } + return activityRepository.save(activity); + } + + /** + * 删除给定活动的指定参与者 + * @param memberId 参与者id + * @param activityId 活动id + * @param type 参与者类型,0表示管理员,1表示普通参与者 + * @return 是否删除成功 + */ + public boolean removeMember(int memberId, int activityId, int type){ + + Optional optionalUser = userRepository.findById(memberId); + Optional optionalActivity = activityRepository.findById(activityId); + try{ + if(!optionalActivity.isPresent() || !optionalUser.isPresent()){ + throw new NotFoundException("No such activity or user."); + } + } catch (Exception e){ + return false; + } + + Activity activity = optionalActivity.get(); + if(type == 0){ + try{ + if(!activity.getManagerIds().contains(memberId)){ + throw new NotFoundException("No such person managed this activity."); + } + }catch (Exception e){ + return false; + } + activity.getManagerIds().remove(memberId); + } else{ + try{ + if(!activity.getParticipantIds().contains(memberId)){ + throw new NotFoundException("No such person participated in this activity."); + } + }catch (Exception e){ + return false; + } + activity.getParticipantIds().remove(memberId); + + } + + activityRepository.save(activity); + return true; + } + + /** + * 为活动相关人员推送DDL提醒 + * @Todo + * @param activityId 活动id + * @return 是否推送成功 + */ + public boolean sendDDLToGroup(int activityId){ + + Optional optionalActivity = activityRepository.findById(activityId); + if(!optionalActivity.isPresent()){ + throw new NotFoundException("No such activity."); + } + Activity activity = optionalActivity.get(); + List targets = new ArrayList<>(); + List memberIds = activity.getParticipantIds(); + for(Integer memberId : memberIds){ + targets.add(userRepository.findById(memberId).get()); + } + for(Integer memberId : activity.getManagerIds()){ + targets.add(userRepository.findById(memberId).get()); + } + targets.add(userRepository.findById(activity.getCreatorId()).get()); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + Message message = messageService.createMessage( + String.format("活动%s即将开始", activity.getTitle()), + formatter.format(activity.getRealBeginDate()) + ); + + + return messageService.sendMessage(message, targets); + + } + +} diff --git a/src/main/java/com/codesdream/ase/service/MessageService.java b/src/main/java/com/codesdream/ase/service/MessageService.java new file mode 100644 index 0000000..7c82a70 --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/MessageService.java @@ -0,0 +1,59 @@ +package com.codesdream.ase.service; + +import com.codesdream.ase.model.message.Message; +import com.codesdream.ase.model.permission.User; +import com.codesdream.ase.repository.message.MessageRepository; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +@Service +public class MessageService { + + @Resource + MessageRepository messageRepository; + + /** + * 创建新消息 + * @see Message + * @param title 消息标题 + * @param text 消息内容 + * @return 持久化的消息 + */ + public Message createMessage(String title, String text){ + Message message = new Message(); + + message.setText(text); + message.setTitle(title); + + return messageRepository.save(message); + } + + /** + * 创建新消息 + * @see Message + * @param title 消息标题 + * @param text 消息内容 + * @param type 消息类型(紧急程度) + * @return 持久化的消息 + */ + public Message createMessage(String title, String text, int type){ + Message message = createMessage(title, text); + message.setType(type); + return messageRepository.save(message); + } + + /** + * 推送消息到指定用户名下 + * @Todo + * @param message 待传递的消息 + * @param targets 目标用户 + * @return 是否推送成功 + */ + public boolean sendMessage(Message message, List targets){ + + return true; + } + +} diff --git a/src/main/java/com/codesdream/ase/service/student/StudentService.java b/src/main/java/com/codesdream/ase/service/StudentService.java similarity index 97% rename from src/main/java/com/codesdream/ase/service/student/StudentService.java rename to src/main/java/com/codesdream/ase/service/StudentService.java index 8644b67..44837ff 100644 --- a/src/main/java/com/codesdream/ase/service/student/StudentService.java +++ b/src/main/java/com/codesdream/ase/service/StudentService.java @@ -1,4 +1,4 @@ -package com.codesdream.ase.service.student; +package com.codesdream.ase.service; import com.codesdream.ase.exception.innerservererror.DataInvalidFormatException; import com.codesdream.ase.exception.innerservererror.InvalidDataException; @@ -8,12 +8,11 @@ import com.codesdream.ase.model.file.File; import com.codesdream.ase.model.file.Image; import com.codesdream.ase.model.permission.UserDetail; import com.codesdream.ase.model.student.Honor; -import com.codesdream.ase.model.student.Notification; +import com.codesdream.ase.model.message.Notification; import com.codesdream.ase.model.student.Student; import com.codesdream.ase.repository.student.HonorRepository; import com.codesdream.ase.repository.student.NotificationRepository; import com.codesdream.ase.repository.student.StudentRepository; -import com.codesdream.ase.service.activity.ActivityService; import com.codesdream.ase.validator.GeneralValidator; import org.springframework.stereotype.Service; @@ -39,6 +38,7 @@ public class StudentService { /** * 用于创建一个公告 + * @see Notification * @param title 公告标题 * @param description 公告内容 * @param files 公告所需附件 @@ -84,6 +84,7 @@ public class StudentService { /** * 创建并持久化一个荣誉 * @see Image + * @see Honor * @param studentId 荣誉所对应的学生id * @param description 荣誉的描述 * @param images 荣誉的证明材料(图片) diff --git a/src/main/java/com/codesdream/ase/service/activity/ActivityService.java b/src/main/java/com/codesdream/ase/service/activity/ActivityService.java deleted file mode 100644 index b6407e7..0000000 --- a/src/main/java/com/codesdream/ase/service/activity/ActivityService.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.codesdream.ase.service.activity; - -import com.codesdream.ase.model.activity.Activity; -import com.codesdream.ase.model.permission.User; -import com.codesdream.ase.repository.activity.ActivityRepository; -import com.codesdream.ase.repository.permission.UserRepository; -import org.springframework.stereotype.Service; - -import javax.annotation.Resource; -import java.util.Optional; - -@Service -public class ActivityService { - - @Resource - ActivityRepository activityRepository; - - @Resource - UserRepository userRepository; - - public Activity addMember(int activityId, int memberId, boolean type){ - - Optional optionalActivity = activityRepository.findById(activityId); - Optional optionalUser = userRepository.findById(memberId); - if(!optionalActivity.isPresent() || !optionalUser.isPresent()){ - return null; - } - Activity activity = optionalActivity.get(); - User user = optionalUser.get(); - if(!type){ - activity.getManager().add(user); - } - else{ - activity.getParticipantIds().add(user.getId()); - } - return activityRepository.save(activity); - } - - - -}