lxw
2022-08-19 209e2fceca9e408bf7863f6de98d2a036caec66d
src/main/java/com/whyc/service/ProductBomService.java
@@ -9,14 +9,12 @@
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Response;
import com.whyc.dto.ZipUtils;
import com.whyc.mapper.MaterialMapper;
import com.whyc.mapper.ProductBomMapper;
import com.whyc.pojo.DocUser;
import com.whyc.pojo.ProductBom;
import com.whyc.pojo.ProductBomApproving;
import com.whyc.pojo.ProductBomHistory;
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.Cell;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,8 +26,6 @@
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
@@ -37,8 +33,15 @@
@Service
public class ProductBomService {
    @Autowired(required = false)
    private ProductBomMapper mapper;
    @Autowired(required = false)
    private MaterialMapper cponentMapper;
    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired
    private DocLogService logService;
@@ -49,52 +52,24 @@
        PageInfo pageInfo=new PageInfo(list);
        return  new Response().setII(1,list.size()>0?true:false,pageInfo,"数据返回");
    }
    //图纸文件下载
    public void downloadCadDrawer(HttpServletRequest req, HttpServletResponse resp, ArrayList<String> pictureUrls) {
        String fileDirName = FileDirPath.getFileDirName();
        String rootFace=fileDirName+ File.separator+"downLoad";
        String pictureName="";
        //将选中的文件存入指定目录下打包下载
        if(pictureUrls!=null&&pictureUrls.size()>0){
            for (String picUrl:pictureUrls) {
                pictureName+=picUrl.substring(picUrl.lastIndexOf("\\")+1)+",";
                File sourceFile=new File(fileDirName+ File.separator+picUrl);
                copyFile(sourceFile,rootFace);
            }
        }
        String timeStr= ActionUtil.sdfwithFTP.format(new Date());
        try {
            File file=new File(rootFace+".zip");
            FileOutputStream forootFace = new FileOutputStream(file);
            ZipUtils.toZip(rootFace, forootFace,true);
            // 转码防止乱码
            resp.addHeader("Content-Disposition", "attachment;filename="
                    + new String(timeStr.getBytes("UTF-8"), "ISO8859-1")
                    + ".zip");
            OutputStream out = resp.getOutputStream();
            FileInputStream in = new FileInputStream(rootFace+".zip");
            int len=0;
            byte[] buffer =new byte[1024];
            //7. 将缓冲区中的数据输出
            while ((len=in.read(buffer))>0){
                out.write(buffer,0,len);
            }
            in.close();
            out.close();
            file.delete();
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
   /* //图纸文件下载
    public void downloadCadDrawer(HttpServletRequest req, HttpServletResponse resp, List<ProductBom> boms) {
        HSSFWorkbook wb = new HSSFWorkbook();
        //字体格式-加粗
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        //生成excel并将dwg文件放在同一报下压缩
        creatBomExcel(req,resp,boms,wb,"pic");
        //记录日志
        DocUser docUser= ActionUtil.getUser();
        String operationDetail="具体图纸为:"+pictureName.substring(0,pictureName.lastIndexOf(","));
        String operationDetail="具体图纸为数量:"+boms.size();
        String opreationMsg="执行了文件打包下载操作";
        String terminalIp=req.getRemoteAddr();
        logService.recordOperationLog(docUser.getId(),docUser.getName(), UserOperation.TYPE_DOWNLOAD.getType(),new Date(),terminalIp,opreationMsg,operationDetail);
    }
    }*/
    //根据子件code获取最终的信息
    public Response getBomBySubcode(String scode) {
        QueryWrapper wrapper=new QueryWrapper();
@@ -115,7 +90,8 @@
    @Transactional
    public void updateNewBom(List<ProductBomHistory> newBomHistoryList) {
        UpdateWrapper<ProductBom> update = Wrappers.update();
        update.eq("parent_model",newBomHistoryList.get(0).getParentModel());
        //lxw注释
        //update.eq("parent_model",newBomHistoryList.get(0).getParentModel());
        mapper.delete(update);
        List<ProductBom> newBomList = new LinkedList<>();
@@ -127,10 +103,10 @@
            newBom.setFileUrl(newBomHis.getFileUrl());
            newBom.setMaterial(newBomHis.getMaterial());
            newBom.setNotes(newBomHis.getNotes());
            newBom.setParentCode(newBomHis.getParentCode());
            newBom.setParentModel(newBomHis.getParentModel());
            newBom.setParentName(newBomHis.getParentName());
            newBom.setParentVersion(newBomHis.getParentVersion());
            //newBom.setParentCode(newBomHis.getParentCode());
            //newBom.setParentModel(newBomHis.getParentModel());
            //newBom.setParentName(newBomHis.getParentName());
            //newBom.setParentVersion(newBomHis.getParentVersion());
            newBom.setPictureUrl(newBomHis.getPictureUrl());
            newBom.setProducer(newBomHis.getProducer());
            newBom.setQuantity(newBomHis.getQuantity());
@@ -142,7 +118,7 @@
            newBom.setType(newBomHis.getType());
            newBom.setUnit(newBomHis.getUnit());
            newBom.setUpUserId(newBomHis.getUpUserId());
            newBom.setVersion(newBomHis.getEVersion());
            //newBom.setVersion(newBomHis.getEVersion());
            newBomList.add(newBom);
        });
@@ -155,7 +131,11 @@
        File destfile = new File(dest);
        if(!destfile.exists()) {
            destfile.mkdir();
        }
        }/*else{
            if(i==0){
                ZipUtils.delDir(dest);
            }
        }*/
        //source是文件,则用字节输入输出流复制文件
        try {
            if(source.isFile()){
@@ -190,36 +170,64 @@
    }
    /**
     * 获取所有产品信息*/
    public Response getAllBom() {
    /*public Response getAllBom(String parentCode,String parentName,String parentModel, int pageCurr,int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.select("distinct parent_code","parent_name","parent_model","version").orderByAsc("id");
        if(parentCode!=null&&!parentCode.isEmpty()){
            wrapper.like("parent_code",parentCode);
        }
        if(parentName!=null&&!parentName.isEmpty()){
            wrapper.like("parent_name",parentName);
        }
        if(parentModel!=null&&!parentModel.isEmpty()){
            wrapper.like("parent_model",parentModel);
        }
        List list=mapper.selectList(wrapper);
        return new Response().setII(1,list!=null?true:false,list,"返回数据");
    }
    //产品打包下载
    public  Response downloadBom(HttpServletRequest req, HttpServletResponse resp, String parentModel) {
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list!=null?true:false,pageInfo,"返回数据");
    }*/
    //产品下载(产品id和版本)
    public  void downloadProduct(HttpServletRequest req, HttpServletResponse resp, int productId , int version) {
        HSSFWorkbook wb = new HSSFWorkbook();
        //字体格式-加粗
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        //读取产品信息
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_model",parentModel);
        List<ProductBom> list=mapper.selectList(wrapper);
        wrapper.eq("id",productId);
        wrapper.last("limit 1");
        Product product=productMapper.selectOne(wrapper);
        //读取产品的子件和关联的物料信息
        List<ProductBom> list=mapper.getBomAndMaterial(productId,version);
        //处理
        //生成excel并将dwg文件放在同一报下压缩
        creatBomExcel(req,resp,list,wb);
        return  new Response().setII(1,true,"","");
        creatBomExcel(req,resp,product,list,wb,"bom");
        //记录日志
        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);
    }
    //根据产品信息创建excel表格并存放在指定目录
    public void creatBomExcel(HttpServletRequest req, HttpServletResponse resp,List<ProductBom> list, HSSFWorkbook wb){
    public void creatBomExcel(HttpServletRequest req, HttpServletResponse resp,Product product,List list, HSSFWorkbook wb,String type){
        String fileDirName = FileDirPath.getFileDirName();
        String rootFace=fileDirName+ File.separator+"downLoad";
        String pictureName="";
        String rootFace="";
        String excelName="";
        //创建单个sheet
        HSSFSheet sheet = wb.createSheet("bom信息");
        HSSFSheet sheet = wb.createSheet("产品信息");
        sheet.setColumnWidth(1,5000);
        sheet.setColumnWidth(2,5000);
        sheet.setColumnWidth(3,5000);
        sheet.setColumnWidth(4,5000);
        sheet.setColumnWidth(6,5000);
        sheet.setColumnWidth(7,5000);
        sheet.setColumnWidth(8,5000);
        sheet.setColumnWidth(16,6000);
        sheet.setDefaultRowHeight((short)(1000));
        //图片元素
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        int rownum = 1;
@@ -240,34 +248,43 @@
        sheet.getRow(rownum).createCell(14).setCellValue("表面处理/物料详情");
        sheet.getRow(rownum).createCell(15).setCellValue("备注");
        sheet.getRow(rownum).createCell(16).setCellValue("图片");
        //构建excel名称和创建文件夹
        excelName=product.getParentCode()+"_"+product.getParentModel();
        String timeStr= ActionUtil.sdfwithFTP.format(new Date());
        if(type.equals("pic")){
            excelName=timeStr;
        }
        rootFace=fileDirName+File.separator+excelName;
        File destfile = new File(rootFace);
        if(!destfile.exists()) {
            destfile.mkdir();
        }
        //将选中的文件存入指定目录下打包下载
        if(list!=null&&list.size()>0){
            for (int i=0;i<list.size();i++) {
                ProductBom bom=list.get(i);
                ProductBom bom= (ProductBom) list.get(i);
                String dwgUrl=bom.getDwgUrl();
                excelName=bom.getParentCode();
                if((dwgUrl!=null)&&(!dwgUrl.isEmpty())){
                    pictureName+=dwgUrl.substring(dwgUrl.lastIndexOf("\\")+1)+",";
                    File sourceFile=new File(fileDirName+ File.separator+dwgUrl);
                    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(bom.getParentCode());
                sheet.getRow(rownum+i+1).createCell(3).setCellValue(bom.getParentName());
                sheet.getRow(rownum+i+1).createCell(4).setCellValue(bom.getParentModel());
                sheet.getRow(rownum+i+1).createCell(5).setCellValue(bom.getCategory());
                sheet.getRow(rownum+i+1).createCell(6).setCellValue(bom.getSubCode());
                sheet.getRow(rownum+i+1).createCell(7).setCellValue(bom.getSubName());
                sheet.getRow(rownum+i+1).createCell(8).setCellValue(bom.getSubModel());
                sheet.getRow(rownum+i+1).createCell(9).setCellValue(bom.getUnit()==null?"":bom.getUnit());
                sheet.getRow(rownum+i+1).createCell(10).setCellValue(bom.getQuantity());
                sheet.getRow(rownum+i+1).createCell(11).setCellValue(bom.getProducer()==null?"":bom.getProducer());
                sheet.getRow(rownum+i+1).createCell(12).setCellValue(bom.getMaterial());
                sheet.getRow(rownum+i+1).createCell(13).setCellValue(bom.getThickness());
                sheet.getRow(rownum+i+1).createCell(14).setCellValue(bom.getSurfaceDetail());
                sheet.getRow(rownum+i+1).createCell(15).setCellValue(bom.getNotes());
                rownum=rownum+i+1;
                Row row=sheet.createRow(rownum);
                sheet.getRow(rownum).createCell(1).setCellValue(i+1);
                sheet.getRow(rownum).createCell(2).setCellValue(product.getParentCode());
                sheet.getRow(rownum).createCell(3).setCellValue(product.getParentName());
                sheet.getRow(rownum).createCell(4).setCellValue(product.getParentModel());
                sheet.getRow(rownum).createCell(5).setCellValue(bom.getCategory());
                sheet.getRow(rownum).createCell(6).setCellValue(bom.getSubCode());
                sheet.getRow(rownum).createCell(7).setCellValue(bom.getSubName());
                sheet.getRow(rownum).createCell(8).setCellValue(bom.getSubModel());
                sheet.getRow(rownum).createCell(9).setCellValue(bom.getUnit()==null?"":bom.getUnit());
                sheet.getRow(rownum).createCell(10).setCellValue(bom.getQuantity());
                sheet.getRow(rownum).createCell(11).setCellValue(bom.getProducer()==null?"":bom.getProducer());
                sheet.getRow(rownum).createCell(12).setCellValue(bom.getMaterial());
                sheet.getRow(rownum).createCell(13).setCellValue(bom.getThickness());
                sheet.getRow(rownum).createCell(14).setCellValue(bom.getSurfaceDetail());
                sheet.getRow(rownum).createCell(15).setCellValue(bom.getNotes());
                if((bom.getPictureUrl()!=null)&&(!bom.getPictureUrl().isEmpty())){
                    ByteArrayOutputStream byteArrayOut = null;
                    try {
@@ -275,7 +292,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));
@@ -309,10 +326,11 @@
                }
            }
        }
        try {
       try {
            File file=new File(rootFace+".zip");
            FileOutputStream forootFace = new FileOutputStream(file);
            ZipUtils.toZip(rootFace, forootFace,true);
            // 转码防止乱码
            resp.addHeader("Content-Disposition", "attachment;filename="
                    + new String(excelName.getBytes("UTF-8"), "ISO8859-1")
@@ -328,11 +346,44 @@
            in.close();
            out.close();
            file.delete();
            ZipUtils.delDir(rootFace);
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //查询所有产品中没有与指定散装件关联的子件
    public Response getAllSubWithOutMaterial(int MaterialId) {
        List<ProductBom> list=mapper.getAllSubWithOutMaterial(MaterialId);
        return new Response().setII(1,list.size()>0?true:false,list,"返回数据");
    }
    /*//根据母料型号查询子件信息及有最新版本关联的散装件信息
    public Response getSubByMaterialProduct(String parentModel) {
        List<ProductBom> list=mapper.getSubByMaterialProduct(parentModel);
        //查询出存在替换关系得散装件
        List<ProductBom> replaceBoms=cponentMapper.getReplaceMaterial(parentModel);
        if(replaceBoms!=null&&replaceBoms.size()>0){
            replaceBoms.stream().forEach(replaceBom->{
                        //如果存在替换关系,查询出原子件的关联关系(将原子件名称放在oldSubName中)
                        if(replaceBom!=null&&!replaceBom.getSubName().isEmpty()) {
                            //replaceBom表示为替换件
                            replaceBom.setReplaceStatus(1);
                            if (replaceBom.getOldSubName() != null && !replaceBom.getOldSubName().isEmpty()) {
                                //查询出存在的旧的关联子件,还要与原BOM版本一致
                                List<Material> oldCponent = mapper.getOldMaterial(replaceBom.getOldSubName());
                                replaceBom.setMaterials(oldCponent);
                            }
                        }
                        list.add(replaceBom);
                    }
            );
        }
        return new Response().setII(1,list.size()>0?true:false,list,"返回数据");
    }*/
    //根据产品id查询子件及其关联的物料信息
    public Response getBomAndMaterial(int productId,int version) {
        List<ProductBom> list=mapper.getBomAndMaterial(productId,version);
        return new Response().setII(1,list.size()>0?true:false,list,"返回物料信息及关联物料");
    }
}