bihuisong 6 mēneši atpakaļ
vecāks
revīzija
7f63426126

+ 20 - 0
songhua-admin/pom.xml

@@ -61,6 +61,26 @@
             <artifactId>songhua-generator</artifactId>
         </dependency>
 
+        <!-- hutool 工具类 -->
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.15</version>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>RELEASE</version>
+            <scope>compile</scope>
+        </dependency>
+
+        <!--hutool 工具类-->
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.0.M1</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 23 - 4
songhua-admin/src/main/java/com/songhua/web/controller/system/PzRawDataController.java

@@ -1,25 +1,31 @@
 package com.songhua.web.controller.system;
 
+import cn.hutool.core.io.FileUtil;
 import com.songhua.common.annotation.Log;
 import com.songhua.common.core.controller.BaseController;
 import com.songhua.common.core.domain.AjaxResult;
 import com.songhua.common.core.page.TableDataInfo;
 import com.songhua.common.enums.BusinessType;
+import com.songhua.common.utils.DateUtils;
 import com.songhua.common.utils.poi.ExcelUtil;
 import com.songhua.system.domain.PzRawData;
 import com.songhua.system.domain.PzRawDataVo;
 import com.songhua.system.service.IPzRawDataService;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.context.request.async.DeferredResult;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedOutputStream;
+import java.io.File;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URLEncoder;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 
 /**
@@ -130,8 +136,21 @@ public class PzRawDataController extends BaseController {
      */
     @Log(title = "导出原始数据导入列表", businessType = BusinessType.EXPORT)
     @PostMapping("/export")
-    public void export(HttpServletResponse response, PzRawData pzRawData) throws IOException {
-        pzRawDataService.export(response, pzRawData);
-//        return AjaxResult.success(pzRawDataService.export(response, pzRawData));
+    public AjaxResult export(HttpServletResponse response, PzRawData pzRawData) throws IOException {
+        try{
+            String fileName = "销售汇总表统计" + DateUtils.dateTimeNow() + ".xlsx";
+            Map<String, String> map = pzRawDataService.export(response, pzRawData);
+            File file = new File(map.get("localFilePath"));
+            response.setContentType("application/octet-stream;charset=utf-8");
+            String responseName = URLEncoder.encode(fileName, "UTF-8");
+            response.setHeader("Content-Disposition", "attachment;filename=" + responseName);
+            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
+            toClient.write(FileUtil.readBytes(file));
+            toClient.flush();
+            toClient.close();
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+        return null;
     }
 }

+ 25 - 0
songhua-common/src/main/java/com/songhua/common/utils/file/FileUtils.java

@@ -9,6 +9,8 @@ import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.*;
+import java.nio.file.attribute.BasicFileAttributes;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.apache.commons.io.IOUtils;
@@ -121,6 +123,29 @@ public class FileUtils
         return flag;
     }
 
