lxw
2023-06-26 6985cc8f84d3f6e6b1dc6b7002edc13bbca457c2
src/main/java/com/whyc/service/SOPService.java
@@ -1,20 +1,33 @@
package com.whyc.service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.Response;
import com.whyc.dto.SopDto;
import com.whyc.mapper.SOPMapper;
import com.whyc.pojo.SOP;
import com.whyc.pojo.SOPFileType;
import com.whyc.pojo.SOPProduct;
import com.whyc.pojo.Software;
import com.whyc.util.ActionUtil;
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 +40,23 @@
    @Resource
    private SOPMapper mapper;
    @Autowired
    private SOPProductService productService;
    public Response<Object> excelParse(InputStream inputStream) throws IOException, InvalidFormatException {
    @Autowired
    private SOPFileTypeService fileTypeService;
    @Transactional
    public Response<Object> excelParse(MultipartFile multipartFile) throws IOException, InvalidFormatException {
        SOP sop = new SOP();
        String userName = ActionUtil.getUser().getName();
        sop.setUploadUser(userName);
        List<SOPProduct> sopProductList = new LinkedList<>();
        sop.setCreateTime(new Date());
        Workbook workbook = null;
        InputStream inputStream = multipartFile.getInputStream();
        workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
        //取第一个sheet表
@@ -82,10 +105,9 @@
                            break;
                        }
                        if(cellValue2.contains("R")){
                            fileTypeSb.append(cellValue).append("-").append(cellValue2).append(";");
                            break;
                            fileTypeSb.append(cellValue).append("-").append(cellValue2.substring(1)).append(";");
                        }
                        cellList.add(cellValue2);
                        cellList.add(cellValue2.substring(1));
                    }
@@ -99,10 +121,9 @@
                            break;
                        }
                        if(cellValue3.contains("R")){
                            fileTypeSb.append(cellValue).append("-").append(cellValue3).append(";");
                            break;
                            fileTypeSb.append(cellValue).append("-").append(cellValue3.substring(1)).append(";");
                        }
                        cellList.add(cellValue3);
                        cellList.add(cellValue3.substring(1));
                    }
@@ -116,10 +137,9 @@
                            break;
                        }
                        if(cellValue4.contains("R")){
                            fileTypeSb.append(cellValue).append("-").append(cellValue4).append(";");
                            break;
                            fileTypeSb.append(cellValue).append("-").append(cellValue4.substring(1)).append(";");
                        }
                        cellList.add(cellValue4);
                        cellList.add(cellValue4.substring(1));
                    }
@@ -133,10 +153,9 @@
                            break;
                        }
                        if(cellValue5.contains("R")){
                            fileTypeSb.append(cellValue).append("-").append(cellValue5).append(";");
                            break;
                            fileTypeSb.append(cellValue).append("-").append(cellValue5.substring(1)).append(";");
                        }
                        cellList.add(cellValue5);
                        cellList.add(cellValue5.substring(1));
                    }
                }
@@ -159,10 +178,9 @@
                            break;
                        }
                        if(cellValue2.contains("R")){
                            fileTypeSb.append(cellValueRight).append("-").append(cellValue2).append(";");
                            break;
                            fileTypeSb.append(cellValueRight).append("-").append(cellValue2.substring(1)).append(";");
                        }
                        cellList.add(cellValue2);
                        cellList.add(cellValue2.substring(1));
                    }
@@ -176,10 +194,9 @@
                            break;
                        }
                        if(cellValue3.contains("R")){
                            fileTypeSb.append(cellValueRight).append("-").append(cellValue3).append(";");
                            break;
                            fileTypeSb.append(cellValueRight).append("-").append(cellValue3.substring(1)).append(";");
                        }
                        cellList.add(cellValue3);
                        cellList.add(cellValue3.substring(1));
                    }
@@ -193,10 +210,9 @@
                            break;
                        }
                        if(cellValue4.contains("R")){
                            fileTypeSb.append(cellValueRight).append("-").append(cellValue4).append(";");
                            break;
                            fileTypeSb.append(cellValueRight).append("-").append(cellValue4.substring(1)).append(";");
                        }
                        cellList.add(cellValue4);
                        cellList.add(cellValue4.substring(1));
                    }
@@ -210,10 +226,9 @@
                            break;
                        }
                        if(cellValue5.contains("R")){
                            fileTypeSb.append(cellValueRight).append("-").append(cellValue5).append(";");
                            break;
                            fileTypeSb.append(cellValueRight).append("-").append(cellValue5.substring(1)).append(";");
                        }
                        cellList.add(cellValue5);
                        cellList.add(cellValue5.substring(1));
                    }
                }
