From 429ed0d941a06ee056f297cc0208df8ea78365a6 Mon Sep 17 00:00:00 2001 From: chuyan <1047381936@qq.com> Date: Wed, 19 Feb 2020 14:34:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B4=BB=E5=8A=A8=E5=AD=90=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BF=AE=E6=94=B9=EF=BC=8C=E5=88=9D=E6=AD=A5?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ase/model/activity/Activity.java | 66 +++++++++++++------ .../ase/model/activity/Attendance.java | 6 +- .../codesdream/ase/model/activity/Period.java | 12 ++-- .../codesdream/ase/model/activity/Report.java | 15 +++-- .../{ => activity}/ActivityRepository.java | 2 +- .../{ => activity}/ReportRepository.java | 2 +- .../ase/service/ActivityService.java | 15 ++--- .../codesdream/ase/service/ReportService.java | 7 +- .../ase/test/ActivityServiceTest.java | 56 ++++++++++++++++ 9 files changed, 134 insertions(+), 47 deletions(-) rename src/main/java/com/codesdream/ase/repository/{ => activity}/ActivityRepository.java (89%) rename src/main/java/com/codesdream/ase/repository/{ => activity}/ReportRepository.java (89%) create mode 100644 src/main/java/com/codesdream/ase/test/ActivityServiceTest.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 c75703f..30c6f4d 100644 --- a/src/main/java/com/codesdream/ase/model/activity/Activity.java +++ b/src/main/java/com/codesdream/ase/model/activity/Activity.java @@ -2,11 +2,10 @@ package com.codesdream.ase.model.activity; import com.codesdream.ase.model.permission.User; import lombok.Data; +import org.springframework.security.core.parameters.P; import javax.persistence.*; -import java.util.Date; -import java.util.List; -import java.util.Set; +import java.util.*; @Data @Entity @@ -21,7 +20,7 @@ public class Activity { private String title; //创建人 - @OneToOne(cascade = CascadeType.ALL) + @ManyToOne(cascade = CascadeType.MERGE) @JoinColumn(nullable = false) private User creator; @@ -30,20 +29,20 @@ public class Activity { private String type; //活动描述 - @Column(nullable = true) + @Column private String description; //活动周期,格式:阿拉伯数字数字+单位,0表示无周期 - @Column(nullable = true) + @Column private String cycle; //自愿参与人列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.DETACH) @JoinTable(name = "act_volunteer") private Set volunteers; //参与人列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.DETACH) @JoinTable(name = "act_participate", joinColumns = { @JoinColumn(name = "act_id") @@ -55,7 +54,7 @@ public class Activity { private Set participateGroup; //实际参与人列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.DETACH) @JoinTable(name = "act_participated", joinColumns = { @JoinColumn(name = "act_id" ) @@ -67,7 +66,7 @@ public class Activity { private Set participatedGroup; //可报名人员列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.DETACH) @JoinTable(name = "act_sign", joinColumns = { @JoinColumn(name = "act_id") @@ -79,7 +78,7 @@ public class Activity { private Set signGroup; //已报名人员列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.MERGE) @JoinTable(name = "act_signed", joinColumns = { @JoinColumn(name = "act_id") @@ -91,7 +90,7 @@ public class Activity { private Set signedGroup; //可见人员列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.DETACH) @JoinTable(name = "act_vis", joinColumns = { @JoinColumn(name = "act_id") @@ -103,7 +102,7 @@ public class Activity { private Set visibleGroup; //通知人员列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.DETACH) @JoinTable(name = "act_inform", joinColumns = { @JoinColumn(name = "act_id") @@ -115,7 +114,7 @@ public class Activity { private Set informGroup; //已通知人员列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.DETACH) @JoinTable(name = "act_informed", joinColumns = { @JoinColumn(name = "act_id") @@ -136,6 +135,7 @@ public class Activity { //提前提醒时间 @Column(name = "remind_time", nullable = true) + @Temporal(TemporalType.TIMESTAMP) private Date remindTime; //附件组(名字) @@ -143,21 +143,21 @@ public class Activity { private List enclosures; //主要负责人 - @OneToOne(cascade = CascadeType.ALL) + @OneToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY) @JoinColumn(nullable = false) private User chiefManager; //次要负责人 - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY) @JoinColumn(name = "act_id") - private List assistManagers; + private Set assistManagers; //是否开始 - @JoinColumn(name = "is_on", nullable = false) + @Column//(name = "is_on", nullable = false) boolean isOn; //是否结束 - @JoinColumn(name = "is_off", nullable = false) + @Column//(name = "is_off", nullable = false) boolean isOff; //考勤安排 @@ -167,4 +167,32 @@ public class Activity { //活动报告 @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) private Report report; + + public Activity(){ + initDefault(); + } + + private void initDefault(){ + this.title = ""; + this.creator = new User(); + this.type = ""; + this.description = ""; + this.cycle = ""; + this.volunteers = new HashSet<>(); + this.participateGroup = new HashSet<>(); + this.participatedGroup = new HashSet<>(); + this.participatedGroup = new HashSet<>(); + this.signGroup = new HashSet<>(); + this.signedGroup = new HashSet<>(); + this.visibleGroup = new HashSet<>(); + this.informGroup = new HashSet<>(); + this.informedGroup = new HashSet<>(); + this.planPeriod = new Period(); + this.realPeriod = new Period(); + this.enclosures = new ArrayList<>(); + this.chiefManager = new User(); + this.assistManagers = new HashSet<>(); + this.isOn = false; + this.isOff = false; + } } \ No newline at end of file diff --git a/src/main/java/com/codesdream/ase/model/activity/Attendance.java b/src/main/java/com/codesdream/ase/model/activity/Attendance.java index c68cb49..e37c93d 100644 --- a/src/main/java/com/codesdream/ase/model/activity/Attendance.java +++ b/src/main/java/com/codesdream/ase/model/activity/Attendance.java @@ -16,11 +16,11 @@ public class Attendance { private int id; //二维码url - @Column(name = "qr_code", nullable = false, unique = true) + @Column(name = "qr_code")//, nullable = false, unique = true) private String QRCode; //是否在线 - @Column(name = "is_online", nullable = false) + @Column(name = "is_online")//, nullable = false) private boolean isOnline; //打卡时段列表 @@ -28,6 +28,6 @@ public class Attendance { private Set clockInPeriods; //打卡方式,0表示被扫,1表示扫 - @Column(nullable = false) + @Column//(nullable = false) private boolean means; } diff --git a/src/main/java/com/codesdream/ase/model/activity/Period.java b/src/main/java/com/codesdream/ase/model/activity/Period.java index f85d56c..38c12ec 100644 --- a/src/main/java/com/codesdream/ase/model/activity/Period.java +++ b/src/main/java/com/codesdream/ase/model/activity/Period.java @@ -4,6 +4,8 @@ import lombok.Data; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import javax.persistence.*; +import java.time.LocalDateTime; +import java.util.Calendar; import java.util.Date; import java.util.Objects; @@ -16,15 +18,15 @@ public class Period { private int id; //开始时间 - @Column(name = "start_time", nullable = false) - private Date startTime; + @Column(name = "start_time")//, nullable = false) + private LocalDateTime startTime = LocalDateTime.of(2020,2,18,16,36); //结束时间 - @Column(name = "end_time", nullable = false) - private Date endTime; + @Column(name = "end_time")//, nullable = false) + private LocalDateTime endTime = LocalDateTime.of(2020,2,18,16,37); //启用状态 - @Column(name = "enabled", nullable = false) + @Column(name = "enabled")//, nullable = false) private boolean enabled; } diff --git a/src/main/java/com/codesdream/ase/model/activity/Report.java b/src/main/java/com/codesdream/ase/model/activity/Report.java index 12f0e56..2bc6a0f 100644 --- a/src/main/java/com/codesdream/ase/model/activity/Report.java +++ b/src/main/java/com/codesdream/ase/model/activity/Report.java @@ -23,8 +23,8 @@ public class Report { private String title; //创建人 - @Column(nullable = false) - private String creator; + @OneToOne(cascade = CascadeType.MERGE) + private User creator; //活动类型 @Column(nullable = false) @@ -39,12 +39,12 @@ public class Report { private String cycle; //自愿参与人列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.MERGE) @JoinTable(name = "act_volunteer") private Set volunteers; //参与人列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.MERGE) @JoinTable(name = "act_participate", joinColumns = { @JoinColumn(name = "act_id") @@ -68,7 +68,7 @@ public class Report { private Set participatedGroup; //可报名人员列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.MERGE) @JoinTable(name = "act_sign", joinColumns = { @JoinColumn(name = "act_id") @@ -80,7 +80,7 @@ public class Report { private Set signGroup; //已报名人员列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.MERGE) @JoinTable(name = "act_signed", joinColumns = { @JoinColumn(name = "act_id") @@ -92,7 +92,7 @@ public class Report { private Set signedGroup; //可见人员列表 - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany(cascade = CascadeType.MERGE) @JoinTable(name = "act_vis", joinColumns = { @JoinColumn(name = "act_id") @@ -170,4 +170,5 @@ public class Report { @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "activity") private Activity activity; + } diff --git a/src/main/java/com/codesdream/ase/repository/ActivityRepository.java b/src/main/java/com/codesdream/ase/repository/activity/ActivityRepository.java similarity index 89% rename from src/main/java/com/codesdream/ase/repository/ActivityRepository.java rename to src/main/java/com/codesdream/ase/repository/activity/ActivityRepository.java index d1dfe34..77c49bd 100644 --- a/src/main/java/com/codesdream/ase/repository/ActivityRepository.java +++ b/src/main/java/com/codesdream/ase/repository/activity/ActivityRepository.java @@ -1,4 +1,4 @@ -package com.codesdream.ase.repository; +package com.codesdream.ase.repository.activity; import com.codesdream.ase.model.activity.Activity; import org.springframework.data.repository.CrudRepository; diff --git a/src/main/java/com/codesdream/ase/repository/ReportRepository.java b/src/main/java/com/codesdream/ase/repository/activity/ReportRepository.java similarity index 89% rename from src/main/java/com/codesdream/ase/repository/ReportRepository.java rename to src/main/java/com/codesdream/ase/repository/activity/ReportRepository.java index 8df84fc..fd2e581 100644 --- a/src/main/java/com/codesdream/ase/repository/ReportRepository.java +++ b/src/main/java/com/codesdream/ase/repository/activity/ReportRepository.java @@ -1,4 +1,4 @@ -package com.codesdream.ase.repository; +package com.codesdream.ase.repository.activity; import com.codesdream.ase.model.activity.Report; import org.springframework.data.repository.CrudRepository; diff --git a/src/main/java/com/codesdream/ase/service/ActivityService.java b/src/main/java/com/codesdream/ase/service/ActivityService.java index 00d71de..3166f03 100644 --- a/src/main/java/com/codesdream/ase/service/ActivityService.java +++ b/src/main/java/com/codesdream/ase/service/ActivityService.java @@ -2,15 +2,17 @@ package com.codesdream.ase.service; import com.codesdream.ase.model.activity.Activity; import com.codesdream.ase.model.activity.Report; -import com.codesdream.ase.repository.ActivityRepository; +import com.codesdream.ase.repository.activity.ActivityRepository; import org.springframework.stereotype.Service; +import javax.annotation.Resource; import java.util.Optional; @Service public class ActivityService implements IActivityService { - ActivityRepository activityRepository; + @Resource + private ActivityRepository activityRepository; @Override public Optional findActivityByTitle(String title) { @@ -24,15 +26,13 @@ public class ActivityService implements IActivityService { @Override public Activity save(Activity activity) { - activityRepository.save(activity); - return activity; + return activityRepository.save(activity); } @Override public Activity addReport(Activity activity, Report report) { activity.setReport(report); - update(activity); - return activity; + return update(activity); } @Override @@ -42,8 +42,7 @@ public class ActivityService implements IActivityService { @Override public Activity update(Activity activity) { - activityRepository.save(activity); - return activity; + return activityRepository.save(activity); } } diff --git a/src/main/java/com/codesdream/ase/service/ReportService.java b/src/main/java/com/codesdream/ase/service/ReportService.java index 4a86fbc..4077aff 100644 --- a/src/main/java/com/codesdream/ase/service/ReportService.java +++ b/src/main/java/com/codesdream/ase/service/ReportService.java @@ -2,12 +2,14 @@ package com.codesdream.ase.service; import com.codesdream.ase.model.activity.Activity; import com.codesdream.ase.model.activity.Report; -import com.codesdream.ase.repository.ReportRepository; +import com.codesdream.ase.repository.activity.ReportRepository; +import javax.annotation.Resource; import java.util.Optional; public class ReportService implements IReportService { + @Resource ReportRepository reportRepository; ActivityService activityService; @@ -27,8 +29,7 @@ public class ReportService implements IReportService { throw new RuntimeException("Activity does not exist."); } activityService.addReport(activity, report); - reportRepository.save(report); - return report; + return reportRepository.save(report); } @Override diff --git a/src/main/java/com/codesdream/ase/test/ActivityServiceTest.java b/src/main/java/com/codesdream/ase/test/ActivityServiceTest.java new file mode 100644 index 0000000..68cbc66 --- /dev/null +++ b/src/main/java/com/codesdream/ase/test/ActivityServiceTest.java @@ -0,0 +1,56 @@ +package com.codesdream.ase.test; + +import com.codesdream.ase.model.activity.Activity; +import com.codesdream.ase.model.activity.Report; +import com.codesdream.ase.model.permission.User; +import com.codesdream.ase.service.ActivityService; +import com.codesdream.ase.service.UserService; +import javafx.util.Pair; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +@SpringBootTest +@RunWith(SpringRunner.class) +public class ActivityServiceTest { + @Resource + ActivityService activityService; + + @Resource + UserService userService; + + //Activity model的增删改查 + @Test + public void baseFuncTest(){ + User creator = new User(); + creator.setUsername("Tom"); + creator.setPassword("123456"); + creator.getUserAuth().setStudentID("2018303026"); + creator.getUserAuth().setMail("937447984@qq.com"); + creator.getUserAuth().setUserQuestion("Your favourite animal?"); + creator.getUserAuth().setUserAnswer("Cat"); + creator.getUserDetail().setAtSchool(true); + creator.getUserDetail().setRealName("张三"); + Pair checker = userService.checkIfUserExists("Tom"); + if(checker.getKey()){ + userService.delete(checker.getValue()); + } + creator = userService.save(creator); + Activity activity = new Activity(); + activity.setTitle("活动1"); + + activity.setCreator(creator); + activity.setType("lo"); + activity.setChiefManager(creator); + Report report = new Report(); + report.setTitle("活动1的报告"); + activity = activityService.save(activity); + activity = activityService.addReport(activity, report); + //activityService.delete(); + //Activity activity1 = new Activity("活动2"); + } + +}