From 3dadf459191d223684f07ee5b1a0f5016e312f93 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Wed, 19 Feb 2020 17:46:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E5=86=99=E5=B9=B6=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=AD=A6=E7=94=9F=E5=9F=BA=E6=9C=AC=E4=BF=A1=E6=81=AF=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E6=9C=8D=E5=8A=A1=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ase/component/datamanager/DataTable.java | 34 +++++++++- .../component/datamanager/DataTableRow.java | 4 +- .../BaseAdministrativeDivision.java | 3 +- .../information/BaseCandidateCategory.java | 1 - .../ase/model/information/BaseCollege.java | 1 - .../ase/model/information/BaseEthnic.java | 1 - .../ase/model/information/BaseMajor.java | 1 - .../information/BasePoliticalStatus.java | 1 - .../model/information/BaseStudentInfo.java | 10 +-- .../BaseAdministrativeDivisionRepository.java | 1 + .../ase/service/BaseInformationService.java | 60 ++++++++++++++-- .../ase/service/IBaseInformationService.java | 6 +- .../data_base_administrative_division.sql | 1 + src/main/resources/data_base_college.sql | 2 +- .../codesdream/ase/BaseInformationTest.java | 30 ++++++++ .../com/codesdream/ase/DataManagerTest.java | 68 +++++++++++++++++-- 16 files changed, 198 insertions(+), 26 deletions(-) create mode 100644 src/test/java/com/codesdream/ase/BaseInformationTest.java diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataTable.java b/src/main/java/com/codesdream/ase/component/datamanager/DataTable.java index 8e6eb1b..cf1217c 100644 --- a/src/main/java/com/codesdream/ase/component/datamanager/DataTable.java +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataTable.java @@ -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 getRow(int index){ + if(index >= getRowsSize()) throw new IndexOutOfBoundsException(); return dataRows.elementAt(index).getRow(); } + // 获得特定行的数据 + public Vector 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 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 row){ - dataRows.add(new DataTableRow(dataRows.size() + 1, row)); + Collection dataRow = new ArrayList<>(row); + dataRows.add(new DataTableRow(dataRows.size() + 1, dataRow)); } // 获得表的列数 diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataTableRow.java b/src/main/java/com/codesdream/ase/component/datamanager/DataTableRow.java index 9e68b1a..ca07c1b 100644 --- a/src/main/java/com/codesdream/ase/component/datamanager/DataTableRow.java +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataTableRow.java @@ -7,8 +7,8 @@ import java.util.Collection; // 描述数据表的行 @Data public class DataTableRow { - int index; - Collection row; + private int index; + private Collection row; public DataTableRow(int index, Collection row){ this.index = index; diff --git a/src/main/java/com/codesdream/ase/model/information/BaseAdministrativeDivision.java b/src/main/java/com/codesdream/ase/model/information/BaseAdministrativeDivision.java index 0820d1a..39dd5c7 100644 --- a/src/main/java/com/codesdream/ase/model/information/BaseAdministrativeDivision.java +++ b/src/main/java/com/codesdream/ase/model/information/BaseAdministrativeDivision.java @@ -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; } diff --git a/src/main/java/com/codesdream/ase/model/information/BaseCandidateCategory.java b/src/main/java/com/codesdream/ase/model/information/BaseCandidateCategory.java index 25535dd..a399a15 100644 --- a/src/main/java/com/codesdream/ase/model/information/BaseCandidateCategory.java +++ b/src/main/java/com/codesdream/ase/model/information/BaseCandidateCategory.java @@ -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; diff --git a/src/main/java/com/codesdream/ase/model/information/BaseCollege.java b/src/main/java/com/codesdream/ase/model/information/BaseCollege.java index e62c514..a2cdae9 100644 --- a/src/main/java/com/codesdream/ase/model/information/BaseCollege.java +++ b/src/main/java/com/codesdream/ase/model/information/BaseCollege.java @@ -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; diff --git a/src/main/java/com/codesdream/ase/model/information/BaseEthnic.java b/src/main/java/com/codesdream/ase/model/information/BaseEthnic.java index 9b6b7ab..4188845 100644 --- a/src/main/java/com/codesdream/ase/model/information/BaseEthnic.java +++ b/src/main/java/com/codesdream/ase/model/information/BaseEthnic.java @@ -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; diff --git a/src/main/java/com/codesdream/ase/model/information/BaseMajor.java b/src/main/java/com/codesdream/ase/model/information/BaseMajor.java index e7076fd..8de7cf5 100644 --- a/src/main/java/com/codesdream/ase/model/information/BaseMajor.java +++ b/src/main/java/com/codesdream/ase/model/information/BaseMajor.java @@ -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; diff --git a/src/main/java/com/codesdream/ase/model/information/BasePoliticalStatus.java b/src/main/java/com/codesdream/ase/model/information/BasePoliticalStatus.java index fd89069..1cc2584 100644 --- a/src/main/java/com/codesdream/ase/model/information/BasePoliticalStatus.java +++ b/src/main/java/com/codesdream/ase/model/information/BasePoliticalStatus.java @@ -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; 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 16ce742..dcf0113 100644 --- a/src/main/java/com/codesdream/ase/model/information/BaseStudentInfo.java +++ b/src/main/java/com/codesdream/ase/model/information/BaseStudentInfo.java @@ -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; } diff --git a/src/main/java/com/codesdream/ase/repository/information/BaseAdministrativeDivisionRepository.java b/src/main/java/com/codesdream/ase/repository/information/BaseAdministrativeDivisionRepository.java index c96f610..825bb3a 100644 --- a/src/main/java/com/codesdream/ase/repository/information/BaseAdministrativeDivisionRepository.java +++ b/src/main/java/com/codesdream/ase/repository/information/BaseAdministrativeDivisionRepository.java @@ -9,4 +9,5 @@ import java.util.Optional; @Repository public interface BaseAdministrativeDivisionRepository extends CrudRepository { Optional findByName(String name); + Optional findByNameContainsAndParentId(String name, int parentId); } diff --git a/src/main/java/com/codesdream/ase/service/BaseInformationService.java b/src/main/java/com/codesdream/ase/service/BaseInformationService.java index dbf7d29..78298dd 100644 --- a/src/main/java/com/codesdream/ase/service/BaseInformationService.java +++ b/src/main/java/com/codesdream/ase/service/BaseInformationService.java @@ -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 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> 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 infoIndex = new Vector<>(); + + for(Optional 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 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) { // 检查 diff --git a/src/main/java/com/codesdream/ase/service/IBaseInformationService.java b/src/main/java/com/codesdream/ase/service/IBaseInformationService.java index 5ec6b94..fbffb32 100644 --- a/src/main/java/com/codesdream/ase/service/IBaseInformationService.java +++ b/src/main/java/com/codesdream/ase/service/IBaseInformationService.java @@ -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); diff --git a/src/main/resources/data_base_administrative_division.sql b/src/main/resources/data_base_administrative_division.sql index dc974c2..0653887 100644 --- a/src/main/resources/data_base_administrative_division.sql +++ b/src/main/resources/data_base_administrative_division.sql @@ -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'); diff --git a/src/main/resources/data_base_college.sql b/src/main/resources/data_base_college.sql index 482ea2f..44425f7 100644 --- a/src/main/resources/data_base_college.sql +++ b/src/main/resources/data_base_college.sql @@ -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'); diff --git a/src/test/java/com/codesdream/ase/BaseInformationTest.java b/src/test/java/com/codesdream/ase/BaseInformationTest.java new file mode 100644 index 0000000..7cdea10 --- /dev/null +++ b/src/test/java/com/codesdream/ase/BaseInformationTest.java @@ -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); + } +} diff --git a/src/test/java/com/codesdream/ase/DataManagerTest.java b/src/test/java/com/codesdream/ase/DataManagerTest.java index ba87338..da416c5 100644 --- a/src/test/java/com/codesdream/ase/DataManagerTest.java +++ b/src/test/java/com/codesdream/ase/DataManagerTest.java @@ -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 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 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 dataCollection = table.getRow(0); + Iterator 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 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")); + } + + }