package com.jeethink.receive.service.impl;

import com.jeethink.bean.domain.ArchivesMaster;
import com.jeethink.common.core.domain.entity.SysUser;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.common.utils.StringUtils;
import com.jeethink.common.utils.file.FileUtils;
import com.jeethink.framework.config.ServerConfig;
import com.jeethink.receive.domain.ArchivesFile;
import com.jeethink.receive.mapper.ArchivesFileMapper;
import com.jeethink.receive.vo.ArchivesVO;
import com.jeethink.common.constant.Constants;
import com.jeethink.common.exception.CustomException;
import com.jeethink.common.utils.SecurityUtils;
import com.jeethink.common.utils.uuid.IdUtils;
import com.jeethink.flow.domain.FlowRecord;
import com.jeethink.flow.service.IFlowService;
import com.jeethink.receive.mapper.ArchivesMapper;
import com.jeethink.receive.service.IArchivesService;
import com.jeethink.system.domain.SystemFile;
import com.jeethink.system.mapper.SysDictDataMapper;
import com.jeethink.system.mapper.SysUserMapper;
import com.jeethink.system.mapper.SystemFileMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.io.File;
import java.util.*;

@Service
public class ArchivesServiceImpl implements IArchivesService {
    private static final Logger log = LoggerFactory.getLogger(ArchivesServiceImpl.class);

    @Autowired
    private ArchivesMapper mapper;
    @Autowired
    private ArchivesFileMapper archivesFileMapper;
    @Autowired
    private IFlowService flowService;
    @Autowired
    private SystemFileMapper fileMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ServerConfig serverConfig;
    /**
     * 获取档案列表
     *
     * @param archives
     * @return
     */
    @Override
    public List<ArchivesMaster> selectArchivesList(ArchivesMaster archives) {

        return mapper.selectArchivesList(archives);
    }

    /**
     * 获取档案详情
     *
     * @param archivesId
     * @return
     */
    @Override
    public ArchivesVO selectArchivesById(String archivesId) {

        ArchivesMaster archives = mapper.selectById(archivesId);
        ArchivesVO vo = new ArchivesVO();
        BeanUtils.copyProperties(archives, vo);

        Map<String, ArchivesFile> fileInfo = selectFileInfo(archives);
        vo.setFileInfo(fileInfo);


        List<FlowRecord> records = new ArrayList<>();
        if (Constants.ARCHIVES_STATUS_RECEIVE_SUBMITED.equals(archives.getNowStatus())
                || Constants.ARCHIVES_STATUS_RECEIVE_APPROVE.equals(archives.getNowStatus())
                || Constants.ARCHIVES_STATUS_RECEIVE_WAIT.equals(archives.getNowStatus())
                || Constants.ARCHIVES_STATUS_RECEIVE_REJECT.equals(archives.getNowStatus())
        ) {
            records = flowService.selectRecord(archivesId, Constants.FLOW_TYPE_RECEIVE);
        }
        if (Constants.ARCHIVES_STATUS_DESTROY_APPLY.equals(archives.getNowStatus())
                || Constants.ARCHIVES_STATUS_DESTROY_APPROVE.equals(archives.getNowStatus())
                || Constants.ARCHIVES_STATUS_DESTROY_WAIT.equals(archives.getNowStatus())
                || Constants.ARCHIVES_STATUS_DESTROY_REJECT.equals(archives.getNowStatus())
        ) {
            records = flowService.selectRecord(archivesId, Constants.FLOW_TYPE_DESTROY);
        }
        vo.setFlowRecords(records);

        return vo;
    }

