package com.koobietech.eas.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.koobietech.eas.common.constant.FileType;
import com.koobietech.eas.common.constant.Gender;
import com.koobietech.eas.common.constant.UniversityCode;
import com.koobietech.eas.common.exception.EasException;
import com.koobietech.eas.common.result.PageDataResult;
import com.koobietech.eas.common.utils.ArchiveManager;
import com.koobietech.eas.common.utils.DateManager;
import com.koobietech.eas.common.utils.FileManager;
import com.koobietech.eas.common.utils.SecurityManager;
import com.koobietech.eas.dao.dto.ArchivesDto;
import com.koobietech.eas.dao.dto.EasArcTlsStudentsDto;
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.*;
import com.koobietech.eas.service.EasArchiveFileDownloadService;
import com.koobietech.eas.service.EasArchivesFilesService;
import com.koobietech.eas.service.EasStuProfileService;
import com.koobietech.eas.service.EasSysUserInfoService;
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.springframework.beans.BeanUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

@Service
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/avatar.jpeg";
    @Resource
    private EasArcTlsStudentsMapper easArcTlsStudentsMapper;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private EasSysStudentMapper easSysStudentMapper;
    @Resource
    private EasArcArchivesMapper easArcArchivesMapper;
    @Resource
    private EasArchivesFilesService easArchivesFilesService;
    @Resource
    private EasArchiveFileDownloadService  easArchiveFileDownloadService;
    @Resource
    private EasSysUserInfoService easSysUserInfoService;

    final String FILETYPE = "prefile";

    @Override
    public ArchivesDto saveUserFile(EasArcTlsStudents easArcTlsStudents, String avatar){

        if ( Objects.isNull(easArcTlsStudents.getEnrollmentDate()) ) {
            easArcTlsStudents.setEnrollmentDate(new Date());
        }
        if ( Objects.isNull(easArcTlsStudents.getGraduationDate()) ) {
            easArcTlsStudents.setGraduationDate(new Date(LocalDate.now().plusYears(2)
                    .atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli()));
        }
        if ( Objects.isNull(easArcTlsStudents.getBirthdate()) ) {
            easArcTlsStudents.setBirthdate(
                    new Date(ArchiveManager.extractBirthDate(
                                    easArcTlsStudents.getStudentIdnumber()
                            ).atStartOfDay()
                                    .toInstant(ZoneOffset.of("+8")).toEpochMilli()) );
        }
        ArchivesDto archivesDto;
        try (InputStream wordStream = getClass().getClassLoader().getResourceAsStream(TEMPLATE_PATH)) {
            assert wordStream != null;
            try (XWPFDocument doc = new XWPFDocument(wordStream)) {
                //调用内部类方法 生成学号
                String studentNumber = "";
                if ( easArcTlsStudents.getStudentNumber() == null || easArcTlsStudents.getStudentNumber().equals("")) {
                    studentNumber = generateStudentNumber(easArcTlsStudents);
                    easArcTlsStudents.setStudentNumber(studentNumber);
                } else {
                    studentNumber = easArcTlsStudents.getStudentNumber();
                }

                Map<String, Object> map = generateDataMap(easArcTlsStudents, studentNumber, avatar);
                replacePlaceholders(doc, map);
                //这里会生成一个学生档案保存本地 目前这里有bug保存不成功 而且文件后缀有错误
                archivesDto = insertEasArcArchives(easArcTlsStudents, doc);
                if (!archivesDto.isStatus()) {
                    throw new EasException("EasArcArchives保存失败", 8002);
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
                throw new EasException("调取模板失败", 8003);
            }
        } catch (IOException e) {
            logger.error(e.getMessage());
            throw new EasException("生成档案失败", 8003);
        }
        return archivesDto;
    }

    @Override
    public ArchivesDto upload(MultipartFile file, String studentNumber, String fileType) {
        InputStream inputStream = null;
        ArchivesDto archivesDto = null;
        try {
            inputStream = file.getInputStream();
            String type = file.getOriginalFilename().substring(
                    file.getOriginalFilename().lastIndexOf(".") + 1).toUpperCase();
            try {
                //如果抛异常 则类型不识别
                FileType.valueOf(type);
            }
            catch (IllegalArgumentException e) {
                type = FileType.FILE.toString();
            }
            archivesDto = easArchivesFilesService.saveArchiveFileTemp(studentNumber, inputStream, type);
            if (archivesDto.isStatus()) {
                if ( fileType.equals("avatar") ) {
                    EasSysStudentExample easSysStudentExample = new EasSysStudentExample();
                    easSysStudentExample.createCriteria().andStudentNumberEqualTo(studentNumber);
                    EasSysStudent easSysStudent = new EasSysStudent();
                    easSysStudent.setAvatar(archivesDto.getPath());
                    easSysStudentMapper.updateByExampleSelective(easSysStudent, easSysStudentExample);
                }
                EasArcTlsStudentsExample easArcTlsStudentsExample = new EasArcTlsStudentsExample();
                easArcTlsStudentsExample.createCriteria().andStudentNumberEqualTo(studentNumber);
                List<EasArcTlsStudents> easArcTlsStudents = easArcTlsStudentsMapper.selectByExample(easArcTlsStudentsExample);
                EasArcTlsStudents arcStudent = easArcTlsStudents.stream().findFirst().orElse(null);
                if ( Objects.nonNull(arcStudent) ) {
                    saveUserFile(arcStudent, archivesDto.getPath() );
                }
            }
        } catch (IOException ignore) {
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ignore) {}
            }
        }
        return archivesDto;
    }

    /**
     * 判断是否是图片后缀的文件
     * @param path
     * @return
     */

    private boolean isImage(String path) {
        String filePath = path.toUpperCase();
        return filePath.endsWith(".JPG") || filePath.endsWith(".JPEG")
                || filePath.endsWith(".PNG") || filePath.endsWith(".GIF") ||
                filePath.endsWith(".BMP") || filePath.endsWith(".TIF")
                 || filePath.endsWith(".TIFF") ||  filePath.endsWith(".ICO");
    }
    @Override
    public boolean addArchives(EasArcArchives arc) {

        FileManager fileManager = new FileManager();

        if ( Objects.isNull(arc) || Objects.isNull(arc.getFilePath()) ||
                !StringUtils.hasText(arc.getFilePath()) || !fileManager.isFileExists(arc.getFilePath()) ) {
            throw new EasException("档案不能为空");
        }
        String fileType = arc.getFilePath().substring(arc.getFilePath().lastIndexOf(".") + 1).toUpperCase();
        String arctype = fileType;
        if ( isImage(arc.getFilePath()) ) {
            arctype =  "IMAGE";
        }
        try {
            //如果抛异常 则类型不识别
            FileType.valueOf(fileType);
        }catch (IllegalArgumentException e) {
            arctype = FileType.FILE.toString();
        }
        String archiveNumber = arc.getFilePath().substring(
                arc.getFilePath().lastIndexOf("\\") + 1,
                arc.getFilePath().lastIndexOf("."));

        String archiveSavePath = easArchivesFilesService.getArchiveSavePath(arc.getStudentNumber(), archiveNumber, fileType);
        fileManager.moveFile( arc.getFilePath(), archiveSavePath, true );

        arc.setFilePath( archiveSavePath );
        arc.setArctype(arctype);
        arc.setCreateTime(new Date());
        arc.setModifyTime(new Date());
        arc.setArchiveNumber( archiveNumber );
        arc.setValidityTime( new Date(LocalDate.now().plusMonths(20)
                .atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli()) );
        arc.setManagerId( SecurityManager.getLoginUid().intValue() );
        arc.setCreateUid( SecurityManager.getLoginUid().intValue() );
        arc.setCreateDate(new Date());
        return easArcArchivesMapper.insert(arc) == 1;
    }

    @Override
    public boolean delArchives(Integer id) {
        EasArcArchives easArcArchives = easArcArchivesMapper.selectByPrimaryKey(id);
        if (
            easArcArchives.getFiletype().equals("prefile") ||
            easArcArchives.getFiletype().equals("attendance") ||
            easArcArchives.getFiletype().equals("scores")
        ) {
            return false;
        }
        easArchivesFilesService.deleteArchiveFile(easArcArchives.getFilePath());
        return easArcArchivesMapper.deleteByPrimaryKey(id) == 1;
    }

    @Override
    public String downloadArchives(String studentNumber) {
        if ( studentNumber == null || studentNumber.isEmpty() || studentNumber.length() < 10 ) {
            return null;
        }
        EasSysStudentExample easSysStudentExample = new EasSysStudentExample();
        easSysStudentExample.createCriteria().andStudentNumberEqualTo(studentNumber);
        List<EasSysStudent> easSysStudents = easSysStudentMapper.selectByExample(easSysStudentExample);
        if ( easSysStudents.size() == 0 ) {
            return null;
        }
        String zipArchives = easArchivesFilesService.createZipArchives(studentNumber);
        return easArchiveFileDownloadService.getArchiveDownloadToken( studentNumber , zipArchives);
    }

    @Override
    public boolean add(EasArcTlsStudents easArcTlsStudents) {

        ArchivesDto archivesDto = saveUserFile(easArcTlsStudents, PHOTO_PATH);

        //将easArcTlsStudents 保存到数据库
        easArcTlsStudents.setArchiveNumber(archivesDto.getArchiveCode());
        boolean isTlsStudentsInsert = insertEasArcTlsStudents(easArcTlsStudents);
        if (!isTlsStudentsInsert) {
            throw new EasException("EasArcTlsStudents保存失败", 8000);
        }
        boolean isSysStudentsInsert = insertEasSysStudents(easArcTlsStudents);
        if (!isSysStudentsInsert) {
            throw new EasException("EasSysStudents保存失败", 8001);
        }
        return true;
    }

    @Override
    public PageDataResult query(EasArcTlsStudents studentDto) {

        EasArcTlsStudentsExample easArcTlsStudentsExample = new EasArcTlsStudentsExample();
        EasArcTlsStudentsExample.Criteria criteria = easArcTlsStudentsExample.createCriteria();
        if ( Objects.nonNull(studentDto) ) {
            if (StringUtils.hasText(studentDto.getStudentName())) {
                criteria.andStudentNameLike("%" + studentDto.getStudentName() + "%");
            }
            if (StringUtils.hasText(studentDto.getStudentNumber())) {
                criteria.andStudentNumberLike("%" + studentDto.getStudentNumber() + "%");
            }
            if (Objects.nonNull(studentDto.getGender())) {
                criteria.andGenderEqualTo(studentDto.getGender());
            }
            if (StringUtils.hasText(studentDto.getStudentIdnumber())) {
                criteria.andStudentIdnumberEqualTo(studentDto.getStudentIdnumber());
            }
            if (StringUtils.hasText(studentDto.getPhone())) {
                criteria.andPhoneLike("%"+studentDto.getPhone() + "% %" + studentDto.getPhone() + "%");
            }
            if (StringUtils.hasText(studentDto.getUniversity())) {
                criteria.andUniversityLike("%" + studentDto.getUniversity() + "%");
            }
            if (StringUtils.hasText(studentDto.getAddress())) {
                criteria.andAddressLike("%" + studentDto.getAddress() + "%");
            }
            if (StringUtils.hasText(studentDto.getEmail())) {
                criteria.andEmailEqualTo(studentDto.getEmail());
            }
            if (Objects.nonNull(studentDto.getEnrollmentDate())) {
                criteria.andEnrollmentDateEqualTo(studentDto.getEnrollmentDate());
            }
        }
        List<EasArcTlsStudents> easArcTlsStudents = easArcTlsStudentsMapper.selectByExample(easArcTlsStudentsExample);
        long l = easArcTlsStudentsMapper.countByExample(easArcTlsStudentsExample);
        return PageDataResult.init(easArcTlsStudents, l);
    }

    @Override
    public boolean update(EasArcTlsStudents studentDto) {
        EasSysStudent easSysStudent = new EasSysStudent();
        if ( studentDto.getGraduation().equals("Y") ) {
            easSysStudent.setDisabled("Y");
        } else {
            easSysStudent.setDisabled("N");
        }
        EasSysStudentExample easSysStudentExample = new EasSysStudentExample();
        easSysStudentExample.createCriteria().andStudentNumberEqualTo(studentDto.getStudentNumber());
        easSysStudentMapper.updateByExampleSelective(easSysStudent, easSysStudentExample);
        return easArcTlsStudentsMapper.updateByPrimaryKeySelective (studentDto) == 1;
    }

    @Override
    public boolean delete(int id) {
        EasArcTlsStudents easArcTlsStudents = easArcTlsStudentsMapper.selectByPrimaryKey(id);
        if ( Objects.nonNull(easArcTlsStudents) ) {
            EasSysStudentExample easSysStudentExample = new EasSysStudentExample();
            easSysStudentExample.createCriteria().andStudentNumberEqualTo( easArcTlsStudents.getStudentNumber() );
            int i = easSysStudentMapper.deleteByExample(easSysStudentExample);
            int i1 = easArcTlsStudentsMapper.deleteByPrimaryKey(id);
            return i == 1 && i1 == 1;
        }
        return false;
    }

    @Override
    public boolean importExcel(InputStream inputStream) {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        try {
            EasArcTlsStudents easArcTlsStudents = null;
                    List<EasArcTlsStudentsDto> studentList = ExcelImportUtil.importExcel(
                    inputStream, EasArcTlsStudentsDto.class, params);
            for (EasArcTlsStudentsDto studentDto : studentList) {
                easArcTlsStudents = new EasArcTlsStudents();
                BeanUtils.copyProperties(studentDto, easArcTlsStudents);
                add(easArcTlsStudents);
            }
            return true;
        } catch (Exception ignored){
            logger.error(ignored.getMessage());
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ignored) {
                    logger.error(ignored.getMessage());
                }
            }
        }
        return false;
    }

    @Override
    public List<EasSysStudent> getAll() {
        EasSysStudentExample easSysStudentExample = new EasSysStudentExample();
        EasSysStudentExample.Criteria criteria = easSysStudentExample.createCriteria();
        criteria.andDisabledEqualTo("N");
        return easSysStudentMapper.selectByExample(easSysStudentExample);
    }

    @Override
    public List<EasSysStudent> getAllOrStudentNumber(String studentNumber){
        EasSysStudentExample easSysStudentExample = new EasSysStudentExample();
        EasSysStudentExample.Criteria criteria = easSysStudentExample.createCriteria();
        criteria.andDisabledEqualTo("N");
        if ( Objects.nonNull(studentNumber) ) {
            criteria.andStudentNumberEqualTo(studentNumber);
        }
        return easSysStudentMapper.selectByExample(easSysStudentExample);
    }

    @Override
    public List<EasSysStudent> getStudentByKeyword(String keyword) {
        EasSysStudentExample easSysStudentExample = new EasSysStudentExample();
        EasSysStudentExample.Criteria criteria = easSysStudentExample.createCriteria();
        criteria.andStudentNameLike("%" + keyword + "%");
        EasSysStudentExample.Criteria criteria1 = easSysStudentExample.createCriteria();
        criteria1.andStudentNumberLike("STX" + keyword + "%");
        easSysStudentExample.or(criteria1);
        EasSysStudentExample.Criteria criteria2 = easSysStudentExample.createCriteria();
        criteria2.andPhoneEqualTo(keyword );
        easSysStudentExample.or(criteria2);
        if ( isNumeric(keyword) ) {
            EasSysStudentExample.Criteria criteria3 = easSysStudentExample.createCriteria();
            criteria3.andIdEqualTo(Long.parseLong(keyword));
            easSysStudentExample.or(criteria3);
        }
        return easSysStudentMapper.selectByExample(easSysStudentExample);
    }

    public boolean isNumeric(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        Pattern pattern = compile("^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$");
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    private Map<String, Object> generateDataMap(EasArcTlsStudents easArcTlsStudents, String studentNumber, String avatar) {
        EasSysUserinfo easSysUserinfo = null;
        if ( Objects.nonNull(easArcTlsStudents.getAdmissionsId()) ) {
            easSysUserinfo = easSysUserInfoService.queryByUid(easArcTlsStudents.getAdmissionsId().longValue());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("student_name", easArcTlsStudents.getStudentName());
        map.put("gender", convertToGender(easArcTlsStudents.getGender()));
        map.put("enrollment_date", DateManager.convertToYearMonthDay(easArcTlsStudents.getEnrollmentDate()));
        map.put("graduation_date", DateManager.convertToYearMonthDay(easArcTlsStudents.getGraduationDate()));
        map.put("phone", easArcTlsStudents.getPhone());
        map.put("university", easArcTlsStudents.getUniversity());
        map.put("student_idnumber", easArcTlsStudents.getStudentIdnumber());
        map.put("avatar", avatar);
        map.put("student_number", studentNumber);
        map.put("birthdate", DateManager.convertToYearMonthDay(easArcTlsStudents.getBirthdate()));
        map.put("admissionsd", ( Objects.nonNull(easSysUserinfo) && Objects.nonNull(easSysUserinfo.getRelname()) ) ? easSysUserinfo.getRelname() : "");
        return map;
    }

    public ArchivesDto insertEasArcArchives(EasArcTlsStudents easArcTlsStudents,  XWPFDocument doc) {

        // 删除文件
        EasArcArchivesExample easArcArchivesExample = new EasArcArchivesExample();
        EasArcArchivesExample.Criteria criteria = easArcArchivesExample.createCriteria();
        criteria.andStudentNumberEqualTo( easArcTlsStudents.getStudentNumber() );
        criteria.andFiletypeEqualTo( FILETYPE );
        List<EasArcArchives> easArcArchivesOld = easArcArchivesMapper.selectByExample(easArcArchivesExample);
        if ( easArcArchivesOld.size() > 0) {
            for ( EasArcArchives arc : easArcArchivesOld) {
                easArchivesFilesService.deleteArchiveFile(arc.getFilePath());
            }
            easArcArchivesMapper.deleteByExample(easArcArchivesExample);
        }


        // 生成档案号
        String archiveCode = ArchiveManager.generateArchiveCode(easArcTlsStudents.getStudentNumber(), String.valueOf(FileType.DOCX.getValue()));

        // 保存学员档案文件
        ArchivesDto archivesDto = easArchivesFilesService.saveArchiveStudentsFile(easArcTlsStudents.getStudentNumber(), doc);

        // 设置时间和有效期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, 5);
        Date validityTime = calendar.getTime();

        // 创建 EasArcArchives 对象并设置属性
        EasArcArchives easArcArchives = new EasArcArchives();
        easArcArchives.setArchiveNumber(archiveCode);
        easArcArchives.setStudentNumber(easArcTlsStudents.getStudentNumber());
        easArcArchives.setFilePath(archivesDto.getPath());
        easArcArchives.setFiletype(FILETYPE);
        easArcArchives.setArctype(archivesDto.getFileType());
        easArcArchives.setCreateTime(easArcTlsStudents.getCreateTime());
        easArcArchives.setModifyTime(new Date());
        easArcArchives.setValidityTime(validityTime);
        easArcArchives.setManagerId(easArcTlsStudents.getManagerId());
        easArcArchives.setCreateUid(SecurityManager.getLoginUid().intValue());
        easArcArchives.setCreateDate(new Date());

        // 插入到数据库
        int result = easArcArchivesMapper.insert(easArcArchives);
        archivesDto.setStatus( result == 1 );
        // 返回是否成功插入数据库
        return archivesDto;
    }


    private boolean insertEasSysStudents(EasArcTlsStudents easArcTlsStudents) {
        EasSysStudent easSysStudents = new EasSysStudent();
        BeanUtils.copyProperties(easArcTlsStudents, easSysStudents);
        //设置初始密码 使用 passwordEncoder 设置初始密码为123456
        easSysStudents.setPasswd(passwordEncoder.encode("123456"));
        easSysStudents.setDisabled("N");
        easSysStudents.setAvatar("占位图片.jpg");
        return easSysStudentMapper.insert(easSysStudents) == 1;
    }

    private boolean insertEasArcTlsStudents(EasArcTlsStudents easArcTlsStudents) {
        //判断是否插入成功 成功的话返回true 不成功的话返回false
        return easArcTlsStudentsMapper.insert(easArcTlsStudents) == 1;
    }

    private String generateStudentNumber(EasArcTlsStudents easArcTlsStudents) {
        Calendar calendar = Calendar.getInstance();
        if ( Objects.isNull(easArcTlsStudents.getEnrollmentDate()) ) {
            easArcTlsStudents.setEnrollmentDate(new Date());
        }
        calendar.setTime(easArcTlsStudents.getEnrollmentDate());
        String year = String.valueOf(calendar.get(Calendar.YEAR));

        return ArchiveManager.generateStudentCode("",
                easArcTlsStudents.getStudentIdnumber(), String.valueOf(UniversityCode.getCode(easArcTlsStudents.getUniversity().trim())), year);
    }

    private String convertToGender(String gender) {
        return Gender.getkeyByString(gender);
    }

    private void replacePlaceholders(XWPFDocument document, Map<String, Object> map) {
        InputStream resourceAsStream = null;
        FileManager fileManager = new FileManager();
        try {
            if (map.get("avatar").toString().equals(PHOTO_PATH)
                    || Objects.isNull(map.get("avatar"))
                    || map.get("avatar").toString().equals("") ) {
                resourceAsStream = getClass().getClassLoader().getResourceAsStream(PHOTO_PATH);
            } else if ( fileManager.isFileExists(map.get("avatar").toString()) ) {
                resourceAsStream = new FileInputStream(new File(map.get("avatar").toString()));
            }
            map.put("avatar", resourceAsStream);
            for (XWPFTable table : document.getTables()) {
                for (XWPFTableRow row : table.getRows()) {
                    for (XWPFTableCell cell : row.getTableCells()) {
                        String cellText = cell.getText();
                        if (!cellText.contains("${")) {
                            continue;
                        }
                        map.forEach((key, value) -> {
                            String placeholder = "${" + key + "}";
                            if (cellText.contains(placeholder) && Objects.nonNull(value)) {
                                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));
                                    } else {
                                        cell.setText(cellText.replace(placeholder, value.toString()));
                                    }
                                } catch (IOException | InvalidFormatException e) {
                                    throw new EasException("学员导入失败");

                                }
                            }
                        });
                    }
                }
            }
        } catch (Exception ignored) {} finally {
            if ( Objects.nonNull(resourceAsStream) ) {
                try {
                    resourceAsStream.close();
                } catch (IOException ignored) {}
            }
        }
    }
}


