|
@@ -1,20 +1,33 @@
|
|
|
package com.koobietech.eas.service.impl;
|
|
|
|
|
|
+import com.koobietech.eas.common.constant.FileType;
|
|
|
import com.koobietech.eas.common.result.JsonResult;
|
|
|
+import com.koobietech.eas.common.utils.DateUtils;
|
|
|
import com.koobietech.eas.common.utils.StudentArchiveGenerator;
|
|
|
+import com.koobietech.eas.dao.dto.ArchivesDto;
|
|
|
+import com.koobietech.eas.mbg.mapper.EasArcArchivesMapper;
|
|
|
+import com.koobietech.eas.mbg.mapper.EasArcTlsStudentsMapper;
|
|
|
+import com.koobietech.eas.mbg.mapper.EasSysStudentMapper;
|
|
|
+import com.koobietech.eas.mbg.model.EasArcArchives;
|
|
|
import com.koobietech.eas.mbg.model.EasArcTlsStudents;
|
|
|
+import com.koobietech.eas.mbg.model.EasSysStudent;
|
|
|
+import com.koobietech.eas.service.EasArchivesFilesService;
|
|
|
import com.koobietech.eas.service.EasStuProfileService;
|
|
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
+import org.apache.poi.util.Units;
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFTable;
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.apache.poi.util.Units;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.*;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Service
|
|
@@ -23,56 +36,115 @@ public class EasStuProfileServiceImpl implements EasStuProfileService {
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(EasStuProfileServiceImpl.class);
|
|
|
private static final String TEMPLATE_PATH = "temp/StuRegistTemp.docx";
|
|
|
private static final String PHOTO_PATH = "temp/kun.jpeg";
|
|
|
- private static final String OUTPUT_PATH = "D:\\myDesk\\test.docx";
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private EasArcTlsStudentsMapper easArcTlsStudentsMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private PasswordEncoder passwordEncoder;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private EasSysStudentMapper easSysStudentMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private EasArcArchivesMapper easArcArchivesMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private EasArchivesFilesService easArchivesFilesService;
|
|
|
|
|
|
@Override
|
|
|
- public JsonResult StuProfileDownload(EasArcTlsStudents easArcTlsStudents) {
|
|
|
+ public JsonResult StuProfileDownload(EasArcTlsStudents easArcTlsStudents,Integer manager_id) {
|
|
|
LOGGER.info("开始学员档案导出:{}", easArcTlsStudents);
|
|
|
|
|
|
try (InputStream wordStream = getClass().getClassLoader().getResourceAsStream(TEMPLATE_PATH)) {
|
|
|
assert wordStream != null;
|
|
|
- XWPFDocument doc = new XWPFDocument(wordStream);
|
|
|
-
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
- map.put("student_name", easArcTlsStudents.getStudentName());
|
|
|
- map.put("gender", easArcTlsStudents.getGender());
|
|
|
- map.put("major", easArcTlsStudents.getMajor());
|
|
|
- map.put("grade", easArcTlsStudents.getGrade());
|
|
|
- map.put("enrollment_date", easArcTlsStudents.getEnrollmentDate());
|
|
|
- map.put("phone", easArcTlsStudents.getPhone());
|
|
|
- map.put("university", easArcTlsStudents.getUniversity());
|
|
|
-
|
|
|
- InputStream photo = getClass().getClassLoader().getResourceAsStream(PHOTO_PATH);
|
|
|
- System.out.println( photo );
|
|
|
- map.put("avatar", photo);
|
|
|
-
|
|
|
- /**
|
|
|
- public static String generateStudentCode(String studentNumber, String studentId, String schoolName, String enrollmentDate) {
|
|
|
- LocalDate localDateEnrollmentDate = LocalDate.of(Integer.parseInt(enrollmentDate), 1, 1);
|
|
|
- String studentAge = String.valueOf(getStudentAge(studentId));
|
|
|
- String studentNum = getStudentNum(studentNumber);
|
|
|
- String studentGender = getStudentGender(studentId).substring(0, 1); // 取性别代码的首字母
|
|
|
- String graduationDate = String.valueOf(getStudentGraduationDate(localDateEnrollmentDate).getYear());
|
|
|
-
|
|
|
- String studentCode = STUDENT_CODE_PEREFIX + studentNum + studentAge +
|
|
|
- studentGender + graduationDate + schoolName;
|
|
|
-
|
|
|
- // 如果生成的档案编码长度不足,用UUID填充
|
|
|
- if (studentCode.length() < STUDENT_CODE_LENGTH) {
|
|
|
- studentCode += UUID.randomUUID().toString()
|
|
|
- .substring(0, STUDENT_CODE_LENGTH - studentCode.length());
|
|
|
- }
|
|
|
- return studentCode;
|
|
|
- }
|
|
|
- */
|
|
|
- //使用工具类生成学员档案号
|
|
|
- String student_number = StudentArchiveGenerator.generateStudentCode(
|
|
|
- "", "232126199003194781", "11273", "2023");
|
|
|
-
|
|
|
-
|
|
|
- replacePlaceholders(doc, map);
|
|
|
-
|
|
|
- saveDocument(doc);
|
|
|
+ try (XWPFDocument doc = new XWPFDocument(wordStream)) {
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("student_name", easArcTlsStudents.getStudentName());
|
|
|
+
|
|
|
+ //调用内部类方法 把MF转换成 男女
|
|
|
+ map.put("gender", convertToGender(easArcTlsStudents.getGender()));
|
|
|
+
|
|
|
+ map.put("major", easArcTlsStudents.getMajor());
|
|
|
+ map.put("grade", easArcTlsStudents.getGrade());
|
|
|
+
|
|
|
+ //调用自定义的时间格式转换器 把前端传过来的带时分秒时间转换成 2019年12月12日
|
|
|
+ map.put("enrollment_date", DateUtils.convertToYearMonthDay(easArcTlsStudents.getEnrollmentDate()));
|
|
|
+
|
|
|
+ map.put("phone", easArcTlsStudents.getPhone());
|
|
|
+ map.put("university", easArcTlsStudents.getUniversity());
|
|
|
+ map.put("student_idnumber", easArcTlsStudents.getStudentIdnumber());
|
|
|
+ map.put("avatar", getClass().getClassLoader().getResourceAsStream(PHOTO_PATH));
|
|
|
+
|
|
|
+ //调用内部类方法 生成学号
|
|
|
+ String studentNumber = generateStudentNumber(easArcTlsStudents);
|
|
|
+ map.put("student_number", studentNumber);
|
|
|
+
|
|
|
+ replacePlaceholders(doc, map);
|
|
|
+ //saveDocument(doc);
|
|
|
+
|
|
|
+ //将easArcTlsStudents 保存到数据库
|
|
|
+ easArcTlsStudentsMapper.insert(easArcTlsStudents);
|
|
|
+
|
|
|
+ //使用BeanUtils 将easArcTlsStudents 转换成 eassysstudent
|
|
|
+ EasSysStudent easSysStudents = new EasSysStudent();
|
|
|
+ BeanUtils.copyProperties(easArcTlsStudents,easSysStudents);
|
|
|
+ //设置初始密码 使用 passwordEncoder 设置初始密码为123456
|
|
|
+ easSysStudents.setPasswd(passwordEncoder.encode("123456"));
|
|
|
+ easSysStudents.setDisabled("N");
|
|
|
+
|
|
|
+ easSysStudentMapper.insert(easSysStudents);
|
|
|
+
|
|
|
+ //组合成档案对象 保存到数据库
|
|
|
+ /**
|
|
|
+ CREATE TABLE `eas_arc_archives` (
|
|
|
+ 1 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
|
+ 1 `archive_number` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '文件电子档案号',
|
|
|
+ 1 `student_number` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '当前电子档案归属那一个学员档案下',
|
|
|
+ 1 `file_path` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '文件存储的路径',
|
|
|
+ 1 `arctype` int(11) DEFAULT NULL COMMENT '文件类型Id',
|
|
|
+ 1 `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
|
|
+ 1 `modify_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
|
|
|
+ 1 `validity_time` datetime DEFAULT NULL COMMENT '档案有效期截至时间',
|
|
|
+ 1 `manager_id` int(11) DEFAULT NULL COMMENT '档案归属负责人',
|
|
|
+ ? `create_date` date DEFAULT NULL COMMENT '档案创建时间, 用于文件归档用',
|
|
|
+ ? `create_uid` int(11) DEFAULT NULL COMMENT '创建用户ID',
|
|
|
+ PRIMARY KEY (`id`) USING BTREE
|
|
|
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='文件档案表';
|
|
|
+ */
|
|
|
+ String archiveCode = StudentArchiveGenerator.generateArchiveCode(studentNumber, String.valueOf(FileType.DOCX));
|
|
|
+ ArchivesDto archivesDto = easArchivesFilesService.saveArchiveStudentsFile(easArcTlsStudents.getStudentIdnumber(), doc);
|
|
|
+ String filePath = archivesDto.getPath();
|
|
|
+ String arctype = archivesDto.getFileType();
|
|
|
+ Date creat_time = DateUtils.convertToYearMonthDayToDate(new Date());
|
|
|
+ Date modify_time = new Date();
|
|
|
+
|
|
|
+ //validity_time 暂且设置成create_date + 1年
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(creat_time);
|
|
|
+ calendar.add(Calendar.YEAR, 1);
|
|
|
+ Date create_date = calendar.getTime();
|
|
|
+
|
|
|
+ int create_uid = 0;
|
|
|
+
|
|
|
+ EasArcArchives easArcArchives = new EasArcArchives();
|
|
|
+ easArcArchives.setArchiveNumber(archiveCode);
|
|
|
+ easArcArchives.setStudentNumber(studentNumber);
|
|
|
+ easArcArchives.setFilePath(filePath);
|
|
|
+ easArcArchives.setArctype(arctype);
|
|
|
+ easArcArchives.setCreateTime(creat_time);
|
|
|
+ easArcArchives.setModifyTime(modify_time);
|
|
|
+ easArcArchives.setValidityTime(create_date);
|
|
|
+ easArcArchives.setManagerId(manager_id);
|
|
|
+ easArcArchives.setCreateUid(create_uid);
|
|
|
+ easArcArchives.setCreateDate(new Date());
|
|
|
+
|
|
|
+ easArcArchivesMapper.insert(easArcArchives);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
} catch (IOException e) {
|
|
|
LOGGER.error("学员档案导出失败:{}", e.getMessage(), e);
|
|
|
return JsonResult.fail("学员档案导出失败!");
|
|
@@ -82,16 +154,29 @@ public class EasStuProfileServiceImpl implements EasStuProfileService {
|
|
|
return JsonResult.ok("学员档案导出成功!");
|
|
|
}
|
|
|
|
|
|
+ private String generateStudentNumber(EasArcTlsStudents easArcTlsStudents) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(easArcTlsStudents.getEnrollmentDate());
|
|
|
+ String year = String.valueOf(calendar.get(Calendar.YEAR));
|
|
|
+
|
|
|
+ return StudentArchiveGenerator.generateStudentCode(
|
|
|
+ "", easArcTlsStudents.getStudentIdnumber(), easArcTlsStudents.getUniversity(), year);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String convertToGender(String gender) {
|
|
|
+ if (gender.equals("M")) {
|
|
|
+ return "男";
|
|
|
+ } else if (gender.equals("F")) {
|
|
|
+ return "女";
|
|
|
+ } else {
|
|
|
+ return "未知";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void replacePlaceholders(XWPFDocument document, Map<String, Object> map) {
|
|
|
- Iterator<XWPFTable> it = document.getTablesIterator();
|
|
|
-
|
|
|
- while (it.hasNext()) {
|
|
|
- XWPFTable table = it.next();
|
|
|
- int rcount = table.getNumberOfRows();
|
|
|
- for (int n = 0; n < rcount; n++) {
|
|
|
- XWPFTableRow wrow = table.getRow(n);
|
|
|
- List<XWPFTableCell> cells = wrow.getTableCells();
|
|
|
- for (XWPFTableCell cell : cells) {
|
|
|
+ for (XWPFTable table : document.getTables()) {
|
|
|
+ for (XWPFTableRow row : table.getRows()) {
|
|
|
+ for (XWPFTableCell cell : row.getTableCells()) {
|
|
|
String cellText = cell.getText();
|
|
|
if (!cellText.contains("${")) {
|
|
|
continue;
|
|
@@ -102,7 +187,6 @@ public class EasStuProfileServiceImpl implements EasStuProfileService {
|
|
|
try {
|
|
|
cell.removeParagraph(0);
|
|
|
if (value instanceof InputStream) {
|
|
|
- //如果是放置图片的单元格,在这里添加一个计算图片合适大小的方法,按比例
|
|
|
cell.addParagraph().createRun().addPicture((InputStream) value,
|
|
|
XWPFDocument.PICTURE_TYPE_JPEG, "avatar.jpg",
|
|
|
Units.pixelToEMU(110), Units.pixelToEMU(140));
|
|
@@ -118,12 +202,6 @@ public class EasStuProfileServiceImpl implements EasStuProfileService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- private void saveDocument(XWPFDocument document) throws IOException {
|
|
|
- try (FileOutputStream outputStream = new FileOutputStream(OUTPUT_PATH)) {
|
|
|
- document.write(outputStream);
|
|
|
- }
|
|
|
- }
|
|
|
+}
|
|
|
|
|
|
|
|
|
-}
|