ActivityService进一步完善;

修改部分Model并重置分组
消息处理机制仍待解决
This commit is contained in:
chuyan 2020-09-10 20:58:43 +08:00
parent 49d8892b1d
commit f8c9d17dc6
9 changed files with 227 additions and 58 deletions

View File

@ -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<User> manager;
@ElementCollection
List<Integer> managerIds = new ArrayList<>();
@ElementCollection
List<Integer> participantIds = new ArrayList<>();

View File

@ -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;

View File

@ -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;

View File

@ -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<Message, Integer> {
List<Message> findByTitle(String title);
List<Message> findByTitle(String title, Sort sort);
List<Message> findByTitleContaining(String title);
List<Message> findByTitleContaining(String title, Sort sort);
}

View File

@ -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;

View File

@ -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<Activity> optionalActivity = activityRepository.findById(activityId);
Optional<User> 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<User> optionalUser = userRepository.findById(memberId);
Optional<Activity> 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<Activity> optionalActivity = activityRepository.findById(activityId);
if(!optionalActivity.isPresent()){
throw new NotFoundException("No such activity.");
}
Activity activity = optionalActivity.get();
List<User> targets = new ArrayList<>();
List<Integer> 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);
}
}

View File

@ -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<User> targets){
return true;
}
}

View File

@ -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 荣誉的证明材料图片

View File

@ -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<Activity> optionalActivity = activityRepository.findById(activityId);
Optional<User> 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);
}
}