编写基本信息接口及服务;

This commit is contained in:
Saturneric 2020-02-18 17:34:05 +08:00
parent d64a4aca62
commit eef59c2eb5
9 changed files with 301 additions and 11 deletions

View File

@ -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();
}
}

View File

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

View File

@ -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();
}
}

View File

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

View File

@ -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<BaseAdministrativeDivision> administrativeDivision =
administrativeDivisionRepository.findByName(name);
return administrativeDivision.isPresent();
}
@Override
public boolean checkCollege(String name) {
Optional<BaseCollege> college =
collegeRepository.findByName(name);
return college.isPresent();
}
@Override
public boolean checkEthnic(String name) {
Optional<BaseEthnic> ethnic =
ethnicRepository.findByName(name);
return ethnic.isPresent();
}
@Override
public boolean checkMajor(String name) {
Optional<BaseMajor> major =
majorRepository.findByName(name);
return major.isPresent();
}
@Override
public boolean checkPoliticalStatus(String name) {
Optional<BasePoliticalStatus> politicalStatus =
politicalStatusRepository.findByName(name);
return false;
}
@Override
public boolean checkCandidateStatus(String name) {
Optional<BaseCandidateCategory> candidateCategory =
candidateCategoryRepository.findByName(name);
return false;
}
@Override
public boolean checkStudentInfo(String studentId) {
Optional<BaseStudentInfo> studentInfo =
studentInfoRepository.findByStudentId(studentId);
return studentInfo.isPresent();
}
@Override
public BaseAdministrativeDivision findAdministrativeDivisionByName(String name) {
Optional<BaseAdministrativeDivision> administrativeDivision =
administrativeDivisionRepository.findByName(name);
// 检查
if(!administrativeDivision.isPresent())
throw new BaseInformationNotExistException(BaseAdministrativeDivision.class);
return administrativeDivision.get();
}
@Override
public BaseCollege findCollegeByName(String name) {
Optional<BaseCollege> college =
collegeRepository.findByName(name);
// 检查
if(!college.isPresent()) throw new BaseInformationNotExistException(BaseCollege.class);
return college.get();
}
@Override
public BaseEthnic findEthnicByName(String name) {
Optional<BaseEthnic> ethnic =
ethnicRepository.findByName(name);
if(!ethnic.isPresent()) throw new BaseInformationNotExistException(BaseEthnic.class);
return ethnic.get();
}
@Override
public BaseMajor findMajorByName(String name) {
Optional<BaseMajor> major =
majorRepository.findByName(name);
if(!major.isPresent()) throw new BaseInformationNotExistException(BaseMajor.class);
return major.get();
}
@Override
public BasePoliticalStatus findPoliticalStatusByName(String name) {
Optional<BasePoliticalStatus> politicalStatus =
politicalStatusRepository.findByName(name);
if(!politicalStatus.isPresent())
throw new BaseInformationNotExistException(BasePoliticalStatus.class);
return politicalStatus.get();
}
@Override
public BaseCandidateCategory findCandidateCategoryByName(String name) {
Optional<BaseCandidateCategory> candidateCategory =
candidateCategoryRepository.findByName(name);
if(!candidateCategory.isPresent())
throw new BaseInformationNotExistException(BaseCandidateCategory.class);
return candidateCategory.get();
}
@Override
public BaseStudentInfo findStudentInfoByStudentId(String studentId) {
Optional<BaseStudentInfo> 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;
}
}

View File

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

View File

@ -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)保存密码

View File

@ -12,7 +12,9 @@ import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* 测试DataModel相关查找器
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class DataManagerTest {

View File