|
@@ -2,9 +2,14 @@ package com.rongwei.bscommon.sys.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.io.file.FileNameUtil;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.core.util.ZipUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.rongwei.bscommon.sys.service.ZhcxInsideInspectionDispatchService;
|
|
|
import com.rongwei.bscommon.sys.service.ZhcxInsideInspectionOperLogService;
|
|
@@ -28,12 +33,21 @@ import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
import com.rongwei.rwcommon.utils.StringUtils;
|
|
|
import com.rongwei.rwcommon.vo.generalsql.MasterSlaveUpdateVo;
|
|
|
+import com.rongwei.rwcommoncomponent.excel.aspose.ExcelFormData;
|
|
|
+import com.rongwei.rwcommoncomponent.excel.vo.FormData;
|
|
|
+import com.rongwei.rwcommoncomponent.file.service.SysFileItemService;
|
|
|
+import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
|
|
|
import com.rongwei.rwcommonentity.commonservers.vo.SysSerialVo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -70,6 +84,12 @@ public class ZhcxInsideInspectionServiceImpl extends ServiceImpl<ZhcxInsideInspe
|
|
|
@Autowired
|
|
|
private ZhcxInsideInspectionDao dao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SysFileItemService sysFileItemService;
|
|
|
+
|
|
|
+ @Value("${temp.filepath:#{null}}")
|
|
|
+ private String filepath;
|
|
|
+
|
|
|
/**
|
|
|
* 保存后操作
|
|
|
*
|
|
@@ -270,6 +290,145 @@ public class ZhcxInsideInspectionServiceImpl extends ServiceImpl<ZhcxInsideInspe
|
|
|
operLogService.saveOperLog(req, operDto, descMap);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 下载附件
|
|
|
+ *
|
|
|
+ * @param exportReq
|
|
|
+ * @param response
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int downloadFile(ZhcxInsideExportRequest exportReq, HttpServletResponse response) {
|
|
|
+ if(StringUtils.isEmpty(exportReq.getInsideId())){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ String insideId = exportReq.getInsideId();
|
|
|
+ ZhcxInsideInspectionDo inspection = getById(exportReq.getInsideId());
|
|
|
+ LambdaQueryWrapper<ZhcxInsideInspectionDispatchDo> wrapper = Wrappers.lambdaQuery();
|
|
|
+ wrapper.eq(ZhcxInsideInspectionDispatchDo::getInsideid, insideId);
|
|
|
+ ZhcxInsideInspectionDispatchDo dispatch = dispatchService.getOne(wrapper);
|
|
|
+ List<String> fileIds = new ArrayList<>();
|
|
|
+ if(ObjectUtil.isNotNull(dispatch)){
|
|
|
+ String sfiles = dispatch.getSfiles();
|
|
|
+ if(StringUtils.isNotBlank(sfiles)){
|
|
|
+ zhcxCommon.getIds(sfiles, fileIds);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String files = inspection.getFiles();
|
|
|
+ if(StringUtils.isNotBlank(files)){
|
|
|
+ zhcxCommon.getIds(files, fileIds);
|
|
|
+ }
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String classPath = filepath;
|
|
|
+ // 临时目录 以uuid+时间戳 为目录名称
|
|
|
+ String temp = classPath + File.separator + IdUtil.simpleUUID() + System.currentTimeMillis();
|
|
|
+ log.info("临时目录:"+temp);
|
|
|
+ // 临时目录如果存在则删除
|
|
|
+ File tempDir = new File(temp);
|
|
|
+ File zipFile = null;
|
|
|
+ log.info("临时目录是否存在:"+tempDir.exists());
|
|
|
+ try {
|
|
|
+ if (tempDir.exists()) {
|
|
|
+ FileUtil.del(tempDir);
|
|
|
+ }
|
|
|
+ // 创建临时目录
|
|
|
+ boolean mkdir = tempDir.mkdir();
|
|
|
+ log.info("临时目录是否创建:"+mkdir);
|
|
|
+ Map<String, Integer> fileNameCount = new HashMap<>();
|
|
|
+ String format = formatter.format(inspection.getReservationinspectiontime());
|
|
|
+ String fileFullName = inspection.getProjectcode()+"-"+inspection.getProjectname()+
|
|
|
+ "-"+inspection.getStructurename()+"-"+inspection.getInspectionpoint()+
|
|
|
+ "-"+format;
|
|
|
+ fileFullName = fileFullName.replaceAll("/","_");
|
|
|
+
|
|
|
+ //模板处理
|
|
|
+ ExcelFormData excelData = exportReq.getExcelData();
|
|
|
+ if(ObjectUtil.isNotEmpty(excelData.getTemplateId())) {
|
|
|
+ excelData.setExcelName(fileFullName+".pdf");
|
|
|
+ SysFileItemDo sysFileItemDo = sysFileItemService.getById(excelData.getTemplateId());
|
|
|
+ FormData formData = zhcxCommon.parseExcelPage2Data(excelData, sysFileItemDo);
|
|
|
+ zhcxCommon.parseTemplateByAspose(sysFileItemDo, excelData, temp, formData);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(fileIds.size() > 0){
|
|
|
+ QueryWrapper<SysFileItemDo> qw = new QueryWrapper();
|
|
|
+ qw.in("ID", fileIds);
|
|
|
+ List<SysFileItemDo> list = sysFileItemService.list(qw);
|
|
|
+ log.info("下载数据数量:"+list.size());
|
|
|
+ if(list.size() > 0){
|
|
|
+ if (mkdir) {
|
|
|
+ list.forEach(item->{
|
|
|
+ String filename = item.getFilename();
|
|
|
+ boolean boo = fileNameCount.containsKey(filename);
|
|
|
+ int i = 0;
|
|
|
+ if(boo){
|
|
|
+ i = fileNameCount.get(filename);
|
|
|
+ }
|
|
|
+ fileNameCount.put(filename,i+1);
|
|
|
+ if(i > 0){
|
|
|
+ String prefix = FileNameUtil.getPrefix(filename);
|
|
|
+ String extName = FileNameUtil.extName(filename);
|
|
|
+ filename = prefix+"("+i+")"+"."+extName;
|
|
|
+ }
|
|
|
+ FileUtil.copyFile(new File(item.getFullpath()),
|
|
|
+ new File(temp+File.separator+filename));
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //没有附件且模板为空时
|
|
|
+ if(ObjectUtil.isEmpty(excelData.getTemplateId())) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String encodedFileName = URLEncoder.encode(fileFullName+".zip", "UTF-8").replaceAll("\\+", "%20");
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setHeader("content-type", "application/octet-stream");
|
|
|
+ response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", encodedFileName));
|
|
|
+
|
|
|
+ // 将临时目录压缩成zip
|
|
|
+ zipFile = ZipUtil.zip(temp);
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ FileInputStream fis = null;
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ try{
|
|
|
+ fis = new FileInputStream(zipFile);
|
|
|
+ bis = new BufferedInputStream(fis);
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
+ int i = bis.read(buffer);
|
|
|
+ while (i != -1) {
|
|
|
+ os.write(buffer, 0, i);
|
|
|
+ i = bis.read(buffer);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("下载压缩资料报错:"+e);
|
|
|
+ }finally {
|
|
|
+ if (bis != null) {
|
|
|
+ try {
|
|
|
+ bis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fis != null) {
|
|
|
+ try {
|
|
|
+ fis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("下载资料报错, {}", e);
|
|
|
+ return 1;
|
|
|
+ }finally {
|
|
|
+ FileUtil.del(tempDir);
|
|
|
+ FileUtil.del(zipFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ return 2;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 派单和执行
|
|
|
*
|