Fixiaobai
2023-10-30 10e199f950f5c74b25e446433ceb51cffe675be4
mes-technology/src/main/java/com/chinaztt/mes/technology/service/impl/DocumentServiceImpl.java
@@ -17,6 +17,7 @@
package com.chinaztt.mes.technology.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.BooleanUtil;
@@ -53,6 +54,7 @@
import com.chinaztt.mes.technology.entity.*;
import com.chinaztt.mes.technology.excel.DocumentTestStandardData;
import com.chinaztt.mes.technology.excel.MaterialCostData;
import com.chinaztt.mes.technology.feign.MesFileApi;
import com.chinaztt.mes.technology.mapper.*;
import com.chinaztt.mes.technology.service.DocumentJgtService;
import com.chinaztt.mes.technology.service.DocumentService;
@@ -72,13 +74,13 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.LocalDateTime;
@@ -127,6 +129,13 @@
   private DocumentJgtMapper documentJgtMapper;
   private DocumentJgtService documentJgtService;
   private DocumentSamplingRuleMapper documentSamplingRuleMapper;
   @Autowired
   private MesFileApi mesFileApi;
   private static final String locationImg="D:/file/img";
   private static final String file_img_url = "http://127.0.0.1:6066/Image/img";
    /**
     * @Author: Hans
@@ -600,23 +609,61 @@
    @SneakyThrows
    @Override
    public R uploadJgt(List<MultipartFile> file, Long documentId) {
        List<DocumentJgt> list = new ArrayList<>();
        for (MultipartFile multipartFile:file) {
            String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(multipartFile.getOriginalFilename());
            minioTemplate.putObject(OSS_BUCKET, fileName, multipartFile.getInputStream());
            DocumentJgt jgt = new DocumentJgt();
            jgt.setFileName(fileName);
            jgt.setDocumentId(documentId);
            list.add(jgt);
    public R uploadJgt(MultipartFile file, Long documentId) {
      String requestUrl="";
      String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
      //minioTemplate.putObject(OSS_BUCKET, fileName, multipartFile.getInputStream());
      //创建文件夹
      String timeForder= DateUtil.format(DateUtil.date(),"yyyy/MM/dd");
      String forder=locationImg+"/"+OSS_BUCKET+"/"+timeForder;
      File filePath = new File(forder);
      if(!filePath.exists()){
         filePath.mkdirs();
        }
        //工艺主表更新时间,更新人
        Document document = new Document();
        document.setId(documentId);
        baseMapper.updateById(doUpdateRealUser(document));
        documentJgtService.saveBatch(list);
        List<String> fileNameList = list.stream().map(a -> a.getFileName()).collect(Collectors.toList());
        return R.ok(org.apache.commons.lang3.StringUtils.join(fileNameList,","));
      //合并文件
      RandomAccessFile raFile = null;
      BufferedInputStream inputStream=null;
      try{
         File dirFile = new File(forder, fileName);
         //以读写的方式打开目标文件
         raFile = new RandomAccessFile(dirFile, "rw");
         raFile.seek(raFile.length());
         inputStream = new BufferedInputStream(file.getInputStream());
         byte[] buf = new byte[1024];
         int length = 0;
         while ((length = inputStream.read(buf)) != -1) {
            raFile.write(buf, 0, length);
         }
         requestUrl=OSS_BUCKET+"/"+timeForder+"/"+fileName;
      }catch(Exception e){
         e.printStackTrace();
      }finally{
         try {
            if (inputStream != null) {
               inputStream.close();
            }
            if (raFile != null) {
               raFile.close();
            }
         }catch(Exception ignored){
         }
      }
      String url = requestUrl;
      QueryWrapper<DocumentJgt>queryWrapper=new QueryWrapper<>();
      queryWrapper.lambda().eq(DocumentJgt::getDocumentId,documentId);
      DocumentJgt documentJgt = documentJgtMapper.selectOne(queryWrapper);
      if(Objects.nonNull(documentJgt)){
         UpdateWrapper<DocumentJgt>updateWrapper=new UpdateWrapper<>();
         updateWrapper.lambda().eq(DocumentJgt::getDocumentId,documentId).set(DocumentJgt::getFileName,locationImg+"/"+url);
         documentJgtMapper.update(null,updateWrapper);
      }else {
         DocumentJgt jgt=new DocumentJgt();
         jgt.setDocumentId(documentId);
         jgt.setFileName(locationImg+"/"+url);
         documentJgtMapper.insert(jgt);
      }
        return R.ok(locationImg+"/"+url);
    }
    @SneakyThrows
@@ -668,7 +715,7 @@
    @Override
    public void getFile(String fileName, HttpServletResponse response) {
        try (InputStream inputStream = minioTemplate.getObject(OSS_BUCKET, fileName)) {
        try (InputStream inputStream = new FileInputStream(fileName)) {
            response.setContentType("application/octet-stream; charset=UTF-8");
            IoUtil.copy(inputStream, response.getOutputStream());
        } catch (Exception e) {
@@ -795,6 +842,10 @@
        Routing routing = routings.get(0);
        joinDocumentBomRouting.setRoutingId(routing.getId());
        joinDocumentBomRouting.setBomId(routing.getBomId());
      Routing routing1 = routingMapper.selectById(joinDocumentBomRouting.getRoutingId());
      if(Objects.equals(routing1.getState(),RoutingStateStringValues.DRAFT)){
         throw new RuntimeException("工艺路线状态未通过!");
      }
        joinDocumentBomRoutingMapper.insertAndGetId(joinDocumentBomRouting);
        // 添加检测标准
        addDocTestStandard(joinDocumentBomRouting);