From eef59c2eb5198f82985417c7d74bf2874419ae31 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Tue, 18 Feb 2020 17:34:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E5=86=99=E5=9F=BA=E6=9C=AC=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=8E=A5=E5=8F=A3=E5=8F=8A=E6=9C=8D=E5=8A=A1;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BaseInformationAlreadyExistException.java | 15 ++ .../BaseInformationIllegalException.java | 17 ++ .../BaseInformationNotExistException.java | 15 ++ .../model/information/BaseStudentInfo.java | 18 +- .../ase/service/BaseInformationService.java | 200 ++++++++++++++++++ .../ase/service/IBaseInformationService.java | 41 ++++ .../codesdream/ase/service/UserService.java | 2 +- .../codesdream/ase/test/DataManagerTest.java | 4 +- src/main/resources/data_base_college.sql | 0 9 files changed, 301 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/codesdream/ase/exception/BaseInformationAlreadyExistException.java create mode 100644 src/main/java/com/codesdream/ase/exception/BaseInformationIllegalException.java create mode 100644 src/main/java/com/codesdream/ase/exception/BaseInformationNotExistException.java create mode 100644 src/main/java/com/codesdream/ase/service/BaseInformationService.java create mode 100644 src/main/resources/data_base_college.sql diff --git a/src/main/java/com/codesdream/ase/exception/BaseInformationAlreadyExistException.java b/src/main/java/com/codesdream/ase/exception/BaseInformationAlreadyExistException.java new file mode 100644 index 0000000..a75a9f8 --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/BaseInformationAlreadyExistException.java @@ -0,0 +1,15 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class BaseInformationAlreadyExistException extends RuntimeException { + private String className; + + public BaseInformationAlreadyExistException(Class aClass){ + super(); + this.className = aClass.getName(); + } +} diff --git a/src/main/java/com/codesdream/ase/exception/BaseInformationIllegalException.java b/src/main/java/com/codesdream/ase/exception/BaseInformationIllegalException.java new file mode 100644 index 0000000..ab0fea7 --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/BaseInformationIllegalException.java @@ -0,0 +1,17 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class BaseInformationIllegalException extends RuntimeException { + String type; + String value; + + public BaseInformationIllegalException(String type, String value){ + super(); + this.type = type; + this.value = value; + } +} diff --git a/src/main/java/com/codesdream/ase/exception/BaseInformationNotExistException.java b/src/main/java/com/codesdream/ase/exception/BaseInformationNotExistException.java new file mode 100644 index 0000000..3102dc6 --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/BaseInformationNotExistException.java @@ -0,0 +1,15 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class BaseInformationNotExistException extends RuntimeException { + private String className; + + public BaseInformationNotExistException(Class baseInformationClass){ + super(); + this.className = baseInformationClass.getName(); + } +} diff --git a/src/main/java/com/codesdream/ase/model/information/BaseStudentInfo.java b/src/main/java/com/codesdream/ase/model/information/BaseStudentInfo.java index f5e2c8f..16ce742 100644 --- a/src/main/java/com/codesdream/ase/model/information/BaseStudentInfo.java +++ b/src/main/java/com/codesdream/ase/model/information/BaseStudentInfo.java @@ -16,27 +16,27 @@ public class BaseStudentInfo { @GeneratedValue(strategy = GenerationType.AUTO) private int id; // 真实姓名 - private String name; + private String name = ""; // 学号 - private String studentId; + private String studentId = ""; // 班号 - private String classId; + private String classId = ""; // 性别 - private String sex; + private String sex = ""; // 民族 @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) - private BaseEthnic ethnic; + private BaseEthnic ethnic = null; // 学院 @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) - private BaseCollege college; + private BaseCollege college = null; // 专业 @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) - private BaseMajor major; + private BaseMajor major = null; // 政治面貌 @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) - private BasePoliticalStatus politicalStatus; + private BasePoliticalStatus politicalStatus = null; // 省份地区 @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) - private BaseAdministrativeDivision administrativeDivision; + private BaseAdministrativeDivision administrativeDivision = null; } diff --git a/src/main/java/com/codesdream/ase/service/BaseInformationService.java b/src/main/java/com/codesdream/ase/service/BaseInformationService.java new file mode 100644 index 0000000..dbf7d29 --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/BaseInformationService.java @@ -0,0 +1,200 @@ +package com.codesdream.ase.service; + +import com.codesdream.ase.exception.BaseInformationAlreadyExistException; +import com.codesdream.ase.exception.BaseInformationIllegalException; +import com.codesdream.ase.exception.BaseInformationNotExistException; +import com.codesdream.ase.model.information.*; +import com.codesdream.ase.repository.information.*; + +import javax.annotation.Resource; +import java.util.Optional; + +public class BaseInformationService implements IBaseInformationService { + + @Resource + BaseAdministrativeDivisionRepository administrativeDivisionRepository; + + @Resource + BaseCandidateCategoryRepository candidateCategoryRepository; + + @Resource + BaseCollegeRepository collegeRepository; + + @Resource + BaseEthnicRepository ethnicRepository; + + @Resource + BaseMajorRepository majorRepository; + + @Resource + BasePoliticalStatusRepository politicalStatusRepository; + + @Resource + BaseStudentInfoRepository studentInfoRepository; + + @Override + public boolean checkAdministrativeDivision(String name) { + Optional administrativeDivision = + administrativeDivisionRepository.findByName(name); + return administrativeDivision.isPresent(); + } + + @Override + public boolean checkCollege(String name) { + Optional college = + collegeRepository.findByName(name); + return college.isPresent(); + } + + @Override + public boolean checkEthnic(String name) { + Optional ethnic = + ethnicRepository.findByName(name); + return ethnic.isPresent(); + } + + @Override + public boolean checkMajor(String name) { + Optional major = + majorRepository.findByName(name); + return major.isPresent(); + } + + @Override + public boolean checkPoliticalStatus(String name) { + Optional politicalStatus = + politicalStatusRepository.findByName(name); + return false; + } + + @Override + public boolean checkCandidateStatus(String name) { + Optional candidateCategory = + candidateCategoryRepository.findByName(name); + + return false; + } + + @Override + public boolean checkStudentInfo(String studentId) { + Optional studentInfo = + studentInfoRepository.findByStudentId(studentId); + return studentInfo.isPresent(); + } + + @Override + public BaseAdministrativeDivision findAdministrativeDivisionByName(String name) { + Optional administrativeDivision = + administrativeDivisionRepository.findByName(name); + // 检查 + if(!administrativeDivision.isPresent()) + throw new BaseInformationNotExistException(BaseAdministrativeDivision.class); + return administrativeDivision.get(); + } + + @Override + public BaseCollege findCollegeByName(String name) { + Optional college = + collegeRepository.findByName(name); + // 检查 + if(!college.isPresent()) throw new BaseInformationNotExistException(BaseCollege.class); + return college.get(); + } + + @Override + public BaseEthnic findEthnicByName(String name) { + Optional ethnic = + ethnicRepository.findByName(name); + if(!ethnic.isPresent()) throw new BaseInformationNotExistException(BaseEthnic.class); + return ethnic.get(); + } + + @Override + public BaseMajor findMajorByName(String name) { + Optional major = + majorRepository.findByName(name); + if(!major.isPresent()) throw new BaseInformationNotExistException(BaseMajor.class); + return major.get(); + } + + @Override + public BasePoliticalStatus findPoliticalStatusByName(String name) { + Optional politicalStatus = + politicalStatusRepository.findByName(name); + if(!politicalStatus.isPresent()) + throw new BaseInformationNotExistException(BasePoliticalStatus.class); + return politicalStatus.get(); + } + + @Override + public BaseCandidateCategory findCandidateCategoryByName(String name) { + Optional candidateCategory = + candidateCategoryRepository.findByName(name); + if(!candidateCategory.isPresent()) + throw new BaseInformationNotExistException(BaseCandidateCategory.class); + return candidateCategory.get(); + } + + @Override + public BaseStudentInfo findStudentInfoByStudentId(String studentId) { + Optional studentInfo = + studentInfoRepository.findByStudentId(studentId); + if(!studentInfo.isPresent()) + throw new BaseInformationNotExistException(BaseStudentInfo.class); + return studentInfo.get(); + } + + @Override + public BaseStudentInfo constructStudentInfo(String studentId, + String classId, + String realName, String sex, + String college, + String major, String ethnic, + String candidateCategory, String politicalStatus, + String administrativeDivision) + { + // 检查 + if(!sex.equals("男") && !sex.equals("女")) + throw new BaseInformationIllegalException("sex", sex); + if(classId.length() != 8) + throw new BaseInformationIllegalException("classId", classId); + if(studentId.length() != 10) + throw new BaseInformationIllegalException("studentId",studentId); + if(realName.length() > 64) + throw new BaseInformationIllegalException("realName",realName); + + BaseStudentInfo studentInfo = new BaseStudentInfo(); + studentInfo.setSex(sex); + studentInfo.setClassId(classId); + studentInfo.setName(realName); + studentInfo.setStudentId(studentId); + studentInfo.setAdministrativeDivision(findAdministrativeDivisionByName(administrativeDivision)); + studentInfo.setCollege(findCollegeByName(college)); + studentInfo.setEthnic(findEthnicByName(ethnic)); + studentInfo.setMajor(findMajorByName(major)); + studentInfo.setPoliticalStatus(findPoliticalStatusByName(politicalStatus)); + + return studentInfo; + } + + @Override + public BaseStudentInfo save(BaseStudentInfo baseStudentInfo) { + if(baseStudentInfo.getAdministrativeDivision() == null + || baseStudentInfo.getCollege() == null + || baseStudentInfo.getEthnic() == null + || baseStudentInfo.getMajor() == null + || baseStudentInfo.getPoliticalStatus() == null + || baseStudentInfo.getClassId().equals("") + || baseStudentInfo.getStudentId().equals("") + || baseStudentInfo.getName().equals("")) + throw new BaseInformationIllegalException("studentInfo", "didn't fully initialize"); + + // 检查学号重复 + if(checkStudentInfo(baseStudentInfo.getStudentId())) + throw new BaseInformationAlreadyExistException(baseStudentInfo.getClass()); + + studentInfoRepository.save(baseStudentInfo); + + return null; + } +} diff --git a/src/main/java/com/codesdream/ase/service/IBaseInformationService.java b/src/main/java/com/codesdream/ase/service/IBaseInformationService.java index 8c55369..5ec6b94 100644 --- a/src/main/java/com/codesdream/ase/service/IBaseInformationService.java +++ b/src/main/java/com/codesdream/ase/service/IBaseInformationService.java @@ -1,5 +1,46 @@ package com.codesdream.ase.service; +import com.codesdream.ase.model.information.*; + public interface IBaseInformationService { + // 检查行政区域是否合法 boolean checkAdministrativeDivision(String name); + + // 检查学院名称是否合法 + boolean checkCollege(String name); + + // 检查民族名称是否合法 + boolean checkEthnic(String name); + + // 检查专业信息是否合法 + boolean checkMajor(String name); + + // 检查政治面貌信息是否合法 + boolean checkPoliticalStatus(String name); + + // 检查考生类型是否合法 + boolean checkCandidateStatus(String name); + + // 检查学生信息是否存在 + boolean checkStudentInfo(String studentId); + + BaseAdministrativeDivision findAdministrativeDivisionByName(String name); + + BaseCollege findCollegeByName(String name); + + BaseEthnic findEthnicByName(String name); + + BaseMajor findMajorByName(String name); + + BasePoliticalStatus findPoliticalStatusByName(String name); + + BaseCandidateCategory findCandidateCategoryByName(String name); + + BaseStudentInfo findStudentInfoByStudentId(String studentId); + + BaseStudentInfo constructStudentInfo(String studentId, + String classId, String realName, String sex, String college, String major, String ethnic, String candidateCategory, String politicalStatus, + String administrativeDivision); + + BaseStudentInfo save(BaseStudentInfo baseStudentInfo); } diff --git a/src/main/java/com/codesdream/ase/service/UserService.java b/src/main/java/com/codesdream/ase/service/UserService.java index 10372be..3d09ea8 100644 --- a/src/main/java/com/codesdream/ase/service/UserService.java +++ b/src/main/java/com/codesdream/ase/service/UserService.java @@ -83,7 +83,7 @@ public class UserService implements IUserService { || user.getUserAuth().getUserQuestion().length() > 255 || user.getUserAuth().getStudentID().length() > 24 || user.getUserAuth().getMail().length() > 64 - || user.getUserDetail().getRealName().length() > 12) + || user.getUserDetail().getRealName().length() > 64) throw new UserInformationIllegalException(user.getUsername()); // 强制以哈希值(sha256)保存密码 diff --git a/src/main/java/com/codesdream/ase/test/DataManagerTest.java b/src/main/java/com/codesdream/ase/test/DataManagerTest.java index 9404bb5..09c9353 100644 --- a/src/main/java/com/codesdream/ase/test/DataManagerTest.java +++ b/src/main/java/com/codesdream/ase/test/DataManagerTest.java @@ -12,7 +12,9 @@ import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; - +/** + * 测试DataModel相关查找器 + */ @RunWith(SpringRunner.class) @SpringBootTest public class DataManagerTest { diff --git a/src/main/resources/data_base_college.sql b/src/main/resources/data_base_college.sql new file mode 100644 index 0000000..e69de29