whycxzp
2022-10-17 04d13e3a41ef798e07c25c1d3ad2002270a9cb76
src/main/java/com/whyc/service/ProductService.java
@@ -3,7 +3,6 @@
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.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.FileUrlDTO;
import com.whyc.dto.Response;
@@ -11,21 +10,20 @@
import com.whyc.pojo.*;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
import com.whyc.util.DateUtil;
import com.whyc.util.FileUtil;
import com.whyc.util.Zip4jUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
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 java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
@Service
@@ -48,9 +46,12 @@
    @Autowired
    private MaterialService mService;
    @Autowired
    private ProductLockLogService productLockLogService;
    //查询出所有的产品信息(分页加模糊查询<产品的编码,型号,名字,定制表编号>
    public Response getAllProduct(String parentCode, String parentName, String parentModel, String customCode, int pageCurr, int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
    public Response getAllProduct(String subCode,String parentCode, String parentName, String parentModel, String customCode, int pageCurr, int pageSize) {
        /*PageHelper.startPage(pageCurr,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
        if(parentCode!=null){
            wrapper.like("parent_code",parentCode);
@@ -69,7 +70,8 @@
            }
        }
        wrapper.orderByAsc("id");
        List list=mapper.selectList(wrapper);
        List list=mapper.selectList(wrapper);*/
        List list=mapper.getAllProduct(subCode,parentCode,parentName,parentModel,customCode);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list.size()>0,pageInfo,"返回产品信息");
    }
