diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataExcelGenerator.java b/src/main/java/com/codesdream/ase/component/datamanager/DataExcelGenerator.java new file mode 100644 index 0000000..1ca17b2 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataExcelGenerator.java @@ -0,0 +1,84 @@ +package com.codesdream.ase.component.datamanager; + +import com.codesdream.ase.exception.DataFileNotFoundException; +import com.codesdream.ase.exception.DataIOException; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; + +/** + * 利用数据生成Excel文件 + */ +public class DataExcelGenerator implements DataGenerator { + Workbook workbook = new XSSFWorkbook(); + + Sheet sheet; + Integer colNumber; + String path; + + public DataExcelGenerator(String path) { + sheet = workbook.createSheet("Data"); + this.path = path; + } + + public void setTableTitle(Collection titles){ + Row sheetRow = sheet.createRow(0); + int idx = 0; + for(String title : titles){ + sheetRow.createCell(idx).setCellValue(title); + idx++; + } + colNumber = titles.size(); + } + + public void insertRow(int rowIndex, Collection dataCollection){ + Row row = sheet.createRow(rowIndex); + int idx = 0; + for(String data : dataCollection){ + // 限制表头与表体的数据数目 + if(idx >= colNumber) break; + row.createCell(idx).setCellValue(data); + idx++; + } + } + + public void insertRow(Collection dataCollection){ + insertRow(sheet.getLastRowNum() + 1, dataCollection); + + } + + public void insertRowDataALL(Collection dataCollections){ + int cellIdx = 0; + Collection dataCollection = new ArrayList<>(); + for(String dataCollectionItem :dataCollections){ + dataCollection.add(dataCollectionItem); + cellIdx++; + if(cellIdx % colNumber == 0) { + insertRow(dataCollection); + dataCollection.clear(); + cellIdx = 0; + } + + } + } + + + public void save() { + try { + FileOutputStream stream = new FileOutputStream(path, false); + workbook.write(stream); + stream.close(); + } catch (FileNotFoundException e) { + throw new DataFileNotFoundException(path); + } catch (IOException e) { + throw new DataIOException(); + } + } +} diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataExcelReader.java b/src/main/java/com/codesdream/ase/component/datamanager/DataExcelReader.java new file mode 100644 index 0000000..ae2e2e6 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataExcelReader.java @@ -0,0 +1,119 @@ +package com.codesdream.ase.component.datamanager; + + +import com.codesdream.ase.exception.*; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.ss.usermodel.*; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; + +/** + * 读取简单表格式的Excel文件 + */ +public class DataExcelReader implements DataReader { + Workbook workbook = null; + Sheet sheet = null; + short colNumber = 0; + int rowNumberNow = 0; + String path; + boolean hasReadFile = false; + + public DataExcelReader(String path){ + this.path = path; + readFile(); + } + + public void readFile(){ + if(hasReadFile) return; + try{ + FileInputStream fileInputStream = new FileInputStream(path); + workbook = WorkbookFactory.create(fileInputStream); + sheet = workbook.getSheetAt(0); + fileInputStream.close(); + } catch (FileNotFoundException e) { + throw new DataFileNotFoundException(path); + } catch (InvalidFormatException e) { + throw new DataInvalidFormatException(e); + } catch (IOException e) { + throw new DataIOException(); + } + } + + public Collection readColsTitle(){ + Row titleRow = sheet.getRow(0); + colNumber = titleRow.getLastCellNum(); + // 表头项目个数不可为0 + if(colNumber == 0) throw new DataIllegalTableFormatException(); + Collection title = new ArrayList<>(); + for(int cellIdx = 0; cellIdx < colNumber; cellIdx++){ + title.add(readCell(titleRow.getCell(cellIdx))); + } + return title; + } + + public Collection readRow(int idx){ + // 检查是否获取表头数据 + if(this.colNumber == 0) readColsTitle(); + if(idx > getRowsSize()) throw new DataReaderRowIndexOutOfRangeException(); + this.rowNumberNow = idx; + Collection data = new ArrayList<>(); + Row dataRow = sheet.getRow(rowNumberNow); + // 检查列数是否合适 + if(dataRow.getLastCellNum() > colNumber) throw new DataIllegalTableFormatException(); + for(int cellIdx = 0; cellIdx < colNumber; cellIdx++){ + data.add(readCell(dataRow.getCell(cellIdx))); + } + return data; + } + + @Override + public int getRowsSize() { + return lastDataRowIndex() - firstDataRowIndex(); + } + + @Override + public int firstDataRowIndex() { + return 1; + } + + @Override + public int lastDataRowIndex() { + return sheet.getLastRowNum(); + } + + public Collection readRow(){ + if(rowNumberNow >= getRowsSize()) return null; + return readRow(rowNumberNow + 1); + } + + private String readCell(Cell cell){ + String cellValue = ""; + switch (cell.getCellType()) { + case Cell.CELL_TYPE_STRING: + cellValue = cell.getRichStringCellValue().getString(); + break; + case Cell.CELL_TYPE_NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + cellValue = cell.getDateCellValue().toString(); + } else { + cellValue = String.valueOf(cell.getNumericCellValue()); + } + break; + case Cell.CELL_TYPE_BOOLEAN: + cellValue = String.valueOf(cell.getBooleanCellValue()); + break; + case Cell.CELL_TYPE_FORMULA: + cellValue = String.valueOf(cell.getCellFormula()); + break; + case Cell.CELL_TYPE_BLANK: + break; + default: + } + return cellValue; + } + +} diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataGenerator.java b/src/main/java/com/codesdream/ase/component/datamanager/DataGenerator.java new file mode 100644 index 0000000..4a9f061 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataGenerator.java @@ -0,0 +1,17 @@ +package com.codesdream.ase.component.datamanager; + +import java.util.Collection; + +/** + * 表给狗数据文件生成器接口 + */ +public interface DataGenerator { + // 读取表头信息 + void setTableTitle(Collection titles); + + // 向表中写入一行数据 + void insertRow(int rowIndex, Collection dataCollection); + + // 将修改保存表到文件中 + void save(); +} diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java b/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java index c3b9c63..3844039 100644 --- a/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataModelRepositorySearcher.java @@ -8,7 +8,7 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; /** - * DataModel对应Repository层查询器 + * Model对应的Repository层查询器 */ @Data @Component @@ -43,7 +43,6 @@ public class DataModelRepositorySearcher { return null; } - public static String doCheckName(String string) { char[] charArray = string.toCharArray(); if(Character.isLowerCase(charArray[0])) charArray[0] -= 32; diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java b/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java index d4c7925..0493d1c 100644 --- a/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataModelSearcher.java @@ -10,7 +10,7 @@ import java.util.ArrayList; import java.util.Collection; /** - * 根据子系统及名称查找特定的Data Model + * 根据子系统及名称查找特定的MModel */ @Data @Component diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataReader.java b/src/main/java/com/codesdream/ase/component/datamanager/DataReader.java new file mode 100644 index 0000000..4b9dd21 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataReader.java @@ -0,0 +1,27 @@ +package com.codesdream.ase.component.datamanager; + +import java.util.Collection; + +/** + * 表结构信息读取器接口 + */ +public interface DataReader { + // 从文件中读取数据(在使用上要求这个调用可有可无) + void readFile(); + + // 获得表头列的数目 + Collection readColsTitle(); + + // 读取特定序号的行的数据 + Collection readRow(int row); + + // 得到数据的总行数 + int getRowsSize(); + + // 得到第一数据行的序号 + int firstDataRowIndex(); + + // 得到最后一行数据行的序号 + int lastDataRowIndex(); + +} diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataTable.java b/src/main/java/com/codesdream/ase/component/datamanager/DataTable.java new file mode 100644 index 0000000..8e6eb1b --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataTable.java @@ -0,0 +1,61 @@ +package com.codesdream.ase.component.datamanager; + +import com.codesdream.ase.exception.DataIllegalTableFormatException; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.util.*; + +// 描述一张数据表 +@Component +@Scope("prototype") +public class DataTable { + Collection titleCollection = new ArrayList<>(); + Vector dataRows = new Vector<>(); + + // 为表添加列及列名 + public DataTable addColTitle(String title){ + titleCollection.add(title); + return this; + } + + // 获得特定行的数据 + public Collection getRow(int index){ + return dataRows.elementAt(index).getRow(); + } + + // 从DataReader导入特定表 + public void ImportTable(DataReader reader){ + // 从文件中读取数据 + reader.readFile(); + // 读取列信息 + titleCollection = reader.readColsTitle(); + + int rowsSize = reader.getRowsSize(); + int index = 0; + for(int i = reader.firstDataRowIndex(); i < reader.lastDataRowIndex(); i++){ + dataRows.add(new DataTableRow(index++, reader.readRow(i))); + // 检查是否列数一致 + if(dataRows.lastElement().getColsSize() != this.getColsSize()) { + // 清空表数据 + this.dataRows.clear(); + throw new DataIllegalTableFormatException(); + } + } + } + + // 为表添加行 + public void addRow(Collection row){ + dataRows.add(new DataTableRow(dataRows.size() + 1, row)); + } + + // 获得表的列数 + public int getColsSize(){ + return titleCollection.size(); + } + + // 获得表的行数 + public int getRowsSize(){ + return dataRows.size(); + } +} diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataTableRow.java b/src/main/java/com/codesdream/ase/component/datamanager/DataTableRow.java new file mode 100644 index 0000000..9e68b1a --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataTableRow.java @@ -0,0 +1,21 @@ +package com.codesdream.ase.component.datamanager; + +import lombok.Data; + +import java.util.Collection; + +// 描述数据表的行 +@Data +public class DataTableRow { + int index; + Collection row; + + public DataTableRow(int index, Collection row){ + this.index = index; + this.row = row; + } + + public int getColsSize(){ + return row.size(); + } +} 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/exception/DataFileNotFoundException.java b/src/main/java/com/codesdream/ase/exception/DataFileNotFoundException.java new file mode 100644 index 0000000..c85958a --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/DataFileNotFoundException.java @@ -0,0 +1,15 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class DataFileNotFoundException extends RuntimeException { + private String path; + + public DataFileNotFoundException(String filePath){ + super(); + this.path = filePath; + } +} diff --git a/src/main/java/com/codesdream/ase/exception/DataIOException.java b/src/main/java/com/codesdream/ase/exception/DataIOException.java new file mode 100644 index 0000000..f220cac --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/DataIOException.java @@ -0,0 +1,9 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class DataIOException extends RuntimeException { +} diff --git a/src/main/java/com/codesdream/ase/exception/DataIllegalTableFormatException.java b/src/main/java/com/codesdream/ase/exception/DataIllegalTableFormatException.java new file mode 100644 index 0000000..5c01b8c --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/DataIllegalTableFormatException.java @@ -0,0 +1,9 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class DataIllegalTableFormatException extends RuntimeException { +} diff --git a/src/main/java/com/codesdream/ase/exception/DataInvalidFormatException.java b/src/main/java/com/codesdream/ase/exception/DataInvalidFormatException.java new file mode 100644 index 0000000..e06394f --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/DataInvalidFormatException.java @@ -0,0 +1,15 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class DataInvalidFormatException extends RuntimeException { + String information; + + public DataInvalidFormatException(Exception e){ + super(); + information = e.getMessage(); + } +} diff --git a/src/main/java/com/codesdream/ase/exception/DataReaderRowIndexOutOfRangeException.java b/src/main/java/com/codesdream/ase/exception/DataReaderRowIndexOutOfRangeException.java new file mode 100644 index 0000000..fb004a4 --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/DataReaderRowIndexOutOfRangeException.java @@ -0,0 +1,9 @@ +package com.codesdream.ase.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class DataReaderRowIndexOutOfRangeException extends RuntimeException { +} 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/IUserService.java b/src/main/java/com/codesdream/ase/service/IUserService.java index 277d413..1759447 100644 --- a/src/main/java/com/codesdream/ase/service/IUserService.java +++ b/src/main/java/com/codesdream/ase/service/IUserService.java @@ -31,6 +31,9 @@ public interface IUserService { // 根据学号生成随机用户名 void generateRandomUsernameByStudentID(User user, String id); + // 随机生成一个用户名 + void generateRandomUsername(User user); + // 注册用户 User save(User user); diff --git a/src/main/java/com/codesdream/ase/service/UserService.java b/src/main/java/com/codesdream/ase/service/UserService.java index 10372be..390b383 100644 --- a/src/main/java/com/codesdream/ase/service/UserService.java +++ b/src/main/java/com/codesdream/ase/service/UserService.java @@ -17,6 +17,7 @@ import javax.annotation.Resource; import java.util.Collection; import java.util.List; import java.util.Optional; +import java.util.UUID; @Service public class UserService implements IUserService { @@ -72,6 +73,11 @@ public class UserService implements IUserService { user.setUsername(usernameEncoder.encode(id)); } + @Override + public void generateRandomUsername(User user) { + user.setUsername(usernameEncoder.encode(UUID.randomUUID().toString())); + } + @Override public User save(User user) { // 查找用户名是否已经被注册 @@ -83,7 +89,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 deleted file mode 100644 index 9404bb5..0000000 --- a/src/main/java/com/codesdream/ase/test/DataManagerTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.codesdream.ase.test; - -import com.codesdream.ase.component.ASESpringUtil; -import com.codesdream.ase.component.datamanager.DataModelRepositorySearcher; -import com.codesdream.ase.component.datamanager.DataModelSearcher; -import com.codesdream.ase.repository.permission.UserRepository; -import org.junit.Assert; -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; - - -@RunWith(SpringRunner.class) -@SpringBootTest -public class DataManagerTest { - @Resource - ASESpringUtil springUtil; - - @Test - public void dataModelSearcherTest() { - DataModelSearcher dataModelSearcher = springUtil.getBean(DataModelSearcher.class); - dataModelSearcher.getDataModelClass("permission", "Tag"); - Assert.assertTrue(dataModelSearcher.isPresent()); - - for(String param : dataModelSearcher.getDataModelParamArgs()){ - System.out.println(param); - } - - } - - @Test - public void dataModelRepositorySearcherTest(){ - DataModelRepositorySearcher dataModelRepositorySearcher = - springUtil.getBean(DataModelRepositorySearcher.class); - - dataModelRepositorySearcher.getDataModelRepositoryClass("permission", "User"); - Assert.assertTrue(dataModelRepositorySearcher.isPresent()); - UserRepository userRepository = dataModelRepositorySearcher.getDataModelRepositoryInstance(); - - } - -} diff --git a/src/main/resources/base_political_status.sql b/src/main/resources/base_political_status.sql new file mode 100644 index 0000000..bba8879 --- /dev/null +++ b/src/main/resources/base_political_status.sql @@ -0,0 +1,14 @@ +INSERT INTO `base_political_status`(`id`, `name`) VALUES (0, '----'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (1, '中共党员'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (2, '中共预备党员'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (3, '共青团员'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (4, '民革会员'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (5, '民盟盟员'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (6, '民建会员'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (7, '民进会员'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (8, '农工党党员'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (9, '致公党党员'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (10, '九三学社社员'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (11, '台盟盟员'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (12, '无党派民主人士'); +INSERT INTO `base_political_status`(`id`, `name`) VALUES (13, '群众'); diff --git a/src/main/resources/data_base_college.sql b/src/main/resources/data_base_college.sql new file mode 100644 index 0000000..482ea2f --- /dev/null +++ b/src/main/resources/data_base_college.sql @@ -0,0 +1,25 @@ +-- ---------------------------- +-- Records of base_college +-- ---------------------------- +INSERT INTO `base_college` VALUES ('01', '航空学院', '1'); +INSERT INTO `base_college` VALUES ('02', '航天学院', '2'); +INSERT INTO `base_college` VALUES ('03', '航海学院', '3'); +INSERT INTO `base_college` VALUES ('04', '材料学院', '4'); +INSERT INTO `base_college` VALUES ('05', '机电学院', '5'); +INSERT INTO `base_college` VALUES ('06', '力学与土木建筑学院', '6'); +INSERT INTO `base_college` VALUES ('07', '动力与能源学院', '7'); +INSERT INTO `base_college` VALUES ('08', '电子信息学院', '8'); +INSERT INTO `base_college` VALUES ('09', '自动化学院', '9'); +INSERT INTO `base_college` VALUES ('10', '计算机学院', '10'); +INSERT INTO `base_college` VALUES ('11', '理学院', '11'); +INSERT INTO `base_college` VALUES ('12', '管理学院', '12'); +INSERT INTO `base_college` VALUES ('13', '人文与经法学院', '13'); +INSERT INTO `base_college` VALUES ('14', '软件学院学院', '14'); +INSERT INTO `base_college` VALUES ('15', '生命学院', '15'); +INSERT INTO `base_college` VALUES ('16', '外国语学院', '16'); +INSERT INTO `base_college` VALUES ('17', '教育实验学院', '17'); +INSERT INTO `base_college` VALUES ('18', '西北工业大学伦敦玛丽女王大学工程学院', '18'); +INSERT INTO `base_college` VALUES ('19', '马克思主义学院', '19'); +INSERT INTO `base_college` VALUES ('20', '微电子学院', '20'); +INSERT INTO `base_college` VALUES ('21', '网络空间安全学院', '21'); +INSERT INTO `base_college` VALUES ('22', '民航学院', '22'); diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..d80081d --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/src/main/java/com/codesdream/ase/test/ActivityServiceTest.java b/src/test/java/com/codesdream/ase/ActivityServiceTest.java similarity index 100% rename from src/main/java/com/codesdream/ase/test/ActivityServiceTest.java rename to src/test/java/com/codesdream/ase/ActivityServiceTest.java diff --git a/src/test/java/com/codesdream/ase/DataManagerTest.java b/src/test/java/com/codesdream/ase/DataManagerTest.java new file mode 100644 index 0000000..ba87338 --- /dev/null +++ b/src/test/java/com/codesdream/ase/DataManagerTest.java @@ -0,0 +1,108 @@ +package com.codesdream.ase; + +import com.codesdream.ase.component.ASESpringUtil; +import com.codesdream.ase.component.datamanager.DataExcelGenerator; +import com.codesdream.ase.component.datamanager.DataExcelReader; +import com.codesdream.ase.component.datamanager.DataModelRepositorySearcher; +import com.codesdream.ase.component.datamanager.DataModelSearcher; +import com.codesdream.ase.repository.permission.UserRepository; +import org.junit.Assert; +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; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +/** + * 测试DataModel相关查找器 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class DataManagerTest { + @Resource + ASESpringUtil springUtil; + + // Model 查找器测试 + @Test + public void dataModelSearcherTest() { + DataModelSearcher dataModelSearcher = springUtil.getBean(DataModelSearcher.class); + dataModelSearcher.getDataModelClass("permission", "Tag"); + Assert.assertTrue(dataModelSearcher.isPresent()); + + for(String param : dataModelSearcher.getDataModelParamArgs()){ + System.out.println(param); + } + + } + + // Repository 查找器测试 + @Test + public void dataModelRepositorySearcherTest(){ + DataModelRepositorySearcher dataModelRepositorySearcher = + springUtil.getBean(DataModelRepositorySearcher.class); + + dataModelRepositorySearcher.getDataModelRepositoryClass("permission", "User"); + Assert.assertTrue(dataModelRepositorySearcher.isPresent()); + UserRepository userRepository = dataModelRepositorySearcher.getDataModelRepositoryInstance(); + + } + + // 测试Excel导出功能 + @Test + public void dataExcelGeneratorTest(){ + DataExcelGenerator dataExcelGenerator = new DataExcelGenerator("DataExcelGeneratorTest.xlsx"); + + // 设置表头信息 + Collection titles = new ArrayList<>(); + titles.add("Name"); + titles.add("Sex"); + titles.add("Age"); + dataExcelGenerator.setTableTitle(titles); + + Collection dataCollection = new ArrayList<>(); + dataCollection.add("Tom"); + dataCollection.add("M"); + dataCollection.add("18"); + + dataExcelGenerator.insertRow(1, dataCollection); + dataExcelGenerator.insertRow(dataCollection); + + dataCollection.add("Tom"); + dataCollection.add("M"); + dataCollection.add("18"); + dataExcelGenerator.insertRowDataALL(dataCollection); + + // 保存数据 + dataExcelGenerator.save(); + + } + + // 测试Excel导入功能 + @Test + public void dataExcelReaderTest(){ + DataExcelReader dataExcelReader = new DataExcelReader("DataExcelGeneratorTest.xlsx"); + + // 从文件中读取数据 + dataExcelReader.readFile(); + // 读取表头信息 + Collection title = dataExcelReader.readColsTitle(); + // 读一行取数据 + Collection data = dataExcelReader.readRow(); + + Iterator iterator = title.iterator(); + Assert.assertEquals(iterator.next(), "Name"); + Assert.assertEquals(iterator.next(), "Sex"); + Assert.assertEquals(iterator.next(), "Age"); + + iterator = data.iterator(); + Assert.assertEquals(iterator.next(), "Tom"); + Assert.assertEquals(iterator.next(), "M"); + Assert.assertEquals(iterator.next(), "18"); + } + + +} diff --git a/src/main/java/com/codesdream/ase/test/UserTest.java b/src/test/java/com/codesdream/ase/UserTest.java similarity index 98% rename from src/main/java/com/codesdream/ase/test/UserTest.java rename to src/test/java/com/codesdream/ase/UserTest.java index 1b0c25e..177a0db 100644 --- a/src/main/java/com/codesdream/ase/test/UserTest.java +++ b/src/test/java/com/codesdream/ase/UserTest.java @@ -1,4 +1,4 @@ -package com.codesdream.ase.test; +package com.codesdream.ase; import com.codesdream.ase.model.permission.User;