| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.whyc.constant.UserOperation; |
| | | import com.whyc.dto.FileDirPath; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.dto.ZipUtils; |
| | | import com.whyc.mapper.ProductBomHistoryMapper; |
| | | import com.whyc.pojo.DocUser; |
| | | import com.whyc.pojo.ProductBom; |
| | | import com.whyc.pojo.ProductBomHistory; |
| | | 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.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.*; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | public class ProductBomHistoryService { |
| | | @Autowired(required = false) |
| | | private ProductBomHistoryMapper mapper; |
| | | |
| | | @Autowired |
| | | private DocLogService logService; |
| | | |
| | | @Autowired |
| | | private ProductBomService bomService; |
| | | //根据子件名称和母料型号查询历史版本记录 |
| | | public Response getBomHistoryByPModelAndSName(String pmodel, String sname) { |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | |
| | | List<ProductBomHistory> list=mapper.selectList(wrapper); |
| | | return new Response().setII(1,list.size()>0?true:false,list,""); |
| | | } |
| | | //下载指定版本的产品 |
| | | public void downloaByVersion(HttpServletRequest req, HttpServletResponse resp, String parentModel, int version) { |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.eq("parent_model",parentModel); |
| | | wrapper.le("s_version",version); |
| | | wrapper.ge("e_version",version); |
| | | List<ProductBomHistory> list=mapper.selectList(wrapper); |
| | | HSSFWorkbook wb = new HSSFWorkbook(); |
| | | //字体格式-加粗 |
| | | HSSFCellStyle cellStyle = wb.createCellStyle(); |
| | | HSSFFont font = wb.createFont(); |
| | | font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); |
| | | cellStyle.setFont(font); |
| | | //生成excel并将dwg文件放在同一报下压缩 |
| | | creatBomHsitoryExcel(req,resp,list,wb,version); |
| | | //记录日志 |
| | | DocUser docUser= ActionUtil.getUser(); |
| | | String operationDetail="具体产品母料型号为:"+parentModel; |
| | | String opreationMsg="执行了最新版产品下载操作"; |
| | | String terminalIp=req.getRemoteAddr(); |
| | | logService.recordOperationLog(docUser.getId(),docUser.getName(), UserOperation.TYPE_DOWNLOAD.getType(),new Date(),terminalIp,opreationMsg,operationDetail); |
| | | } |
| | | //根据产品信息创建excel表格并存放在指定目录 |
| | | public void creatBomHsitoryExcel(HttpServletRequest req, HttpServletResponse resp,List<ProductBomHistory> list, HSSFWorkbook wb,int version){ |
| | | String fileDirName = FileDirPath.getFileDirName(); |
| | | String rootFace=""; |
| | | String pictureName=""; |
| | | String excelName=""; |
| | | //创建单个sheet |
| | | HSSFSheet sheet = wb.createSheet("bom_"+version+"信息"); |
| | | sheet.setColumnWidth(16,6000); |
| | | sheet.setDefaultRowHeight((short)(1000)); |
| | | //图片元素 |
| | | 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("图片"); |
| | | //将选中的文件存入指定目录下打包下载 |
| | | if(list!=null&&list.size()>0){ |
| | | for (int i=0;i<list.size();i++) { |
| | | ProductBomHistory bomHistory= (ProductBomHistory) list.get(i); |
| | | String dwgUrl=bomHistory.getDwgUrl(); |
| | | if(i==0){ |
| | | excelName=bomHistory.getParentCode()+"_"+version; |
| | | rootFace=fileDirName+ File.separator+excelName; |
| | | File destfile = new File(rootFace); |
| | | if(!destfile.exists()) { |
| | | destfile.mkdir(); |
| | | } |
| | | } |
| | | if((dwgUrl!=null)&&(!dwgUrl.isEmpty())){ |
| | | pictureName+=dwgUrl.substring(dwgUrl.lastIndexOf("\\")+1)+","; |
| | | 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(bomHistory.getParentCode()); |
| | | sheet.getRow(rownum+i+1).createCell(3).setCellValue(bomHistory.getParentName()); |
| | | sheet.getRow(rownum+i+1).createCell(4).setCellValue(bomHistory.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()); |
| | | if((bomHistory.getPictureUrl()!=null)&&(!bomHistory.getPictureUrl().isEmpty())){ |
| | | ByteArrayOutputStream byteArrayOut = null; |
| | | try { |
| | | byteArrayOut = new ByteArrayOutputStream(); |
| | | BufferedImage bufferImg = ImageIO.read(new FileInputStream(new File(fileDirName+File.separator+bomHistory.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); |
| | | anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE); |
| | | //插入图片 |
| | | patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | }finally { |
| | | if (byteArrayOut != null) { |
| | | try { |
| | | byteArrayOut.close(); |
| | | } catch (IOException e) { |
| | | System.out.println("关闭ByteArrayOutputStream失败"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | FileOutputStream fileOut =null; |
| | | try { |
| | | fileOut = new FileOutputStream(rootFace+File.separator+excelName+".xls"); |
| | | // 写入excel文件 |
| | | wb.write(fileOut); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if(fileOut != null){ |
| | | try { |
| | | fileOut.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | 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") |
| | | + ".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(); |
| | | ZipUtils.delDir(rootFace); |
| | | } catch (FileNotFoundException | UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |