|
@@ -1,21 +1,33 @@
|
|
|
package com.xunmei.system.service.impl;
|
|
package com.xunmei.system.service.impl;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
+import com.xunmei.common.core.constant.Constants;
|
|
|
|
|
+import com.xunmei.common.core.domain.registerbook.vo.PdfToZipTempVo;
|
|
|
|
|
+import com.xunmei.common.core.utils.DateHelper;
|
|
|
import com.xunmei.system.mapper.ObjectMapper;
|
|
import com.xunmei.system.mapper.ObjectMapper;
|
|
|
import com.xunmei.system.service.ExportSqlService;
|
|
import com.xunmei.system.service.ExportSqlService;
|
|
|
import com.xunmei.system.util.ExportDatabaseUtil;
|
|
import com.xunmei.system.util.ExportDatabaseUtil;
|
|
|
import com.xunmei.system.util.Sm4Util;
|
|
import com.xunmei.system.util.Sm4Util;
|
|
|
|
|
+import org.apache.tools.zip.ZipEntry;
|
|
|
|
|
+import org.apache.tools.zip.ZipOutputStream;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.boot.system.ApplicationHome;
|
|
import org.springframework.boot.system.ApplicationHome;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
|
|
+import java.io.FileInputStream;
|
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStream;
|
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
|
|
+import java.util.Date;
|
|
|
import java.util.LinkedHashMap;
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+import java.util.concurrent.CountDownLatch;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
public class ExportSqlServiceImpl implements ExportSqlService {
|
|
public class ExportSqlServiceImpl implements ExportSqlService {
|
|
@@ -58,6 +70,22 @@ public class ExportSqlServiceImpl implements ExportSqlService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
|
|
+ public void exportTableOneWithFilter(HttpServletResponse response, String tableName, String filterColumnName, String startDate, String endDate) throws Exception {
|
|
|
|
|
+ ApplicationHome home = new ApplicationHome(getClass());
|
|
|
|
|
+ String path = home.getSource().getParent();
|
|
|
|
|
+ File file = new File(path,tableName+"_"+startDate +"_"+endDate+ ".sql");
|
|
|
|
|
+ if(file.exists()) {
|
|
|
|
|
+ file.delete();
|
|
|
|
|
+ }
|
|
|
|
|
+ //为方便处理日期边界问题 将结束日期加1天
|
|
|
|
|
+ Date end= DateUtil.parse(endDate);
|
|
|
|
|
+ String newEndDateStr =DateUtil.format(DateUtil.offsetDay(end, 1), Constants.DAILY_FORMAT);
|
|
|
|
|
+ ExportDatabaseUtil.exportTableSqlWithDateFilter(url,username,password,tableName,filterColumnName,startDate,newEndDateStr,file);
|
|
|
|
|
+ downloadCompressFile(response,file);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
public void exportTables(HttpServletResponse response, List<String> tableNames) throws Exception {
|
|
public void exportTables(HttpServletResponse response, List<String> tableNames) throws Exception {
|
|
|
ApplicationHome home = new ApplicationHome(getClass());
|
|
ApplicationHome home = new ApplicationHome(getClass());
|
|
|
String path = home.getSource().getParent();
|
|
String path = home.getSource().getParent();
|
|
@@ -119,4 +147,84 @@ public class ExportSqlServiceImpl implements ExportSqlService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 导出数据
|
|
|
|
|
+ * @param response
|
|
|
|
|
+ * @param file
|
|
|
|
|
+ */
|
|
|
|
|
+ private void downloadCompressFile(HttpServletResponse response,File file){
|
|
|
|
|
+// OutputStream os;//新建一个输出流对象
|
|
|
|
|
+ try {
|
|
|
|
|
+ response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName()+".zip", "UTF-8"));
|
|
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
|
|
+ ServletOutputStream outputStream = response.getOutputStream();
|
|
|
|
|
+ ZipOutputStream zos = new ZipOutputStream(outputStream);
|
|
|
|
|
+ zos.setEncoding("GBK");
|
|
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
|
|
+ zos.putNextEntry(new ZipEntry(file.getName()));
|
|
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
|
|
+ int len;
|
|
|
|
|
+ while ((len = fileInputStream.read(buffer)) > 0) {
|
|
|
|
|
+ zos.write(buffer, 0, len);
|
|
|
|
|
+ }
|
|
|
|
|
+ zos.closeEntry();
|
|
|
|
|
+ fileInputStream.close();
|
|
|
|
|
+ zos.flush();
|
|
|
|
|
+ zos.close();
|
|
|
|
|
+ outputStream.close();
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+ System.out.println("文件下载失败");
|
|
|
|
|
+ }finally {
|
|
|
|
|
+ if(file.exists()){
|
|
|
|
|
+ file.delete();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// try {
|
|
|
|
|
+// String zipName = URLEncoder.encode(pdfDto.getFileName() + DateHelper.getDateString(new Date()) + ".zip", "UTF-8");
|
|
|
|
|
+// final CountDownLatch count = new CountDownLatch(registerBookPdfList.size());
|
|
|
|
|
+// ServletOutputStream outputStream = response.getOutputStream();
|
|
|
|
|
+// ZipOutputStream zos = new ZipOutputStream(outputStream);
|
|
|
|
|
+// response.setContentType("application/octet-stream");
|
|
|
|
|
+// response.setHeader("Content-Disposition", "attachment; filename=" + zipName);
|
|
|
|
|
+// List<PdfToZipTempVo> pdfToZipTempVoList = registerBookPdfList.parallelStream().map(pdf -> {
|
|
|
|
|
+// return resolve(pdf, count);
|
|
|
|
|
+// }).filter(Objects::nonNull)
|
|
|
|
|
+// .collect(Collectors.toList());
|
|
|
|
|
+// count.await();
|
|
|
|
|
+// pdfToZipTempVoList.removeIf(pdfToZipTempVo -> !FileUtil.exist(pdfToZipTempVo.getFile()));
|
|
|
|
|
+// log.info("登记簿全部下载完成,开始压缩文件,数量:{}", pdfToZipTempVoList.size());
|
|
|
|
|
+// for (PdfToZipTempVo tempVo : pdfToZipTempVoList) {
|
|
|
|
|
+// log.info("当前开始处理第{}个文件 ", pdfToZipTempVoList.indexOf(tempVo) + 1);
|
|
|
|
|
+// deal(zos, tempVo);
|
|
|
|
|
+// }
|
|
|
|
|
+// zos.flush();
|
|
|
|
|
+// zos.close();
|
|
|
|
|
+// outputStream.close();
|
|
|
|
|
+// log.info("登记簿批量导出压缩文件完成,文件数量:{}", pdfToZipTempVoList.size());
|
|
|
|
|
+// } catch (Throwable e) {
|
|
|
|
|
+// String errMsg = String.format("登记簿导出失败:%s", e);
|
|
|
|
|
+// log.error(errMsg);
|
|
|
|
|
+// } finally {
|
|
|
|
|
+// final File temp = new File(TEMP_DIR_NAME);
|
|
|
|
|
+// if (temp.exists()) {
|
|
|
|
|
+// FileUtil.del(temp);
|
|
|
|
|
+// log.info("临时目录已删除");
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void deal(ZipOutputStream zos, PdfToZipTempVo zipTempVo) throws Throwable {
|
|
|
|
|
+ zos.setEncoding("GBK");
|
|
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(zipTempVo.getFile());
|
|
|
|
|
+ zos.putNextEntry(new ZipEntry(zipTempVo.getEntryName()));
|
|
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
|
|
+ int len;
|
|
|
|
|
+ while ((len = fileInputStream.read(buffer)) > 0) {
|
|
|
|
|
+ zos.write(buffer, 0, len);
|
|
|
|
|
+ }
|
|
|
|
|
+ zos.closeEntry();
|
|
|
|
|
+ fileInputStream.close();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|