管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-01-11 ce82c3290ed4a544f76aa55e82b68e2c57ffb9c2
src/main/java/com/lf/server/service/data/UploadService.java
@@ -1,20 +1,20 @@
package com.lf.server.service.data;
import com.lf.server.entity.all.BaseEntity;
import com.lf.server.entity.all.StaticData;
import com.lf.server.entity.data.CoordEntity;
import com.lf.server.entity.data.DirEntity;
import com.lf.server.entity.data.FmeLogEntity;
import com.lf.server.entity.data.MetaFileEntity;
import com.lf.server.entity.data.*;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.StringHelper;
import com.lf.server.helper.*;
import com.lf.server.mapper.all.BasicMapper;
import com.lf.server.mapper.data.UploadMapper;
import com.lf.server.service.all.BaseQueryService;
import com.lf.server.service.all.BaseUploadService;
import org.apache.commons.collections4.queue.PredicatedQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
@@ -29,6 +29,9 @@
    @Autowired
    FmeService fmeService;
    @Autowired
    MetaService metaService;
    @Override
    public List<CoordEntity> selectCoords(String zoning) {
@@ -53,40 +56,342 @@
    /**
     * 插入文件
     */
    public void insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) {
    public List<MetaEntity> insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) {
        checkMetaFiles(ue, list);
        loadExcel(list, req);
        excelLoader(list, req);
        return dataLoader(list);
    }
    /**
     * 检查元数据文件
     */
    private void checkMetaFiles(UserEntity ue, List<MetaFileEntity> list) {
        Timestamp createTime = WebHelper.getCurrentTimestamp();
        String tempPath = pathHelper.getConfig().getTempPath();
        for (MetaFileEntity mf : list) {
            mf.setCreateUser(ue.getId());
            mf.setCreateTime(createTime);
            mf.setDepid(ue.getDepid());
            mf.setPath(tempPath + File.separator + mf.getPath());
            File f = new File(tempPath + File.separator + mf.getPath());
            File f = new File(mf.getPath());
            if (!f.exists()) {
                mf.setMsg("文件丢失");
                mf.setRows(-1);
                mf.setMsg("文件不存在");
            }
        }
    }
    /**
     * Excel入库
     * 加载数据
     */
    private void loadExcel(List<MetaFileEntity> list, HttpServletRequest req) {
        List<MetaFileEntity> xlsList = getExcelFiles(list);
        if (xlsList.isEmpty()) {
    private List<MetaEntity> dataLoader(List<MetaFileEntity> list) {
        loadData(list);
        copyFiles(list);
        return insertMetas(list);
    }
    /**
     * 加载数据
     */
    private void loadData(List<MetaFileEntity> list) {
        for (MetaFileEntity mf : list) {
            if (StringHelper.isEmpty(mf.getEntity())) {
                continue;
            }
            if (StaticData.SHP.equals(mf.getExtName()) || StaticData.GDB.equals(mf.getExtName())) {
                loadSpatialData(mf);
            }
        }
    }
    /**
     * 加载空间数据
     */
    private void loadSpatialData(MetaFileEntity mf) {
        BasicMapper basicMapper = ClassHelper.getBasicMapper(mf.getEntity());
        if (null == basicMapper) {
            return;
        }
        MetaFileEntity meta = getExcelMeta(xlsList);
        String guid = fmeService.excelLoader(meta, req);
        String tabName = BaseQueryService.getTabName(basicMapper);
        String className = ClassHelper.getClassName(basicMapper);
        Class clazz = ClassHelper.getEntityClass(className);
        if (null == clazz || null == tabName) {
            return;
        }
        List<?> list = null;
        if (StaticData.SHP.equals(mf.getExtName())) {
            list = ShpHelper.readData(clazz, mf.getPath());
        } else {
            list = GdbHelper.readData(clazz, mf.getPath(), mf.getTab());
        }
        if (null == list || list.isEmpty()) {
            return;
        }
        setCreateInfo(list, mf);
        int rows = basicMapper.insertBatch(list);
        if (rows > 0) {
            mf.setTab(tabName);
            mf.setRows(rows);
        }
    }
    /**
     * 设置创建信息
     */
    private <T> void setCreateInfo(List<T> list, MetaFileEntity mf) {
        try {
            if (!(list.get(0) instanceof BaseEntity)) {
                return;
            }
            for (T t : list) {
                BaseEntity be = (BaseEntity) t;
                be.setEventid(StringHelper.getGuid());
                be.setParentid(mf.getEventid());
                be.setDirid(mf.getDirid());
                be.setDepid(mf.getDepid());
                be.setVerid(mf.getVerid());
                be.setCreateuser(mf.getCreateUser());
                be.setCreatetime(mf.getCreateTime());
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
    /**
     * 复制文件
     */
    private void copyFiles(List<MetaFileEntity> list) {
        List<String> gdbList = new ArrayList<>();
        for (MetaFileEntity mf : list) {
            switch (mf.getExtName()) {
                case StaticData.MPT:
                    copyMultiFile(mf, StaticData.MPT_EXT);
                    break;
                case StaticData.IMG:
                    copyMultiFile(mf, StaticData.IMG_EXT);
                    break;
                case StaticData.TIF:
                    copyMultiFile(mf, StaticData.TIF_EXT);
                    break;
                case StaticData.TIFF:
                    copyMultiFile(mf, StaticData.TIFF_EXT);
                    break;
                case StaticData.SHP:
                    copyMultiFile(mf, StaticData.SHP_EXT);
                    break;
                case StaticData.GDB:
                    if (gdbList.contains(mf.getPath())) {
                        String path = findPathByGuid(list, mf);
                        if (null != path) {
                            mf.setPath(path);
                        }
                        continue;
                    }
                    gdbList.add(mf.getPath());
                    copyFolderFile(mf);
                    break;
                default:
                    copySingleFile(mf);
                    break;
            }
        }
    }
    /**
     * 复制单个文件
     */
    private int copySingleFile(MetaFileEntity mf) {
        File file = new File(mf.getPath());
        if (!file.exists()) {
            mf.setMsg("文件丢失");
            return -1;
        }
        MetaEntity old = metaService.selectByGuid(mf.getGuid());
        if (null != old) {
            mf.setMsg("文件已存在");
            file.delete();
            return 0;
        }
        String uploadPath = pathHelper.getUploadFullPath();
        String targetPath = uploadPath + File.separator + mf.getGuid() + mf.getExtName();
        File newFile = new File(targetPath);
        if (newFile.exists()) {
            mf.setMsg("文件已存在");
            file.delete();
            return 0;
        }
        file.renameTo(newFile);
        String subPath = FileHelper.getRelativePath(targetPath);
        mf.setPath(subPath);
        return 1;
    }
    /**
     * 复制多个文件
     */
    private void copyMultiFile(MetaFileEntity mf, List<String> extList) {
        String path = mf.getPath();
        int status = copySingleFile(mf);
        if (status < 1) {
            for (int i = 0, c = extList.size(); i < c; i++) {
                String subPath = path.replace(mf.getExtName(), extList.get(i));
                File file = new File(subPath);
                if (file.exists()) {
                    file.delete();
                }
            }
            return;
        }
        String uploadPath = pathHelper.getConfig().getUploadPath();
        for (int i = 0, c = extList.size(); i < c; i++) {
            String sourcePath = path.replace(mf.getExtName(), extList.get(i));
            File file = new File(sourcePath);
            if (!file.exists()) {
                continue;
            }
            String targetPath = uploadPath + File.separator + mf.getPath().replace(mf.getExtName(), extList.get(i));
            File newFile = new File(targetPath);
            if (newFile.exists()) {
                continue;
            }
            file.renameTo(newFile);
        }
    }
    /**
     * 复制文件夹文件
     */
    private void copyFolderFile(MetaFileEntity mf) {
        File file = new File(mf.getPath());
        if (!file.exists()) {
            mf.setMsg("文件丢失");
            return;
        }
        MetaEntity old = metaService.selectByGuid(mf.getGuid());
        if (null != old) {
            mf.setMsg("文件已存在");
            FileHelper.deleteFiles(file);
            return;
        }
        String uploadPath = pathHelper.getUploadFullPath();
        String targetPath = uploadPath + File.separator + mf.getGuid() + mf.getExtName();
        File newFile = new File(targetPath);
        if (newFile.exists() && newFile.isDirectory()) {
            mf.setMsg("文件已存在");
            FileHelper.deleteFiles(file);
            return;
        }
        newFile.mkdirs();
        File[] files = file.listFiles();
        if (null == files || files.length == 0) {
            return;
        }
        for (File f : files) {
            String subPath = targetPath + File.separator + FileHelper.getFileName(f.getPath());
            f.renameTo(new File(subPath));
        }
        String subPath = FileHelper.getRelativePath(targetPath);
        mf.setPath(subPath);
    }
    /**
     * 根据GUID查找路径
     */
    private String findPathByGuid(List<MetaFileEntity> list, MetaFileEntity mf) {
        for (MetaFileEntity meta : list) {
            if (meta.getGuid().equals(mf.getGuid())) {
                return meta.getPath();
            }
        }
        return null;
    }
    /**
     * 插入元数据
     */
    private List<MetaEntity> insertMetas(List<MetaFileEntity> list) {
        List<MetaEntity> metas = new ArrayList<>();
        for (MetaFileEntity mf : list) {
            if (StringHelper.isEmpty(mf.getMsg())) {
                metas.add(createMeta(mf));
            }
        }
        if (metas.isEmpty()) {
            return null;
        }
        int rows = metaService.inserts(metas);
        return rows > 0 ? metas : null;
    }
    /**
     * 创建元数据
     */
    private MetaEntity createMeta(MetaFileEntity mf) {
        MetaEntity me = new MetaEntity();
        me.setEventid(mf.getEventid());
        me.setDirid(mf.getDirid());
        me.setDepid(mf.getDepid());
        me.setVerid(mf.getVerid());
        me.setName(mf.getName());
        me.setType(mf.getType());
        me.setGuid(mf.getGuid());
        me.setPath(mf.getPath());
        me.setSizes(mf.getSizes());
        me.setTab(mf.getTab());
        me.setRows(mf.getRows());
        me.setCreateUser(mf.getCreateUser());
        me.setCreateTime(mf.getCreateTime());
        return me;
    }
    /**
     * Excel入库
     */
    private String excelLoader(List<MetaFileEntity> list, HttpServletRequest req) {
        List<MetaFileEntity> xlsList = getExcelFiles(list);
        if (xlsList.isEmpty()) {
            return "";
        }
        String guid = null;
        try {
            MetaFileEntity meta = getExcelMeta(xlsList);
            guid = fmeService.excelLoader(meta, req);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        return guid;
    }
    /**
@@ -112,9 +417,8 @@
        List<String> dirList = new ArrayList<>();
        List<String> pidList = new ArrayList<>();
        String tempPath = pathHelper.getConfig().getTempPath();
        for (MetaFileEntity mf : xlsList) {
            pathList.add(tempPath + File.separator + mf.getPath());
            pathList.add(mf.getPath());
            dirList.add(mf.getDirid() + "");
            pidList.add(mf.getEventid());
        }