whycxzp
2022-09-01 0fc7f00553649eb30e403f17cbcbd1b9cbbd9658
产品bom新增更新
6个文件已修改
228 ■■■■■ 已修改文件
src/main/java/com/whyc/mapper/MaterialMapper.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/MaterialService.java 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/ProductBomHistoryService.java 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/ProductBomService.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/ProductService.java 199 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/MaterialMapper.xml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/mapper/MaterialMapper.java
@@ -1,6 +1,7 @@
package com.whyc.mapper;
import com.whyc.pojo.Material;
import com.whyc.pojo.ProductBom;
import java.util.List;
@@ -24,4 +25,6 @@
    void disableStatus(List<Material> materialList);
    List<Material> getListByCodeAndModelList2(List<ProductBom> bomList);
}
src/main/java/com/whyc/service/MaterialService.java
@@ -1,11 +1,13 @@
package com.whyc.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.Response;
import com.whyc.mapper.MaterialMapper;
import com.whyc.pojo.Material;
import com.whyc.pojo.ProductBom;
import com.whyc.util.CommonUtil;
import com.whyc.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
@@ -151,4 +153,14 @@
    private List<Material> getListByCodeAndModelList(List<Material> materialList) {
        return mapper.getListByCodeAndModelList(materialList);
    }
    public List<Material> getListByCodeAndModelList2(List<ProductBom> bomList) {
        return mapper.getListByCodeAndModelList2(bomList);
    }
    public Material getByCodeAndModel(String subCode, String subModel) {
        QueryWrapper<Material> query = Wrappers.query();
        query.eq("sub_code",subCode).eq("sub_model",subModel).last(" limit 1");
        return mapper.selectOne(query);
    }
}
src/main/java/com/whyc/service/ProductBomHistoryService.java
@@ -9,7 +9,6 @@
import com.whyc.mapper.MaterialMapper;
import com.whyc.mapper.ProductBomHistoryMapper;
import com.whyc.mapper.ProductHistoryMapper;
import com.whyc.mapper.ProductMapper;
import com.whyc.pojo.*;
import com.whyc.util.ActionUtil;
import org.apache.poi.hssf.usermodel.*;
@@ -325,4 +324,7 @@
        return  bomHis;
    }
    public void insertBatch(List<ProductBomHistory> bomHistoryList) {
      mapper.insertBatchSomeColumn(bomHistoryList);
    }
}
src/main/java/com/whyc/service/ProductBomService.java
@@ -466,4 +466,9 @@
        return mapper.selectList(query);
    }
    public void deleteByProductId(Integer productId) {
        UpdateWrapper<ProductBom> update = Wrappers.update();
        update.eq("product_id",productId);
        mapper.delete(update);
    }
}
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.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
@@ -25,6 +26,7 @@
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class ProductService {
@@ -37,6 +39,14 @@
    @Autowired
    private MaterialProductHistoryService mphService;
    @Autowired
    private ProductBomService pbService;
    @Autowired
    private ProductBomHistoryService pbhService;
    @Autowired
    private MaterialService mService;
    //查询出所有的产品信息(分页加模糊查询<产品的编码,型号,名字,定制表编号>
    public Response getAllProduct(String parentCode, String parentName, String parentModel, String customCode, int pageCurr, int pageSize) {
@@ -276,6 +286,9 @@
    public Response add(Product product) {
        String parentCode = product.getParentCode();
        String customCode = product.getCustomCode();
        String parentModel = product.getParentModel();
        List<ProductBom> bomList = product.getBomList();
        String fileUrl = product.getFileUrl();
        //查询产品最新的版本号
        ProductHistory latestProduct = phService.getLatestVersion(parentCode, customCode);
        ProductHistory enabledProduct = phService.getEnabledByParentCodeAndCustomCode(parentCode, customCode);
@@ -302,9 +315,191 @@
        }
        //将产品文件复制至正式路径
        //文件转移,未跟子件挂钩的所有图纸图片转移到产品版本下:doc_file/product/{产品型号}/standard或者{customCode}}/{version}/
        //跟子件挂钩的转移到子件图纸下:doc_file/material/
        File file = new File(fileUrl);
        List<String> fileUrlList = new LinkedList<>();
        List<String> dwgUrlList = null;
        List<String> picUrlList = null;
        //存于物料下,bom内有对应
        List<String> materialUrlList = new LinkedList<>();
        //存于产品下,bom内没对应
        List<String> productUrlList = new LinkedList<>();
        //物料表中不存在的,则添加到物料表中去(包含product这个物料)
        //List<Material> materialDwgUrlNameList = new LinkedList<>();
        return null;
        fileUrlList = FileUtil.getStaticFilePath(file,fileUrlList);
        //图纸dwg 子件/产品
        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());
        dwgUrlList.forEach(dwgUrl->{
            boolean existFlag = false;
            for (ProductBom bom :bomList){
                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())){
                    materialUrlList.add(dwgUrl);
                    existFlag = true;
                    bom.setDwgUrl("doc_file" + File.separator + "material"
                            + File.separator + fileFullName);
                    break;
                }
            }
            if(!existFlag) {
                productUrlList.add(dwgUrl);
            }
        });
        //一定是有对应物料的,从bom内剥离的
        picUrlList.forEach(picUrl->{
            for (ProductBom bom :bomList){
                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())){
                    bom.setPictureUrl("doc_file" + File.separator + "material"
                            + File.separator + fileFullName);
                    break;
                }
            }
        });
        //转移路径
        String projectDir = CommonUtil.getProjectDir();
        //doc_file/product/{产品型号}/standard或者{customCode}}/{version}/
        //跟子件挂钩的转移到子件图纸下:doc_file/material/
        String customCodeString = null;
        if(customCode!=null && !customCode.equals("")) {
            customCodeString = customCode;
        }else{
            customCodeString = "standard";
        }
        String productDir = projectDir + File.separator + "doc_file" + File.separator + "product" + File.separator + parentModel
                + File.separator + customCodeString + File.separator +nextVersion;
        String materialDir = projectDir + File.separator + "doc_file" + File.separator + "material";
        File productDirFile = new File(productDir);
        File materialFile = new File(materialDir);
        if(!productDirFile.exists()){
            productDirFile.mkdirs();
        }
        if(!materialFile.exists()){
            materialFile.mkdirs();
        }
        productUrlList.forEach(productUrl->{
            String dwgName = productUrl.substring(productUrl.lastIndexOf(File.separator) + 1, productUrl.length() - 4);
            try {
                FileCopyUtils.copy(new File(productUrl),new File(productDir+File.separator+dwgName+".dwg"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        materialUrlList.forEach(materialUrl->{
            String dwgName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1, materialUrl.length() - 4);
            try {
                FileCopyUtils.copy(new File(materialUrl),new File(materialDir+File.separator+dwgName+".dwg"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        picUrlList.forEach(picUrl->{
            String picFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1);
            try {
                FileCopyUtils.copy(new File(picUrl),new File(materialDir+File.separator+picFullName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        //更新product/product_bom/product_history/product_bom_history
        // -> product
        product.setCreateTime(new Date());
        product.setVersion(nextVersion);
        deleteAndInsert(product);
        // -> product_history
        ProductHistory productHistory = new ProductHistory();
        productHistory.setParentCode(product.getParentCode());
        productHistory.setParentName(product.getParentName());
        productHistory.setParentModel(product.getParentModel());
        productHistory.setNotes(product.getNotes());
        productHistory.setCustomCode(product.getCustomCode());
        productHistory.setCreateTime(product.getCreateTime());
        productHistory.setVersion(product.getVersion());
        productHistory.setEnabled(1);
        phService.insertAndUpdateEnabled(productHistory);
        // -> product_bom
        bomList.forEach(bom->{
            bom.setCreateDate(new Date());
        });
        pbService.insertBatch(bomList);
        // -> product_bom_history
        List<ProductBomHistory> bomHistoryList = new LinkedList<>();
        bomList.forEach(bom->{
            ProductBomHistory bomHistory = new ProductBomHistory();
            bomHistory.setProductId(productHistory.getId());
            bomHistory.setSubCode(bom.getSubCode());
            bomHistory.setCreateDate(new Date());
            bomHistoryList.add(bomHistory);
        });
        pbhService.insertBatch(bomHistoryList);
        //物料表中不存在的(依据:物料编码+物料型号),则添加到物料表中去(包含product这个物料)
        List<Material> materialExistList = mService.getListByCodeAndModelList2(bomList);
        List<String> subCodeList = materialExistList.stream().map(Material::getSubCode).collect(Collectors.toList());
        List<Material> materialList = new LinkedList<>();
        bomList.forEach(bom->{
            if(!subCodeList.contains(bom.getSubCode())){
                Material material = new Material();
                material.setCategory(bom.getCategory());
                material.setCreateDate(bom.getCreateDate());
                material.setDwgUrl(bom.getDwgUrl());
                material.setFileUrl(bom.getFileUrl());
                material.setMaterial(bom.getMaterial());
                material.setNotes(bom.getNotes());
                material.setPictureUrl(bom.getPictureUrl());
                material.setProducer(bom.getProducer());
                material.setQuantity(bom.getQuantity());
                // TODO 是否要更新同物料编码的老物料状态为0?
                material.setStatus(1);
                material.setSubCode(bom.getSubCode());
                material.setSubModel(bom.getSubModel());
                material.setSubName(bom.getSubName());
                material.setSurfaceDetail(bom.getSurfaceDetail());
                material.setThickness(bom.getThickness());
                material.setType(bom.getType());
                material.setUnit(bom.getUnit());
                materialList.add(material);
            }
        });
        //母料是否存在
        Material materialDB = mService.getByCodeAndModel(product.getParentCode(),product.getParentModel());
        if(materialDB==null) {
            Material material = new Material();
            material.setSubCode(product.getParentCode());
            material.setSubName(product.getParentName());
            material.setSubModel(product.getParentModel());
            materialList.add(material);
        }
        mService.insertBatch(materialList);
        return new Response().setII(1,"新增完成");
    }
    private void deleteAndInsert(Product product) {
        Product productDB = getByProductCodeAndCustomCode(product.getParentCode(), product.getCustomCode());
        mapper.deleteById(productDB.getId());
        pbService.deleteByProductId(productDB.getId());
        mapper.insert(product);
    }
    private Product getByProductCodeAndCustomCode(String parentCode, String customCode) {
        QueryWrapper<Product> query = Wrappers.query();
        query.eq("parent_code",parentCode).eq("custom_code",customCode).last(" limit 1");
        return mapper.selectOne(query);
    }
}
src/main/resources/mapper/MaterialMapper.xml
@@ -234,4 +234,9 @@
            select sub_code,sub_model from db_doc.tb_material where sub_code = #{item.subCode} and sub_model = #{item.subModel}
        </foreach>
    </select>
    <select id="getListByCodeAndModelList2" resultType="com.whyc.pojo.Material">
        <foreach collection="list" item="item" separator=" union ">
            select sub_code,sub_model from db_doc.tb_material where sub_code = #{item.subCode} and sub_model = #{item.subModel}
        </foreach>
    </select>
</mapper>