lxw
2022-09-08 02a01ec276fa3673cb31a37f0ed5e9ef2cf0f43b
src/main/java/com/whyc/service/ProductService.java
@@ -17,6 +17,7 @@
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;
@@ -280,6 +281,7 @@
        return list;
    }
    @Transactional
    public Response add(Product product) throws IOException {
        String parentCode = product.getParentCode();
        String customCode = product.getCustomCode();
@@ -288,12 +290,17 @@
        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;
@@ -308,20 +315,73 @@
            //查询产品对应的关联关系
            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);
            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 (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);
                    } else {
                        //旧版本生效,关联关系版本不连着
            if(relatedProduct == null
                || (relatedProduct.getParentCode().equals(product.getParentCode())
                && relatedProduct.getCustomCode().equals(product.getCustomCode())
                )
            ) { //属于是产品升级或基于标准产品定制
                List<MaterialProductHistory> mpList;
                if (latestProduct != null && enabledProduct != null) { //产品升级
                    mpList = mphService.getListByParentCodeAndCustomCodeAndVersion(parentCode, customCode, enabledProduct.getVersion());
                }else{ //基于标准产品定制
                    int standardEnabledVersion = getVersion(parentCode, "").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);
                    }
@@ -479,10 +539,11 @@
            //物料表中不存在的(依据:物料编码+物料型号),则添加到物料表中去(包含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::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())) {
                if (!subCodeList.contains(bom.getSubCode()+"/"+bom.getSubModel())) {
                    Material material = new Material();
                    material.setCategory(bom.getCategory());
                    material.setCreateDate(bom.getCreateDate());
@@ -512,11 +573,13 @@
                material.setSubCode(product.getParentCode());
                material.setSubName(product.getParentName());
                material.setSubModel(product.getParentModel());
                material.setStatus(1);
                materialList.add(material);
            }
            mService.insertBatch(materialList);
            if(materialList.size()!=0) {
                mService.insertBatch(materialList);
            }
            //更新product_history/product_bom_history/product/product_bom,
            // product的主键沿用对应product_history的
            //根据编码和型号确定物料id并对应quantity,存入数据库.