|
@@ -1,30 +1,191 @@
|
|
|
package com.caimei365.manager;
|
|
|
|
|
|
+import cn.hutool.core.util.ZipUtil;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.support.ExcelTypeEnum;
|
|
|
+import com.caimei365.manager.dao.user.CmMarketShopDao;
|
|
|
import com.caimei365.manager.entity.caimei.shopImport.*;
|
|
|
import com.caimei365.manager.service.caimei.listener.*;
|
|
|
+import com.caimei365.manager.utils.formDataUtils;
|
|
|
+import org.apache.commons.fileupload.disk.DiskFileItem;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Enumeration;
|
|
|
+import java.util.List;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipFile;
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
|
|
|
@SpringBootTest
|
|
|
class ManagerApplicationTests {
|
|
|
|
|
|
+ @Resource private formDataUtils formDataUtils;
|
|
|
+ @Resource private CmMarketShopDao marketShopDao;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void contextLoads() throws Exception{
|
|
|
+ String filePath = "E:\\Users\\Desktop\\采美\\报表文件上传模板.zip";
|
|
|
+
|
|
|
+ String inputPath = "E:\\Users\\Desktop\\dest\\test.zip";
|
|
|
+ String outPath = "E:\\Users\\Desktop\\extract";
|
|
|
+
|
|
|
+ File inputFile = new File(inputPath);
|
|
|
+ if (!inputFile.exists() && !inputFile.isDirectory()) {
|
|
|
+ inputFile.mkdirs();
|
|
|
+ }
|
|
|
+ File outFile = new File(outPath);
|
|
|
+ if (!inputFile.exists() && !outFile.isDirectory()) {
|
|
|
+ outFile.mkdirs();
|
|
|
+ }
|
|
|
+ File zipFile = new File(filePath);
|
|
|
+ /*MultipartFile multipartFile = fileToMultipartFileConverter(zipFile);
|
|
|
+ try {
|
|
|
+ multipartFile.transferTo(inputFile);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }*/
|
|
|
+ ZipUtil.unzip(inputFile, outFile);
|
|
|
+ System.out.println("inputFile======"+inputFile.getPath());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
- void contextLoads() {
|
|
|
- String fileName = "E:\\Users\\Desktop\\采美\\shop-xlsx\\test.xlsx";
|
|
|
-
|
|
|
- EasyExcel.read(fileName, stage.class, new StageListener()).sheet("阶段描述").doRead();
|
|
|
- EasyExcel.read(fileName, funnelModel.class, new FunnelModelListener()).sheet("漏斗模型数据").doRead();
|
|
|
- EasyExcel.read(fileName, ArticleReadVolume.class, new ArticleReadVolumeListener()).sheet("推文阅读量").doRead();
|
|
|
- EasyExcel.read(fileName, Impressions.class, new ImpressionsListener()).sheet("展现量").doRead();
|
|
|
- EasyExcel.read(fileName, ReadVolume.class, new ReadVolumeListener()).sheet("阅读量").doRead();
|
|
|
- EasyExcel.read(fileName, TypeDistribution.class, new TypeDistributionListener()).sheet("SEO汇总").doRead();
|
|
|
- EasyExcel.read(fileName, Visits.class, new VisitsLisener()).sheet("访问量").doRead();
|
|
|
- EasyExcel.read(fileName, Proportion.class, new ProportionListener()).sheet("访客来源分布").doRead();
|
|
|
- EasyExcel.read(fileName, PageDuration.class, new PageDurationListener()).sheet("页面平均时长").doRead();
|
|
|
- EasyExcel.read(fileName, MallHits.class, new MallHitsListener()).sheet("广告图点击量").doRead();
|
|
|
- EasyExcel.read(fileName, Remark.class, new RemarkListener()).sheet("咨询数量").doRead();
|
|
|
+ void test() throws IOException {
|
|
|
+ String fileDirectory = "E:\\Users\\Desktop\\采美\\报表文件上传模板";
|
|
|
+ // SEO 图片集合
|
|
|
+ List<String> seoImagePathList = new ArrayList<>();
|
|
|
+ // 广告图点击量 图片集合
|
|
|
+ List<String> advertImageHomeList = new ArrayList<>();
|
|
|
+ List<String> advertImageInfoList = new ArrayList<>();
|
|
|
+ File file = new File(fileDirectory);
|
|
|
+ if (file.exists() && file.isDirectory()) {
|
|
|
+ for (File fileSpecific: file.listFiles()) {
|
|
|
+ if (fileSpecific.getName().equals("SEO汇总图片") && fileSpecific.exists() && fileSpecific.isDirectory()) {
|
|
|
+ for (File fileSpecificContain : fileSpecific.listFiles()) {
|
|
|
+ if (fileSpecificContain.getName().contains("SEO汇总图")) {
|
|
|
+ // 上传图到服务器并获取图链接,存入数据库,图片顺序需与 SEO 列表顺序一致
|
|
|
+ MultipartFile filex = fileToMultipartFileConverter(fileSpecificContain);
|
|
|
+ String seoImagePath = formDataUtils.saveShopFile(filex);
|
|
|
+ seoImagePathList.add(seoImagePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fileSpecific.getName().equals("广告图片") && fileSpecific.exists() && fileSpecific.isDirectory()) {
|
|
|
+ for (File fileSpecificContain : fileSpecific.listFiles()) {
|
|
|
+ if (fileSpecificContain.getName().contains("首页")) {
|
|
|
+ // 上传图到服务器并获取图链接,存入数据库,图片顺序需与 广告图点击量 列表顺序一致
|
|
|
+ MultipartFile filex = fileToMultipartFileConverter(fileSpecificContain);
|
|
|
+ String advertImagePath = formDataUtils.saveShopFile(filex);
|
|
|
+ advertImageHomeList.add(advertImagePath);
|
|
|
+ }
|
|
|
+ if (fileSpecificContain.getName().contains("信息中心")) {
|
|
|
+ // 上传图到服务器并获取图链接,存入数据库,图片顺序需与 广告图点击量 列表顺序一致
|
|
|
+ MultipartFile filex = fileToMultipartFileConverter(fileSpecificContain);
|
|
|
+ String advertImagePath = formDataUtils.saveShopFile(filex);
|
|
|
+ advertImageInfoList.add(advertImagePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fileSpecific.getName().endsWith(".xlsx")) {
|
|
|
+ updateFile(fileSpecific, seoImagePathList, advertImageHomeList, advertImageInfoList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("seoImagePathList====="+seoImagePathList);
|
|
|
+ System.out.println("advertImageHomeList====="+advertImageHomeList);
|
|
|
+ System.out.println("advertImageInfoList====="+advertImageInfoList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ void updateFile(File file, List<String> seoImagePathList, List<String> advertImageHomeList, List<String> advertImageInfoList) throws IOException {
|
|
|
+ String excelType = "XLSX";
|
|
|
+ if (file.getName().endsWith(".xls")) {
|
|
|
+ excelType = "XLS";
|
|
|
+ }
|
|
|
+ Integer marketReportId = 1;
|
|
|
+ MultipartFile multipartFile = fileToMultipartFileConverter(file);
|
|
|
+ InputStream inputStream = new BufferedInputStream(multipartFile.getInputStream());
|
|
|
+ InputStream is = inputStream;
|
|
|
+ EasyExcel.read(is, Stage.class, new StageListener(marketReportId, marketShopDao)).excelType(ExcelTypeEnum.valueOf(excelType)).sheet("阶段描述").doRead();
|
|
|
+ is = new BufferedInputStream(multipartFile.getInputStream());
|
|
|
+ EasyExcel.read(is, FunnelModel.class, new FunnelModelListener(marketReportId, marketShopDao)).excelType(ExcelTypeEnum.valueOf(excelType)).sheet("漏斗模型数据").doRead();
|
|
|
+ is = new BufferedInputStream(multipartFile.getInputStream());
|
|
|
+ EasyExcel.read(is, ArticleReadVolume.class, new ArticleReadVolumeListener(marketReportId, marketShopDao)).excelType(ExcelTypeEnum.valueOf(excelType)).sheet("推文阅读量").doRead();
|
|
|
+ is = new BufferedInputStream(multipartFile.getInputStream());
|
|
|
+ EasyExcel.read(is, Impressions.class, new ImpressionsListener(marketReportId, marketShopDao)).excelType(ExcelTypeEnum.valueOf(excelType)).sheet("展现量").doRead();
|
|
|
+ is = new BufferedInputStream(multipartFile.getInputStream());
|
|
|
+ EasyExcel.read(is, ReadVolume.class, new ReadVolumeListener(marketReportId, marketShopDao)).excelType(ExcelTypeEnum.valueOf(excelType)).sheet("点击量").doRead();
|
|
|
+ is = new BufferedInputStream(multipartFile.getInputStream());
|
|
|
+ EasyExcel.read(is, Summary.class, new SummaryListener(marketReportId, marketShopDao, seoImagePathList)).excelType(ExcelTypeEnum.valueOf(excelType)).sheet("SEO汇总").doRead();
|
|
|
+ is = new BufferedInputStream(multipartFile.getInputStream());
|
|
|
+ EasyExcel.read(is, Visits.class, new VisitsLisener(marketReportId, marketShopDao)).excelType(ExcelTypeEnum.valueOf(excelType)).sheet("访问量").doRead();
|
|
|
+ is = new BufferedInputStream(multipartFile.getInputStream());
|
|
|
+ EasyExcel.read(is, Proportion.class, new ProportionListener(marketReportId, marketShopDao)).excelType(ExcelTypeEnum.valueOf(excelType)).sheet("访客来源分布").doRead();
|
|
|
+ is = new BufferedInputStream(multipartFile.getInputStream());
|
|
|
+ EasyExcel.read(is, PageDuration.class, new PageDurationListener(marketReportId, marketShopDao)).excelType(ExcelTypeEnum.valueOf(excelType)).sheet("页面平均时长").doRead();
|
|
|
+ is = new BufferedInputStream(multipartFile.getInputStream());
|
|
|
+ EasyExcel.read(is, AdvertHits.class, new AdvertHitsListener(marketReportId, marketShopDao, advertImageHomeList, advertImageInfoList)).excelType(ExcelTypeEnum.valueOf(excelType)).sheet("广告图点击量").doRead();
|
|
|
+ is = new BufferedInputStream(multipartFile.getInputStream());
|
|
|
+ EasyExcel.read(is, Remark.class, new RemarkListener(marketReportId, marketShopDao)).excelType(ExcelTypeEnum.valueOf(excelType)).sheet("咨询数量").doRead();
|
|
|
+ }
|
|
|
+
|
|
|
+ MultipartFile fileToMultipartFileConverter(File file) throws IOException {
|
|
|
+ DiskFileItem diskFileItem = new DiskFileItem("File", "application/octet-stream", true, file.getName(), (int) file.length(), file.getParentFile());
|
|
|
+ try(InputStream is = new FileInputStream(file)) {
|
|
|
+ diskFileItem.getOutputStream().write(IOUtils.toByteArray(is));
|
|
|
+ }
|
|
|
+ return new CommonsMultipartFile(diskFileItem);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testNumber() {
|
|
|
+ String zipFilePath = "E:\\Users\\Desktop\\dest\\test.zip";
|
|
|
+ String destDirectory = "E:\\Users\\Desktop\\extract";
|
|
|
+ try {
|
|
|
+ unzip(zipFilePath, destDirectory);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void unzip(String zipFilePath, String destDirectory) throws IOException {
|
|
|
+ File destDir = new File(destDirectory);
|
|
|
+ if (!destDir.exists()) {
|
|
|
+ destDir.mkdir();
|
|
|
+ }
|
|
|
+ ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
|
|
|
+ ZipEntry entry = zipIn.getNextEntry();
|
|
|
+ while (entry != null) {
|
|
|
+ String filePath = destDirectory + File.separator + entry.getName();
|
|
|
+ if (!entry.isDirectory()) {
|
|
|
+ extractFile(zipIn, filePath);
|
|
|
+ } else {
|
|
|
+ File dir = new File(filePath);
|
|
|
+ dir.mkdir();
|
|
|
+ }
|
|
|
+ zipIn.closeEntry();
|
|
|
+ entry = zipIn.getNextEntry();
|
|
|
+ }
|
|
|
+ zipIn.close();
|
|
|
+ }
|
|
|
|
|
|
+ private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
|
|
|
+ File file = new File(filePath);
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(file)) {
|
|
|
+ byte[] bytes = new byte[4096];
|
|
|
+ int length;
|
|
|
+ while ((length = zipIn.read(bytes)) != -1) {
|
|
|
+ fos.write(bytes, 0, length);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|