+
+    /**
+     * 删除全部文件
+     *
+     * @param path 文件
+     * @return
+     */
+    public static void deleteAll(String path){
+        try {
+            Files.walkFileTree(Paths.get(path),new SimpleFileVisitor<Path>() {
+                // 先去遍历删除文件
+                public FileVisitResult visitFile(Path file,
+                                                 BasicFileAttributes attrs) throws IOException {
+                    Files.delete(file);
+                    System.out.printf("文件被删除 : %s%n", file);
+                    return FileVisitResult.CONTINUE;
+                }
+            });
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     /**
      * 文件名称验证
      * 

+ 12 - 0
songhua-system/src/main/java/com/songhua/system/enums/TicketRulesEnum.java

@@ -144,5 +144,17 @@ public enum TicketRulesEnum {
         return returnList;
     }
 
+    /**
+     * 获取所有枚举的键
+     * @return 包含所有键的列表
+     */
+    public static List<String> getAllKeys() {
+        List<String> returnList = new ArrayList<>();
+        for (TicketRulesEnum ticketRulesEnum : TicketRulesEnum.values()) {
+            returnList.add(ticketRulesEnum.getKey());
+        }
+        return returnList;
+    }
+
 
 }

+ 2 - 1
songhua-system/src/main/java/com/songhua/system/service/IPzRawDataService.java

@@ -9,6 +9,7 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 
 /**
@@ -73,7 +74,7 @@ public interface IPzRawDataService {
 
     Long checkIfDataExists(String ticketName);
 
-    void export(HttpServletResponse response, PzRawData pzRawData) throws IOException;
+    Map<String, String> export(HttpServletResponse response, PzRawData pzRawData) throws IOException;
 
     /**
      * 导入用户数据

+ 55 - 27
songhua-system/src/main/java/com/songhua/system/service/impl/PzRawDataServiceImpl.java

@@ -2,7 +2,7 @@ package com.songhua.system.service.impl;
 
 import com.songhua.common.exception.ServiceException;
 import com.songhua.common.utils.DateUtils;
-import com.songhua.common.utils.poi.ExcelUtil;
+import com.songhua.common.utils.file.FileUtils;
 import com.songhua.system.domain.PzRawData;
 import com.songhua.system.domain.PzRawDataVo;
 import com.songhua.system.domain.vo.ShhTicketRulesReqVO;
@@ -11,7 +11,6 @@ import com.songhua.system.enums.TicketRulesEnum;
 import com.songhua.system.mapper.PzRawDataMapper;
 import com.songhua.system.service.IPzRawDataService;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.collections4.CollectionUtils;
 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
@@ -24,10 +23,10 @@ import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.math.BigDecimal;
-import java.net.URLEncoder;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.*;
@@ -46,6 +45,9 @@ public class PzRawDataServiceImpl implements IPzRawDataService {
     @Autowired
     private PzRawDataMapper pzRawDataMapper;
 
+    private final String reportFileUrlWin = "D:\\report\\";
+
+    private final String reportFileUrlLinux = "/u01/report/";
 
     /**
      * 查询原始数据导入
@@ -271,8 +273,7 @@ public class PzRawDataServiceImpl implements IPzRawDataService {
 
 
     @Override
-    public void export(HttpServletResponse response, PzRawData pzRawData) throws IOException {
-        ExcelUtil<ShhTicketRulesReqVO> util = new ExcelUtil<ShhTicketRulesReqVO>(ShhTicketRulesReqVO.class);
+    public Map<String, String> export(HttpServletResponse response, PzRawData pzRawData) throws IOException {
         List<ShhTicketRulesReqVO> list = calculateTotalPrices(pzRawDataMapper.selectPzRawDataList(pzRawData));
         // 创建
         Workbook workbook = new XSSFWorkbook();
@@ -312,14 +313,43 @@ public class PzRawDataServiceImpl implements IPzRawDataService {
             dataCell2.setCellValue(String.valueOf(data.getAmount()));
             dataCell2.setCellStyle(style);
         }
-        try {
-            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
-            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(sheet + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xlsx", "UTF-8"));
-            workbook.write(response.getOutputStream());
+        String fileName = "销售汇总表统计" + DateUtils.dateTimeNow() + ".xlsx";
+        //生成文档
+        String dssFileDownloadUrl = isWindows() ? reportFileUrlWin : reportFileUrlLinux;
+        String localFilePath = dssFileDownloadUrl + fileName;
+        //先把这个文件夹下的历史文件删除,再生成新的
+        FileUtils.deleteAll(dssFileDownloadUrl);
+        try (FileOutputStream fileOut = new FileOutputStream(localFilePath)) {
+            workbook.write(fileOut);
             workbook.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
+        Map<String, String> map = new HashMap<>();
+        map.put("fileName", fileName);
+        map.put("filePath", dssFileDownloadUrl);
+        map.put("localFilePath", localFilePath);
+        return map;
+    }
+
+    /**
+     * 判断操作系统是否是 Windows
+     *
+     * @return true:操作系统是 Windows
+     * false:其它操作系统
+     */
+    public static boolean isWindows() {
+        String osName = getOsName();
+        return osName != null && osName.startsWith("Windows");
+    }
+
+    /**
+     * 获取操作系统名称
+     *
+     * @return os.name 属性值
+     */
+    public static String getOsName() {
+        return System.getProperty("os.name");
     }
 
     /**
@@ -330,26 +360,24 @@ public class PzRawDataServiceImpl implements IPzRawDataService {
      */
     public List<ShhTicketRulesReqVO> calculateTotalPrices(List<PzRawDataVo> list) {
         List<ShhTicketRulesReqVO> returnList = new ArrayList<>();
-        Map<String, PzRawDataVo> map = new HashMap<>();
+        Map<String, ShhTicketRulesReqVO> map = new HashMap<>();
+        List<String> keyByValue = TicketRulesEnum.getAllKeys();
+        keyByValue.forEach(name -> {
+            ShhTicketRulesReqVO ticketRulesReqVO = new ShhTicketRulesReqVO();
+            ticketRulesReqVO.setTicketName(name);
+            map.put(name, ticketRulesReqVO);
+        });
         list.forEach(e -> {
-            List<String> keyByValue = TicketRulesEnum.getKeyByValues(e.getTicketId());
-            if (!CollectionUtils.isEmpty(keyByValue)) {
-                keyByValue.forEach(name -> {
-                    if (map.containsKey(name)) {
-                        //如果有相同的票种,则累加数量和金额
-                        PzRawDataVo pzRawDataVo = map.get(name);
-                        pzRawDataVo.setNumber(pzRawDataVo.getNumber() + e.getNumber());
-                        pzRawDataVo.setMoney(pzRawDataVo.getMoney() == null ? BigDecimal.ZERO : pzRawDataVo.getMoney().add(e.getMoney()));
-                    } else {
-                        PzRawDataVo newPzRawDataVo = new PzRawDataVo();
-                        newPzRawDataVo.setNumber(e.getNumber());
-                        newPzRawDataVo.setMoney(e.getMoney());
-                        map.put(name, newPzRawDataVo);
-                    }
-                });
-            }
+            keyByValue.forEach(name -> {
+                if(name.equals(TicketRulesEnum.getKeyByValue(e.getTicketId()))) {
+                    //如果有相同的票种,则累加数量和金额
+                    ShhTicketRulesReqVO ticketRulesReqVO = map.get(name);
+                    ticketRulesReqVO.setNumberSheets(ticketRulesReqVO.getNumberSheets() + e.getNumber());
+                    ticketRulesReqVO.setAmount(ticketRulesReqVO.getAmount() == null ? BigDecimal.ZERO : ticketRulesReqVO.getAmount().add(e.getMoney()));
+                }
+            });
         });
-        map.forEach((k, v) -> returnList.add(new ShhTicketRulesReqVO(k, v.getNumber(), v.getMoney())));
+        map.forEach((k, v) -> returnList.add(new ShhTicketRulesReqVO(k, v.getNumberSheets(), v.getAmount())));
         return returnList;
     }
 

+ 68 - 0
songhua-system/src/main/java/com/songhua/system/utils/FileToMultipartFile.java

@@ -0,0 +1,68 @@
+package com.songhua.system.utils;
+
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.*;
+
+public class FileToMultipartFile implements MultipartFile {
+
+    private final File file;
+
+    public FileToMultipartFile(File file) {
+        this.file = file;
+    }
+
+    @Override
+    public String getName() {
+        return file.getName();
+    }
+
+    @Override
+    public String getOriginalFilename() {
+        return file.getName();
+    }
+
+    @Override
+    public String getContentType() {
+        return "application/octet-stream";
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return file.length() == 0;
+    }
+
+    @Override
+    public long getSize() {
+        return file.length();
+    }
+
+    @Override
+    public byte[] getBytes() throws IOException {
+        InputStream is = new FileInputStream(file);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        byte[] buffer = new byte[1024];
+        int len;
+        while ((len = is.read(buffer)) != -1) {
+            baos.write(buffer, 0, len);
+        }
+        return baos.toByteArray();
+    }
+
+    @Override
+    public InputStream getInputStream() throws IOException {
+        return new FileInputStream(file);
+    }
+
+    @Override
+    public void transferTo(File dest) throws IOException, IllegalStateException {
+        try (InputStream is = new FileInputStream(file);
+             OutputStream os = new FileOutputStream(dest)) {
+            byte[] buffer = new byte[1024];
+            int len;
+            while ((len = is.read(buffer)) != -1) {
+                os.write(buffer, 0, len);
+            }
+        }
+    }
+}