whyclxw
2024-07-24 fb7534b29aec26dae6dedb4f1f31d196a38fdd1f
src/main/java/com/whyc/service/ProductBomService.java
@@ -9,6 +9,7 @@
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Response;
import com.whyc.dto.ZipUtils;
import com.whyc.mapper.AttachLockMapper;
import com.whyc.mapper.MaterialMapper;
import com.whyc.mapper.ProductBomMapper;
import com.whyc.mapper.ProductMapper;
@@ -16,16 +17,18 @@
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;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
@@ -43,6 +46,9 @@
    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired(required = false)
    private AttachLockMapper attachLockMapper;
    @Autowired
    private DocLogService logService;
@@ -187,6 +193,35 @@
        }
    }
    //文件夹的拷贝移除productId对应的被锁定的
    public  void copyDirWithOutProductId(String sourcePathDir, String newPathDir,int productId) {
        File start = new File(sourcePathDir);
        File end = new File(newPathDir);
        if(!start.exists()) {
            return;
        }
        String[] filePath = start.list();//获取该文件夹下的所有文件以及目录的名字
        if(!end.exists()) {
            end.mkdir();
        }
        if(filePath.length>=0){
            for(String temp : filePath) {
                //添加满足情况的条件
                if(new File(sourcePathDir + File.separator + temp ).isFile()) {
                    QueryWrapper wrapper=new QueryWrapper();
                    wrapper.eq("product_id",productId);
                    wrapper.eq("attach_name",temp);
                    wrapper.eq("lock_flag",1);
                    wrapper.last("limit 1");
                    AttachLock attachLock=attachLockMapper.selectOne(wrapper);
                    if(attachLock==null){
                        //为文件则进行拷贝
                        copyFile(new File(sourcePathDir + File.separator + temp ), newPathDir );
                    }
                }
            }
        }
    }
    public void updateUrl(List<ProductBomApproving> fileBomApprovingList) {
        mapper.updateUrl(fileBomApprovingList);
    }
