whyclxw
3 天以前 12154b62b42df29173cdc54d7fd35d02d9a6422b
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);
@@ -146,8 +149,11 @@
        File parentFile = new File(filePath);
        String originalFilename = file.getOriginalFilename();
        File zipFile = new File(filePath + File.separator + originalFilename);
        if (!zipFile.exists()) {
        /*if (!zipFile.exists()) {
            zipFile.mkdirs();
        }*/
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        file.transferTo(zipFile);
        //解压文件夹
@@ -246,6 +252,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();
@@ -362,6 +371,14 @@
                                if (!pictureRowSet.isEmpty() && pictureRowSet.contains(k)) {
                                    PictureData pictureData = pictureDataMap.get(k);
                                    //判断物料型号是否包含非法字符(图片会转化为路径)
                                    if(bom.getSubModel()!=null) {
                                        String regexStr = "[\\/*?\"|:<>\t]";
                                        boolean matches = Pattern.compile(regexStr).matcher(bom.getSubModel()).find();
                                        if (matches) {
                                            return response.set(1, false, "型号为"+bom.getSubModel()+"的物料包含非法字符:\\/*?\"|:<>或前置空格");
                                        }
                                    }
                                    //图片存储 product_submit/username/2022-07/
                                    String approvingPath = rootFile + File.separator + "product_submit" + File.separator + user.getName() + File.separator + dateFormat + File.separator + timeStamp;
                                    File provingFile = new File(approvingPath);
@@ -446,6 +463,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;
        //校验凡是有定制表单号的产品,不能使用已上传过的标准产品编码
@@ -572,36 +591,65 @@
            if(bom.getRelatedMaterialCodes()!=null) {
                String relatedMaterialCodes = bom.getRelatedMaterialCodes().trim();
                if (!relatedMaterialCodes.equals("")) {
                    if (subMaterialListInDB.contains(bom.getSubCode()+"_"+bom.getSubModel())) { //当前物料有替代料
                        String[] relatedList;
                        if (relatedMaterialCodes.contains(",")) {
                            relatedList = relatedMaterialCodes.split(",");
                        } else {
                            relatedList = relatedMaterialCodes.split(",");
                        }
                        for (String related : relatedList) {
                            bom.setRelatedMaterialCodes(related);
                            boolean exists = false;
                            for (MaterialProductHistory mp : mpListInDB) {
                                //物料相同,替代料相同
                                if ((mp.getSubMaterial().getSubCode()+"_"+mp.getSubMaterial().getSubModel()).equals(bom.getSubCode()+"_"+bom.getSubModel())
                                        && mp.getRelatedSubMaterial().getSubCode().equals(related)) {
                                    //只可能存在0 和 1 的情况,因为查询的是 >=上个版本
                                    if (nextVersion - mp.getEVersion() == 0) { //已存在,无需添加
                                        //break;
                                    } else if (nextVersion - mp.getEVersion() == 1) {  //更新到当前版本
                                        mp.setEVersion(nextVersion);
                                        versionUpdateList.add(mp);
                    if(subMaterialListInDB!=null&&subMaterialListInDB.size()>0){
                        if (subMaterialListInDB.contains(bom.getSubCode()+"_"+bom.getSubModel())) { //当前物料有替代料
                            String[] relatedList;
                            if (relatedMaterialCodes.contains(",")) {
                                relatedList = relatedMaterialCodes.split(",");
                            } else {
                                relatedList = relatedMaterialCodes.split(",");
                            }
                            for (String related : relatedList) {
                                bom.setRelatedMaterialCodes(related);
                                boolean exists = false;
                                for (MaterialProductHistory mp : mpListInDB) {
                                    //物料相同,替代料相同
                                    if ((mp.getSubMaterial().getSubCode()+"_"+mp.getSubMaterial().getSubModel()).equals(bom.getSubCode()+"_"+bom.getSubModel())
                                            && mp.getRelatedSubMaterial().getSubCode().equals(related)) {
                                        //只可能存在0 和 1 的情况,因为查询的是 >=上个版本
                                        if (nextVersion - mp.getEVersion() == 0) { //已存在,无需添加
                                            //break;
                                        } else if (nextVersion - mp.getEVersion() == 1) {  //更新到当前版本
                                            mp.setEVersion(nextVersion);
                                            versionUpdateList.add(mp);
                                        }
                                        exists = true;
                                    }
                                    exists = true;
                                }
                                if(!exists) { //不存在相邻及相同的记录版本,新增
                                    MaterialProductHistory newTemp = new MaterialProductHistory();
                                    newTemp.setParentCode(parentCode);
                                    newTemp.setCustomCode(customCode);
                                    newTemp.setSubCode(bom.getSubCode());
                                    Material material = mService.getByCodeAndModel(bom.getSubCode(), bom.getSubModel(),true);
                                    newTemp.setSubMaterialId(material.getId());
                                    newTemp.setSVersion(nextVersion);
                                    newTemp.setEVersion(nextVersion);
                                    newTemp.setQuantity(bom.getQuantity());
                                    //2323-09-08 与杨红兰微信确认<就按照我上传清单的时间,系统里最新的是啥型号,就取什么型号>
                                    Material relatedMaterial = mService.getLastByCode(related);
                                    if(relatedMaterial == null){
                                        throw new RuntimeException("当前替料"+related+",编码在平台中不存在,无法关联替代");
                                    }
                                    newTemp.setMaterialId(relatedMaterial.getId());
                                    newTemp.setCreateTime(date);
                                    newAddedList.add(newTemp);
                                }
                            }
                            if(!exists) { //不存在相邻及相同的记录版本,新增
                        }
                        else{
                            String[] relatedList;
                            if (relatedMaterialCodes.contains(",")) {
                                relatedList = relatedMaterialCodes.split(",");
                            } else {
                                relatedList = relatedMaterialCodes.split(",");
                            }
                            for (String related : relatedList) {
                                MaterialProductHistory newTemp = new MaterialProductHistory();
                                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);
@@ -615,33 +663,6 @@
                                newTemp.setCreateTime(date);
                                newAddedList.add(newTemp);
                            }
                        }
                    }
                    else{
                        String[] relatedList;
                        if (relatedMaterialCodes.contains(",")) {
                            relatedList = relatedMaterialCodes.split(",");
                        } else {
                            relatedList = relatedMaterialCodes.split(",");
                        }
                        for (String related : relatedList) {
                            MaterialProductHistory newTemp = new MaterialProductHistory();
                            newTemp.setParentCode(parentCode);
                            newTemp.setCustomCode(customCode);
                            newTemp.setSubCode(bom.getSubCode());
                            Material material = mService.getByCodeAndModel(bom.getSubCode(), bom.getSubModel());
                            newTemp.setSubMaterialId(material.getId());
                            newTemp.setSVersion(nextVersion);
                            newTemp.setEVersion(nextVersion);
                            newTemp.setQuantity(bom.getQuantity());
                            //2323-09-08 与杨红兰微信确认<就按照我上传清单的时间,系统里最新的是啥型号,就取什么型号>
                            Material relatedMaterial = mService.getLastByCode(related);
                            if(relatedMaterial == null){
                                throw new RuntimeException("当前替料"+related+",编码在平台中不存在,无法关联替代");
                            }
                            newTemp.setMaterialId(relatedMaterial.getId());
                            newTemp.setCreateTime(date);
                            newAddedList.add(newTemp);
                        }
                    }
                }
@@ -735,13 +756,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 +772,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 +830,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 +915,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 +941,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 +952,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 +1041,58 @@
                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);
                    if(materialWithSameCodeLatest!=null){
                        //附件转移并设置
                        mService.transferCopiedAttachment(materialWithSameCodeLatest,material);
                    }
                }
            }
        }
        //添加新增日志到tb_product_lock_log表
@@ -1022,12 +1145,12 @@
        return new Response().setIII(1,listMaterial.size()>0||listProduct.size()>0,listMaterial,listProduct,"");
    }
    //反馈管理-查询当前使用的所有的产品
    //反馈管理-查询所有的产品
    public Response getFkProduct() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.ne("version",-1);
        List list=mapper.selectList(wrapper);
        return new Response().setII(1,list.size()>0,list,"反馈管理-查询当前使用的所有的产品");
        //QueryWrapper wrapper=new QueryWrapper();
        //wrapper.ne("version",-1);
        List list=mapper.selectList(null);
        return new Response().setII(1,list.size()>0,list,"反馈管理-查询所有的产品");
    }
    //产品对比下拉选中