lxw
2022-09-05 599fb37cdd2d4c0c07ad6f832094c47d6d4f78ad
src/main/java/com/whyc/service/ProductBomHistoryService.java
@@ -9,12 +9,11 @@
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.*;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -23,9 +22,8 @@
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class ProductBomHistoryService {
@@ -43,6 +41,10 @@
    @Autowired
    private ProductBomService bomService;
    @Autowired
    private ProductService productService;
    //根据子件名称和母料型号查询历史版本记录
    public Response getBomHistoryByPModelAndSName(String pmodel, String sname) {
        QueryWrapper wrapper=new QueryWrapper();
@@ -50,7 +52,7 @@
        wrapper.eq("sub_name",sname);
        wrapper.orderByAsc("s_version");
        List list=mapper.selectList(wrapper);
        return new Response().setII(1,list.size()>0?true:false,list,"返回数据");
        return new Response().setII(1,list.size()>0,list,"返回数据");
    }
    /**指定版本的产品bom*/
@@ -85,19 +87,21 @@
                });
            }
        }
        return  new Response().setII(1,list.size()>0?true:false,list,"");
        return  new Response().setII(1,list.size()>0,list,"");
    }*/
  //历史产品信息查看(根据历史产品产品id和版本查询子件及其关联的物料信息)
  public Response getBomHistoryAndMaterial(int productId, int version) {
      List<ProductBomHistory> list=mapper.getBomHistoryAndMaterial(productId,version);
      //查询产品的软件
      List<ProductSoftware> listsoft=mapper.selectSoftware(productId);
      //将没有管理BOM的dwg文件
      QueryWrapper wrapper=new QueryWrapper();
      wrapper.eq("id",productId);
      wrapper.eq("version",version);
      wrapper.last("limit 1");
      ProductHistory pHistory=pHistoryMapper.selectOne(wrapper);
      List<ProductBomHistory> list=mapper.getBomHistoryAndMaterial(productId,pHistory.getSubVersionMax());
      //查询产品的软件
      List<ProductSoftware> listsoft=mapper.selectSoftware(productId);
      //将没有管理BOM的dwg文件
      String withOutDwg="doc_file/product"+File.separator+pHistory.getParentModel();
      if(pHistory.getCustomCode()!=null&&!pHistory.getCustomCode().isEmpty()){
          withOutDwg+=File.separator+pHistory.getCustomCode()+File.separator+version;
@@ -108,22 +112,16 @@
      //将没有管理BOM的dwg文件拷贝下载
      fileDirName=fileDirName+File.separator+withOutDwg;
      File start = new File(fileDirName);
      if(!start.exists()) {
          start.mkdir();
      }
      String[] filePath = start.list();//获取该文件夹下的所有文件名字
      if(filePath.length>=0){
          for(String temp : filePath) {
              ProductBomHistory p=new ProductBomHistory();
              p.setId(-1);
              p.setProductId(productId);
              p.setParentCode(pHistory.getParentModel());
              p.setVersion(version);
              p.setDwgUrl(withOutDwg+File.separator+temp);
              list.add(p);
      String[] files =new String[]{};
      if(start.exists()) {
          files= start.list();//获取该文件夹下的所有文件名字
          if(files.length>=0){
              for(int i=0;i<files.length;i++) {
                  files[i]=withOutDwg+File.separator+files[i];
              }
          }
      }
      return new Response().setIII(1,list.size()>0?true:false,list,listsoft,"返回物料信息及关联物料");
      return new Response().setIIII(1,list.size()>0,list,listsoft,files,"返回物料信息及关联物料");
  }
    //历史产品下载(产品id和版本<下载的版本>)
    public void downloadProductHistory(HttpServletRequest req, HttpServletResponse resp, int productId, int version) {
@@ -134,10 +132,11 @@
        wrapper.last("limit 1");
        ProductHistory pHistory=pHistoryMapper.selectOne(wrapper);
        //读取指定版本产品的子件和关联的物料信息
        List<ProductBomHistory> list=mapper.getBomHistoryAndMaterial(productId,version);
        List<ProductBomHistory> list=mapper.getBomHistoryAndMaterial(productId,pHistory.getSubVersionMax());
        //处理存在关联关系的物料
        List<ProductBomHistory> endList=new ArrayList<>();
        list.stream().forEach(bomHistory -> {
            bomHistory.setConnFlag(0);
            endList.add(bomHistory);
            if(bomHistory.getMaterials()!=null&&bomHistory.getMaterials().size()>0){
                for (Material m:bomHistory.getMaterials()) {
@@ -147,12 +146,6 @@
                }
            }
        });
        HSSFWorkbook wb = new HSSFWorkbook();
        //字体格式-加粗
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        String withOutDwg="doc_file/product"+File.separator+pHistory.getParentModel();
        if(pHistory.getCustomCode()!=null&&!pHistory.getCustomCode().isEmpty()){
            withOutDwg+=File.separator+pHistory.getCustomCode()+File.separator+version;
@@ -160,7 +153,7 @@
            withOutDwg+=File.separator+"standard"+File.separator+version;
        }
        //生成excel并将dwg文件放在同一报下压缩
        creatBomHsitoryExcel(req,resp,pHistory,endList,wb,withOutDwg);
        creatBomHsitoryExcel(req,resp,pHistory,endList,withOutDwg);
        //记录日志
        DocUser docUser= ActionUtil.getUser();
        String operationDetail="具体产品为:"+pHistory.toString();
@@ -169,11 +162,23 @@
        logService.recordOperationLog(docUser.getId(),docUser.getName(), UserOperation.TYPE_DOWNLOAD.getType(),new Date(),terminalIp,opreationMsg,operationDetail);
    }
    //根据产品信息创建excel表格并存放在指定目录
    public void creatBomHsitoryExcel(HttpServletRequest req, HttpServletResponse resp,ProductHistory pHistory,List<ProductBomHistory> list, HSSFWorkbook wb,String withOutDwg){
    public void creatBomHsitoryExcel(HttpServletRequest req, HttpServletResponse resp,ProductHistory pHistory,List<ProductBomHistory> list,String withOutDwg){
        String fileDirName = FileDirPath.getFileDirName();
        String rootFace="";
        String excelName="";
        String[] titleNames=new String[]{"序列","母物料编码","母物料名称","母物料型号","类别","子件编码","子件名称","子件型号"
                ,"基本单位","子件数量","生产商","封装类型/材质","元件编号/料厚","表面处理/物料详情","备注","图片"};
        //创建单个sheet
        HSSFWorkbook wb = new HSSFWorkbook();
        //字体格式-加粗
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(IndexedColors.GOLD.getIndex());//添加前景色,内容看的清楚
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //font.setColor(HSSFFont.COLOR_RED);
        cellStyle.setFont(font);
        HSSFSheet sheet = wb.createSheet("bom_"+pHistory.getVersion()+"信息");
        sheet.setColumnWidth(1,5000);
        sheet.setColumnWidth(2,5000);
@@ -187,24 +192,14 @@
        //图片元素
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        int rownum = 1;
        sheet.createRow(rownum);
        sheet.getRow(rownum).createCell(1).setCellValue("序列");
        sheet.getRow(rownum).createCell(2).setCellValue("母物料编码");
        sheet.getRow(rownum).createCell(3).setCellValue("母物料名称");
        sheet.getRow(rownum).createCell(4).setCellValue("母物料型号");
        sheet.getRow(rownum).createCell(5).setCellValue("类别");
        sheet.getRow(rownum).createCell(6).setCellValue("子件编码");
        sheet.getRow(rownum).createCell(7).setCellValue("子件名称");
        sheet.getRow(rownum).createCell(8).setCellValue("子件型号");
        sheet.getRow(rownum).createCell(9).setCellValue("基本单位");
        sheet.getRow(rownum).createCell(10).setCellValue("子件数量");
        sheet.getRow(rownum).createCell(11).setCellValue("生产商");
        sheet.getRow(rownum).createCell(12).setCellValue("封装类型/材质");
        sheet.getRow(rownum).createCell(13).setCellValue("元件编号/料厚");
        sheet.getRow(rownum).createCell(14).setCellValue("表面处理/物料详情");
        sheet.getRow(rownum).createCell(15).setCellValue("备注");
        sheet.getRow(rownum).createCell(16).setCellValue("图片");
        HSSFRow row=sheet.createRow(rownum);
        HSSFCell cell=row.createCell(0);
        if(titleNames.length>0){
            for (int i=0;i<titleNames.length;i++) {
                cell=row.createCell(i+1);
                cell.setCellValue(titleNames[i]);
            }
        }
        excelName=pHistory.getParentCode()+"_"+pHistory.getParentModel()+"_"+pHistory.getVersion();
        rootFace=fileDirName+ File.separator+excelName;
        File destfile = new File(rootFace);
@@ -224,23 +219,93 @@
                    File sourceFile=new File(fileDirName+ File.separator+dwgUrl);
                    bomService.copyFile(sourceFile,rootFace);
                }
                Row row=sheet.createRow(rownum+i+1);
                //row.setHeight((short)(1500));
                sheet.getRow(rownum+i+1).createCell(1).setCellValue(i+1);
                sheet.getRow(rownum+i+1).createCell(2).setCellValue(pHistory.getParentCode());
                sheet.getRow(rownum+i+1).createCell(3).setCellValue(pHistory.getParentName());
                sheet.getRow(rownum+i+1).createCell(4).setCellValue(pHistory.getParentModel());
                sheet.getRow(rownum+i+1).createCell(5).setCellValue(bomHistory.getCategory());
                sheet.getRow(rownum+i+1).createCell(6).setCellValue(bomHistory.getSubCode());
                sheet.getRow(rownum+i+1).createCell(7).setCellValue(bomHistory.getSubName());
                sheet.getRow(rownum+i+1).createCell(8).setCellValue(bomHistory.getSubModel());
                sheet.getRow(rownum+i+1).createCell(9).setCellValue(bomHistory.getUnit()==null?"":bomHistory.getUnit());
                sheet.getRow(rownum+i+1).createCell(10).setCellValue(bomHistory.getQuantity());
                sheet.getRow(rownum+i+1).createCell(11).setCellValue(bomHistory.getProducer()==null?"":bomHistory.getProducer());
                sheet.getRow(rownum+i+1).createCell(12).setCellValue(bomHistory.getMaterial());
                sheet.getRow(rownum+i+1).createCell(13).setCellValue(bomHistory.getThickness());
                sheet.getRow(rownum+i+1).createCell(14).setCellValue(bomHistory.getSurfaceDetail());
                sheet.getRow(rownum+i+1).createCell(15).setCellValue(bomHistory.getNotes());
                rownum=rownum+1;
                row=sheet.createRow(rownum);
                if(bomHistory.getConnFlag()==1){
                    cell=row.createCell(0);
                    cell.setCellValue("替换件");
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(1);
                    cell.setCellValue(i+1);
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(2);
                    cell.setCellValue(pHistory.getParentCode());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(3);
                    cell.setCellValue(pHistory.getParentName());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(4);
                    cell.setCellValue(pHistory.getParentModel());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(5);
                    cell.setCellValue(bomHistory.getCategory());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(6);
                    cell.setCellValue(bomHistory.getSubCode());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(7);
                    cell.setCellValue(bomHistory.getSubName());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(8);
                    cell.setCellValue(bomHistory.getSubModel());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(9);
                    cell.setCellValue(bomHistory.getUnit()==null?"":bomHistory.getUnit());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(10);
                    cell.setCellValue(bomHistory.getQuantity()==null?"": bomHistory.getQuantity().toString());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(11);
                    cell.setCellValue(bomHistory.getProducer()==null?"":bomHistory.getProducer());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(12);
                    cell.setCellValue(bomHistory.getMaterial());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(12);
                    cell.setCellValue(bomHistory.getThickness());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(13);
                    cell.setCellValue(bomHistory.getThickness());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(14);
                    cell.setCellValue(bomHistory.getSurfaceDetail());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(15);
                    cell.setCellValue(bomHistory.getNotes());
                    cell.setCellStyle(cellStyle);
                }else{
                    row.createCell(1).setCellValue(i+1);
                    row.createCell(2).setCellValue(pHistory.getParentCode());
                    row.createCell(3).setCellValue(pHistory.getParentName());
                    row.createCell(4).setCellValue(pHistory.getParentModel());
                    row.createCell(5).setCellValue(bomHistory.getCategory());
                    row.createCell(6).setCellValue(bomHistory.getSubCode());
                    row.createCell(7).setCellValue(bomHistory.getSubName());
                    row.createCell(8).setCellValue(bomHistory.getSubModel());
                    row.createCell(9).setCellValue(bomHistory.getUnit()==null?"":bomHistory.getUnit());
                    row.createCell(10).setCellValue(bomHistory.getQuantity()==null?"":bomHistory.getQuantity().toString());
                    row.createCell(11).setCellValue(bomHistory.getProducer()==null?"":bomHistory.getProducer());
                    row.createCell(12).setCellValue(bomHistory.getMaterial());
                    row.createCell(13).setCellValue(bomHistory.getThickness());
                    row.createCell(14).setCellValue(bomHistory.getSurfaceDetail());
                    row.createCell(15).setCellValue(bomHistory.getNotes());
                }
                if((bomHistory.getPictureUrl()!=null)&&(!bomHistory.getPictureUrl().isEmpty())){
                    ByteArrayOutputStream byteArrayOut = null;
                    try {
@@ -318,7 +383,6 @@
        bomHis.setSubName(m.getSubName());
        bomHis.setSubModel(m.getSubModel());
        bomHis.setUnit(m.getUnit());
        bomHis.setQuantity(m.getQuantity());
        bomHis.setProducer(m.getProducer());
        bomHis.setMaterial(m.getMaterial());
        bomHis.setThickness(m.getThickness());
@@ -328,7 +392,119 @@
        bomHis.setDwgUrl(m.getDwgUrl());
        bomHis.setFileUrl(m.getFileUrl());
        bomHis.setCreateDate(m.getCreateDate());
        bomHis.setConnFlag(1);
        return  bomHis;
    }
    public void insertBatch(List<ProductBomHistory> bomHistoryList) {
      mapper.insertBatchSomeColumn(bomHistoryList);
    }
    /**
     * 对比
     * @param preProductId 旧的版本,作为基准
     * @param productId 新的产品版本
     * @return
     */
    public Response compare(int preProductId, int productId) {
        //查询两个版本对应的bom最新小版本
        List<ProductBomHistory> baseBomHistoryList = getLatestBomListByProductId(preProductId);
        List<ProductBomHistory> bomHistoryList = getLatestBomListByProductId(productId);
        List<ProductBomHistory> diffList = new LinkedList<>();
        List<ProductBomHistory> addList = new LinkedList<>();
        List<ProductBomHistory> deleteList = new LinkedList<>();
        //物料id,对比字段:数量
        baseBomHistoryList.forEach(baseBom->{
            bomHistoryList.forEach(bom->{
                if(bom.getMaterialId().equals(baseBom.getMaterialId())){
                    if(!bom.getQuantity().equals(baseBom.getQuantity())){
                        ProductBomHistory history = new ProductBomHistory();
                        String diffQuantity = "原数量:"+baseBom.getQuantity()+"/新数量:"+bom.getQuantity();
                        history.setNotes(diffQuantity);
                        history.setMaterialObj(bom.getMaterialObj());
                        diffList.add(history);
                    }
                }
            });
        });
        List<Integer> baseCodeList = baseBomHistoryList.stream().map(ProductBomHistory::getMaterialId).collect(Collectors.toList());
        List<Integer> codeList = bomHistoryList.stream().map(ProductBomHistory::getMaterialId).collect(Collectors.toList());
        bomHistoryList.forEach(bom->{
            if(!baseCodeList.contains(bom.getMaterialId())){
                addList.add(bom);
            }
        });
        baseBomHistoryList.forEach(baseBom->{
            if(!codeList.contains(baseBom.getMaterialId())){
                deleteList.add(baseBom);
            }
        });
        return new Response().setIII(1,diffList,addList,deleteList,"对比完成");
    }
    /**
     * @param baseProduct 基准产品为null,则直接找上传产品的最新标准版本
     * @param product 上传产品
     * @return
     */
    public Map<String, List> parseCompare(Product baseProduct, Product product) {
        List<ProductBom> bomList = product.getBomList();
        List<ProductBom> baseProductBomList = null;
        Map<String,List> compareMap = new HashMap<>();
        List<ProductBom> diffList = new LinkedList<>();
        List<ProductBom> addList = new LinkedList<>();
        List<ProductBom> deleteList = new LinkedList<>();
        //查找到基准产品
        if(baseProduct == null){ //查找上传的产品最新标准版本
            baseProductBomList = bomService.getEnabledBomListByParentCodeAndCustomCode(product.getParentCode(),"0");
        }else{ //根据baseProduct的code和custom_code
            baseProductBomList = bomService.getEnabledBomListByParentCodeAndCustomCode(baseProduct.getParentCode(),baseProduct.getCustomCode());
        }
        //对比:根据code+model定位,比较数量
        List<String> baseCodeModelList = baseProductBomList.stream().map(bom -> bom.getMaterialObj().getSubCode() + "/" + bom.getMaterialObj().getSubModel()).collect(Collectors.toList());
        Map<String, List<ProductBom>> groupedBaseBomList = baseProductBomList.stream().collect(Collectors.groupingBy(bom -> bom.getMaterialObj().getSubCode() + "/" + bom.getMaterialObj().getSubModel()));
        List<String> codeModelList = bomList.stream().map(bom -> bom.getSubCode() + "/" + bom.getSubModel()).collect(Collectors.toList());
        bomList.forEach(bom->{
            String codeModel = bom.getSubCode() + "/" + bom.getSubModel();
            if(!baseCodeModelList.contains(codeModel)){
                addList.add(bom);
            }else{
                ProductBom baseBom = groupedBaseBomList.get(codeModel).get(0);
                if(bom.getQuantity().intValue()!=baseBom.getQuantity()){
                    ProductBom diffBom = new ProductBom();
                    String diffQuantity =  "原数量:"+baseBom.getQuantity()+"/新数量:"+bom.getQuantity();
                    diffBom.setNotes(diffQuantity);
                    diffBom.setMaterialObj(bom.getMaterialObj());
                    diffList.add(diffBom);
                }
            }
        });
        baseProductBomList.forEach(bom->{
            String codeModel = bom.getMaterialObj().getSubCode() + "/" + bom.getMaterialObj().getSubModel();
            if(!codeModelList.contains(codeModel)){
                deleteList.add(bom);
            }
        });
        compareMap.put("diffList",diffList);
        compareMap.put("addList",addList);
        compareMap.put("deleteList",deleteList);
        return compareMap;
    }
    /**关联查询到具体的bom内物料信息*/
    private List<ProductBomHistory> getLatestBomListByProductId(int productId) {
        QueryWrapper<ProductBomHistory> query = Wrappers.query();
        query.select("max(sub_e_version) as sub_e_version").eq("product_id",productId);
        Integer latestSubVersion = mapper.selectOne(query).getSubEVersion();
        //QueryWrapper<ProductBomHistory> query2 = Wrappers.query();
        //query2.eq("product_id",productId).le("sub_s_version",latestSubVersion).ge("sub_e_version",latestSubVersion);
        return mapper.getBomListByProductIdAndSubVersion(productId,latestSubVersion);
    }
}