whycxzp
2024-01-03 66db1d15cf5d4b51a346a42b66f0505bd7f31ce0
src/main/java/com/whyc/service/ProductService.java
@@ -57,6 +57,9 @@
    @Autowired
    private ProductLockLogService productLockLogService;
    @Autowired
    private MaterialHistoryService materialHistoryService;
    //查询出所有的产品信息(分页加模糊查询<产品的编码,型号,名字,定制表编号>
    public Response getAllProduct(String subCode,String parentCode, String parentName, String parentModel, String customCode,Integer enabled, int pageCurr, int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
@@ -246,6 +249,9 @@
                for (int k = 2; k < lastRowNum + 1; k++) {
                    if(k==2){
                        product.setParentCode(sheet.getRow(2).getCell(1).getStringCellValue());
                        if(!originalFilename.contains(product.getParentCode())){
                            return response.set(1,false,"上传的产品压缩包必须包含产品编码");
                        }
                        product.setParentName(sheet.getRow(2).getCell(2).getStringCellValue());
                        String parentModel = sheet.getRow(2).getCell(3).getStringCellValue();
                        parentModel = parentModel.trim();
@@ -446,6 +452,8 @@
        List<ProductBom> bomList = product.getBomList();
        String zipFilePath = product.getFileUrl();
        Date date = new Date();
        String dateStr = DateUtil.YYYY_MM_DD_HH_MM_SS_UNION.format(date);
        Long userId = ActionUtil.getUser().getId();
        boolean isCopyCustom = false;
        Product relatedProduct = null;
        //校验凡是有定制表单号的产品,不能使用已上传过的标准产品编码
@@ -601,7 +609,7 @@
                                newTemp.setParentCode(parentCode);
                                newTemp.setCustomCode(customCode);
                                newTemp.setSubCode(bom.getSubCode());
                                Material material = mService.getByCodeAndModel(bom.getSubCode(), bom.getSubModel());
                                Material material = mService.getByCodeAndModel(bom.getSubCode(), bom.getSubModel(),true);
                                newTemp.setSubMaterialId(material.getId());
                                newTemp.setSVersion(nextVersion);
                                newTemp.setEVersion(nextVersion);
@@ -629,7 +637,7 @@
                            newTemp.setParentCode(parentCode);
                            newTemp.setCustomCode(customCode);
                            newTemp.setSubCode(bom.getSubCode());
                            Material material = mService.getByCodeAndModel(bom.getSubCode(), bom.getSubModel());
                            Material material = mService.getByCodeAndModel(bom.getSubCode(), bom.getSubModel(),true);
                            newTemp.setSubMaterialId(material.getId());
                            newTemp.setSVersion(nextVersion);
                            newTemp.setEVersion(nextVersion);
@@ -735,13 +743,14 @@
                for (ProductBom bom : finalBomList) {
                    String filename = excelExcludeUr.substring(excelExcludeUr.lastIndexOf(File.separator) + 1, excelExcludeUr.lastIndexOf("."));
                    String fileFullName = excelExcludeUr.substring(excelExcludeUr.lastIndexOf(File.separator) + 1);
                    String fileSuffix = fileFullName.substring(fileFullName.lastIndexOf("."));
                    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);
                        bom.setDwgUrl("doc_file" + File.separator + "material" + File.separator + bom.getSubCode() + "-" + bom.getSubModel()
                                + File.separator + filename + "_" + dateStr + fileSuffix);
                        break;
                    }
                    else if((bom.getSubModel().toUpperCase()+"-BOM").equals(filename.toUpperCase()) && (excelExcludeUr.substring(excelExcludeUr.lastIndexOf(".")+1).equals("png") ||excelExcludeUr.substring(excelExcludeUr.lastIndexOf(".")+1).equals("jpeg"))
@@ -750,8 +759,8 @@
                        existFlag = true;
                        bom.setPictureUrl("doc_file" + File.separator + "material"
                                + File.separator + fileFullName);
                        bom.setPictureUrl("doc_file" + File.separator + "material" + File.separator + bom.getSubCode() + "-" + bom.getSubModel()
                                + File.separator + filename + "_" + dateStr + fileSuffix);
                        break;
                    }
                }
@@ -808,36 +817,83 @@
                }
            });
            materialUrlList.forEach(materialUrl -> {
                String dwgName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1, materialUrl.length() - 4);
            //物料dwg图纸存到对应的 物料编码+型号 下面
            for (String materialUrl : materialUrlList) {
                try {
                    File dwgFile = new File(materialDir + File.separator + dwgName + ".dwg");
                    if(!dwgFile.exists()) {
                    String dwgName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1, materialUrl.length() - 4);
                    //确定物料的具体型号
                    String subModel = null;
                    String subCode = null;
                    for (ProductBom bom : bomList) {
                        String bomSubMode = bom.getSubModel();
                        if (bomSubMode.toUpperCase().equals(dwgName.toUpperCase())) {
                            subModel = bomSubMode;
                            subCode = bom.getSubCode();
                            break;
                        }
                    }
                    File dwgFile;
                    if (subModel != null) {
                        dwgFile = new File(materialDir + File.separator + subCode + "-" + subModel + File.separator + dwgName + "_" + dateStr + ".dwg");
                        File dwgDir = new File(materialDir + File.separator + subCode + "-" + subModel);
                        if(!dwgDir.exists()){
                            dwgDir.mkdirs();
                        }
                    } else {
                        dwgFile = new File(materialDir + File.separator + dwgName + "_" + dateStr + ".dwg");
                    }
                    if (!dwgFile.exists()) {
                        FileCopyUtils.copy(new File(materialUrl), dwgFile);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            }
            picUrlList.forEach(picUrl -> {
            for (String picUrl : picUrlList) {
                String picFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1);
                String picSubModel = picFullName.substring(0, picFullName.lastIndexOf("."));
                String picSuffix = picFullName.substring(picFullName.lastIndexOf("."));
                try {
                    File picFile = new File(materialDir + File.separator + picFullName);
                    if(!picFile.exists()) {
                    //确定物料的具体型号和编码
                    String subModel = null;
                    String subCode = null;
                    for (ProductBom bom : bomList) {
                        String bomSubMode = bom.getSubModel();
                        if ((bomSubMode + "-bom").toUpperCase().equals(picSubModel.toUpperCase())) {
                            subModel = bomSubMode;
                            subCode = bom.getSubCode();
                            break;
                        }
                    }
                    File picFile;
                    if (subModel != null) {
                        picFile = new File(materialDir + File.separator + subCode + "-" + subModel + File.separator + picSubModel + "_" + dateStr + picSuffix);
                        File picDir = new File(materialDir + File.separator + subCode + "-" + subModel);
                        if(!picDir.exists()){
                            picDir.mkdirs();
                        }
                    } else {
                        picFile = new File(materialDir + File.separator + picSubModel + "_" + dateStr + picSuffix);
                    }
                    if (!picFile.exists()) {
                        FileCopyUtils.copy(new File(picUrl), picFile);
                    }
                } 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<>();
            //需要更新到物料表中的物料(存在图纸或者图片)
            List<MaterialHistory> materialHistoryList = new LinkedList<>();
            //新物料
            List<Material> newMaterialList = new LinkedList<>();
            //已经存在的物料
            List<Material> materialUpdateList = new LinkedList<>();
            for (ProductBom bom : bomList) {
                if (!subCodeList.contains(bom.getSubCode() + "/" + bom.getSubModel())) {
@@ -846,16 +902,17 @@
                    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());
                    material.setUpUserId(userId);
                    materialList.add(material);
                    newMaterialList.add(material);
                } else { //物料已经存在的
                    // 物料历史表是为了存储物料的历史图片和dwg图纸
                    Material material = new Material();
                    if (bom.getPictureUrl() != null) {
                        material.setPictureUrl(bom.getPictureUrl());
@@ -871,10 +928,10 @@
                }
            }
            //对新增到物料表中的物料,根据 物料编码+物料型号 进行去重处理
            materialList = materialList.stream().filter(distinctByKey(m->m.getSubCode()+"/"+m.getSubModel())).collect(Collectors.toList());
            newMaterialList = newMaterialList.stream().filter(distinctByKey(m->m.getSubCode()+"/"+m.getSubModel())).collect(Collectors.toList());
            //母料是否存在
            Material materialDB = mService.getByCodeAndModel(product.getParentCode(), product.getParentModel());
            Material materialDB = mService.getByCodeAndModel(product.getParentCode(), product.getParentModel(),true);
            if (materialDB == null) {
                Material material = new Material();
                material.setSubCode(product.getParentCode());
@@ -882,11 +939,12 @@
                material.setSubModel(product.getParentModel());
                material.setCreateDate(date);
                material.setStatus(1);
                material.setUpUserId(userId);
                materialList.add(material);
                newMaterialList.add(material);
            }
            if(materialList.size()!=0) {
                mService.insertBatch(materialList);
            if(newMaterialList.size()!=0) {
                mService.insertBatch(newMaterialList);
            }
            if(materialUpdateList.size()!=0) {
                mService.updateDwgUrlAndPicUrl(materialUpdateList);
@@ -970,6 +1028,56 @@
                bomHistoryList.add(bomHistory);
            });
            pbhService.insertBatch(bomHistoryList);
            //插入到物料历史表的,
            // 包含新物料包含图纸图片的,
            // 也包含旧物料包含图纸图片的
            if(newMaterialList.size()>0){
                for (Material material : newMaterialList) {
                    //存在图纸或者图片,就插入历史
                    if(material.getDwgUrl()!=null || material.getPictureUrl()!=null) {
                        MaterialHistory materialHistory = new MaterialHistory();
                        materialHistory.setPictureUrl(material.getPictureUrl());
                        materialHistory.setProductId(productHistory.getId());
                        materialHistory.setDwgUrl(material.getDwgUrl());
                        materialHistory.setUpUserId(userId.intValue());
                        materialHistory.setCreateTime(date);
                        materialHistory.setMaterialId(material.getId());
                        materialHistoryList.add(materialHistory);
                    }
                }
            }
            if(materialUpdateList.size()>0) {
                //更新物料图纸图片的同时,添加新的图纸图片信息到物料历史表中
                for (Material material : materialUpdateList) {
                    MaterialHistory materialHistory = new MaterialHistory();
                    materialHistory.setPictureUrl(material.getPictureUrl());
                    materialHistory.setProductId(productHistory.getId());
                    materialHistory.setDwgUrl(material.getDwgUrl());
                    materialHistory.setUpUserId(userId.intValue());
                    materialHistory.setCreateTime(date);
                    for (Material materialInDB : materialExistList) {
                        if ((material.getSubCode() + "/" + material.getSubModel()).equals(materialInDB.getSubCode() + "/" + materialInDB.getSubModel())) {
                            materialHistory.setMaterialId(materialInDB.getId());
                            break;
                        }
                    }
                    materialHistoryList.add(materialHistory);
                }
            }
            if(materialHistoryList.size()>0) {
                materialHistoryService.addBatch(materialHistoryList);
            }
            //如果新上传的bom中存在0108、0109开头的物料,(肯定会被识别成新物料,之前的操作已经被插入到表中)
            // 在数据库存在相同的物料编码存在且不同的物料型号,则将数据库最新的物料的附件复制一份并状态同步,填充到新的物料表中
            for (Material material : newMaterialList) { //新物料
                String subCode = material.getSubCode();
                String subModel = material.getSubModel();
                if(subCode.startsWith("0108") || subCode.startsWith("0109")){
                    Material materialWithSameCodeLatest = mService.getByCodeAndModel(subCode, subModel, false);
                    //附件转移并设置
                    mService.transferCopiedAttachment(materialWithSameCodeLatest,material);
                }
            }
        }
        //添加新增日志到tb_product_lock_log表
@@ -985,7 +1093,7 @@
        lockLog.setLockFlag(-1);
        productLockLogService.insert(lockLog);
        return new Response().setII(1,"新增完成");
        return new Response().set(1,true,"新增完成");
    }
    private void deleteAndInsert(Product product) {
@@ -1039,5 +1147,9 @@
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null;
    }
    //验证母料编码是否存在产品中
    public Response judgeParentCode(String parentCode,String customCode) {
        int count = mapper.judgeParentCode(parentCode,customCode);
        return  new Response().set(1,count>0,"验证母料编码是否存在产品中");
    }
}