管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-09-18 a338dc252ced3f019f74a710153c28cb0a9329a1
src/main/java/com/lf/server/service/data/UploadService.java
@@ -1,5 +1,6 @@
package com.lf.server.service.data;
import com.google.common.collect.Lists;
import com.lf.server.entity.all.BaseEntity;
import com.lf.server.entity.all.StaticData;
import com.lf.server.entity.data.*;
@@ -16,6 +17,10 @@
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
@@ -94,12 +99,26 @@
            if (!f.exists()) {
                mf.setMsg("文件丢失");
            }
            if (f.exists() && StringHelper.isNull(mf.getGuid())) {
                mf.setGuid(FileHelper.getFileMd5(f.getPath()));
            }
            MetaEntity old = metaService.selectByGuid(mf.getGuid(), null);
            MetaEntity old = metaService.selectByGuid(mf.getGuid(), getDirCode(mf), null);
            if (null != old) {
                mf.setMsg("已存在");
            }
        }
    }
    /**
     * 获取目录编码
     */
    private String getDirCode(MetaFileEntity mf) {
        if (StringHelper.isEmpty(mf.getDircode())) {
            return null;
        }
        return StringHelper.getRightLike(mf.getDircode().substring(0, 2));
    }
    /**
@@ -208,7 +227,7 @@
            return;
        }
        MetaEntity old = metaService.selectByGuid(mf.getGuid(), tabName);
        MetaEntity old = metaService.selectByGuid(mf.getGuid(), getDirCode(mf), tabName);
        if (null != old) {
            mf.setMsg("已存在");
            return;
@@ -216,21 +235,40 @@
        List<?> list;
        if (StaticData.SHP.equals(mf.getExtName())) {
            list = ShpHelper.readData(clazz, mf.getPath());
            list = ShpHelper.readData(clazz, mf.getPath(), true);
        } else {
            list = GdbHelper.readData(clazz, mf.getPath(), mf.getTab());
            list = GdbHelper.readData(clazz, mf.getPath(), mf.getTab(), true);
        }
        if (null == list || list.isEmpty()) {
            return;
        }
        mf.setRecords(list.size());
        setCreateInfo(list, mf);
        int rows = basicMapper.insertBatch(list);
        int rows = batchInserts(basicMapper, list);
        if (rows > 0) {
            mf.setEntity(mf.getTab());
            mf.setTab(tabName);
            mf.setRows(rows);
        }
    }
    /**
     * 批量插入
     */
    private <T> int batchInserts(BasicMapper basicMapper, List<T> list) {
        int rows = 0;
        //int count = (int) Math.ceil(list.size() / StaticData.D100)
        List<List<T>> subLists = Lists.partition(list, StaticData.I50);
        for (List<T> sub : subLists) {
            try {
                rows += basicMapper.insertBatch(sub);
            } catch (Exception ex) {
                log.error(ex);
            }
        }
        return rows;
    }
    /**
@@ -286,6 +324,9 @@
                case StaticData.SHP:
                    copyMultiFile(mf, StaticData.SHP_EXT);
                    break;
                case StaticData.OSGB:
                    copyFolderFile(mf);
                    break;
                case StaticData.GDB:
                    if (gdbList.contains(mf.getPath())) {
                        String path = findPathByGuid(list, mf);
@@ -315,9 +356,10 @@
            return -1;
        }
        MetaEntity old = metaService.selectByGuid(mf.getGuid(), null);
        MetaEntity old = metaService.selectByGuid(mf.getGuid(), null, null);
        if (null != old) {
            mf.setMsg("已存在");
            // mf.setMsg("已存在")
            setOldMeta(mf, old);
            file.delete();
            return 0;
        }
@@ -337,6 +379,16 @@
        mf.setPath(subPath);
        return 1;
    }
    /**
     * 设置旧元数据信息
     */
    private void setOldMeta(MetaFileEntity mf, MetaEntity old) {
        mf.setPath(old.getPath());
        mf.setTab(old.getTab());
        mf.setRows(old.getRows());
        mf.setEntity(old.getEventid());
    }
    /**
@@ -384,9 +436,10 @@
            return;
        }
        MetaEntity old = metaService.selectByGuid(mf.getGuid(), null);
        MetaEntity old = metaService.selectByGuid(mf.getGuid(), null, null);
        if (null != old) {
            mf.setMsg("已存在");
            // mf.setMsg("已存在")
            setOldMeta(mf, old);
            FileHelper.deleteFiles(file);
            return;
        }
@@ -402,15 +455,11 @@
            return;
        }
        newFile.mkdirs();
        File[] files = file.listFiles();
        if (null == files || files.length == 0) {
            return;
        }
        for (File f : files) {
            String subFile = targetPath + File.separator + FileHelper.getFileName(f.getPath());
            f.renameTo(new File(subFile));
        try {
            // newFile.mkdirs()
            FileUtils.moveDirectory(file, newFile);
        } catch (Exception ex) {
            log.error(ex);
        }
        mf.setPath(subPath);
    }
@@ -441,15 +490,25 @@
     */
    private void insertMetas(List<MetaFileEntity> list) {
        int metaId = insertParentMeta(list);
        List<String> guids = new ArrayList<>();
        for (MetaFileEntity mf : list) {
            if (null != mf.getMsg()) {
                continue;
            }
            MetaEntity me = createMeta(mf, metaId);
            metaService.insert(me);
            if (StaticData.NGDB.equals(me.getType())) {
                if (guids.contains(me.getGuid())) {
                    me.setIsmeta((short) -1);
                } else {
                    guids.add(me.getGuid());
                }
            }
            mf.setMsg(me.getId() > 0 ? "成功" : "失败");
            metaService.insert(me);
            String err = mf.getRows() < mf.getRecords() ? "(" + (mf.getRecords() - mf.getRows()) + " 条失败)" : "";
            mf.setMsg(me.getId() > 0 ? String.format("成功%s", err) : "失败");
        }
    }