@@ -210,7 +245,8 @@
        return new Response().setII(1,list!=null,pageInfo,"返回数据");
    }*/
    //产品下载(产品id和版本)
    public  void downloadProduct(HttpServletRequest req, HttpServletResponse resp, int productId , int version) {
    public  void downloadProduct(HttpServletRequest req, HttpServletResponse resp, int productId , int version
       , @RequestParam String oprateReason, @RequestParam String oprateInfo) {
        HSSFWorkbook wb = new HSSFWorkbook();
        //读取产品信息
        QueryWrapper wrapper=new QueryWrapper();
@@ -224,6 +260,17 @@
        List<ProductBom> endList=new ArrayList<>();
        list.stream().forEach(bom -> {
            bom.setConnFlag(0);
            if(bom.getDwgUrl()!=null){
                QueryWrapper qwrapper=new QueryWrapper();
                qwrapper.eq("material_id",bom.getMaterialId());
                qwrapper.eq("attach_name",bom.getDwgUrl().substring(bom.getDwgUrl().lastIndexOf("\\")+1));
                qwrapper.eq("lock_flag",1);
                qwrapper.last("limit 1");
                AttachLock attachLock=attachLockMapper.selectOne(qwrapper);
                if(attachLock!=null){
                    bom.setDwgUrl("");
                }
            }
            endList.add(bom);
            if(bom.getMaterials()!=null&&bom.getMaterials().size()>0){
                for (Material m:bom.getMaterials()) {
@@ -241,18 +288,13 @@
            withOutDwg+=File.separator+"standard"+File.separator+version;
        }
        //生成excel并将dwg文件放在同一报下压缩
        creatBomExcel(req,resp,product,endList,wb,withOutDwg);
        //记录日志
        DocUser docUser= ActionUtil.getUser();
        String operationDetail="具体产品信息为:"+product.toString();
        String opreationMsg="执行了最新版产品下载操作";
        String terminalIp=req.getRemoteAddr();
        logService.recordOperationLog(docUser.getId(),docUser.getName(), UserOperation.TYPE_DOWNLOAD.getType(),new Date(),terminalIp,opreationMsg,operationDetail);
        creatBomExcel(req,resp,product,endList,wb,withOutDwg,ActionUtil.sdfwithALL.format(product.getVersionTime()),oprateReason,oprateInfo);
    }
    //根据产品信息创建excel表格并存放在指定目录
    private void creatBomExcel(HttpServletRequest req, HttpServletResponse resp,Product product,List list, HSSFWorkbook wb,String withOutDwg){
    private void creatBomExcel(HttpServletRequest req, HttpServletResponse resp,Product product,List list, HSSFWorkbook wb,String withOutDwg
            , String oprateVersion, @RequestParam String oprateReason, @RequestParam String oprateInfo){
        String fileDirName = FileDirPath.getFileDirName();
        String rootFace="";
        String excelName="";
@@ -260,9 +302,11 @@
                                        ,"基本单位","子件数量","生产商","封装类型/材质","元件编号/料厚","表面处理/物料详情","备注","图片"};
        //字体格式-加粗
        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);
        //font.setColor(HSSFFont.COLOR_RED);
        cellStyle.setFont(font);
        //创建单个sheet
        HSSFSheet sheet = wb.createSheet("产品信息");
@@ -296,7 +340,7 @@
        }
        //将没有管理BOM的dwg文件拷贝下载
        withOutDwg=fileDirName+File.separator+withOutDwg;
        copyDir(withOutDwg,rootFace);
        copyDirWithOutProductId(withOutDwg,rootFace,product.getId());
        //将选中的文件存入指定目录下打包下载
        if(list!=null&&list.size()>0){
            for (int i=0;i<list.size();i++) {
@@ -400,7 +444,7 @@
                        BufferedImage bufferImg = ImageIO.read(new FileInputStream(new File(fileDirName+File.separator+bom.getPictureUrl())));
                        ImageIO.write(bufferImg, "png", byteArrayOut);
                        //anchor主要用于设置图片的属性
                        HSSFClientAnchor anchor = new HSSFClientAnchor(50, 20, 1000, 230,(short) 16, rownum+i+1, (short) 16, rownum+i+1);
                        HSSFClientAnchor anchor = new HSSFClientAnchor(50, 20, 1000, 230,(short) 16, rownum, (short) 16, rownum);
                        anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE);
                        //插入图片
                        patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
@@ -440,9 +484,10 @@
            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;
@@ -460,6 +505,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);
    }
    //查询所有产品中没有与指定散装件关联的子件
    public Response getAllSubWithOutMaterial(int materialId) {
@@ -492,6 +540,20 @@
    //根据产品id查询子件及其关联的物料信息
    public Response getBomAndMaterial(int productId,int version) {
        List<ProductBom> list=mapper.getBomAndMaterial(productId,version);
        if(list!=null&&list.size()>0){
            list.stream().forEach(bom -> {
                if(bom.getDwgUrl()!=null){
                    QueryWrapper wrapper=new QueryWrapper();
                    wrapper.eq("material_id",bom.getMaterialId());
                    wrapper.eq("attach_name",bom.getDwgUrl().substring(bom.getDwgUrl().lastIndexOf("\\")+1));
                    wrapper.last("limit 1");
                    AttachLock attachLock=attachLockMapper.selectOne(wrapper);
                    if(attachLock!=null){
                        bom.setDwgUrl("");
                    }
                }
            });
        }
        return new Response().setII(1,list.size()>0,list,"返回物料信息及关联物料");
    }
@@ -499,17 +561,17 @@
    private ProductBom copyMaterialToBom(Material m) {
        ProductBom bom=new ProductBom();
        bom.setType(m.getType());
        bom.setCategory(m.getCategory());
        //bom.setCategory(m.getCategory());
        bom.setSubCode(m.getSubCode());
        bom.setSubName(m.getSubName());
        bom.setSubModel(m.getSubModel());
        bom.setUnit(m.getUnit());
        //bom.setQuantity(m.getQuantity());
        bom.setProducer(m.getProducer());
        bom.setMaterial(m.getMaterial());
        bom.setThickness(m.getThickness());
        bom.setSurfaceDetail(m.getSurfaceDetail());
        bom.setNotes(m.getNotes());
        //bom.setProducer(m.getProducer());
        //bom.setMaterial(m.getMaterial());
        //bom.setThickness(m.getThickness());
        //bom.setSurfaceDetail(m.getSurfaceDetail());
        //bom.setNotes(m.getNotes());
        bom.setPictureUrl(m.getPictureUrl());
        bom.setDwgUrl(m.getDwgUrl());
        bom.setFileUrl(m.getFileUrl());
@@ -538,7 +600,12 @@
        return mapper.getListByCodeAndModelList2(bomList);
    }
    public ProductBom getEnabledStandardBomListByParentCode(String parentCode) {
        return mapper.getEnabledStandardBomListByParentCode(parentCode);
    public List<ProductBom> getEnabledBomListByParentCodeAndCustomCode(String parentCode, String customCode) {
        return mapper.getEnabledBomListByParentCodeAndCustomCode(parentCode,customCode);
    }
    public void updateMaterialField2Bom() {
        List<ProductBom> bomList = mapper.selectListWithMaterialField();
        mapper.updateMaterialField2BomBatch(bomList);
    }
}