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.*; import com.lf.server.entity.sys.UserEntity; 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 com.lf.server.service.sys.DepService; import org.apache.commons.io.FileUtils; 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; /** * 数据上传服务类 * * @author WWW */ @Service public class UploadService extends BaseUploadService implements UploadMapper { @Autowired UploadMapper uploadMapper; @Autowired FmeService fmeService; @Autowired MetaService metaService; @Autowired DepService depService; @Autowired DirService dirService; @Override public List selectCoords(String zoning) { return uploadMapper.selectCoords(zoning); } @Override public Integer selectCount4Coord(String epsgCode) { return uploadMapper.selectCount4Coord(epsgCode); } @Override public List selectProject() { return uploadMapper.selectProject(); } @Override public List selectFmeLog(String parentid) { return uploadMapper.selectFmeLog(parentid); } /** * 插入文件 */ public void insertFiles(UserEntity ue, List list, HttpServletRequest req) { checkMetaFiles(ue, list); List xlsList = getExcelFiles(list); loadData(list); copyFiles(list); insertMetas(list); if (xlsList.size() > 0) { String guid = excelLoader(xlsList, req); } } /** * 检查元数据文件 */ private void checkMetaFiles(UserEntity ue, List list) { Timestamp createTime = WebHelper.getCurrentTimestamp(); String tempPath = pathHelper.getConfig().getTempPath(); for (MetaFileEntity mf : list) { mf.setCreateUser(ue.getId()); mf.setCreateTime(createTime); mf.setDepcode(ue.getDepcode()); mf.setPath(tempPath + File.separator + mf.getPath()); mf.setMsg(null); File f = new File(mf.getPath()); if (!f.exists()) { mf.setMsg("文件丢失"); } 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)); } /** * 获取Excel元数据文件 */ private List getExcelFiles(List list) { List xlsList = new ArrayList<>(); for (MetaFileEntity mf : list) { if (null == mf.getMsg() && isExcel(mf)) { xlsList.add(mf); } } if (xlsList.isEmpty()) { return xlsList; } String xlsBasePath = getXlsPath(xlsList.get(0).getPath()); for (int i = 0, c = xlsList.size(); i < c; i++) { MetaFileEntity mf = xlsList.get(i); String xlsPath = copyXlsFile(xlsBasePath, i, mf); mf.setXlsPath(xlsPath); } return xlsList; } /** * 是/否为Excel */ private boolean isExcel(MetaFileEntity mf) { return StaticData.XLS.equals(mf.getExtName()) || StaticData.XLSX.equals(mf.getExtName()); } /** * 获取Xls目录 */ private String getXlsPath(String filePath) { String tempPath = pathHelper.getConfig().getTempPath() + File.separator; String subPath = filePath.substring(tempPath.length()); subPath = tempPath + subPath.substring(0, subPath.indexOf(File.separator)).replace("_zip", "") + "_xls"; File f = new File(subPath); if (!f.exists() || !f.isDirectory()) { f.mkdirs(); } return subPath; } /** * 复制Xls文件 */ private String copyXlsFile(String xlsBasePath, int i, MetaFileEntity mf) { try { String xlsPath = xlsBasePath + File.separator + i; File file = new File(xlsPath); if (!file.exists() || !file.isDirectory()) { file.mkdirs(); } file = new File(mf.getPath()); File newFile = new File(xlsPath + File.separator + FileHelper.getFileName(file.getPath())); FileUtils.copyFile(file, newFile); return newFile.getPath(); } catch (Exception ex) { log.error(ex.getMessage(), ex); return null; } } /** * 加载数据 */ private void loadData(List list) { for (MetaFileEntity mf : list) { if (null != mf.getMsg() || StringHelper.isEmpty(mf.getEntity())) { continue; } if (StaticData.SHP.equals(mf.getExtName()) || StaticData.GDB.equals(mf.getExtName())) { loadSpatialData(mf); } } } /** * 加载空间数据 */ public void loadSpatialData(MetaFileEntity mf) { 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; } File file = new File(mf.getPath()); if (!file.exists()) { mf.setMsg("文件丢失"); return; } MetaEntity old = metaService.selectByGuid(mf.getGuid(), getDirCode(mf), tabName); if (null != old) { mf.setMsg("已存在"); return; } List list; 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.setEntity(mf.getTab()); mf.setTab(tabName); mf.setRows(rows); } } /** * 设置创建信息 */ private void setCreateInfo(List 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.getDircode()); be.setDepid(mf.getDepcode()); be.setVerid(mf.getVerid()); be.setCreateuser(mf.getCreateUser()); be.setCreatetime(mf.getCreateTime()); } } catch (Exception ex) { log.error(ex.getMessage(), ex); } } /** * 复制文件 */ private void copyFiles(List list) { List gdbList = new ArrayList<>(); for (MetaFileEntity mf : list) { if (null != mf.getMsg()) { continue; } switch (mf.getExtName()) { case StaticData.MPT: copyMultiFile(mf, StaticData.MPT_EXT); break; case StaticData.JPG: copyMultiFile(mf, StaticData.JPG_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(), null, null); if (null != old) { // mf.setMsg("已存在") setOldMeta(mf, old); file.delete(); return 0; } String uploadPath = pathHelper.getUploadFullPath(); String targetPath = uploadPath + File.separator + mf.getGuid() + mf.getExtName(); String subPath = FileHelper.getRelativePath(targetPath); File newFile = new File(targetPath); if (newFile.exists()) { mf.setPath(subPath); file.delete(); return 0; } file.renameTo(newFile); 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()); } /** * 复制多个文件 */ private void copyMultiFile(MetaFileEntity mf, List 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(), null, null); if (null != old) { // mf.setMsg("已存在") setOldMeta(mf, old); FileHelper.deleteFiles(file); return; } String uploadPath = pathHelper.getUploadFullPath(); String targetPath = uploadPath + File.separator + mf.getGuid() + mf.getExtName(); String subPath = FileHelper.getRelativePath(targetPath); File newFile = new File(targetPath); if (newFile.exists() && newFile.isDirectory()) { mf.setPath(subPath); FileHelper.deleteFiles(file); 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)); } mf.setPath(subPath); } /** * 根据GUID查找路径 */ private String findPathByGuid(List list, MetaFileEntity mf) { for (MetaFileEntity meta : list) { if (meta.getGuid().equals(mf.getGuid()) && !meta.getPath().equals(mf.getPath())) { return meta.getPath(); } } return null; } /** * 创建文件链接 */ private void createFileLink(String source, String target) { String cmd = String.format("cmd /c mklink \"%s\" \"%s\"", target, source); WebHelper.exec(cmd); } /** * 插入元数据 */ private void insertMetas(List list) { int metaId = insertParentMeta(list); for (MetaFileEntity mf : list) { if (null != mf.getMsg()) { continue; } MetaEntity me = createMeta(mf, metaId); metaService.insert(me); mf.setMsg(me.getId() > 0 ? "成功" : "失败"); } } /** * 创建元数据 */ private MetaEntity createMeta(MetaFileEntity mf, int metaId) { MetaEntity me = new MetaEntity(); me.setMetaid(metaId); me.setEventid(mf.getEventid()); me.setDircode(mf.getDircode()); me.setDepcode(mf.getDepcode()); me.setVerid(mf.getVerid()); me.setName(mf.getName()); me.setType(mf.getType()); me.setGuid(mf.getGuid()); me.setPath(mf.getPath()); me.setSizes(mf.getSizes()); if (mf.getRows() > 0) { me.setTab(mf.getTab()); } if (!StringHelper.isEmpty(mf.getEntity())) { me.setLayer(mf.getEntity()); } me.setRows(mf.getRows()); me.setCreateUser(mf.getCreateUser()); me.setCreateTime(mf.getCreateTime()); return me; } /** * 插入父元数据 */ private int insertParentMeta(List list) { for (MetaFileEntity mf : list) { if (null != mf.getMsg() || !mf.getIsMeta()) { continue; } MetaEntity me = createMeta(mf, 0); me.setIsmeta((short) 1); metaService.insert(me); mf.setMsg(me.getId() > 0 ? "成功" : "失败"); return me.getId(); } return 0; } /** * Excel入库 */ private String excelLoader(List xlsList, HttpServletRequest req) { try { MetaFileEntity xlsMeta = getExcelMeta(xlsList); if (null != xlsMeta) { return fmeService.excelLoader(xlsMeta, req); } } catch (Exception ex) { log.error(ex.getMessage(), ex); } return null; } /** * 获取Excel的元数据 */ private MetaFileEntity getExcelMeta(List xlsList) { if (null == xlsList || xlsList.isEmpty()) { return null; } List pathList = new ArrayList<>(); List dirList = new ArrayList<>(); List pidList = new ArrayList<>(); for (int i = 0, c = xlsList.size(); i < c; i++) { MetaFileEntity mf = xlsList.get(i); if ("成功".equals(mf.getMsg()) && null != mf.getXlsPath()) { pathList.add(mf.getXlsPath()); dirList.add(mf.getDircode()); pidList.add(mf.getEventid()); } } MetaFileEntity first = xlsList.get(0); MetaFileEntity meta = new MetaFileEntity(); meta.setPath(StringHelper.join(pathList, ",")); meta.setEpsgCode(first.getEpsgCode()); meta.setName(StringHelper.join(dirList, ";")); meta.setDepcode(first.getDepcode()); meta.setVerid(first.getVerid()); meta.setCreateUser(first.getCreateUser()); meta.setEventid(StringHelper.join(pidList, ";")); return meta; } }