@@ -550,4 +609,136 @@
        return meta;
    }
    /**
     * 插入KML文件
     */
    public void insertKml(UserEntity ue, List<MetaFileEntity> list) {
        checkMetaFiles(ue, list);
        loadKml(list);
        copyFiles(list);
        insertMetas(list);
    }
    /**
     * 加载Kml
     */
    private void loadKml(List<MetaFileEntity> list) {
        for (MetaFileEntity mf : list) {
            if (null != mf.getMsg() || StringHelper.isEmpty(mf.getEntity()) || !StaticData.KML.equals(mf.getExtName())) {
                continue;
            }
            String wkt = readKml(mf.getPath());
            if (StringHelper.isEmpty(wkt)) {
                continue;
            }
            loadKmlData(mf, wkt);
        }
    }
    /**
     * 读取KML
     */
    private String readKml(String path) {
        try {
            List<String> list = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8);
            for (String str : list) {
                if (!str.contains("<coordinates>")) {
                    continue;
                }
                return getWktFromCoordinates(str);
            }
            return null;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
    /**
     * 获取KWT
     */
    private String getWktFromCoordinates(String coords) {
        String[] strs = coords.replace("\t", "").replace("<coordinates>", "").replace("</coordinates>", "").split(" ");
        if (strs.length == 0) {
            return null;
        }
        // MULTILINESTRING((108.99843373100003 34.736292580000054,108.99818970700005 34.73764945000005,108.99831780400007 34.738880547000065,108.99812184300004 34.739564029000064))
        StringBuilder sb = new StringBuilder();
        sb.append("MULTILINESTRING((");
        for (String str : strs) {
            if (StringHelper.isEmpty(str)) {
                continue;
            }
            String[] vals = str.split(StaticData.COMMA);
            sb.append(vals[0]).append(" ").append(vals[1]).append(StaticData.COMMA);
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("))");
        return sb.toString();
    }
    /**
     * 加载Kml数据
     */
    private void loadKmlData(MetaFileEntity mf, String wkt) {
        BasicMapper basicMapper = ClassHelper.getBasicMapper(mf.getEntity());
        if (null == basicMapper) {
            return;
        }
        String tabName = BaseQueryService.getTabName(basicMapper);
        String className = ClassHelper.getClassName(basicMapper);
        Class clazz = ClassHelper.getEntityClass(className);
        if (null == clazz || null == tabName) {
            return;
        }
        String name = FileHelper.getName(mf.getPath());
        List<?> list = createKmlEntity(clazz, wkt, name);
        if (null == list || list.isEmpty()) {
            return;
        }
        mf.setRecords(list.size());
        setCreateInfo(list, mf);
        int rows = batchInserts(basicMapper, list);
        if (rows > 0) {
            mf.setEntity(mf.getTab());
            mf.setTab(tabName);
            mf.setRows(rows);
        }
    }
    /**
     * 创建KML实体类
     */
    private <T> List<T> createKmlEntity(Class clazz, String wkt, String name) {
        try {
            T t = (T) clazz.newInstance();
            Field gField = clazz.getSuperclass().getDeclaredField("geom");
            gField.setAccessible(true);
            gField.set(t, null == wkt ? "null" : String.format("ST_GeomFromText('%s')", wkt));
            Field pField = clazz.getDeclaredField("pipename");
            pField.setAccessible(true);
            pField.set(t, name);
            List<T> list = new ArrayList<>();
            list.add(t);
            return list;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
}