编写并测试学生基本信息导入服务模块

This commit is contained in:
Saturneric 2020-02-19 17:46:51 +08:00
parent 0a975dd82a
commit 3dadf45919
16 changed files with 198 additions and 26 deletions

View File

@ -4,6 +4,8 @@ import com.codesdream.ase.exception.DataIllegalTableFormatException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.persistence.Table;
import javax.swing.text.html.Option;
import java.util.*;
// 描述一张数据表
@ -21,11 +23,18 @@ public class DataTable {
// 获得特定行的数据
public Collection<String> getRow(int index){
if(index >= getRowsSize()) throw new IndexOutOfBoundsException();
return dataRows.elementAt(index).getRow();
}
// 获得特定行的数据
public Vector<String> getRowVector(int index){
if(index >= getRowsSize()) throw new IndexOutOfBoundsException();
return new Vector<>(dataRows.elementAt(index).getRow());
}
// 从DataReader导入特定表
public void ImportTable(DataReader reader){
public void importTable(DataReader reader){
// 从文件中读取数据
reader.readFile();
// 读取列信息
@ -44,9 +53,30 @@ public class DataTable {
}
}
// 查找有无相关表头项
public Optional<Integer> getTitleIndex(String title){
int index = 0;
for(String dataTitle :titleCollection){
if(dataTitle.equals(title))
return Optional.of(index);
index++;
}
return Optional.empty();
}
// 导出表数据
public void exportTable(DataGenerator dataGenerator){
dataGenerator.setTableTitle(titleCollection);
for(DataTableRow row : dataRows){
dataGenerator.insertRow(row.getIndex(), row.getRow());
}
dataGenerator.save();
}
// 为表添加行
public void addRow(Collection<String> row){
dataRows.add(new DataTableRow(dataRows.size() + 1, row));
Collection<String> dataRow = new ArrayList<>(row);
dataRows.add(new DataTableRow(dataRows.size() + 1, dataRow));
}
// 获得表的列数

View File

@ -7,8 +7,8 @@ import java.util.Collection;
// 描述数据表的行
@Data
public class DataTableRow {
int index;
Collection<String> row;
private int index;
private Collection<String> row;
public DataTableRow(int index, Collection<String> row){
this.index = index;

View File

@ -12,10 +12,9 @@ import javax.persistence.*;
@Table(name = "base_administrative_division")
public class BaseAdministrativeDivision {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private int parent_id;
private int parentId;
}

View File

@ -12,7 +12,6 @@ import javax.persistence.*;
@Table(name = "base_candidate_category")
public class BaseCandidateCategory {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;

View File

@ -12,7 +12,6 @@ import javax.persistence.*;
@Table(name = "base_college")
public class BaseCollege {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;

View File

@ -12,7 +12,6 @@ import javax.persistence.*;
@Table(name = "base_ethnic")
public class BaseEthnic {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;

View File

@ -12,7 +12,6 @@ import javax.persistence.*;
@Table(name = "base_major")
public class BaseMajor {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;

View File

@ -12,7 +12,6 @@ import javax.persistence.*;
@Table(name = "base_political_status")
public class BasePoliticalStatus {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;

View File

@ -24,19 +24,19 @@ public class BaseStudentInfo {
// 性别
private String sex = "";
// 民族
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private BaseEthnic ethnic = null;
// 学院
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private BaseCollege college = null;
// 专业
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private BaseMajor major = null;
// 政治面貌
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private BasePoliticalStatus politicalStatus = null;
// 省份地区
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private BaseAdministrativeDivision administrativeDivision = null;
}

View File

@ -9,4 +9,5 @@ import java.util.Optional;
@Repository
public interface BaseAdministrativeDivisionRepository extends CrudRepository<BaseAdministrativeDivision, Integer> {
Optional<BaseAdministrativeDivision> findByName(String name);
Optional<BaseAdministrativeDivision> findByNameContainsAndParentId(String name, int parentId);
}

View File

@ -1,14 +1,20 @@
package com.codesdream.ase.service;
import com.codesdream.ase.component.datamanager.DataTable;
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 org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import java.util.Vector;
@Service
public class BaseInformationService implements IBaseInformationService {
@Resource
@ -82,13 +88,21 @@ public class BaseInformationService implements IBaseInformationService {
return studentInfo.isPresent();
}
// 查找省级行政区域
@Override
public BaseAdministrativeDivision findAdministrativeDivisionByName(String name) {
Optional<BaseAdministrativeDivision> administrativeDivision =
administrativeDivisionRepository.findByName(name);
administrativeDivisionRepository.findByNameContainsAndParentId(name, 0);
// 检查
if(!administrativeDivision.isPresent())
throw new BaseInformationNotExistException(BaseAdministrativeDivision.class);
if(!administrativeDivision.isPresent()) {
// 如果填入未知数据
administrativeDivision = administrativeDivisionRepository.findByName("未知");
if(administrativeDivision.isPresent()) {
return administrativeDivision.get();
}
else throw new BaseInformationNotExistException(BaseAdministrativeDivision.class);
}
return administrativeDivision.get();
}
@ -144,13 +158,51 @@ public class BaseInformationService implements IBaseInformationService {
return studentInfo.get();
}
@Override
public void studentInfoImportFromDataTable(DataTable table) {
Collection<Optional<Integer>> infoIndexOptional = new ArrayList<>();
infoIndexOptional.add(table.getTitleIndex("学号"));
infoIndexOptional.add(table.getTitleIndex("班号"));
infoIndexOptional.add(table.getTitleIndex("姓名"));
infoIndexOptional.add(table.getTitleIndex("性别"));
infoIndexOptional.add(table.getTitleIndex("学院名称"));
infoIndexOptional.add(table.getTitleIndex("专业名称"));
infoIndexOptional.add(table.getTitleIndex("民族名称"));
infoIndexOptional.add(table.getTitleIndex("政治面貌名称"));
infoIndexOptional.add(table.getTitleIndex("省份名称"));
Vector<Integer> infoIndex = new Vector<>();
for(Optional<Integer> infoIdx : infoIndexOptional){
if(!infoIdx.isPresent()) throw new RuntimeException("Unfit Data Table");
else infoIndex.add(infoIdx.get());
}
int dataRowsSize = table.getRowsSize();
for(int i = 0; i <dataRowsSize; i++){
Vector<String> row = table.getRowVector(i);
BaseStudentInfo studentInfo =
constructStudentInfo(row.elementAt(infoIndex.elementAt(0)),
row.elementAt(infoIndex.elementAt(1)),
row.elementAt(infoIndex.elementAt(2)),
row.elementAt(infoIndex.elementAt(3)),
row.elementAt(infoIndex.elementAt(4)),
row.elementAt(infoIndex.elementAt(5)),
row.elementAt(infoIndex.elementAt(6)),
row.elementAt(infoIndex.elementAt(7)),
row.elementAt(infoIndex.elementAt(8)));
save(studentInfo);
}
}
@Override
public BaseStudentInfo constructStudentInfo(String studentId,
String classId,
String realName, String sex,
String college,
String major, String ethnic,
String candidateCategory, String politicalStatus,
String politicalStatus,
String administrativeDivision)
{
// 检查

View File

@ -1,5 +1,6 @@
package com.codesdream.ase.service;
import com.codesdream.ase.component.datamanager.DataTable;
import com.codesdream.ase.model.information.*;
public interface IBaseInformationService {
@ -38,8 +39,11 @@ public interface IBaseInformationService {
BaseStudentInfo findStudentInfoByStudentId(String studentId);
// 从文件中导入学生基本信息
void studentInfoImportFromDataTable(DataTable table);
BaseStudentInfo constructStudentInfo(String studentId,
String classId, String realName, String sex, String college, String major, String ethnic, String candidateCategory, String politicalStatus,
String classId, String realName, String sex, String college, String major, String ethnic, String politicalStatus,
String administrativeDivision);
BaseStudentInfo save(BaseStudentInfo baseStudentInfo);

View File

@ -1,6 +1,7 @@
-- ----------------------------
-- Records of base_administrative_division
-- ----------------------------
INSERT INTO `base_administrative_division` VALUES ('000000', '未知', '0');
INSERT INTO `base_administrative_division` VALUES ('110000', '北京市', '0');
INSERT INTO `base_administrative_division` VALUES ('110100', '北京市市辖区', '110000');
INSERT INTO `base_administrative_division` VALUES ('110101', '北京市东城区', '110100');

View File

@ -14,7 +14,7 @@ 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 ('14', '软件学院', '14');
INSERT INTO `base_college` VALUES ('15', '生命学院', '15');
INSERT INTO `base_college` VALUES ('16', '外国语学院', '16');
INSERT INTO `base_college` VALUES ('17', '教育实验学院', '17');

View File

@ -0,0 +1,30 @@
package com.codesdream.ase;
import com.codesdream.ase.component.ASESpringUtil;
import com.codesdream.ase.component.datamanager.DataExcelReader;
import com.codesdream.ase.component.datamanager.DataTable;
import com.codesdream.ase.service.IBaseInformationService;
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 BaseInformationTest {
@Resource
private IBaseInformationService informationService;
@Resource
private ASESpringUtil springUtil;
@Test
public void studentInfoImportTest(){
DataTable table = springUtil.getBean(DataTable.class);
table.importTable(new DataExcelReader("SoftInformation.xlsx"));
informationService.studentInfoImportFromDataTable(table);
}
}

View File

@ -1,10 +1,7 @@
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.component.datamanager.*;
import com.codesdream.ase.repository.permission.UserRepository;
import org.junit.Assert;
import org.junit.Test;
@ -105,4 +102,67 @@ public class DataManagerTest {
}
// Data Table 基本测试
@Test
public void dataTableBaseTest(){
DataTable table = springUtil.getBean(DataTable.class);
table.addColTitle("Name").addColTitle("Sex").addColTitle("Age");
Collection<String> dataCollection = new ArrayList<>();
dataCollection.add("Tom");
dataCollection.add("M");
dataCollection.add("18");
table.addRow(dataCollection);
dataCollection.clear();
dataCollection.add("Pat");
dataCollection.add("F");
dataCollection.add("16");
table.addRow(dataCollection);
dataCollection = table.getRow(0);
Iterator<String> iterator = dataCollection.iterator();
Assert.assertEquals(iterator.next(), "Tom");
Assert.assertEquals(iterator.next(), "M");
Assert.assertEquals(iterator.next(), "18");
dataCollection = table.getRow(1);
iterator = dataCollection.iterator();
Assert.assertEquals(iterator.next(), "Pat");
Assert.assertEquals(iterator.next(), "F");
Assert.assertEquals(iterator.next(), "16");
}
@Test
public void dataTableImportTest(){
DataTable table = springUtil.getBean(DataTable.class);
table.importTable(new DataExcelReader("DataExcelGeneratorTest.xlsx"));
Collection<String> dataCollection = table.getRow(0);
Iterator<String> iterator = dataCollection.iterator();
Assert.assertEquals(iterator.next(), "Tom");
Assert.assertEquals(iterator.next(), "M");
Assert.assertEquals(iterator.next(), "18");
}
@Test
public void dataTableExportTest(){
DataTable table = springUtil.getBean(DataTable.class);
table.addColTitle("Name").addColTitle("Sex").addColTitle("Age");
Collection<String> dataCollection = new ArrayList<>();
dataCollection.add("Tom");
dataCollection.add("M");
dataCollection.add("18");
table.addRow(dataCollection);
dataCollection.clear();
dataCollection.add("Pat");
dataCollection.add("F");
dataCollection.add("16");
table.addRow(dataCollection);
table.exportTable(new DataExcelGenerator("DataTableExport.xlsx"));
}
}