whyclxw
2025-06-16 f52250bf50ff68865a35cdc5c69f6fade2d51156
src/main/java/com/whyc/service/BomAcceptanceService.java
@@ -9,15 +9,26 @@
import com.whyc.pojo.BOMFeedback;
import com.whyc.pojo.BomAcceptance;
import com.whyc.pojo.DefectiveProducts;
import com.whyc.pojo.ProductHistory;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
import com.whyc.util.FileUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
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.List;
@@ -163,12 +174,42 @@
    public Response updateBomAcceptance(BomAcceptance bomAcceptance) {
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("num",bomAcceptance.getNum());
        mapper.update(bomAcceptance,wrapper);
        if(bomAcceptance.getDirectName()!=null){
            wrapper.set("direct_name",bomAcceptance.getDirectName());
        }
        if(bomAcceptance.getBomSeries()!=null){
            wrapper.set("bom_series",bomAcceptance.getBomSeries());
        }
        if(bomAcceptance.getBomModel()!=null){
            wrapper.set("bom_model",bomAcceptance.getBomModel());
        }
        if(bomAcceptance.getDevParam()!=null){
            wrapper.set("dev_param",bomAcceptance.getDevParam());
        }
        if(bomAcceptance.getStandParam()!=null){
            wrapper.set("stand_param",bomAcceptance.getStandParam());
        }
        if(bomAcceptance.getBomSize()!=null){
            wrapper.set("bom_size",bomAcceptance.getBomSize());
        }
        if(bomAcceptance.getDevWeight()!=null){
            wrapper.set("dev_weight",bomAcceptance.getDevWeight());
        }
        if(bomAcceptance.getDevWeightAll()!=null){
            wrapper.set("dev_weight_all",bomAcceptance.getDevWeightAll());
        }
        if(bomAcceptance.getSparePartmodel()!=null){
            wrapper.set("spare_partmodel",bomAcceptance.getSparePartmodel());
        }
        if(bomAcceptance.getBomVersion()!=null){
            wrapper.set("bom_version",bomAcceptance.getBomVersion());
        }
        mapper.update(null,wrapper);
        return new Response().set(1,true,"编辑成功");
    }
   //删除图
    public void delPic(int num, int picNum,String picUrl,String newPicUrl) {
        String rootFile = CommonUtil.getRootFile();//主路径
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("num",num);
        if(picNum==1){
@@ -190,20 +231,41 @@
            wrapper.set("bom_agreement",newPicUrl);
        }
        int flag=mapper.update(null,wrapper);
        if(flag>0){
            rootFile=rootFile.replace("doc_file","");
        if(flag>0&&picUrl.length()>0){
            String rootFile = CommonUtil.getRootFile();//主路径
            picUrl=picUrl.replace("doc_file","");
            //删除图片
            FileUtil.deleteFile(new File(rootFile+picUrl));
        }
    }
    //编辑单个图片上传
    public Response updateDevPic(MultipartFile filePic, int num,int picNum,String picUrl) throws IOException {
    public Response updateDevPic(MultipartFile filePic, int num,int picNum) throws IOException {
        Date date = new Date();
        long time = date.getTime();
        String rootFile = CommonUtil.getRootFile();
        String devDirSuffix = "acceptance_bom" + File.separator + time + File.separator+ "dev" + File.separator;
        String devDir = rootFile + devDirSuffix;
        //后面图
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("num",num);
        wrapper.last("limit 1");
        BomAcceptance bomAcceptance=mapper.selectOne(wrapper);
        String picUrl="";
        if(picNum==1){
            picUrl=bomAcceptance.getDevPicleft();
        }
        if(picNum==2){
            picUrl=bomAcceptance.getDevPicright();
        }
        if(picNum==3){
            picUrl=bomAcceptance.getDevPicfront();
        }
        if(picNum==4){
            picUrl=bomAcceptance.getDevPicback();
        }
        if(picNum==6){
            picUrl=bomAcceptance.getBomAgreement();
        }
       //后面图
        if(filePic!=null){
            //存储文件
            String originalFilename = filePic.getOriginalFilename();
@@ -221,9 +283,13 @@
        }
    }
    //编辑多个图片上传
    public Response updatePartPic(List<MultipartFile> multipartFileList, String picUrl) throws IOException {
    public Response updatePartPic(List<MultipartFile> multipartFileList, int num) throws IOException {
        String rootFile = CommonUtil.getRootFile();
        rootFile=rootFile.replace("doc_file","");
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("num",num);
        wrapper.last("limit 1");
        BomAcceptance bomAcceptance=mapper.selectOne(wrapper);
        String picUrl=bomAcceptance.getSparePicpart();
        //配件存储
        if(multipartFileList!=null && multipartFileList.size()!=0){
            for (int i = 0; i < multipartFileList.size(); i++) {
@@ -241,4 +307,272 @@
            return new Response().set(1,false,"图片不存在");
        }
    }
    //导出产品验收信息
    public void downloadBomAcceptance(HttpServletRequest req, HttpServletResponse resp, int num) {
        //读取产品信息
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("num",num);
        wrapper.last("limit 1");
        BomAcceptance bomAcceptance=mapper.selectOne(wrapper);
        //生成excel
        creatBomAcceptanceExcel(resp,bomAcceptance);
    }
    //生成excel
    private void creatBomAcceptanceExcel( HttpServletResponse resp, BomAcceptance bomAcceptance) {
        String rootFile = CommonUtil.getRootFile();
        List<String> sparePicList=new ArrayList<>();
        if(bomAcceptance.getSparePicpart()!=null||!bomAcceptance.getSparePicpart().isEmpty()){
            String filePath=rootFile+bomAcceptance.getSparePicpart();
            //获取文件夹下所有的图片名
            sparePicList=FileUtil.getFileNameWithOutDirectory(filePath);
        }
        String devRoot=rootFile.replace("doc_file", "");
        List<String> devPicList=new ArrayList<>();
        devPicList.add(bomAcceptance.getDevPicleft());
        devPicList.add(bomAcceptance.getDevPicfront());
        devPicList.add(bomAcceptance.getDevPicright());
        devPicList.add(bomAcceptance.getDevPicback());
        String fileName="产品验收信息记录";
        //创建单个sheet
        HSSFWorkbook wb = new HSSFWorkbook();
        //字体格式-加粗
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        cellStyle.setBorderBottom(BorderStyle.THICK);
        cellStyle.setBorderLeft(BorderStyle.THICK);
        cellStyle.setBorderRight(BorderStyle.THICK);
        cellStyle.setBorderTop(BorderStyle.THICK);
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        HSSFSheet sheet = wb.createSheet("产品验收信息记录");
        sheet.setColumnWidth(0,10000);
        if(sparePicList!=null&&sparePicList.size()>0){
             int length = sparePicList.size();
             length=length>5?length:4;
             for (int i = 1; i < length+1; i++){
                 sheet.setColumnWidth(i,5000);
             }
        }
        sheet.setDefaultRowHeight((short)(1000));
        //图片元素
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        int rowNum=0;
        HSSFRow row=sheet.createRow(rowNum);
        HSSFCell cell=row.createCell(0);
        cell.setCellValue("产品验收信息记录表");
        cell.setCellStyle(cellStyle);
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,1));
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("研发负责人");
        cell.setCellStyle(cellStyle);
        cell=row.createCell(1);
        cell.setCellValue(bomAcceptance.getDirectName());
        cell.setCellStyle(cellStyle);
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("产品系列");
        cell.setCellStyle(cellStyle);
        cell=row.createCell(1);
        cell.setCellValue(bomAcceptance.getBomSeries());
        cell.setCellStyle(cellStyle);
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("产品型号");
        cell.setCellStyle(cellStyle);
        cell=row.createCell(1);
        cell.setCellValue(bomAcceptance.getBomModel());
        cell.setCellStyle(cellStyle);
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("产品版本号");
        cell.setCellStyle(cellStyle);
        cell=row.createCell(1);
        cell.setCellValue(bomAcceptance.getBomVersion());
        cell.setCellStyle(cellStyle);
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("技术指标");
        cell.setCellStyle(cellStyle);
        cell=row.createCell(1);
        cell.setCellValue(bomAcceptance.getDevParam());
        cell.setCellStyle(cellStyle);
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("技术指标");
        cell.setCellStyle(cellStyle);
        cell=row.createCell(1);
        cell.setCellValue(bomAcceptance.getStandParam());
        cell.setCellStyle(cellStyle);
        sheet.addMergedRegion(new CellRangeAddress(4,5,0,0));
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("长x宽x高(mm)");
        cell.setCellStyle(cellStyle);
        cell=row.createCell(1);
        cell.setCellValue(bomAcceptance.getBomSize());
        cell.setCellStyle(cellStyle);
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("主机净重(KG)");
        cell.setCellStyle(cellStyle);
        cell=row.createCell(1);
        cell.setCellValue(bomAcceptance.getDevWeight());
        cell.setCellStyle(cellStyle);
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("整机全重(含包装箱,配件)(KG)");
        cell.setCellStyle(cellStyle);
        cell=row.createCell(1);
        cell.setCellValue(bomAcceptance.getDevWeightAll());
        cell.setCellStyle(cellStyle);
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("主机照片(左侧图,正面图,右侧图,后面图)");
        cell.setCellStyle(cellStyle);
        if(devPicList!=null&&devPicList.size()>0){
            ByteArrayOutputStream byteArrayOut = null;
            for(int i=0;i<devPicList.size();i++){
                String picUrl=devPicList.get(i);
                if(picUrl.length()==0){
                    continue;
                }
                try {
                    byteArrayOut = new ByteArrayOutputStream();
                    String path=devRoot+File.separator+picUrl;
                    BufferedImage bufferImg = ImageIO.read(new FileInputStream(new File(path)));
                    ImageIO.write(bufferImg, "png", byteArrayOut);
                    //anchor主要用于设置图片的属性
                    HSSFClientAnchor anchor = new HSSFClientAnchor(50, 20, 1000, 230,(short) (i+1), rowNum, (short) (i+1), rowNum);
                    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失败");
                        }
                    }
                }
            }
        }
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("配件照片");
        cell.setCellStyle(cellStyle);
        if(sparePicList!=null&&sparePicList.size()>0){
            ByteArrayOutputStream byteArrayOut = null;
            for(int i=0;i<sparePicList.size();i++){
                String picUrl=sparePicList.get(i);
                try {
                    byteArrayOut = new ByteArrayOutputStream();
                    String path=rootFile+File.separator+bomAcceptance.getSparePicpart()+File.separator+picUrl;
                    BufferedImage bufferImg = ImageIO.read(new FileInputStream(new File(path)));
                    ImageIO.write(bufferImg, "png", byteArrayOut);
                    //anchor主要用于设置图片的属性
                    HSSFClientAnchor anchor = new HSSFClientAnchor(50, 20, 1000, 230,(short) (i+1), rowNum, (short) (i+1), rowNum);
                    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失败");
                        }
                    }
                }
            }
        }
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("配件型号规格");
        cell.setCellStyle(cellStyle);
        cell=row.createCell(1);
        cell.setCellValue(bomAcceptance.getSparePartmodel());
        cell.setCellStyle(cellStyle);
        rowNum++;
        row=sheet.createRow(rowNum);
        cell=row.createCell(0);
        cell.setCellValue("端口协议文本");
        cell.setCellStyle(cellStyle);
        cell=row.createCell(1);
        if(bomAcceptance.getBomAgreement()!=null||!bomAcceptance.getBomAgreement().equals("")){
            String agreement=bomAcceptance.getBomAgreement();
            String agreementName = agreement.substring(agreement.lastIndexOf(".")+1);
            cell.setCellValue(agreementName);
            cell.setCellStyle(cellStyle);
        }
        //将文件存到指定位置
        OutputStream os = null;
        try {
            os = resp.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode (fileName+ ".xls", "utf-8"));
            resp.setCharacterEncoding("UTF-8");
            //不保存缓存信息与response.reset同样效果
            resp.addHeader("Pragma", "no-cache");
            resp.addHeader("Cache-Control", "no-cache");
            wb.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //跳转详情
    public Response getBomAcceptanceByNum(int num) {
        String rootFile = CommonUtil.getRootFile();
        //读取产品信息
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("num",num);
        wrapper.last("limit 1");
        BomAcceptance bomAcceptance=mapper.selectOne(wrapper);
        String filePath=rootFile+bomAcceptance.getSparePicpart();
        bomAcceptance.setSparePicpart("doc_file"+File.separator+bomAcceptance.getSparePicpart());
        //获取文件夹下所有的图片名
        bomAcceptance.setNameList(FileUtil.getFileNameWithOutDirectory(filePath));
        return new Response().set(1,bomAcceptance,"跳转详情");
    }
}