package com.lf.server.service.data; import com.lf.server.entity.all.BaseEntity; import com.lf.server.entity.ctrl.TabMapperEntity; import com.lf.server.entity.data.MetaEntity; import com.lf.server.helper.*; import com.lf.server.mapper.all.BasicMapper; import com.lf.server.mapper.all.GeomBaseMapper; import com.lf.server.service.all.BaseQueryService; import com.lf.server.service.all.BaseUploadService; import org.apache.commons.text.StringEscapeUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.File; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; /** * 数据入库 * @author WWW */ @Service public class DataLoaderService extends BaseUploadService { @Autowired MetaService metaService; private final static String XLS = ".xls"; private final static String MDB = ".mdb"; private final static String GDB = ".gdb"; private final static String SHP_ZIP = ".shp.zip"; private final static String GDB_ZIP = ".gdb.zip"; private final static String ZIP = ".zip"; /** * 查询映射 */ public List selectMappers(String subPath) { String root = pathHelper.getConfig().getTempPath() + File.separator + subPath; File file = new File(root); if (!file.exists() && !file.isDirectory()) { return null; } File[] files = file.listFiles(); if (null == files || files.length == 0) { return null; } File zipFile = new File(root + "_zip"); if (!zipFile.exists() || !zipFile.isDirectory()) { zipFile.mkdirs(); } return getMappers(zipFile.getPath(), files); } /** * 获取映射 */ private List getMappers(String zipPath, File[] files) { String temp = pathHelper.getConfig().getTempPath(); List list = new ArrayList<>(); for (File f : files) { String fileName = FileHelper.getFileName(f.getPath()); if (fileName.contains(XLS)) { String path = f.getPath().replace(temp + File.separator, ""); list.add(new TabMapperEntity(fileName, "xls", null, path)); continue; } if (fileName.contains(MDB)) { String path = f.getPath().replace(temp + File.separator, ""); List tabs = MdbHelper.getTabNames(f.getPath()); for (String tab : tabs) { list.add(new TabMapperEntity(fileName, "mdb", tab, path)); } continue; } if (fileName.contains(SHP_ZIP)) { String subPath = zipPath + File.separator + f.getName().toLowerCase().replace(".zip", ""); ZipHelper.unzip(f.getPath(), subPath); getShpFiles(f.getName(), subPath, list); continue; } if (fileName.contains(GDB_ZIP)) { String subPath = zipPath + File.separator + f.getName().toLowerCase().replace(".zip", ""); ZipHelper.unzip(f.getPath(), subPath); getGdbFiles(f.getName(), subPath, list); continue; } if (fileName.contains(ZIP)) { // 暂时不实现 } } return list; } /** * 获取Shp文件 */ private void getShpFiles(String sourceName, String subPath, List list) { List files = new ArrayList<>(); getShpFiles(subPath, files); String root = pathHelper.getConfig().getTempPath() + File.separator; for (String file : files) { String name = FileHelper.getFileName(file); String path = file.replace(root, ""); list.add(new TabMapperEntity(sourceName, "shp", name, path)); } } /** * 获取Shp文件 */ private void getShpFiles(String shpPath, List list) { File file = new File(shpPath); File[] files = file.listFiles(); if (null == files || files.length == 0) { return; } for (File f : files) { if (f.isDirectory()) { getShpFiles(f.getPath(), list); continue; } if (f.getName().toLowerCase().endsWith(".shp")) { list.add(f.getPath()); } } } /** * 获取Gdb文件 */ private void getGdbFiles(String sourceName, String subPath, List list) { List files = new ArrayList<>(); getGdbFiles(subPath, files); String root = pathHelper.getConfig().getTempPath() + File.separator; for (String file : files) { String path = file.replace(root, ""); List tabs = GdbHelper.getTabNames(file); for (String tab : tabs) { list.add(new TabMapperEntity(sourceName, "gdb", tab, path)); } } } /** * 获取Gdb文件 */ private void getGdbFiles(String shpPath, List list) { File file = new File(shpPath); File[] files = file.listFiles(); if (null == files || files.length == 0) { return; } for (File f : files) { if (!f.isDirectory()) { continue; } if (isGdbFile(f)) { list.add(f.getPath()); continue; } getGdbFiles(f.getPath(), list); } } private boolean isGdbFile(File f) { if (f.getName().toLowerCase().endsWith(GDB)) { File[] files = f.listFiles(); if (null == files || files.length == 0) { return false; } for (File file : files) { if ("gdb".equals(file.getName())) { return true; } } } return false; } /** * 插入文件 */ public void insertFiles(MetaEntity entity, List list, List tabList) { try { String temp = pathHelper.getConfig().getTempPath(); String upload = pathHelper.getUploadFullPath(); for (MetaEntity mf : list) { File file = new File(temp + File.separator + mf.getPath()); File newFile = new File(upload + File.separator + mf.getGuid()); String type = getType(mf.getName().toLowerCase()); if (null == type) { file.delete(); continue; } // 获取表映射 List tabs = getTabs(mf, tabList); if (tabs.isEmpty()) { file.delete(); continue; } MetaEntity old = metaService.selectByGuid(mf.getGuid()); String filePath = null == old ? newFile.getPath() : old.getPath(); int count = 0; for (TabMapperEntity tab : tabs) { if (null == tab || StringHelper.isEmpty(tab.getEntity())) { return; } switch (type) { case "shp": case "gdb": case "mdb": insertDb(type, temp, entity, tab); break; // case "xls": default: insertXls(temp, entity, mf, tab); break; } if (0 == tab.getRows()) { continue; } MetaEntity me = createMetaEntity(entity, mf, tab.getTab(), tab.getRows()); if (metaService.insert(me) == 0) { continue; } MetaEntity mfe = createMetaFileEntity(me, mf, filePath); if (metaService.insert(mfe) == 0) { metaService.delete(me.getId()); continue; } count += tab.getRows(); } if (count == 0 || null != old) { file.delete(); } else { file.renameTo(newFile); } } } catch (Exception ex) { log.error(ex.getMessage(), ex); } } /** * 获取 Tabs */ private List getTabs(MetaEntity mfe, List tabs) { List list = new ArrayList<>(); for (TabMapperEntity tab : tabs) { if (!tab.getFileName().equals(mfe.getName()) || StringHelper.isEmpty(tab.getEntity())) { continue; } list.add(tab); } return list; } /** * 获取文件类型 */ private String getType(String name) { if (name.contains(XLS)) { return "xls"; } if (name.contains(MDB)) { return "mdb"; } if (name.contains(SHP_ZIP)) { return "shp"; } if (name.contains(GDB_ZIP)) { return "gdb"; } return null; } /** * 创建元数据实体 */ private MetaEntity createMetaEntity(MetaEntity entity, MetaEntity mf, String tab, int rows) { MetaEntity me = new MetaEntity(); me.setDepid(entity.getDepid()); me.setDirid(entity.getDirid()); me.setVerid(entity.getVerid()); me.setType(getType(mf.getName().toLowerCase())); me.setName(mf.getName()); me.setSizes(mf.getSizes()); me.setBstab(tab); me.setBsrows(rows); me.setCreateTime(entity.getCreateTime()); me.setCreateUser(entity.getCreateUser()); return me; } /** * 创建元数据文件实体 */ private MetaEntity createMetaFileEntity(MetaEntity me, MetaEntity entity, String filePath) { MetaEntity mfe = new MetaEntity(); mfe.setName(entity.getName()); mfe.setMetaid(me.getId()); mfe.setGuid(entity.getGuid()); mfe.setSizes(entity.getSizes()); mfe.setPath(FileHelper.getRelativePath(filePath)); mfe.setCreateUser(me.getCreateUser()); mfe.setCreateTime(me.getCreateTime()); return mfe; } /** * 插入Excel */ private void insertXls(String root, MetaEntity me, MetaEntity mf, TabMapperEntity tab) { BasicMapper basicMapper = ClassHelper.getBasicMapper(tab.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; } List list = ExcelHelper.readExcel(clazz, root + File.separator + mf.getPath()); if (list.isEmpty()) { return; } setCreateInfo(list, me); int rows = 0; for (int i = 0, c = list.size(); i < c; i++) { rows += basicMapper.insert(list.get(i)); } if (basicMapper instanceof GeomBaseMapper) { updateXlsGeom((GeomBaseMapper) basicMapper, list); } tab.setTab(tabName); tab.setRows(rows); } /** * 设置创建信息 */ private void setCreateInfo(List list, MetaEntity me) { try { if (!(list.get(0) instanceof BaseEntity)) { return; } for (T t : list) { BaseEntity be = (BaseEntity) t; be.setCreateuser(me.getCreateUser()); be.setCreatetime(me.getCreateTime()); be.setDirid(me.getDirid()); be.setDepid(me.getDepid()); be.setVerid(me.getVerid()); } } catch (Exception ex) { log.error(ex.getMessage(), ex); } } /** * 设置空间信息 */ private void updateXlsGeom(GeomBaseMapper geomBaseMapper, List list) { try { String tabName = BaseQueryService.getTabName(geomBaseMapper); for (T t : list) { Field xField = t.getClass().getDeclaredField("x"); xField.setAccessible(true); double x = (double) xField.get(t); Field yField = t.getClass().getDeclaredField("y"); yField.setAccessible(true); double y = (double) yField.get(t); BaseEntity baseEntity = (BaseEntity) t; Integer gid = baseEntity.getGid(); String wkt = String.format("POINT(%f %f)", x, y); geomBaseMapper.updateGeom(tabName, gid, wkt); } } catch (Exception ex) { log.error(ex.getMessage(), ex); } } /** * 插入DB */ private void insertDb(String type, String root, MetaEntity me, TabMapperEntity tab) { BasicMapper basicMapper = ClassHelper.getBasicMapper(tab.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 filePath = StringEscapeUtils.escapeJava(root + File.separator + tab.getSubPath()); List list = null; switch (type) { case "shp": list = ShpHelper.readData(clazz, filePath); break; case "gdb": list = GdbHelper.readData(clazz, filePath, tab.getTab()); break; case "mdb": list = MdbHelper.readData(clazz, filePath, tab.getTab()); break; default: break; } if (null == list || list.isEmpty()) { return; } setCreateInfo(list, me); int rows = basicMapper.insertBatch(list); tab.setTab(tabName); tab.setRows(rows); } /** * 设置空间信息 */ private void updateDbGeom(GeomBaseMapper geomBaseMapper, List list) { try { String tabName = BaseQueryService.getTabName(geomBaseMapper); for (T t : list) { BaseEntity baseEntity = (BaseEntity) t; Integer gid = baseEntity.getGid(); Field gField = t.getClass().getDeclaredField("geom"); gField.setAccessible(true); Object obj = gField.get(t); if (null != obj) { geomBaseMapper.updateGeom(tabName, gid, (String) obj); } } } catch (Exception ex) { log.error(ex.getMessage(), ex); } } }