| | |
| | | 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.*; |
| | |
| | | |
| | | /** |
| | | * 数据上传服务类 |
| | | * |
| | | * @author WWW |
| | | */ |
| | | @Service |
| | |
| | | */ |
| | | public void insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) { |
| | | checkMetaFiles(ue, list); |
| | | excelLoader(list, req); |
| | | List<MetaFileEntity> xlsList = getExcelFiles(list); |
| | | loadData(list); |
| | | copyFiles(list); |
| | | insertMetas(list); |
| | | if (xlsList.size() > 0) { |
| | | String guid = excelLoader(xlsList, req); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | mf.setMsg("文件丢失"); |
| | | } |
| | | |
| | | MetaEntity old = metaService.selectByGuid(mf.getGuid(), null); |
| | | MetaEntity old = metaService.selectByGuid(mf.getGuid(), getDirCode(mf), null); |
| | | if (null != old) { |
| | | mf.setMsg("已入库"); |
| | | mf.setMsg("已存在"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Excel入库 |
| | | * 获取目录编码 |
| | | */ |
| | | private String excelLoader(List<MetaFileEntity> list, HttpServletRequest req) { |
| | | List<MetaFileEntity> xlsList = getExcelFiles(list); |
| | | if (xlsList.isEmpty()) { |
| | | return ""; |
| | | private String getDirCode(MetaFileEntity mf) { |
| | | if (StringHelper.isEmpty(mf.getDircode())) { |
| | | return null; |
| | | } |
| | | |
| | | String guid = null; |
| | | try { |
| | | MetaFileEntity meta = getExcelMeta(xlsList); |
| | | guid = fmeService.excelLoader(meta, req); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | |
| | | return guid; |
| | | return StringHelper.getRightLike(mf.getDircode().substring(0, 2)); |
| | | } |
| | | |
| | | /** |
| | |
| | | private List<MetaFileEntity> getExcelFiles(List<MetaFileEntity> list) { |
| | | List<MetaFileEntity> xlsList = new ArrayList<>(); |
| | | for (MetaFileEntity mf : list) { |
| | | if (null != mf.getMsg()) { |
| | | continue; |
| | | } |
| | | if (StaticData.XLS.equals(mf.getExtName()) || StaticData.XLSX.equals(mf.getExtName()) || mf.getName().contains("WBS")) { |
| | | 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的元数据 |
| | | * 是/否为Excel |
| | | */ |
| | | private MetaFileEntity getExcelMeta(List<MetaFileEntity> xlsList) { |
| | | List<String> pathList = new ArrayList<>(); |
| | | List<String> dirList = new ArrayList<>(); |
| | | List<String> pidList = new ArrayList<>(); |
| | | |
| | | String xlsBasePath = getXlsPath(xlsList.get(0).getPath()); |
| | | for (int i = 0, c = xlsList.size(); i < c; i++) { |
| | | MetaFileEntity mf = xlsList.get(i); |
| | | |
| | | String path = copyXlsFile(xlsBasePath, i, mf); |
| | | if (null != path) { |
| | | pathList.add(path); |
| | | 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; |
| | | private boolean isExcel(MetaFileEntity mf) { |
| | | return StaticData.XLS.equals(mf.getExtName()) || StaticData.XLSX.equals(mf.getExtName()); |
| | | } |
| | | |
| | | /** |
| | |
| | | return; |
| | | } |
| | | |
| | | MetaEntity old = metaService.selectByGuid(mf.getGuid(), tabName); |
| | | MetaEntity old = metaService.selectByGuid(mf.getGuid(), getDirCode(mf), tabName); |
| | | if (null != old) { |
| | | mf.setMsg("已入库"); |
| | | mf.setMsg("已存在"); |
| | | return; |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | 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); |
| | |
| | | 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; |
| | | } |
| | |
| | | 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()); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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; |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 发布文件 |
| | | */ |
| | | private void publishFiles(List<MetaFileEntity> list) { |
| | | for (MetaFileEntity mf : list) { |
| | | if (null != mf.getMsg()) { |
| | | continue; |
| | | } |
| | | switch (mf.getExtName()) { |
| | | case StaticData.MPT: |
| | | linkFiles(mf, StaticData.MPT_EXT, pathHelper.getConfig().getUploadPath() + File.separator + "SG"); |
| | | break; |
| | | case StaticData.D3DML: |
| | | linkFiles(mf, null, pathHelper.getConfig().getUploadPath() + File.separator + "SG"); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 链接文件 |
| | | */ |
| | | private void linkFiles(MetaFileEntity mf, List<String> exts, String publishPath) { |
| | | String uploadPath = pathHelper.getConfig().getUploadPath(); |
| | | String source = uploadPath + File.separator + mf.getPath(); |
| | | String target = publishPath + File.separator + mf.getName(); |
| | | |
| | | File sourceFile = new File(source); |
| | | if (!sourceFile.exists() || sourceFile.isDirectory()) { |
| | | return; |
| | | } |
| | | |
| | | createFileLink(source, target); |
| | | if (null == exts) { |
| | | return; |
| | | } |
| | | |
| | | for (String ext : exts) { |
| | | File f = new File(source.replace(mf.getExtName(), ext)); |
| | | if (f.exists() && !f.isDirectory()) { |
| | | createFileLink(source.replace(mf.getExtName(), ext), target.replace(mf.getExtName(), ext)); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 创建文件链接 |
| | | */ |
| | | private void createFileLink(String source, String target) { |
| | |
| | | |
| | | MetaEntity me = createMeta(mf, metaId); |
| | | metaService.insert(me); |
| | | |
| | | mf.setMsg(me.getId() > 0 ? "成功" : "失败"); |
| | | String err = mf.getRows() < mf.getRecords() ? "(" + (mf.getRecords() - mf.getRows()) + " 条失败)" : ""; |
| | | mf.setMsg(me.getId() > 0 ? String.format("成功%s", err) : "失败"); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | /** |
| | | * Excel入库 |
| | | */ |
| | | private String excelLoader(List<MetaFileEntity> 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<MetaFileEntity> xlsList) { |
| | | if (null == xlsList || xlsList.isEmpty()) { |
| | | return null; |
| | | } |
| | | |
| | | List<String> pathList = new ArrayList<>(); |
| | | List<String> dirList = new ArrayList<>(); |
| | | List<String> 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; |
| | | } |
| | | } |