whycxzp
2022-09-06 50dc46df91825d588097535534a0e70a8a8c959e
src/main/java/com/whyc/service/ProductService.java
@@ -280,13 +280,19 @@
        return list;
    }
    public Response add(Product product) {
    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;
        //判断是否为依据产品复制定制的产品
        if(bomList == null){
            bomList = pbService.getBomByProductId(product.getId());
            isCopyCustom = true;
        }
        //查询产品最新的版本号
        ProductHistory latestProduct = phService.getLatestVersion(parentCode, customCode);
        ProductHistory enabledProduct = phService.getEnabledByParentCodeAndCustomCode(parentCode, customCode);
@@ -297,8 +303,17 @@
        Integer nextVersion = currentVersion + 1;
        //产品物料关系迁移
        //查询生效版本的关联关系
        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()));
            mphService.insertBatch(relatedList);
        }else {
        if(latestProduct!=null &&enabledProduct!=null) {
            List<MaterialProductHistory> mpList = mphService.getListByParentCodeAndCustomCodeAndVersion(parentCode, customCode, enabledProduct.getVersion());
                if(mpList.size()!=0) { //存在关联关系
            if (latestProduct.getVersion().intValue() == enabledProduct.getVersion()) {
                //最新版本生效,关联关系版本连着的
                mphService.updateVersionBatch(mpList);
@@ -311,10 +326,58 @@
                mphService.insertBatch(mpList);
            }
        }
            }
        }
        //将产品文件复制至正式路径
        //文件转移,未跟子件挂钩的所有图纸图片转移到产品版本下:doc_file/product/{产品型号}/standard或者{customCode}}/{version}/
        //跟子件挂钩的转移到子件图纸下:doc_file/material/
        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.setSubSVersion(1);
                bomHistory.setSubEVersion(1);
                bomHistoryList.add(bomHistory);
            });
            pbhService.insertBatch(bomHistoryList);
        }else {
        File file = new File(fileUrl);
        List<String> fileUrlList = new LinkedList<>();
        List<String> dwgUrlList = null;
@@ -331,9 +394,10 @@
        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());
            List<ProductBom> finalBomList = bomList;
        dwgUrlList.forEach(dwgUrl->{
            boolean existFlag = false;
            for (ProductBom bom :bomList){
                for (ProductBom bom : finalBomList) {
                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())){
@@ -353,7 +417,7 @@
        //一定是有对应物料的,从bom内剥离的
        picUrlList.forEach(picUrl->{
            for (ProductBom bom :bomList){
                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().equals(filename.toUpperCase())){
@@ -505,13 +569,16 @@
        });
        pbhService.insertBatch(bomHistoryList);
        }
        return new Response().setII(1,"新增完成");
    }
    private void deleteAndInsert(Product product) {
        Product productDB = getByProductCodeAndCustomCode(product.getParentCode(), product.getCustomCode());
        if(productDB!=null) {
        mapper.deleteById(productDB.getId());
        pbService.deleteByProductId(productDB.getId());
        }
        mapper.insert(product);
    }