whycxzp
2025-05-09 f92783da5430037bf80cf28a5cf7091cdecefbb7
src/main/java/com/whyc/service/ProductBomHistoryService.java
@@ -1,6 +1,7 @@
package com.whyc.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.constant.UserOperation;
import com.whyc.dto.FIleSilkLock;
@@ -13,9 +14,12 @@
import com.whyc.mapper.ProductHistoryMapper;
import com.whyc.pojo.*;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
import com.whyc.util.ImageDiff;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
@@ -25,6 +29,7 @@
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
@@ -51,6 +56,9 @@
    @Autowired(required = false)
    private AttachLockMapper attachLockMapper;
    @Autowired
    private MaterialService materialService;
    //根据子件名称和母料型号查询历史版本记录
    public Response getBomHistoryByPModelAndSName(String pmodel, String sname) {
@@ -206,7 +214,8 @@
        return new Response().setIIII(1,list.size()>0,list,listsoft,fIleSilkLock,"返回物料信息及关联物料");
    }
    //历史产品下载(产品id和版本<下载的版本>)
    public void downloadProductHistory(HttpServletRequest req, HttpServletResponse resp, int productId, int version) {
    public void downloadProductHistory(HttpServletRequest req, HttpServletResponse resp, int productId, int version
            ,  String oprateReason,  String oprateInfo) {
        //读取产品信息
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",productId);
@@ -246,16 +255,11 @@
            withOutDwg+=File.separator+"standard"+File.separator+version;
        }
        //生成excel并将dwg文件放在同一报下压缩
        creatBomHsitoryExcel(req,resp,pHistory,endList,withOutDwg);
        //记录日志
        DocUser docUser= ActionUtil.getUser();
        String operationDetail="具体产品为:"+pHistory.toString();
        String opreationMsg="执行了"+pHistory.getVersion()+"版本产品的下载操作";
        String terminalIp=req.getRemoteAddr();
        logService.recordOperationLog(docUser.getId(),docUser.getName(), UserOperation.TYPE_DOWNLOAD.getType(),new Date(),terminalIp,opreationMsg,operationDetail);
        creatBomHsitoryExcel(req,resp,pHistory,endList,withOutDwg,oprateReason,oprateInfo,ActionUtil.sdfwithALL.format(pHistory.getVersionTime()));
    }
    //根据产品信息创建excel表格并存放在指定目录
    public void creatBomHsitoryExcel(HttpServletRequest req, HttpServletResponse resp,ProductHistory pHistory,List<ProductBomHistory> list,String withOutDwg){
    public void creatBomHsitoryExcel(HttpServletRequest req, HttpServletResponse resp,ProductHistory pHistory,List<ProductBomHistory> list,String withOutDwg
            ,  String oprateReason,  String oprateInfo,String oprateVersion){
        String fileDirName = FileDirPath.getFileDirName();
        String rootFace="";
        String excelName="";
@@ -445,9 +449,10 @@
            FileOutputStream forootFace = new FileOutputStream(file);
            ZipUtils.toZip(rootFace, forootFace,true);
            // 转码防止乱码
            resp.addHeader("Content-Disposition", "attachment;filename="
            /*resp.addHeader("Content-Disposition", "attachment;filename="
                    + new String(excelName.getBytes("UTF-8"), "ISO8859-1")
                    + ".zip");
                    + ".zip");*/
            resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode ( excelName+".zip", "utf-8"));
            OutputStream out = resp.getOutputStream();
            FileInputStream in = new FileInputStream(rootFace+".zip");
            int len=0;
@@ -465,6 +470,9 @@
        } catch (IOException e) {
            e.printStackTrace();
        }
        //记录日志
        logService.recordOperationLogDownLoad(ActionUtil.getUser().getId(),ActionUtil.getUser().getName(), UserOperation.TYPE_DOWNLOAD_PRODUCT.getType(),new Date(),req.getRemoteAddr()
                ,excelName+".zip",rootFace+".zip",oprateReason,oprateInfo,oprateVersion);
    }
    //将物料存放为一个bomHistory为下载做准备
