Bladeren bron

代码抽取

shiqian 3 jaren geleden
bovenliggende
commit
3f8aebc468

+ 13 - 15
boman-common/boman-common-security/src/main/java/com/boman/common/security/feign/FeignRequestInterceptor.java

@@ -12,41 +12,39 @@ import feign.RequestTemplate;
 
 /**
  * feign 请求拦截器
- * 
+ *
  * @author ruoyi
  */
 @Component
-public class FeignRequestInterceptor implements RequestInterceptor
-{
+public class FeignRequestInterceptor implements RequestInterceptor {
+
     @Override
-    public void apply(RequestTemplate requestTemplate)
-    {
+    public void apply(RequestTemplate requestTemplate) {
         HttpServletRequest httpServletRequest = ServletUtils.getRequest();
-        if (StringUtils.isNotNull(httpServletRequest))
-        {
+        if (StringUtils.isNotNull(httpServletRequest)) {
             Map<String, String> headers = ServletUtils.getHeaders(httpServletRequest);
             // 传递用户信息请求头,防止丢失
             String userId = headers.get(CacheConstants.DETAILS_USER_ID);
-            if (StringUtils.isNotEmpty(userId))
-            {
+            if (StringUtils.isNotEmpty(userId)) {
                 requestTemplate.header(CacheConstants.DETAILS_USER_ID, userId);
             }
+
             String token = headers.get(CacheConstants.HEADER);
             if (StringUtils.isEmpty(token)) {
                 token = headers.get(CacheConstants.HEADER.toLowerCase());
             }
-            if (StringUtils.isNotEmpty(token))
-            {
+
+            if (StringUtils.isNotEmpty(token)) {
                 requestTemplate.header(CacheConstants.HEADER, token);
             }
+
             String userName = headers.get(CacheConstants.DETAILS_USERNAME);
-            if (StringUtils.isNotEmpty(userName))
-            {
+            if (StringUtils.isNotEmpty(userName)) {
                 requestTemplate.header(CacheConstants.DETAILS_USERNAME, userName);
             }
+
             String authentication = headers.get(CacheConstants.AUTHORIZATION_HEADER);
-            if (StringUtils.isNotEmpty(authentication))
-            {
+            if (StringUtils.isNotEmpty(authentication)) {
                 requestTemplate.header(CacheConstants.AUTHORIZATION_HEADER, authentication);
             }
 

+ 3 - 0
boman-modules/boman-file/src/main/java/com/boman/file/controller/ExcelController.java

@@ -122,13 +122,16 @@ public class ExcelController {
 
         File file = new File(fileAbsPath);
         if (!file.exists()) {
+            // 还没有生成文件,可能还在查询的过程中,无法下载
             return AjaxResult.success(false);
         }
 
         long length = file.length();
         if (length == 0) {
+            // 生成文件中,内容还未写进excel,此时文件的大小为0
             return AjaxResult.success(false);
         } else {
+            // todo 需要优化,删除了用户只可以下载一次
             redisService.deleteObject(redisKey);
             return AjaxResult.success(fileStaticPath, true);
         }

+ 29 - 35
boman-modules/boman-file/src/main/java/com/boman/file/service/LocalSysFileServiceImpl.java

@@ -1,22 +1,20 @@
 package com.boman.file.service;
 
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.alibaba.fastjson.TypeReference;
 import com.boman.common.core.utils.SecurityUtils;
-import com.boman.common.core.utils.ServletUtils;
 import com.boman.common.core.utils.obj.ObjectUtils;
 import com.boman.common.core.utils.poi.ExcelUtil;
 import com.boman.common.redis.RedisKey;
 import com.boman.common.redis.service.RedisService;
-import com.boman.domain.*;
+import com.boman.domain.GenTable;
+import com.boman.domain.GenTableColumn;
+import com.boman.domain.SysDept;
+import com.boman.domain.VaccineInfoOperation;
 import com.boman.domain.constant.CacheConstants;
-import com.boman.domain.constant.MaskConstant;
 import com.boman.domain.dto.AjaxResult;
 import com.boman.domain.dto.ExportExcelDto;
 import com.boman.domain.dto.FormDataDto;
 import com.boman.domain.dto.ImportExcelDto;
-import com.boman.domain.utils.CamelLowerUnderScore;
 import com.boman.domain.utils.ThreadPoolService;
 import com.boman.file.utils.FileUploadUtils;
 import com.boman.system.api.RemoteDeptService;
@@ -24,29 +22,26 @@ import com.boman.system.api.model.LoginUser;
 import com.boman.web.core.api.RemoteAttendanceService;
 import com.boman.web.core.api.RemoteObjService;
 import com.boman.web.core.api.RemoteVaccineInfoService;
-import com.google.common.base.CaseFormat;
-import org.apache.commons.beanutils.BeanUtils;
 import org.apache.commons.lang3.BooleanUtils;
-import org.apache.http.impl.client.HttpClients;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cloud.context.config.annotation.RefreshScope;
 import org.springframework.context.annotation.Primary;
 import org.springframework.stereotype.Service;
-import org.springframework.web.client.RestTemplate;
 import org.springframework.web.multipart.MultipartFile;
+
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
 import java.util.*;
-import java.util.concurrent.TimeUnit;
 
 import static com.boman.common.core.utils.obj.ObjectUtils.*;
+import static com.boman.common.core.utils.poi.ExcelUtil.filterData;
+import static com.boman.domain.constant.MaskConstant.LIST_VISIBLE;
 
 /**
  * 本地文件存储
@@ -174,7 +169,7 @@ public class LocalSysFileServiceImpl implements ISysFileService
 
         GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
         List<GenTableColumn> columns = genTable.getColumns();
-        columns = ExcelUtil.filterData(columns, 4, MaskConstant.LIST_VISIBLE::equals);
+        columns = filterData(columns, 4, LIST_VISIBLE::equals);
         ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
         List<Map<String, Object>> list;
         if (BooleanUtils.isTrue(empty)) {
@@ -210,7 +205,7 @@ public class LocalSysFileServiceImpl implements ISysFileService
 
         GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
         List<GenTableColumn> allColumn = genTable.getColumns();
-        List<GenTableColumn> columns = ExcelUtil.filterData(allColumn, 4, MaskConstant.LIST_VISIBLE::equals);
+        List<GenTableColumn> columns = filterData(allColumn, 4, LIST_VISIBLE::equals);
 
         String filename = UUID.randomUUID() + ".xlsx";
         String fileAbsPath = localFilePath + "/" + filename;
@@ -262,10 +257,7 @@ public class LocalSysFileServiceImpl implements ISysFileService
 //                }
 
                 String key = RedisKey.ASYNC_DOWNLOAD_YMJZ + username;
-                JSONObject jsonObject = new JSONObject();
-                jsonObject.put("fileStaticPath", fileStaticPath);
-                jsonObject.put("fileAbsPath", fileAbsPath);
-                redisService.setCacheObject(key, jsonObject);
+                saveIntoRedis(fileAbsPath, fileStaticPath, key);
             } catch (IOException e) {
                 LOGGER.error("导出失败", e);
                 e.printStackTrace();
@@ -287,26 +279,29 @@ public class LocalSysFileServiceImpl implements ISysFileService
     @Override
     public AjaxResult asyncExportYmjzExcel(HttpServletResponse response, VaccineInfoOperation info) throws IOException {
         GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + "vaccine_info");
-        List<GenTableColumn> allColumn = genTable.getColumns();
-        List<GenTableColumn> columns = ExcelUtil.filterData(allColumn, 4, MaskConstant.LIST_VISIBLE::equals);
+        List<GenTableColumn> columns = filterData(genTable.getColumns(), 4, LIST_VISIBLE::equals);
 
         String filename = UUID.randomUUID() + ".xlsx";
         String fileAbsPath = localFilePath + "/" + filename;
         String fileStaticPath = domain + localFilePrefix + "/" + filename;
-        File file = new File(fileAbsPath);
 
-        ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
-        String username = SecurityUtils.getUsername();
+        asyncHandle(info, columns, fileAbsPath, fileStaticPath);
+        return AjaxResult.success("成功", fileStaticPath);
+    }
+
+    private void asyncHandle(VaccineInfoOperation info, List<GenTableColumn> columns, String fileAbsPath, String fileStaticPath) {
+        final String username = SecurityUtils.getUsername();
+        final ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
+        final File file = new File(fileAbsPath);
 
         ThreadPoolService.execute(() -> {
             List<Map<String, Object>> list;
             LOGGER.info("开始查询, 线程名称: {}", Thread.currentThread().getName());
             long currentTimeMillis = System.currentTimeMillis();
+
             String key = RedisKey.ASYNC_DOWNLOAD_YMJZ + username;
-            JSONObject jsonObject = new JSONObject();
-            jsonObject.put("fileStaticPath", fileStaticPath);
-            jsonObject.put("fileAbsPath", fileAbsPath);
-            redisService.setCacheObject(key, jsonObject);
+            saveIntoRedis(fileAbsPath, fileStaticPath, key);
+
             list = listYmjz(info, columns);
             boolean empty = isEmpty(list);
             int size = BooleanUtils.isFalse(empty) ? 0 : list.size();
@@ -314,20 +309,19 @@ public class LocalSysFileServiceImpl implements ISysFileService
 
             try {
                 util.asyncExportExcelCommon(new FileOutputStream(file), list, "sheet1", columns, empty, false);
-                long timeout = 2L;
-                if (size > 100000 && size < 500000) {
-                    timeout = 5L;
-                } else if (size > 500000) {
-                    timeout = 10L;
-                }
             } catch (IOException e) {
                 LOGGER.error("导出失败", e);
+                redisService.deleteObject(key);
                 e.printStackTrace();
             }
         });
+    }
 
-
-        return AjaxResult.success("成功", fileStaticPath);
+    private void saveIntoRedis(String fileAbsPath, String fileStaticPath, String key) {
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("fileStaticPath", fileStaticPath);
+        jsonObject.put("fileAbsPath", fileAbsPath);
+        redisService.setCacheObject(key, jsonObject);
     }
 
     private List<Map<String, Object>> getData(ExportExcelDto dto, String tableName, List<GenTableColumn> columns) {