whycxzp
2023-06-25 7951879338b046e000d39ebb6fbcfae16ff47324
src/main/java/com/whyc/service/SOPService.java
@@ -7,14 +7,20 @@
import com.whyc.pojo.SOPProduct;
import com.whyc.util.CommonUtil;
import com.whyc.util.DateUtil;
import com.whyc.util.FileUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
@@ -27,13 +33,18 @@
    @Resource
    private SOPMapper mapper;
    @Autowired
    private SOPProductService productService;
    public Response<Object> excelParse(InputStream inputStream) throws IOException, InvalidFormatException {
    @Transactional
    public Response<Object> excelParse(MultipartFile multipartFile) throws IOException, InvalidFormatException {
        SOP sop = new SOP();
        List<SOPProduct> sopProductList = new LinkedList<>();
        sop.setCreateTime(new Date());
        Workbook workbook = null;
        InputStream inputStream = multipartFile.getInputStream();
        workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
        //取第一个sheet表
@@ -273,10 +284,17 @@
            }else{
                product.setModel(model);
            }
            //TODO 存储excel文件
            sopProductList.add(product);
        }
        //存储excel文件,/sop_submit/xxx_202306250506.xlsx
        String originalFilename = multipartFile.getOriginalFilename();
        String[] fileNameSplit = originalFilename.split("\\.");
        String dateFormat = DateUtil.YYYY_MM_DD_HH_MM_SS_UNION.format(new Date());
        String newFileName = fileNameSplit[0]+"_"+dateFormat+"."+fileNameSplit[1];
        String fileUrl = FileUtil.saveFile(multipartFile,"/sop_submit/"+newFileName);
        sop.setFileUrl(fileUrl);
        sopProductList = sopProductList.stream().filter(product -> !product.getCode().equals("")).collect(Collectors.toList());
        sop.setSopProductList(sopProductList);
@@ -284,8 +302,25 @@
        return new Response().setII(1,true,sop,"文件解析成功");
    }
    public Response add(SOP sop) {
        return null;
    @Transactional
    public Response add(SOP sop) throws IOException {
        List<SOPProduct> sopProductList = sop.getSopProductList();
        mapper.insert(sop);
        sopProductList.forEach(product -> product.setSopId(sop.getId()));
        productService.insertBatch(sopProductList);
        //文件转移
        String fileUrl = sop.getFileUrl();
        String fileNameFrom = fileUrl.substring(fileUrl.lastIndexOf("/"));
        String projectDir = CommonUtil.getProjectDir();
        String filePathFrom = projectDir + File.separator + fileUrl;
        File fileFrom = new File(filePathFrom);
        String filePathTo = projectDir + File.separator + "/sop" + fileNameFrom;
        File fileTo = new File(filePathTo);
        if(!fileTo.getParentFile().exists()){
            fileTo.getParentFile().mkdirs();
        }
        FileCopyUtils.copy(fileFrom,fileTo);
        return new Response().setII(1,"上传完成");
    }
    //查询sop信息