@@ -577,6 +585,8 @@
        List<ProductBom> diffList = new LinkedList<>();
        List<ProductBom> addList = new LinkedList<>();
        List<ProductBom> deleteList = new LinkedList<>();
        //新增差异对比-图纸差异
        List<ProductBom> diffDwgList = new LinkedList<>();
        //查找到基准产品
        if(baseProduct == null){ //查找上传的现有产品表内未锁定的版本
@@ -644,9 +654,58 @@
                deleteList.add(bom);
            }
        });
        //图纸差异对比逻辑
        //1.先找出所有需要对比的图纸
        List<ProductBom> bomListWithDwg = bomList.stream().filter(bom -> !StringUtils.isEmpty(bom.getDwgUrl())).collect(Collectors.toList());
        //2.从数据库中找到物料对应的图纸
        if(bomListWithDwg.size()!=0) {
            List<Material> materialListInDB = materialService.getListByCodeAndModelList3(bomListWithDwg);
            if (materialListInDB.size() != 0) {
                //对比图纸
                for (int i = 0; i < materialListInDB.size(); i++) {
                    Material materialInDB = materialListInDB.get(i);
                    String materialStrInDB = materialInDB.getSubCode() + "/" + materialInDB.getSubModel();
                    for (int j = 0; j < bomListWithDwg.size(); j++) {
                        ProductBom material = bomListWithDwg.get(j);
                        String materialStr = material.getSubCode() + "/" + material.getSubModel();
                        if (materialStrInDB.equals(materialStr)) {
                            //如果物料一致,则进行对比
                            String dwgUrlInDB = materialInDB.getDwgUrl();
                            String dwgUrl = material.getDwgUrl();
                            //物料图纸转化为png图片进行对比(弃用)
                            String dwgFileStr = CommonUtil.getProjectDir() + File.separator + dwgUrlInDB;
                            String dwgFileStr2 = CommonUtil.getProjectDir() + File.separator + dwgUrl;
                            /*String absoluteDwgPngPath = dwgFileStr.substring(0, dwgFileStr.lastIndexOf(".")) + "-dwg.png";
                            String absoluteDwgPngPath2 = dwgFileStr2.substring(0, dwgFileStr2.lastIndexOf(".")) + "-dwg.png";
                            if (!new File(absoluteDwgPngPath).exists()) {
                                DwgToPngUtil.dwg2png(new File(dwgFileStr));
                            }
                            if (!new File(absoluteDwgPngPath2).exists()) {
                                DwgToPngUtil.dwg2png(new File(dwgFileStr2));
                            }*/
                            boolean sameImage = ImageDiff.compareImagesCheck(dwgFileStr, dwgFileStr2);
                            if (!sameImage) { //图纸不同
                                //material.setNotes(absoluteDwgPngPath.replace(CommonUtil.getProjectDir()+ File.separator, "") + separator + absoluteDwgPngPath2.replace(CommonUtil.getProjectDir()+ File.separator, ""));
                                //克隆一个新的对象material
                                ProductBom materialDiff = new ProductBom();
                                BeanUtils.copyProperties(material, materialDiff);
                                materialDiff.setNotes(dwgUrlInDB);
                                diffDwgList.add(materialDiff);
                            }
                        }
                    }
                }
            }
        }
        compareMap.put("diffList",diffList);
        compareMap.put("addList",addList);
        compareMap.put("deleteList",deleteList);
        compareMap.put("diffDwgList",diffDwgList);
        return compareMap;
    }
@@ -667,7 +726,7 @@
    }
    //根据产品母料型号,定制单号,版本下载原始压缩包文件
    public Response getOriginalZip(String parentModel, String customCode, int version) {
    public Response getOriginalZip(String parentCode,String parentModel, String customCode, int version) {
        String fileDirName = FileDirPath.getFileDirName();
        String withOutDwg=fileDirName+File.separator+"doc_file"+File.separator+"product"+File.separator+parentModel;
        if(customCode!=null&&!customCode.isEmpty()){
@@ -683,9 +742,10 @@
        String[] filePath = file.list();//获取该文件夹下的所有文件以及目录的名字
        if(filePath!=null&&filePath.length>0){
            for (String tmp:filePath) {
                if(tmp.contains(".zip")){
                if((tmp.contains(parentCode)||tmp.contains(parentModel))&&tmp.contains(".zip")){
                    bl=true;
                    withOutDwg=withOutDwg+File.separator+tmp;
                    break;
                }
            }
        }