@@ -273,10 +288,20 @@
            }else{
                product.setModel(model);
            }
            //TODO 存储excel文件
            if((code.equals("") && model.equals(""))){
                sop.setCurrentFlag(1);//通用
            }
            sopProductList.add(product);
        }
        //存储excel文件,/sop_submit/xxx_202306250506.xlsx(改,不存粗excel了)
        /*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,12 +309,78 @@
        return new Response().setII(1,true,sop,"文件解析成功");
    }
    public Response add(SOP sop) {
        return null;
    @Transactional
    public Response add(SOP sop,MultipartFile multipartFile) throws IOException {
        List<SOPProduct> sopProductList = sop.getSopProductList();
         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 fileUrlTemp = FileUtil.saveFile(multipartFile,"/sop/"+newFileName);
        //String fileUrlTemp = sop.getFileUrl();
        //fileUrlTemp = fileUrlTemp.replace("sop_submit", "sop");
        sop.setFileUrl(fileUrlTemp);
        mapper.insert(sop);
        if(sopProductList.size()>0) {
            sopProductList.forEach(product -> product.setSopId(sop.getId()));
            productService.insertBatch(sopProductList);
        }
        //文件转移
        /*String fileUrl = sop.getFileUrl();
        String projectDir = CommonUtil.getProjectDir();
        String filePathFrom = projectDir + File.separator + fileUrl.replace("sop","sop_submit");
        File fileFrom = new File(filePathFrom);
        String filePathTo = filePathFrom.replace("sop_submit","sop");
        File fileTo = new File(filePathTo);
        if(!fileTo.getParentFile().exists()){
            fileTo.getParentFile().mkdirs();
        }
        FileCopyUtils.copy(fileFrom,fileTo);*/
        //新增的文件类型,追加到表中
        List<SOPFileType> typeList = new LinkedList<>();
        List<Map<String, List<String>>> fileTypeList = sop.getFileTypeList();
        for (int i = 0; i < fileTypeList.size(); i++) {
            Map<String, List<String>> map = fileTypeList.get(i);
            Set<String> type1Set = map.keySet();
            for (String type1 : type1Set) {
                List<String> type2List = map.get(type1);
                for (String type2 : type2List) {
                    SOPFileType fileType = new SOPFileType();
                    fileType.setType1(type1);
                    fileType.setType2(type2);
                    typeList.add(fileType);
                }
            }
        }
        List<SOPFileType> recordListInDB = fileTypeService.getAllInDB(typeList);
        if(typeList.size()!=recordListInDB.size()) {
            List<SOPFileType> newRecordList = new LinkedList<>();
            List<String> uploadTypeStrList = typeList.stream().map(type -> type.getType1() + ";" + type.getType2()).collect(Collectors.toList());
            List<String> dbTypeStrList = recordListInDB.stream().map(type -> type.getType1() + ";" + type.getType2()).collect(Collectors.toList());
            Date now = new Date();
            for (int i = 0; i < uploadTypeStrList.size(); i++) {
                String uploadTypeStr = uploadTypeStrList.get(i);
                if (!dbTypeStrList.contains(uploadTypeStr)) {
                    SOPFileType sopFileType = new SOPFileType();
                    sopFileType.setType1(uploadTypeStr.split(";")[0]);
                    sopFileType.setType2(uploadTypeStr.split(";")[1]);
                    sopFileType.setCreateTime(now);
                    newRecordList.add(sopFileType);
                }
            }
            if (newRecordList.size() > 0) {
                fileTypeService.addBatch(newRecordList);
            }
        }
        return new Response().setII(1,"上传完成");
    }
    //查询sop信息
    public Response getSopInfo(List<SopDto> sqlList, String code, String model) {
    public Response getSopInfo(List<SopDto> sqlList, String code, String model,int pageCurr,int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
        String sqlStr="  ";
        if(sqlList!=null&&sqlList.size()>0){
            for (SopDto dto:sqlList) {
@@ -301,6 +392,19 @@
            }
        }
        List<SOP> list=mapper.getSopInfo(sqlStr,code,model);
        return new Response().setII(1,list.size()>0,list,"查询sop信息");
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list.size()>0,pageInfo,"查询sop信息");
    }
    //更新sop说明(不含锁信息)
    @Transactional
    public Response updateSop(SOP sop) {
        List<SOPProduct> sopProductList = sop.getSopProductList();
        mapper.updateById(sop);
        if(sopProductList.size()>0) {
            sopProductList.forEach(product -> product.setSopId(sop.getId()));
            productService.updateBySid(sopProductList,sop.getId());
        }
        return new Response().setII(1,"更新sop说明完成");
    }
}