    private Map<String, ArchivesFile> selectFileInfo(ArchivesMaster archives) {

        String archivesId = archives.getArchivesId();
        String archivesType = archives.getType();
        Map<String, ArchivesFile> fileInfo = new HashMap<>();
        List<ArchivesFile> archivesFileList = archivesFileMapper.selectArchivesFileList(archivesId);
        if(Constants.ARCHIVES_TYPE_JK.equals(archivesType)){
            Date dt=new Date();
            int year = Integer.parseInt(DateUtils.parseDateToStr("yyyy",dt));
            ArchivesFile archivesFileNew = new ArchivesFile();
            /*for(int i=0;i<5;i++){
                fileInfo.put(String.valueOf(year-i),archivesFileNew);
            }*/
            for(int i=2019;i<=year;i++){
                fileInfo.put(String.valueOf(i),archivesFileNew);
            }
        }
        for (ArchivesFile archivesFile : archivesFileList){
            SystemFile systemFile = new SystemFile();
            systemFile.setFileObjectId(archivesFile.getArchivesFileId());
            List<SystemFile> fileList = fileMapper.selectSystemFileList(systemFile);
            archivesFile.setFileList(fileList);
            if (Constants.ARCHIVES_TYPE_RS.equals(archivesType)) {
                fileInfo.put(archivesFile.getRsFileType(), archivesFile);
            }else if (Constants.ARCHIVES_TYPE_JK.equals(archivesType)) {
                fileInfo.put(archivesFile.getJkFileDate(), archivesFile);
            }
        }
        return fileInfo;
    }

    /**
     * 新增档案
     *
     * @param archives
     */
    @Transient
    @Override
    public void insertArchives(ArchivesVO archives) {

        String archivesId= IdUtils.simpleUUID();
        String nowStatus = Constants.ARCHIVES_STATUS_SAVE;
        Boolean isSubmit = archives.getIsSubmit();
        if (isSubmit) {//保存并提交
            if (Constants.ARCHIVES_TYPE_JK.equals(archives.getType())) {//健康档案无需审核
                nowStatus = Constants.ARCHIVES_STATUS_RECEIVE_WAIT;
            } else {//其他需要审核
                Boolean skipApproval = archives.getSkipApproval();
                if (skipApproval) {//是否跳过审批
                    nowStatus = Constants.ARCHIVES_STATUS_RECEIVE_WAIT;
                }else {
                    nowStatus = Constants.ARCHIVES_STATUS_RECEIVE_SUBMITED;
                    //插入审批流程
                    flowService.insertFlow(archivesId, Constants.FLOW_TYPE_RECEIVE);
                }
            }
        }

        ArchivesMaster master = new ArchivesMaster();
        BeanUtils.copyProperties(archives, master);
        master.setName(archives.getUserNumber()+archives.getNickName());
        master.setArchiveNo((Constants.ARCHIVES_TYPE_JK.equals(archives.getType())?"JK":"RS")+archives.getUserNumber());
        master.setArchivesId(archivesId);
        master.setNowStatus(nowStatus);
        master.setCreateUser(SecurityUtils.getUserId());
        master.setUpdateUser(SecurityUtils.getUserId());
        master.setWarehouseType(Constants.WAREHOUSE_TYPE_ZS);

        mapper.insertArchives(master);

        Map<String, ArchivesFile> fileInfo = archives.getFileInfo();
        for (String type : fileInfo.keySet()) {
            ArchivesFile archivesFile = fileInfo.get(type);
            archivesFile.setArchivesFileId(IdUtils.simpleUUID());
            archivesFile.setArchivesId(archivesId);
            if (Constants.ARCHIVES_TYPE_RS.equals(archives.getType())) {
                archivesFile.setRsFileType(type);
            }else if (Constants.ARCHIVES_TYPE_JK.equals(archives.getType())) {
                archivesFile.setJkFileDate(type);
            }
            archivesFile.setDeleted(Constants.DELETED_NO);
            archivesFile.setCreateUser(SecurityUtils.getUserId());
            archivesFile.setCreateTime(DateUtils.getNowDate());
            archivesFileMapper.insertArchivesFile(archivesFile);

            for (SystemFile systemFile: archivesFile.getFileList()) {
                systemFile.setFileObjectId(archivesFile.getArchivesFileId());
                systemFile.setFileDeleteType(Constants.DELETED_NO);
                fileMapper.updateSystemFile(systemFile);
            }
        }
    }

