From d91c78e445e49cda5430bd33ef281f60f56a0a2d Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期日, 05 二月 2023 20:09:10 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/service/all/UploadAttachService.java | 261 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 249 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/lf/server/service/all/UploadAttachService.java b/src/main/java/com/lf/server/service/all/UploadAttachService.java index 37ac45a..7d6cc80 100644 --- a/src/main/java/com/lf/server/service/all/UploadAttachService.java +++ b/src/main/java/com/lf/server/service/all/UploadAttachService.java @@ -1,17 +1,16 @@ package com.lf.server.service.all; import com.alibaba.fastjson.JSON; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.lf.server.controller.all.BaseController; -import com.lf.server.entity.all.HttpStatus; -import com.lf.server.entity.all.ResponseMsg; -import com.lf.server.entity.all.SettingData; -import com.lf.server.entity.all.StaticData; +import com.lf.server.entity.all.*; +import com.lf.server.entity.ctrl.KeyValueEntity; +import com.lf.server.entity.data.MetaEntity; +import com.lf.server.entity.data.MetaFileEntity; import com.lf.server.entity.sys.AttachEntity; import com.lf.server.entity.sys.UserEntity; -import com.lf.server.helper.FileHelper; -import com.lf.server.helper.PathHelper; -import com.lf.server.helper.StringHelper; -import com.lf.server.helper.WebHelper; +import com.lf.server.helper.*; +import com.lf.server.mapper.all.BasicMapper; import com.lf.server.service.sys.AttachService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -21,6 +20,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -40,7 +40,7 @@ private static String tabs = "''"; - private static Map<String, String> attachTabs = new HashMap<>(); + private final static Map<String, String> ATTACH_TABS = new HashMap<>(); private final static Log log = LogFactory.getLog(UploadAttachService.class); @@ -68,11 +68,11 @@ List<String> keys = new ArrayList<>(); for (String str : strs) { - if (attachTabs.containsKey(str)) { + if (ATTACH_TABS.containsKey(str)) { continue; } - attachTabs.put(str, str.contains("bd.") ? "materiname" : "photono"); + ATTACH_TABS.put(str, str.contains("bd.") ? "materiname" : "photono"); keys.add("'" + str + "'"); } tabs = StringHelper.join(keys, ","); @@ -125,7 +125,7 @@ entity.setName(oldName); entity.setTab(tab); entity.setGuid(md5); - String subPath = PathHelper.getUploadPath() + File.separator + md5; + String subPath = PathHelper.getUploadPath() + File.separator + md5 + FileHelper.getExtension(oldName); entity.setPath(subPath); entity.setSizes(sizes); if (ue != null) { @@ -163,4 +163,241 @@ log.error(ex.getMessage(), ex); } } + + /** + * 涓婁紶Excel闄勪欢 + */ + public List<KeyValueEntity> uploadXlsAnnex(UserEntity ue, List<MetaEntity> ms, List<MetaFileEntity> list, String path) { + List<String> files = getAttachFiles(list, path); + if (files.size() == 0) { + return null; + } + + List<KeyValueEntity> rs = new ArrayList<>(); + for (MetaEntity meta : ms) { + int rows = uploadXlsAnnex(ue, meta, files); + rs.add(new KeyValueEntity(meta.getName(), String.valueOf(rows))); + } + + return rs; + } + + /** + * 鑾峰彇闄勪欢鏂囦欢 + */ + private List<String> getAttachFiles(List<MetaFileEntity> list, String path) { + List<String> files = new ArrayList<>(); + + for (MetaFileEntity mf : list) { + if (mf.getName().contains(StaticData.ZIP)) { + List<String> subs = getZipFiles(mf, path); + if (null == subs || subs.size() > 0) { + files.addAll(subs); + } + continue; + } + + files.add(mf.getPath()); + } + + return files; + } + + /** + * 鑾峰彇zip涓殑鏂囦欢 + */ + private List<String> getZipFiles(MetaFileEntity mf, String path) { + File file = new File(mf.getPath()); + if (!file.exists() || file.isDirectory()) { + return null; + } + + File zipFolder = new File(pathHelper.getConfig().getTempPath() + File.separator + path + "_zip"); + if (!zipFolder.exists() || !zipFolder.isDirectory()) { + zipFolder.mkdirs(); + } + + String subPath = zipFolder + File.separator + mf.getName().toLowerCase().replace(".zip", ""); + ZipHelper.unzip(mf.getPath(), subPath); + + List<String> files = new ArrayList<>(); + getFilesByPath(files, subPath); + + return files; + } + + /** + * 鏍规嵁璺緞鑾峰彇鏂囦欢 + */ + private void getFilesByPath(List<String> list, String path) { + File file = new File(path); + if (!file.isDirectory()) { + list.add(file.getPath()); + return; + } + + File[] files = file.listFiles(); + if (null == files) { + return; + } + + for (File f : files) { + if (f.isDirectory()) { + getFilesByPath(list, f.getPath()); + } else { + list.add(f.getPath()); + } + } + } + + /** + * 涓婁紶Excel闄勪欢 + */ + private int uploadXlsAnnex(UserEntity ue, MetaEntity meta, List<String> files) { + List<?> list = queryXlsData(meta); + if (null == list || list.size() == 0) { + return 0; + } + + Field field = getAnnexField(list.get(0), meta); + String folder = meta.getName().split("\\.")[0].trim().toLowerCase() + File.separator; + + int rows = 0; + for (Object obj : list) { + String name = getAnnexName(obj, field); + if (StringHelper.isEmpty(name)) { + continue; + } + + String file = findAnnexFile(name, folder, files); + if (null == file) { + continue; + } + + BaseEntity be = (BaseEntity) obj; + rows += insertXlsAnnex(ue, meta, be, file); + files.remove(file); + } + + return rows; + } + + /** + * 鏌ヨExcel鏁版嵁 + */ + private List<?> queryXlsData(MetaEntity meta) { + String entity = meta.getTab().substring(meta.getTab().indexOf(".") + 1).replace("_", "").toLowerCase(); + BasicMapper basicMapper = ClassHelper.getBasicMapper(entity); + if (null == basicMapper) { + return null; + } + + QueryWrapper wrapper = new QueryWrapper(); + wrapper.eq("parentid", meta.getEventid()); + + List<?> list = basicMapper.selectList(wrapper); + if (null == list || list.size() == 0) { + return null; + } + if (!(list.get(0) instanceof BaseEntity)) { + return null; + } + + return list; + } + + /** + * 鑾峰彇闄勪欢瀛楁 + */ + private Field getAnnexField(Object obj, MetaEntity meta) { + try { + String str = ATTACH_TABS.get(meta.getTab()); + + Field field = obj.getClass().getDeclaredField(str); + field.setAccessible(true); + + return field; + } catch (Exception ex) { + return null; + } + } + + /** + * 鑾峰彇闄勪欢鍚嶇О + */ + private String getAnnexName(Object obj, Field field) { + try { + Object val = field.get(obj); + + return null == val ? null : val.toString().trim(); + } catch (Exception ex) { + return null; + } + } + + /** + * 鏌ユ壘闄勪欢鏂囦欢 + */ + private String findAnnexFile(String name, String folder, List<String> files) { + name = File.separator + name.split("\\.")[0].trim().toLowerCase(); + for (String file : files) { + if (file.toLowerCase().contains(folder) && file.toLowerCase().contains(name)) { + return file; + } + } + for (String file : files) { + if (file.toLowerCase().contains(name)) { + return file; + } + } + + return null; + } + + /** + * 鎻掑叆Excel闄勪欢 + */ + private int insertXlsAnnex(UserEntity ue, MetaEntity meta, BaseEntity be, String file) { + File f = new File(file); + if (!f.exists() || f.isDirectory()) { + return 0; + } + + String md5 = FileHelper.getFileMd5(file); + AttachEntity old = attachService.selectByTabAndGuid(meta.getTab(), be.getEventid(), md5); + if (null != old) { + f.delete(); + return 0; + } + + String fileName = FileHelper.getFileName(file); + double sizes = FileHelper.sizeToMb(f.length()); + + AttachEntity ae = getAttachEntity(ue, meta.getTab(), be.getEventid(), fileName, md5, sizes); + String targetPath = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath(); + + f.renameTo(new File(targetPath)); + int rows = attachService.insert(ae); + + return rows > 0 ? 1 : 0; + } + + /** + * 鑾峰彇闄勪欢瀹炰綋绫� + */ + protected AttachEntity getAttachEntity(UserEntity ue, String tabName, String eventid, String fileName, String md5, double sizes) { + AttachEntity entity = new AttachEntity(); + entity.setTab(tabName); + entity.setTabGuid(eventid); + entity.setName(fileName); + entity.setGuid(md5); + String subPath = PathHelper.getUploadPath() + File.separator + md5 + FileHelper.getExtension(fileName); + entity.setPath(subPath); + entity.setSizes(sizes); + if (ue != null) { + entity.setCreateUser(ue.getId()); + } + + return entity; + } } -- Gitblit v1.9.3