whyclxw
2025-03-06 eb33c60306f707ec7b30a048388e4de702ca2f15
src/main/java/com/whyc/service/SOPService.java
@@ -1,13 +1,17 @@
package com.whyc.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.SOPLockLog;
import com.whyc.pojo.SOPProduct;
import com.whyc.util.CommonUtil;
import com.whyc.util.DateUtil;
import com.whyc.util.FileUtil;
import com.whyc.util.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
@@ -16,11 +20,9 @@
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;
@@ -36,10 +38,18 @@
    @Autowired
    private SOPProductService productService;
    @Autowired
    private SOPFileTypeService fileTypeService;
    @Autowired
    private SOPLockLogService sopLockLogService;
    @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());
@@ -51,7 +61,8 @@
        Sheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        //文件名
        sop.setFileName(sheet.getRow(2).getCell(2).getStringCellValue());
        String fileName = sheet.getRow(2).getCell(2).getStringCellValue().trim();
        sop.setFileName(fileName);
        //下面的row是动态的,因为文件类型的行数是动态拓展的
        //锚点:文件版本
        int fileVersionIndex = 0;
@@ -228,8 +239,18 @@
        sop.setFileTypeList(fileTypeList);
        sop.setFileType(fileTypeSb.toString());
        sop.setFileVersion(sheet.getRow(fileVersionIndex).getCell(2).getStringCellValue());
        //校验文件版本是否已经存在
        String fileVersion = sheet.getRow(fileVersionIndex).getCell(2).getStringCellValue().trim();
        QueryWrapper<SOP> query = Wrappers.query();
        query.select("file_version").eq("file_name",fileName);
        List<SOP> sopListExists = mapper.selectList(query);
        for (int i = 0; i < sopListExists.size(); i++) {
            SOP sopExists = sopListExists.get(i);
            if(sopExists.getFileVersion().equals(fileVersion)){
                return new Response().set(1,false,"文件版本已存在,请确认SOP版本是否正确!");
            }
        }
        sop.setFileVersion(fileVersion);
        sop.setFileRelatedVersion(sheet.getRow(fileVersionIndex).getCell(7).getStringCellValue());
        sop.setEditor(sheet.getRow(fileVersionIndex+1).getCell(2).getStringCellValue());
@@ -256,6 +277,7 @@
        sop.setReleaseNotes(releaseNotes);
        //第10行开始,倒数第2行截止
        int currentFlag=0;  //通用表示(0:非通用,1:通用)
        for (int i = fileSuitableProductIndex+1; i < lastRowNum; i++) {
            SOPProduct product = new SOPProduct();
            //取第3列,第5列
@@ -276,17 +298,20 @@
            }else{
                product.setModel(model);
            }
            if((!code.equals("") && !model.equals(""))){
                currentFlag+=1;
            }
            sopProductList.add(product);
        }
        //存储excel文件,/sop_submit/xxx_202306250506.xlsx
        String originalFilename = multipartFile.getOriginalFilename();
        sop.setCurrentFlag(currentFlag>0?0:1);//通用
        //存储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);
        sop.setFileUrl(fileUrl);*/
        sopProductList = sopProductList.stream().filter(product -> !product.getCode().equals("")).collect(Collectors.toList());
        sop.setSopProductList(sopProductList);
@@ -295,28 +320,97 @@
    }
    @Transactional
    public Response add(SOP sop) throws IOException {
    public Response add(SOP sop,MultipartFile multipartFile) throws IOException {
        //是否存在旧版本,存在则把所有旧版本状态更新为0
        String userName = ActionUtil.getUser().getName();
        Date now = new Date();
        QueryWrapper<SOP> query = Wrappers.query();
        query.eq("file_name",sop.getFileName()).eq("status",1);
        List<SOP> sopListExists = mapper.selectList(query);
        if(sopListExists.size()>0){
            for (SOP sopExists : sopListExists) {
                sopExists.setStatus(0);
                mapper.updateById(sopExists);
                SOPLockLog lockLog = new SOPLockLog();
                lockLog.setSopId(sopExists.getId());
                lockLog.setReason("新版本上传,系统自动锁定旧版本");
                lockLog.setUserName(userName);
                lockLog.setCreateTime(now);
                lockLog.setStatus(0);
                sopLockLogService.insert(lockLog);
            }
        }
        List<SOPProduct> sopProductList = sop.getSopProductList();
         String originalFilename = multipartFile.getOriginalFilename();
        String[] fileNameSplit = originalFilename.split("\\.");
        String dateFormat = DateUtil.YYYY_MM_DD_HH_MM_SS_UNION.format(now);
        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);
        sop.setStatus(1);
        mapper.insert(sop);
        sopProductList.forEach(product -> product.setSopId(sop.getId()));
        productService.insertBatch(sopProductList);
        if(sopProductList.size()>0) {
            sopProductList.forEach(product -> product.setSopId(sop.getId()));
            productService.insertBatch(sopProductList);
        }
        //文件转移
        String fileUrl = sop.getFileUrl();
        String fileNameFrom = fileUrl.substring(fileUrl.lastIndexOf("/"));
        /*String fileUrl = sop.getFileUrl();
        String projectDir = CommonUtil.getProjectDir();
        String filePathFrom = projectDir + File.separator + fileUrl;
        String filePathFrom = projectDir + File.separator + fileUrl.replace("sop","sop_submit");
        File fileFrom = new File(filePathFrom);
        String filePathTo = projectDir + File.separator + "/sop" + fileNameFrom;
        String filePathTo = filePathFrom.replace("sop_submit","sop");
        File fileTo = new File(filePathTo);
        if(!fileTo.getParentFile().exists()){
            fileTo.getParentFile().mkdirs();
        }
        FileCopyUtils.copy(fileFrom,fileTo);
        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());
            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, Integer status,String fileName, int pageCurr, int pageSize) {
        String sqlStr="  ";
        if(sqlList!=null&&sqlList.size()>0){
            for (SopDto dto:sqlList) {
@@ -324,10 +418,54 @@
                    for (int i=0;i<dto.getChileType().size();i++){
                        sqlStr+=" and file_type like "+"'%"+dto.getParentType()+"-"+dto.getChileType().get(i)+"%' ";
                    }
                }else{
                    sqlStr+=" and file_type like "+"'%"+dto.getParentType()+"%' ";
                }
            }
        }
        if(status!=null) {
            sqlStr += " and status = "+status;
        }
        if(fileName!=null) {
            sqlStr += " and file_name like "+"'%"+fileName+"%' ";
        }
        List<SOP> list=mapper.getSopInfo(sqlStr,code,model);
        return new Response().setII(1,list.size()>0,list,"查询sop信息");
        PageInfo pageInfo= PageInfoUtils.list2PageInfo(list,pageCurr,pageSize);
        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说明完成");
    }
    @Transactional
    public Response updateStatusById(SOPLockLog lockLog) {
        Integer id = lockLog.getSopId();
        Integer status = lockLog.getStatus();
        UpdateWrapper<SOP> update = Wrappers.update();
        update.set("status",status).eq("id",id);
        mapper.update(null,update);
        //添加日志
        String name = ActionUtil.getUser().getName();
        lockLog.setUserName(name);
        lockLog.setCreateTime(new Date());
        sopLockLogService.insert(lockLog);
        return new Response().setII(1,"设置完成");
    }
    //查询sop历史
    public Response getSopHis(String fileName) {
        List<SOP> list=mapper.getSopHis(fileName);
        return new Response().setII(1,list.size()>0,list,"查询sop历史");
    }
}