    /**
     * 修改档案
     *
     * @param archives
     */
    @Transactional
    @Override
    public void updateArchives(ArchivesVO archives) {

        ArchivesMaster master = mapper.selectById(archives.getArchivesId());
        //档案能够修改的三种状态：保存未提交，未通过审批，已入库
        if (!Constants.ARCHIVES_STATUS_SAVE.equals(master.getNowStatus())
            && !Constants.ARCHIVES_STATUS_RECEIVE_REJECT.equals(master.getNowStatus())
            && !Constants.ARCHIVES_STATUS_RK.equals(master.getNowStatus())
        ) {
            throw new CustomException("档案当前状态不能修改");
        }
        //审批未通过状态修改会先将上次流程结束
        if (Constants.ARCHIVES_STATUS_RECEIVE_REJECT.equals(archives.getNowStatus())) {
            flowService.destroyFlow(archives.getArchivesId(), Constants.FLOW_TYPE_RECEIVE);
        }

        Boolean isSubmit = archives.getIsSubmit();
        String nowStatus = Constants.ARCHIVES_STATUS_SAVE;
        if (isSubmit) {
            if (Constants.ARCHIVES_TYPE_JK.equals(archives.getType())) {//健康档案无需审核
                nowStatus = Constants.ARCHIVES_STATUS_RECEIVE_WAIT;
            } else {//其他需要审核
                Boolean skipApproval = archives.getSkipApproval();
                if (skipApproval) {//是否跳过审批
                    nowStatus = Constants.ARCHIVES_STATUS_RECEIVE_WAIT;
                }else {
                    nowStatus = Constants.ARCHIVES_STATUS_RECEIVE_SUBMITED;
                    //插入审批流程
                    flowService.insertFlow(archives.getArchivesId(), Constants.FLOW_TYPE_RECEIVE);
                }
            }
        }

        ArchivesMaster archivesMaster = new ArchivesMaster();
        BeanUtils.copyProperties(archives, archivesMaster);
        archivesMaster.setNowStatus(nowStatus);
        archivesMaster.setUpdateUser(SecurityUtils.getUserId());
        mapper.updateArchive(archivesMaster);

        //附件重新保存，删除原数据
        archivesFileMapper.removeByArchivesId(archives.getArchivesId());
        //保存附件
        Map<String, ArchivesFile> fileInfo = archives.getFileInfo();
        for (String type : fileInfo.keySet()) {
            ArchivesFile archivesFile = fileInfo.get(type);
            archivesFile.setArchivesFileId(IdUtils.simpleUUID());
            archivesFile.setArchivesId(archives.getArchivesId());
            if (Constants.ARCHIVES_TYPE_RS.equals(archives.getType())) {
                archivesFile.setRsFileType(type);
            }else if (Constants.ARCHIVES_TYPE_JK.equals(archives.getType())) {
                archivesFile.setJkFileDate(type);
            }
            archivesFile.setDeleted(Constants.DELETED_NO);
            archivesFile.setCreateUser(SecurityUtils.getUserId());
            archivesFile.setCreateTime(DateUtils.getNowDate());
            archivesFileMapper.insertArchivesFile(archivesFile);

            if(archivesFile.getFileList()!=null){
                for (SystemFile systemFile: archivesFile.getFileList()) {
                    systemFile.setFileObjectId(archivesFile.getArchivesFileId());
                    systemFile.setFileDeleteType(Constants.DELETED_NO);
                    fileMapper.updateSystemFile(systemFile);
                }
            }
        }
    }

    /**
     * 提交档案
     *
     * @param archives
     */
    @Transient
    @Override
    public void submitArchives(ArchivesVO archives) {
        ArchivesMaster master = mapper.selectById(archives.getArchivesId());
        if (!Constants.ARCHIVES_STATUS_SAVE.equals(master.getNowStatus())) {
            throw new CustomException("当前档案所出状态不能提交");
        }
        if (Constants.ARCHIVES_TYPE_JK.equals(master.getType()) || archives.getSkipApproval()) {
            //健康档案无需审批，其他档案可跳过审批
            mapper.changeNowStatus(archives.getArchivesId(), Constants.ARCHIVES_STATUS_RECEIVE_WAIT);
        }else {
            mapper.changeNowStatus(archives.getArchivesId(), Constants.ARCHIVES_STATUS_RECEIVE_SUBMITED);
            flowService.insertFlow(archives.getArchivesId(), Constants.FLOW_TYPE_RECEIVE);
        }
    }

