whyclxw
2024-07-23 a2cb0eed5a0bb7cf9d5eb8afe74f4ba6836205b4
src/main/java/com/whyc/service/ProductService.java
@@ -3,9 +3,11 @@
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;
import com.whyc.mapper.ProductHistoryMapper;
import com.whyc.mapper.ProductMapper;
import com.whyc.pojo.*;
import com.whyc.util.DateUtil;
@@ -34,6 +36,9 @@
    @Autowired(required = false)
    private ProductMapper mapper;
    @Autowired(required = false)
    private ProductHistoryMapper hisMapper;
    @Autowired
    private ProductHistoryService phService;
@@ -52,10 +57,13 @@
    @Autowired
    private ProductLockLogService productLockLogService;
    @Autowired
    private MaterialHistoryService materialHistoryService;
    //查询出所有的产品信息(分页加模糊查询<产品的编码,型号,名字,定制表编号>
    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();
    public Response getAllProduct(String subCode,String parentCode, String parentName, String parentModel, String customCode,Integer enabled, int pageCurr, int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
        /*QueryWrapper wrapper=new QueryWrapper();
        if(parentCode!=null){
            wrapper.like("parent_code",parentCode);
        }
@@ -74,7 +82,15 @@
        }
        wrapper.orderByAsc("id");
        List list=mapper.selectList(wrapper);*/
        List list=mapper.getAllProduct(subCode,parentCode,parentName,parentModel,customCode);
        List<Product> list=mapper.getAllProduct(subCode,parentCode,parentName,parentModel,customCode,enabled);
        /*list.stream().forEach(product -> {
            //1.查询是否存在该记录
            QueryWrapper qwrapper=new QueryWrapper();
            qwrapper.eq("id",product.getId());
            qwrapper.last("limit 1");
            ProductHistory pHis=hisMapper.selectOne(qwrapper);
            product.setEnabled(pHis.getEnabled());
        });*/
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list.size()>0,pageInfo,"返回产品信息");
    }
@@ -133,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);
        //解压文件夹
@@ -233,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();
@@ -433,6 +455,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;
        //校验凡是有定制表单号的产品,不能使用已上传过的标准产品编码
@@ -559,36 +583,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);
@@ -602,33 +655,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);
                        }
                    }
                }
@@ -669,18 +695,17 @@
            his.setEnabled(-1);
            phService.insertAndUpdateEnabled(his);
            //phService.insert(his);
            // -> product 现有产品和产品bom不需要插入,但是要做删除原来的旧版本
            // -> product 现有产品和产品bom需要插入,version默认为-1,同时要做删除原来的旧版本
            product.setId(his.getId());
            product.setCreateTime(date);
            product.setVersion(1);
            //deleteAndInsert(product);
            delete(product);
            product.setVersion(-1);
            deleteAndInsert(product);
            // -> bom
            /*bomList.forEach(bom-> {
            bomList.forEach(bom-> {
                bom.setProductId(product.getId());
                bom.setSubVersion(1);
            });
            pbService.insertBatch(bomList);*/
            pbService.insertBatch(bomList);
            // -> bom_his
            List<ProductBomHistory> bomHistoryList = new LinkedList<>();
            bomList.forEach(bom->{
@@ -723,13 +748,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"))
@@ -738,8 +764,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;
                    }
                }
@@ -796,36 +822,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())) {
@@ -834,16 +907,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());
@@ -859,10 +933,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());
@@ -870,11 +944,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);
@@ -912,14 +987,13 @@
            productHistory.setEnabled(-1);
            phService.insertAndUpdateEnabled(productHistory);
            // -> product 现有产品和产品bom不需要插入,但是要做删除原来的旧版本
            // -> product 现有产品和产品bom需要插入,version默认为-1,同时要做删除原来的旧版本
            product.setId(productHistory.getId());
            product.setCreateTime(date);
            product.setVersion(nextVersion);
            //deleteAndInsert(product);
            delete(product);
            product.setVersion(-1);
            deleteAndInsert(product);
            // -> product_bom
            /*List<ProductBom> productBomList = new LinkedList<>();
            List<ProductBom> productBomList = new LinkedList<>();
            bomMaterialList.forEach(bomMaterial -> {
                ProductBom bom = new ProductBom();
                bom.setProductId(product.getId());
@@ -937,7 +1011,7 @@
                productBomList.add(bom);
            });
            pbService.insertBatch(productBomList);*/
            pbService.insertBatch(productBomList);
            // -> product_bom_history
            List<ProductBomHistory> bomHistoryList = new LinkedList<>();
            bomMaterialList.forEach(bomMaterial -> {
@@ -959,6 +1033,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表
@@ -974,7 +1100,7 @@
        lockLog.setLockFlag(-1);
        productLockLogService.insert(lockLog);
        return new Response().setII(1,"新增完成");
        return new Response().set(1,true,"新增完成");
    }
    private void deleteAndInsert(Product product) {
@@ -1028,5 +1154,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,"验证母料编码是否存在产品中");
    }
}