@@ -110,7 +112,7 @@
        mapper.insert(product);
    }
    public Response zipParse(MultipartFile file) throws IOException, InvalidFormatException {
    public Response zipParse(MultipartFile file, Product baseProduct) throws IOException, InvalidFormatException {
        Product product = new Product();
        List<ProductBom> bomList = new LinkedList<>();
        Response response = new Response();
@@ -143,12 +145,34 @@
        for (int i = 0; i < fileList.size(); i++) {
            String fileTempUrl = fileList.get(i);
            //查询需上传的bom数据
            if (fileTempUrl.contains(".xls") && fileTempUrl.contains("(BOM)")) {
            //if (fileTempUrl.contains(".xls") && fileTempUrl.contains("(BOM)")) {
            if (fileTempUrl.substring(fileTempUrl.lastIndexOf(File.separator)+1).contains("(BOM).xls")) {
                Workbook workbook = null;
                InputStream inputStream = new FileInputStream(new File(fileTempUrl));
                workbook = WorkbookFactory.create(inputStream);
                inputStream.close();
                List<? extends PictureData> allPictures = workbook.getAllPictures();
                if(fileTempUrl.substring(fileTempUrl.lastIndexOf(".")+1).equals("xlsx")) {
                    Collections.sort(allPictures, new Comparator<PictureData>() {
                        @Override
                        public int compare(PictureData o1, PictureData o2) {
                            // Name: /xl/media/image1.png - Content Type: image/png
                            String s1 = o1.toString();
                            int contentTypeIndex = s1.indexOf(" - Content Type");
                            String s1Front = s1.substring(0, contentTypeIndex);
                            String imageNumStr1 = s1Front.substring(s1Front.lastIndexOf("/") + 1, s1Front.lastIndexOf(".")).replace("image", "");
                            String s2 = o2.toString();
                            int contentTypeIndex2 = s2.indexOf(" - Content Type");
                            String s2Front = s2.substring(0, contentTypeIndex2);
                            String imageNumStr2 = s2Front.substring(s2Front.lastIndexOf("/") + 1, s2Front.lastIndexOf(".")).replace("image", "");
                            return Integer.parseInt(imageNumStr1) - Integer.parseInt(imageNumStr2);
                        }
                    });
                }
                //取第一个sheet表
                Sheet sheet = workbook.getSheetAt(0);
                int lastRowNum = sheet.getLastRowNum();
@@ -171,6 +195,9 @@
                            cellValueInt = cell.getNumericCellValue();
                        } else if (j != 15) {
                            cellValue = cell.getStringCellValue();
                        }
                        if(cellValue!=null) {
                            cellValue = cellValue.trim();
                        }
                        switch (j) {
                            case 0: {
@@ -245,8 +272,8 @@
                                        provingFile.mkdirs();
                                    }
                                    String suffix = pictureData.suggestFileExtension();
                                    String picturePath = approvingPath + File.separator + bom.getSubModel() + "." + suffix;
                                    String picturePathFront = "doc_file" + File.separator + "product_submit" + File.separator + user.getName() + File.separator + dateFormat + File.separator + timeStamp + File.separator + bom.getSubModel() + "." + suffix;
                                    String picturePath = approvingPath + File.separator + bom.getSubModel() + "-bom." + suffix;
                                    String picturePathFront = "doc_file" + File.separator + "product_submit" + File.separator + user.getName() + File.separator + dateFormat + File.separator + timeStamp + File.separator + bom.getSubModel() + "-bom." + suffix;
                                    byte[] data = pictureData.getData();
                                    FileOutputStream fileOutputStream = null;
                                    File pictureFile = new File(picturePath);
@@ -264,7 +291,9 @@
                product.setBomList(bomList);
            }
        }
        return response.setII(1, true, product, filePath);
        //产品bom对比
        Map<String,List> compareMap = pbhService.parseCompare(baseProduct,product);
        return response.setIII(1, true, product,compareMap, filePath);
    }
@@ -274,20 +303,33 @@
            if(fileUrl.substring(fileUrl.lastIndexOf(".")+1).equals("dwg")) {
                FileUrlDTO dto = new FileUrlDTO();
                dto.setFileName(fileUrl.substring(fileUrl.lastIndexOf(File.separator) + 1));
                dto.setHttpFileUrl(fileUrl.substring(fileUrl.lastIndexOf("doc_file" + File.separator + "product_approving")));
                dto.setHttpFileUrl(fileUrl.substring(fileUrl.lastIndexOf("doc_file" + File.separator + "product_submit")));
                list.add(dto);
            }
        });
        return list;
    }
    public Response add(Product product) {
    @Transactional
    public Response add(Product product) throws IOException {
        String parentCode = product.getParentCode();
        String customCode = product.getCustomCode();
        String parentModel = product.getParentModel();
        List<ProductBom> bomList = product.getBomList();
        String fileUrl = product.getFileUrl();
        //查询产品最新的版本号
        Date date = new Date();
        boolean isCopyCustom = false;
        Product relatedProduct = null;
        //判断是否为依据产品复制定制的产品
        if(bomList == null){
            bomList = pbService.getBomByProductId(product.getId());
            isCopyCustom = true;
        }else{
            if(product.getId() != null){ //关联关系从产品id继承
                relatedProduct = getById(product.getId());
            }
        }
        //查询新增产品最新的版本号
        ProductHistory latestProduct = phService.getLatestVersion(parentCode, customCode);
        ProductHistory enabledProduct = phService.getEnabledByParentCodeAndCustomCode(parentCode, customCode);
        int currentVersion = 0;
@@ -297,220 +339,377 @@
        Integer nextVersion = currentVersion + 1;
        //产品物料关系迁移
        //查询生效版本的关联关系
        if(latestProduct!=null &&enabledProduct!=null) {
            List<MaterialProductHistory> mpList = mphService.getListByParentCodeAndCustomCodeAndVersion(parentCode, customCode, enabledProduct.getVersion());
            if (latestProduct.getVersion().intValue() == enabledProduct.getVersion()) {
                //最新版本生效,关联关系版本连着的
                mphService.updateVersionBatch(mpList);
            } else {
                //旧版本生效,关联关系版本不连着
                mpList.forEach(mp -> {
                    mp.setSVersion(nextVersion);
                    mp.setEVersion(nextVersion);
                });
                mphService.insertBatch(mpList);
        Product copyCustomProduct = null;
        if(isCopyCustom){
            //查询产品对应的关联关系
            copyCustomProduct = getById(product.getId());
            List<MaterialProductHistory> relatedList = mphService.getListByParentCodeAndCustomCodeAndVersion(copyCustomProduct.getParentCode(), copyCustomProduct.getCustomCode(), copyCustomProduct.getVersion());
            relatedList.forEach(related->{
                related.setCustomCode(product.getCustomCode());
                related.setSVersion(nextVersion);
                related.setEVersion(nextVersion);
                related.setCreateTime(date);
            });
            if(relatedList.size()!=0){
                mphService.insertBatch(relatedList);
            }
        }else {
            if(relatedProduct == null
                || (relatedProduct.getParentCode().equals(product.getParentCode())
                && relatedProduct.getCustomCode().equals(product.getCustomCode())
                )
            ) { //属于是产品升级或基于标准产品定制
                List<MaterialProductHistory> mpList = new LinkedList<>();
                if (latestProduct != null && enabledProduct != null) { //产品升级
                    mpList = mphService.getListByParentCodeAndCustomCodeAndVersion(parentCode, customCode, enabledProduct.getVersion());
                }else{ //基于标准产品定制/新添加标准产品
                    Product version = getVersion(parentCode, "");
                    if(version!=null) { //新添加标准产品
                        int standardEnabledVersion = version.getVersion();
                        mpList = mphService.getListByParentCodeAndCustomCodeAndVersion(parentCode, "", standardEnabledVersion);
                    }
                }
                if (mpList.size() != 0) { //存在关联关系
                    //当前的产品bom明细包含 继承的产品子件物料,继承关系保留
                    //设置mpList关联的物料详细信息
                    mpList = mphService.getListWithMaterialInfo(mpList);
                    List<String> newBomMaterialStrList = bomList.stream().map(bom -> bom.getSubCode() + "/" + bom.getSubModel()).collect(Collectors.toList());
                    mpList = mpList.stream().filter(mp -> newBomMaterialStrList.contains(mp.getSubMaterial().getSubCode() + "/" + mp.getSubMaterial().getSubModel())).collect(Collectors.toList());
                    if(mpList.size()!=0) {
                        if ((latestProduct !=null && enabledProduct!=null) && latestProduct.getVersion().intValue() == enabledProduct.getVersion()) {
                            //最新版本生效,关联关系版本连着的
                            mphService.updateVersionBatch(mpList);
                        } else {
                            //关联关系版本不连着,生成新纪录:包含 升级和基于标准产品定制
                            if(customCode.equals("")) {
                                mpList.forEach(mp -> {
                                    mp.setSVersion(nextVersion);
                                    mp.setEVersion(nextVersion);
                                    mp.setCreateTime(date);
                                });
                            }else{
                                mpList.forEach(mp -> {
                                    mp.setCustomCode(customCode);
                                    mp.setSVersion(nextVersion);
                                    mp.setEVersion(nextVersion);
                                    mp.setCreateTime(date);
                                });
                            }
                            mphService.insertBatch(mpList);
                        }
                    }
                }
            }else{ //属于不同产品继承关联关系
                List<MaterialProductHistory> mpList = mphService.getListByParentCodeAndCustomCodeAndVersion(relatedProduct.getParentCode(), relatedProduct.getCustomCode(), relatedProduct.getVersion());
                if(mpList.size()!=0) {
                    //当前的产品bom明细包含 继承的产品子件物料,保留
                    //设置mpList关联的物料详细信息
                    mpList = mphService.getListWithMaterialInfo(mpList);
                    List<String> newBomMaterialStrList = bomList.stream().map(bom -> bom.getSubCode() + "/" + bom.getSubModel()).collect(Collectors.toList());
                    mpList = mpList.stream().filter(mp -> newBomMaterialStrList.contains(mp.getSubMaterial().getSubCode() + "/" + mp.getSubMaterial().getSubModel())).collect(Collectors.toList());
                    if(mpList.size()!=0) {
                        mpList.forEach(mp -> {
                            mp.setParentCode(parentCode);
                            mp.setCustomCode(customCode);
                            mp.setSVersion(nextVersion);
                            mp.setEVersion(nextVersion);
                            mp.setCreateTime(date);
                        });
                        mphService.insertBatch(mpList);
                    }
                }
            }
        }
        //将产品文件复制至正式路径
        //文件转移,未跟子件挂钩的所有图纸图片转移到产品版本下:doc_file/product/{产品型号}/standard或者{customCode}}/{version}/
        //跟子件挂钩的转移到子件图纸下:doc_file/material/
        File file = new File(fileUrl);
        List<String> fileUrlList = new LinkedList<>();
        List<String> dwgUrlList = null;
        List<String> picUrlList = null;
        //存于物料下,bom内有对应
        List<String> materialUrlList = new LinkedList<>();
        //存于产品下,bom内没对应
        List<String> productUrlList = new LinkedList<>();
        if(isCopyCustom){
            //product下的图纸复制到新的路径
            String rootFile = CommonUtil.getRootFile();
            String customStr = copyCustomProduct.getCustomCode().equals("")?"standard":copyCustomProduct.getCustomCode();
            String fromDir = rootFile + "product" + File.separator + product.getParentModel() + File.separator + customStr + File.separator + copyCustomProduct.getVersion();
            String toDir = rootFile + "product" + File.separator + product.getParentModel() + File.separator + product.getCustomCode() + File.separator + 1;
            org.aspectj.util.FileUtil.copyDir(new File(fromDir),new File(toDir));
            //product_history/product/bom/bom_history
            // -> his
            ProductHistory his = new ProductHistory();
            his.setParentCode(product.getParentCode());
            his.setParentName(product.getParentName());
            his.setParentModel(product.getParentModel());
            his.setCustomCode(product.getCustomCode());
            his.setCreateTime(date);
            his.setVersionTime(product.getVersionTime());
            his.setVersion(1);
            his.setSubVersionMax(1);
            his.setEnabled(1);
            phService.insertAndUpdateEnabled(his);
            //phService.insert(his);
            // -> product
            product.setId(his.getId());
            product.setCreateTime(date);
            product.setVersion(1);
            //insert(product);
            deleteAndInsert(product);
            // -> bom
            bomList.forEach(bom-> {
                bom.setProductId(product.getId());
                bom.setSubVersion(1);
            });
            pbService.insertBatch(bomList);
            // -> bom_his
            List<ProductBomHistory> bomHistoryList = new LinkedList<>();
            bomList.forEach(bom->{
                ProductBomHistory bomHistory = new ProductBomHistory();
                bomHistory.setProductId(his.getId());
                bomHistory.setMaterialId(bom.getMaterialId());
                bomHistory.setQuantity(bom.getQuantity());
                bomHistory.setCategory(bom.getCategory());
                bomHistory.setMaterial(bom.getMaterial());
                bomHistory.setNotes(bom.getNotes());
                bomHistory.setProducer(bom.getProducer());
                bomHistory.setSurfaceDetail(bom.getSurfaceDetail());
                bomHistory.setThickness(bom.getThickness());
                bomHistory.setSubSVersion(1);
                bomHistory.setSubEVersion(1);
                bomHistoryList.add(bomHistory);
            });
            pbhService.insertBatch(bomHistoryList);
        }else {
            File file = new File(fileUrl);
            List<String> fileUrlList = new LinkedList<>();
            List<String> excelExcludeUrlList = null;
            List<String> picUrlList = new LinkedList<>();
            //存于物料下,bom内有对应
            List<String> materialUrlList = new LinkedList<>();
            //存于产品下,bom内没对应
            List<String> productUrlList = new LinkedList<>();
        //List<Material> materialDwgUrlNameList = new LinkedList<>();
            //List<Material> materialDwgUrlNameList = new LinkedList<>();
        fileUrlList = FileUtil.getStaticFilePath(file,fileUrlList);
        //图纸dwg 子件/产品
        dwgUrlList = fileUrlList.stream().filter(url->url.contains(".dwg")).collect(Collectors.toList());
        picUrlList = fileUrlList.stream().filter(url->url.contains(".png") || url.contains(".jpeg")).collect(Collectors.toList());
            fileUrlList = FileUtil.getStaticFilePath(file, fileUrlList);
            //图纸dwg 子件/产品
            excelExcludeUrlList = fileUrlList.stream().filter(url -> !(url.contains(".xls") || url.contains(".xlsx"))).collect(Collectors.toList());
            //picUrlList = fileUrlList.stream().filter(url -> url.contains(".png") || url.contains(".jpeg")).collect(Collectors.toList());
        dwgUrlList.forEach(dwgUrl->{
            boolean existFlag = false;
            for (ProductBom bom :bomList){
                String filename = dwgUrl.substring(dwgUrl.lastIndexOf(File.separator) + 1, dwgUrl.length() - 4);
                String fileFullName = dwgUrl.substring(dwgUrl.lastIndexOf(File.separator) + 1);
                if(bom.getSubModel().toUpperCase().equals(filename.toUpperCase())){
                    materialUrlList.add(dwgUrl);
                    existFlag = true;
            List<ProductBom> finalBomList = bomList;
            excelExcludeUrlList.forEach(excelExcludeUr -> {
                boolean existFlag = false;
                for (ProductBom bom : finalBomList) {
                    String filename = excelExcludeUr.substring(excelExcludeUr.lastIndexOf(File.separator) + 1, excelExcludeUr.lastIndexOf("."));
                    String fileFullName = excelExcludeUr.substring(excelExcludeUr.lastIndexOf(File.separator) + 1);
                    if (bom.getSubModel().toUpperCase().equals(filename.toUpperCase()) && excelExcludeUr.substring(excelExcludeUr.lastIndexOf(".")+1).equals("dwg")) {
                        materialUrlList.add(excelExcludeUr);
                        existFlag = true;
                    bom.setDwgUrl("doc_file" + File.separator + "material"
                            + File.separator + fileFullName);
                    break;
                        bom.setDwgUrl("doc_file" + File.separator + "material"
                                + File.separator + fileFullName);
                        break;
                    }
                    else if((bom.getSubModel().toUpperCase()+"-BOM").equals(filename.toUpperCase()) && (excelExcludeUr.substring(excelExcludeUr.lastIndexOf(".")+1).equals("png") ||excelExcludeUr.substring(excelExcludeUr.lastIndexOf(".")+1).equals("jpeg"))
                    ){
                        picUrlList.add(excelExcludeUr);
                        existFlag = true;
                        bom.setPictureUrl("doc_file" + File.separator + "material"
                                + File.separator + fileFullName);
                        break;
                    }
                }
            }
            if(!existFlag) {
                productUrlList.add(dwgUrl);
            }
        });
        //一定是有对应物料的,从bom内剥离的
        picUrlList.forEach(picUrl->{
            for (ProductBom bom :bomList){
                String filename = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1,picUrl.lastIndexOf("."));
                String fileFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1);
                if(bom.getSubModel().toUpperCase().equals(filename.toUpperCase())){
                    bom.setPictureUrl("doc_file" + File.separator + "material"
                            + File.separator + fileFullName);
                    break;
                if (!existFlag) {
                    productUrlList.add(excelExcludeUr);
                }
            });
            /*//一定是有对应物料的,包含从bom内剥离的(删除掉:上面已经处理)
            picUrlList.forEach(picUrl -> {
                for (ProductBom bom : finalBomList) {
                    String filename = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1, picUrl.lastIndexOf("."));
                    String fileFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1);
                    if ((bom.getSubModel().toUpperCase()+"-BOM").equals(filename.toUpperCase())) {
                        bom.setPictureUrl("doc_file" + File.separator + "material"
                                + File.separator + fileFullName);
                        break;
                    }
                }
            });*/
            //转移路径
            String projectDir = CommonUtil.getProjectDir();
            //doc_file/product/{产品型号}/standard或者{customCode}}/{version}/
            //跟子件挂钩的转移到子件图纸下:doc_file/material/
            String customCodeString = null;
            if (customCode != null && !customCode.equals("")) {
                customCodeString = customCode;
            } else {
                customCodeString = "standard";
            }
        });
        //转移路径
        String projectDir = CommonUtil.getProjectDir();
        //doc_file/product/{产品型号}/standard或者{customCode}}/{version}/
        //跟子件挂钩的转移到子件图纸下:doc_file/material/
        String customCodeString = null;
        if(customCode!=null && !customCode.equals("")) {
            customCodeString = customCode;
        }else{
            customCodeString = "standard";
        }
        String productDir = projectDir + File.separator + "doc_file" + File.separator + "product" + File.separator + parentModel
                + File.separator + customCodeString + File.separator +nextVersion;
        String materialDir = projectDir + File.separator + "doc_file" + File.separator + "material";
        File productDirFile = new File(productDir);
        File materialFile = new File(materialDir);
        if(!productDirFile.exists()){
            productDirFile.mkdirs();
        }
        if(!materialFile.exists()){
            materialFile.mkdirs();
        }
        productUrlList.forEach(productUrl->{
            String dwgName = productUrl.substring(productUrl.lastIndexOf(File.separator) + 1, productUrl.length() - 4);
            try {
                FileCopyUtils.copy(new File(productUrl),new File(productDir+File.separator+dwgName+".dwg"));
            } catch (IOException e) {
                e.printStackTrace();
            String productDir = projectDir + File.separator + "doc_file" + File.separator + "product" + File.separator + parentModel
                    + File.separator + customCodeString + File.separator + nextVersion;
            String materialDir = projectDir + File.separator + "doc_file" + File.separator + "material";
            File productDirFile = new File(productDir);
            File materialFile = new File(materialDir);
            if (!productDirFile.exists()) {
                productDirFile.mkdirs();
            }
        });
        materialUrlList.forEach(materialUrl->{
            String dwgName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1, materialUrl.length() - 4);
            try {
                FileCopyUtils.copy(new File(materialUrl),new File(materialDir+File.separator+dwgName+".dwg"));
            } catch (IOException e) {
                e.printStackTrace();
            if (!materialFile.exists()) {
                materialFile.mkdirs();
            }
        });
            productUrlList.forEach(productUrl -> {
                String fileName = productUrl.substring(productUrl.lastIndexOf(File.separator) + 1);
        picUrlList.forEach(picUrl->{
            String picFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1);
            try {
                FileCopyUtils.copy(new File(picUrl),new File(materialDir+File.separator+picFullName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
                try {
                    FileCopyUtils.copy(new File(productUrl), new File(productDir + File.separator + fileName));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        //物料表中不存在的(依据:物料编码+物料型号),则添加到物料表中去(包含product这个物料)
        List<Material> materialExistList = mService.getListByCodeAndModelList2(bomList);
        List<String> subCodeList = materialExistList.stream().map(Material::getSubCode).collect(Collectors.toList());
        List<Material> materialList = new LinkedList<>();
        bomList.forEach(bom->{
            if(!subCodeList.contains(bom.getSubCode())){
            materialUrlList.forEach(materialUrl -> {
                String dwgName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1, materialUrl.length() - 4);
                try {
                    FileCopyUtils.copy(new File(materialUrl), new File(materialDir + File.separator + dwgName + ".dwg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            picUrlList.forEach(picUrl -> {
                String picFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1);
                try {
                    FileCopyUtils.copy(new File(picUrl), new File(materialDir + File.separator + picFullName));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            //物料表中不存在的(依据:物料编码+物料型号),则添加到物料表中去(包含product这个物料)
            List<Material> materialExistList = mService.getListByCodeAndModelList2(bomList);
            //List<String> subCodeList = materialExistList.stream().map(Material::getSubCode).collect(Collectors.toList());
            List<String> subCodeList = materialExistList.stream().map(material -> material.getSubCode()+"/"+material.getSubModel()).collect(Collectors.toList());
            List<Material> materialList = new LinkedList<>();
            bomList.forEach(bom -> {
                if (!subCodeList.contains(bom.getSubCode()+"/"+bom.getSubModel())) {
                    Material material = new Material();
                    material.setCreateDate(date);
                    material.setDwgUrl(bom.getDwgUrl());
                    material.setFileUrl(bom.getFileUrl());
                    material.setPictureUrl(bom.getPictureUrl());
                    // TODO 是否要更新同物料编码的老物料状态为0?
                    material.setStatus(1);
                    material.setSubCode(bom.getSubCode());
                    material.setSubModel(bom.getSubModel());
                    material.setSubName(bom.getSubName());
                    material.setType(bom.getType());
                    material.setUnit(bom.getUnit());
                    materialList.add(material);
                }
            });
            //母料是否存在
            Material materialDB = mService.getByCodeAndModel(product.getParentCode(), product.getParentModel());
            if (materialDB == null) {
                Material material = new Material();
                material.setCategory(bom.getCategory());
                material.setCreateDate(bom.getCreateDate());
                material.setDwgUrl(bom.getDwgUrl());
                material.setFileUrl(bom.getFileUrl());
                material.setMaterial(bom.getMaterial());
                material.setNotes(bom.getNotes());
                material.setPictureUrl(bom.getPictureUrl());
                material.setProducer(bom.getProducer());
                // TODO 是否要更新同物料编码的老物料状态为0?
                material.setSubCode(product.getParentCode());
                material.setSubName(product.getParentName());
                material.setSubModel(product.getParentModel());
                material.setCreateDate(date);
                material.setStatus(1);
                material.setSubCode(bom.getSubCode());
                material.setSubModel(bom.getSubModel());
                material.setSubName(bom.getSubName());
                material.setSurfaceDetail(bom.getSurfaceDetail());
                material.setThickness(bom.getThickness());
                material.setType(bom.getType());
                material.setUnit(bom.getUnit());
                materialList.add(material);
            }
        });
        //母料是否存在
        Material materialDB = mService.getByCodeAndModel(product.getParentCode(),product.getParentModel());
        if(materialDB==null) {
            Material material = new Material();
            material.setSubCode(product.getParentCode());
            material.setSubName(product.getParentName());
            material.setSubModel(product.getParentModel());
            if(materialList.size()!=0) {
                mService.insertBatch(materialList);
            }
            //更新product_history/product_bom_history/product/product_bom,
            // product的主键沿用对应product_history的
            //根据编码和型号确定物料id并对应quantity,存入数据库.
            //List<Material> bomMaterialList = mService.getListByCodeAndModelList2(bomList);
            List<ProductBom> bomMaterialList = pbService.getListByCodeAndModelList2(bomList);
            // -> product_history
            ProductHistory productHistory = new ProductHistory();
            productHistory.setParentCode(product.getParentCode());
            productHistory.setParentName(product.getParentName());
            productHistory.setParentModel(product.getParentModel());
            productHistory.setNotes(product.getNotes());
            productHistory.setCustomCode(product.getCustomCode());
            productHistory.setCreateTime(date);
            productHistory.setVersionTime(product.getVersionTime());
            productHistory.setVersion(nextVersion);
            //版本新增,初始bom子件版本为1
            productHistory.setSubVersionMax(1);
            productHistory.setEnabled(1);
            phService.insertAndUpdateEnabled(productHistory);
            // -> product
            product.setId(productHistory.getId());
            product.setCreateTime(date);
            product.setVersion(nextVersion);
            deleteAndInsert(product);
            // -> product_bom
            List<ProductBom> productBomList = new LinkedList<>();
            bomMaterialList.forEach(bomMaterial -> {
                ProductBom bom = new ProductBom();
                bom.setProductId(product.getId());
                bom.setMaterialId(bomMaterial.getId());
                bom.setQuantity(bomMaterial.getQuantity());
                bom.setCategory(bomMaterial.getCategory());
                bom.setMaterial(bomMaterial.getMaterial());
                bom.setNotes(bomMaterial.getNotes());
                bom.setProducer(bomMaterial.getProducer());
                bom.setSurfaceDetail(bomMaterial.getSurfaceDetail());
                bom.setThickness(bomMaterial.getThickness());
                bom.setSubVersion(1);
                bom.setCreateDate(date);
            materialList.add(material);
                productBomList.add(bom);
            });
            pbService.insertBatch(productBomList);
            // -> product_bom_history
            List<ProductBomHistory> bomHistoryList = new LinkedList<>();
            bomMaterialList.forEach(bomMaterial -> {
                ProductBomHistory bomHistory = new ProductBomHistory();
                bomHistory.setProductId(productHistory.getId());
                bomHistory.setMaterialId(bomMaterial.getId());
                bomHistory.setQuantity(bomMaterial.getQuantity());
                bomHistory.setCategory(bomMaterial.getCategory());
                bomHistory.setMaterial(bomMaterial.getMaterial());
                bomHistory.setNotes(bomMaterial.getNotes());
                bomHistory.setProducer(bomMaterial.getProducer());
                bomHistory.setSurfaceDetail(bomMaterial.getSurfaceDetail());
                bomHistory.setThickness(bomMaterial.getThickness());
                bomHistory.setSubSVersion(1);
                bomHistory.setSubEVersion(1);
                bomHistory.setCreateDate(date);
                bomHistoryList.add(bomHistory);
            });
            pbhService.insertBatch(bomHistoryList);
        }
        mService.insertBatch(materialList);
        //添加新增日志到tb_product_lock_log表
        ProductLockLog lockLog = new ProductLockLog();
        lockLog.setParentCode(parentCode);
        lockLog.setCustomCode(customCode);
        lockLog.setVersion(nextVersion);
        lockLog.setVersionTime(DateUtil.YYYY_MM_DD_HH_MM_SS.format(product.getVersionTime()));
        lockLog.setReason("产品bom上传");
        lockLog.setOwner(ActionUtil.getUser().getName());
        lockLog.setCreateTime(date);
        //-1代表日志类型为:产品bom上传
        lockLog.setLockFlag(-1);
        //更新product_history/product_bom_history/product/product_bom,
        // product的主键沿用对应product_history的
        //根据编码和型号确定物料id并对应quantity,存入数据库
        //List<Material> bomMaterialList = mService.getListByCodeAndModelList2(bomList);
        List<ProductBom> bomMaterialList = pbService.getListByCodeAndModelList2(bomList);
        // -> product_history
        ProductHistory productHistory = new ProductHistory();
        productHistory.setParentCode(product.getParentCode());
        productHistory.setParentName(product.getParentName());
        productHistory.setParentModel(product.getParentModel());
        productHistory.setNotes(product.getNotes());
        productHistory.setCustomCode(product.getCustomCode());
        productHistory.setCreateTime(product.getCreateTime());
        productHistory.setVersion(product.getVersion());
        //版本新增,初始bom子件版本为1
        productHistory.setSubVersionMax(1);
        productHistory.setEnabled(1);
        phService.insertAndUpdateEnabled(productHistory);
        // -> product
        product.setId(productHistory.getId());
        product.setCreateTime(new Date());
        product.setVersion(nextVersion);
        deleteAndInsert(product);
        // -> product_bom
        List<ProductBom> productBomList = new LinkedList<>();
        bomMaterialList.forEach(bomMaterial->{
            ProductBom bom = new ProductBom();
            bom.setProductId(product.getId());
            bom.setMaterialId(bomMaterial.getId());
            bom.setQuantity(bomMaterial.getQuantity());
            bom.setSubVersion(1);
            bom.setCreateDate(new Date());
            productBomList.add(bom);
        });
        pbService.insertBatch(productBomList);
        // -> product_bom_history
        List<ProductBomHistory> bomHistoryList = new LinkedList<>();
        bomMaterialList.forEach(bomMaterial->{
            ProductBomHistory bomHistory = new ProductBomHistory();
            bomHistory.setProductId(productHistory.getId());
            bomHistory.setMaterialId(bomMaterial.getId());
            bomHistory.setQuantity(bomMaterial.getQuantity());
            bomHistory.setSubSVersion(1);
            bomHistory.setSubEVersion(1);
            bomHistory.setCreateDate(new Date());
            bomHistoryList.add(bomHistory);
        });
        pbhService.insertBatch(bomHistoryList);
        productLockLogService.insert(lockLog);
        return new Response().setII(1,"新增完成");
    }
    private void deleteAndInsert(Product product) {
        Product productDB = getByProductCodeAndCustomCode(product.getParentCode(), product.getCustomCode());
        mapper.deleteById(productDB.getId());
        pbService.deleteByProductId(productDB.getId());
        if(productDB!=null) {
            mapper.deleteById(productDB.getId());
            pbService.deleteByProductId(productDB.getId());
        }
        mapper.insert(product);
    }
@@ -524,4 +723,10 @@
        List list=mapper.selectList(null);
        return new Response().setII(1,list.size()>0,list,"");
    }
    //根据产品id查询被锁定的物料dwg和产品丝印
    public Response getLockedByProductId(int productId) {
        List listMaterial=mapper.selectListMaterial(productId);
        List listProduct=mapper.selectListProduct(productId);
        return new Response().setIII(1,listMaterial.size()>0||listProduct.size()>0,listMaterial,listProduct,"");
    }
}