    /**
     * 撤销提交
     *
     * @param archivesId
     */
    @Transient
    @Override
    public void revocationArchives(String archivesId) {

        ArchivesMaster master = mapper.selectById(archivesId);
        if (!Constants.ARCHIVES_STATUS_RECEIVE_SUBMITED.equals(master.getNowStatus())) {
            throw new CustomException("该档案已经不能撤销提交");
        }
        mapper.changeNowStatus(archivesId, Constants.ARCHIVES_STATUS_SAVE);
        flowService.destroyFlow(archivesId, Constants.FLOW_TYPE_RECEIVE);
    }

    /**
     * 删除档案
     *
     * @param archivesId
     * @return
     */
    @Override
    public int removeArchives(String archivesId) {

        ArchivesMaster master = mapper.selectById(archivesId);
        if (!Constants.ARCHIVES_STATUS_SAVE.equals(master.getNowStatus())
           && !Constants.ARCHIVES_STATUS_RECEIVE_REJECT.equals(master.getNowStatus())) {
            throw new CustomException("该档案已经不能删除");
        }
        return mapper.romoveById(archivesId);
    }

    @Override
    public void changeNowStatus(String archivesId, String nowStatus) {

        mapper.changeNowStatus(archivesId, nowStatus);
    }

    @Override
    public void uploadArchiveFileList() {
        String path = "E:\\jeethink\\uploadPath\\uploadFiles";
        List<File> list = new ArrayList<>();
        list = FileUtils.getFiles(path,list);
        for (File file:list){
            String filePath = file.getPath();
            String[] filePathArray=filePath.split("\\\\");
            String archiveType = filePathArray[filePathArray.length-4];
            String userNum = filePathArray[filePathArray.length-3];
            String fileType = filePathArray[filePathArray.length-2];
            String fileName = filePathArray[filePathArray.length-1];
/*            if("person".equals(archiveType)){//人事档案目录
                ArchivesMaster archivesMaster = new ArchivesMaster();
                archivesMaster.setUserNumber(userNum);
                archivesMaster.setType(Constants.ARCHIVES_TYPE_RS);
                List<ArchivesMaster> archiveList = mapper.selectArchivesByUser(archivesMaster);
                if(archiveList.size()>0){
                    ArchivesMaster rsArchive = archiveList.get(0);
                    List<ArchivesFile> archivesFileList = archivesFileMapper.selectArchivesFileList(rsArchive.getArchivesId());
                    for (ArchivesFile archivesFile:archivesFileList) {
                        if(fileType.equals(archivesFile.getRsFileType())){
                            SystemFile systemFile = new SystemFile();
                            systemFile.setFileId(IdUtils.simpleUUID());
                            systemFile.setFileObjectId(archivesFile.getArchivesFileId());
                            systemFile.setFileSize(file.length());
                            String[] fileNames = fileName.split("\\.");
                            String fileName1 = fileName.split("\\.")[0];
                            String fileName2 = fileName.split("\\.")[1];
                            systemFile.setFileName(fileName.split("\\.")[0]);
                            systemFile.setFileSuffix(fileName.split("\\.")[1]);
                            systemFile.setFilePath(filePath);
//                          String fileUrl = serverConfig.getUrl()+"/profile/"+filePathArray[filePathArray.length-6]+"/"+filePathArray[filePathArray.length-5]+"/"+archiveType+"/"+userNum+"/"+fileType+"/"+fileName;
                            String fileUrl = "http://10.10.10.141:9001/profile/"+filePathArray[filePathArray.length-6]+"/"+filePathArray[filePathArray.length-5]+"/"+archiveType+"/"+userNum+"/"+fileType+"/"+fileName;
                            systemFile.setFileUrl(fileUrl);
                            systemFile.setFileDeleteType(Constants.DELETED_NO);
                            systemFile.setFileCreateTime(DateUtils.getNowDate());
                            systemFile.setCreateUserId(SecurityUtils.getUserId());
                            fileMapper.insertSystemFile(systemFile);
                        }
                    }
                }
            }*/
            if("2021".equals(fileType)){
                if("health".equals(archiveType)){//健康档案目录
                    ArchivesMaster archivesMaster = new ArchivesMaster();
                    archivesMaster.setUserNumber(userNum);
                    archivesMaster.setType(Constants.ARCHIVES_TYPE_JK);
                    List<ArchivesMaster> archiveList = mapper.selectArchivesByUser(archivesMaster);
                    if(archiveList.size()>0){
                        ArchivesMaster rsArchive = archiveList.get(0);
                        List<ArchivesFile> archivesFileList = archivesFileMapper.selectArchivesFileList(rsArchive.getArchivesId());
                        for (ArchivesFile archivesFile:archivesFileList) {
                            if(fileType.equals(archivesFile.getJkFileDate())){
                                SystemFile systemFile = new SystemFile();
                                systemFile.setFileId(IdUtils.simpleUUID());
                                systemFile.setFileObjectId(archivesFile.getArchivesFileId());
                                systemFile.setFileSize(file.length());
                                systemFile.setFileName(fileName.split("\\.")[0]);
                                systemFile.setFileSuffix(fileName.split("\\.")[1]);
                                systemFile.setFilePath(filePath);
                                systemFile.setFileCreateTime(DateUtils.getNowDate());
//                            String fileUrl = serverConfig.getUrl()+"/profile"+"/"+filePathArray[filePathArray.length-5]+"/"+archiveType+"/"+userNum+"/"+fileType+"/"+fileName;
                                String fileUrl = "http://10.10.10.141:9001/profile/"+filePathArray[filePathArray.length-6]+"/"+filePathArray[filePathArray.length-5]+"/"+archiveType+"/"+userNum+"/"+fileType+"/"+fileName;
                                systemFile.setFileUrl(fileUrl);
                                systemFile.setFileDeleteType(Constants.DELETED_NO);
                                systemFile.setCreateUserId(SecurityUtils.getUserId());
                                fileMapper.insertSystemFile(systemFile);
                            }
                        }
                    }
                }
            }


        }
    }

    @Override
    public String importArchives(List<ArchivesMaster> archivesMasterList, boolean updateSupport) {
        if (StringUtils.isNull(archivesMasterList) || archivesMasterList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ArchivesMaster archivesMaster : archivesMasterList){
            try{
                // 验证是否存在这个用户
                String userNumber = archivesMaster.getUserNumber();
                SysUser u = userMapper.selectUserByUserNumber(userNumber);
                if (StringUtils.isNotNull(u)){
                    archivesMaster.setArchivesId(IdUtils.simpleUUID());
                    archivesMaster.setArchiveNo((Constants.ARCHIVES_TYPE_JK.equals(archivesMaster.getType())?"JK":"RS")+archivesMaster.getUserNumber());
                    archivesMaster.setName(archivesMaster.getUserNumber()+archivesMaster.getNickName());
                    archivesMaster.setDeleted(Constants.DELETED_NO);
                    archivesMaster.setCreateUser(SecurityUtils.getUserId());
                    archivesMaster.setCreateTime(DateUtils.getNowDate());
                    archivesMaster.setUserId(u.getUserId());
                    archivesMaster.setNowStatus(Constants.ARCHIVES_STATUS_RK);
                    archivesMaster.setGdPeopleId(SecurityUtils.getUserId());
                    archivesMaster.setType(Constants.ARCHIVES_TYPE_RS);
                    archivesMaster.setWarehouseType(Constants.WAREHOUSE_TYPE_ZS);
                    archivesMaster.setTier("0");
                    archivesMaster.setIfPaper("0");
                    archivesMaster.setPeopleType("2");
                    archivesMaster.setClassified("0");
                    archivesMaster.setDuration("0");
                    mapper.insertArchives(archivesMaster);
                    for (int i = 0; i <= 10; i++) {
                        ArchivesFile archivesFile = new ArchivesFile();
                        archivesFile.setArchivesFileId(IdUtils.simpleUUID());
                        archivesFile.setArchivesId(archivesMaster.getArchivesId());
                        archivesFile.setRsFileType(String.valueOf(i));
                        archivesFile.setDeleted(Constants.DELETED_NO);
                        archivesFile.setCreateUser(SecurityUtils.getUserId());
                        archivesFile.setCreateTime(DateUtils.getNowDate());
                        archivesFileMapper.insertArchivesFile(archivesFile);
                    }
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、员工编号 " + archivesMaster.getUserNumber() + " 的人事档案导入成功");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、员工编号 " + archivesMaster.getUserNumber() + " 的人事档案导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public String importArchives2(List<ArchivesMaster> archivesMasterList, boolean updateSupport) {
        if (StringUtils.isNull(archivesMasterList) || archivesMasterList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ArchivesMaster archivesMaster : archivesMasterList){
            try{
                // 验证是否存在这个用户
                String userNumber = archivesMaster.getUserNumber();
                SysUser u = userMapper.selectUserByUserNumber(userNumber);
                if (StringUtils.isNotNull(u)){
                    ArchivesFile archivesFile = new ArchivesFile();
                    ArchivesMaster m=mapper.selectJk(userNumber);
                    if(StringUtils.isNull(m)){
                        archivesMaster.setArchivesId(IdUtils.simpleUUID());
                        archivesMaster.setArchiveNo("JK"+archivesMaster.getUserNumber());
                        archivesMaster.setName(archivesMaster.getUserNumber()+archivesMaster.getNickName());
                        archivesMaster.setDeleted(Constants.DELETED_NO);
                        archivesMaster.setCreateUser(SecurityUtils.getUserId());
                        archivesMaster.setCreateTime(DateUtils.getNowDate());
                        archivesMaster.setUserId(u.getUserId());
                        archivesMaster.setNowStatus(Constants.ARCHIVES_STATUS_RK);
                        archivesMaster.setGdPeopleId(SecurityUtils.getUserId());
                        archivesMaster.setType(Constants.ARCHIVES_TYPE_JK);
                        archivesMaster.setWarehouseType(Constants.WAREHOUSE_TYPE_ZS);
                        archivesMaster.setTier("0");
                        archivesMaster.setIfPaper("0");
                        archivesMaster.setPeopleType("2");
                        archivesMaster.setClassified("0");
                        archivesMaster.setDuration("0");
                        mapper.insertArchives(archivesMaster);
                        archivesFile.setArchivesId(archivesMaster.getArchivesId());
                        archivesFile.setArchivesFileId(IdUtils.simpleUUID());
                        archivesFile.setJkFileDate(archivesMaster.getJkFileDate());
                        archivesFile.setCheckConclusion(archivesMaster.getCheckConclusion());
                        archivesFile.setCheckDate(archivesMaster.getCheckDate());
                        archivesFile.setCheckHospital(archivesMaster.getCheckHospital());
                        archivesFile.setDeleted(Constants.DELETED_NO);
                        archivesFile.setCreateUser(SecurityUtils.getUserId());
                        archivesFile.setCreateTime(DateUtils.getNowDate());
                        archivesFileMapper.insertArchivesFile(archivesFile);
                    }else {
                        archivesFile.setArchivesId(m.getArchivesId());
                        archivesFile.setArchivesFileId(IdUtils.simpleUUID());
                        archivesFile.setJkFileDate(archivesMaster.getJkFileDate());
                        archivesFile.setCheckConclusion(archivesMaster.getCheckConclusion());
                        archivesFile.setCheckDate(archivesMaster.getCheckDate());
                        archivesFile.setCheckHospital(archivesMaster.getCheckHospital());
                        archivesFile.setDeleted(Constants.DELETED_NO);
                        archivesFile.setCreateUser(SecurityUtils.getUserId());
                        archivesFile.setCreateTime(DateUtils.getNowDate());
                        archivesFileMapper.insertArchivesFile(archivesFile);
                    }


                    successNum++;
                    /*successMsg.append("<br/>" + successNum + "、员工编号 " + archivesMaster.getUserNumber() + " 的健康档案导入成功");*/
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、员工编号 " + archivesMaster.getUserNumber() + " 的健康档案导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public ArchivesMaster selectById(String archivesId) {
        return mapper.selectById(archivesId);
    }

    @Override
    public String selectArchiveFileId(String userNumber, String type) {
        return archivesFileMapper.selectArchiveFileId(userNumber,type);
    }
    @Override
    public List<HashMap> selectArchivesFileById(String archivesId) {
        return archivesFileMapper.selectArchivesFileById(archivesId);
    }
}
