whyclxw
3 天以前 12154b62b42df29173cdc54d7fd35d02d9a6422b
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;
@@ -44,6 +47,9 @@
    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired(required = false)
    private AttachLockMapper attachLockMapper;
    @Autowired
    private DocLogService logService;
    //图纸分类检索
@@ -51,7 +57,7 @@
        PageHelper.startPage(pageCurr,pageSize);
        List list=mapper.searchCadDrawer(productBom);
        PageInfo pageInfo=new PageInfo(list);
        return  new Response().setII(1,list.size()>0?true:false,pageInfo,"数据返回");
        return  new Response().setII(1,list.size()>0,pageInfo,"数据返回");
    }
   /* //图纸文件下载
    public void downloadCadDrawer(HttpServletRequest req, HttpServletResponse resp, List<ProductBom> boms) {
@@ -77,7 +83,7 @@
        wrapper.eq("sub_code",scode);
        wrapper.last("limit 1");
        ProductBom productBom=mapper.selectOne(wrapper);
        return new Response().setII(1,productBom!=null?true:false,productBom,"返回数据");
        return new Response().setII(1,productBom!=null,productBom,"返回数据");
    }
    /**获取产品的信息(不包含子料)*/
@@ -118,7 +124,7 @@
            newBom.setThickness(newBomHis.getThickness());
            newBom.setType(newBomHis.getType());
            newBom.setUnit(newBomHis.getUnit());
            newBom.setUpUserId(newBomHis.getUpUserId());
            //newBom.setUpUserId(newBomHis.getUpUserId());
            //newBom.setVersion(newBomHis.getEVersion());
            newBomList.add(newBom);
@@ -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);
    }
@@ -207,10 +242,11 @@
        }
        List list=mapper.selectList(wrapper);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list!=null?true:false,pageInfo,"返回数据");
        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();
@@ -223,6 +259,18 @@
        //处理存在关联关系的物料
        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()) {
@@ -240,26 +288,25 @@
            withOutDwg+=File.separator+"standard"+File.separator+version;
        }
        //生成excel并将dwg文件放在同一报下压缩
        creatBomExcel(req,resp,product,endList,wb,"bom",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 type,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="";
        String[] titleNames=new String[]{"序列","母物料编码","母物料名称","母物料型号","类别","子件编码","子件名称","子件型号"
                                        ,"基本单位","子件数量","生产商","封装类型/材质","元件编号/料厚","表面处理/物料详情","备注","图片"};
        //字体格式-加粗
        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("产品信息");
@@ -275,29 +322,17 @@
        //图片元素
        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("图片");
        HSSFRow row=sheet.createRow(rownum);
        HSSFCell cell=row.createCell(0);
        if(titleNames.length>0){
            for (int i=0;i<titleNames.length;i++) {
                cell=row.createCell(i+1);
                cell.setCellValue(titleNames[i]);
            }
        }
        //构建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()) {
@@ -305,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++) {
@@ -315,23 +350,93 @@
                    File sourceFile=new File(fileDirName+ File.separator+dwgUrl);
                    copyFile(sourceFile,rootFace);
                }
                Row row=sheet.createRow(rownum+i+1);
                row.setRowStyle(cellStyle);
                sheet.getRow(rownum+i+1).createCell(1).setCellValue(i+1);
                sheet.getRow(rownum+i+1).createCell(2).setCellValue(product.getParentCode());
                sheet.getRow(rownum+i+1).createCell(3).setCellValue(product.getParentName());
                sheet.getRow(rownum+i+1).createCell(4).setCellValue(product.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+1;
                row=sheet.createRow(rownum);
                if(bom.getConnFlag()==1){
                    cell=row.createCell(0);
                    cell.setCellValue("替换件");
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(1);
                    cell.setCellValue(i+1);
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(2);
                    cell.setCellValue(product.getParentCode());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(3);
                    cell.setCellValue(product.getParentName());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(4);
                    cell.setCellValue(product.getParentModel());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(5);
                    cell.setCellValue(bom.getCategory());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(6);
                    cell.setCellValue(bom.getSubCode());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(7);
                    cell.setCellValue(bom.getSubName());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(8);
                    cell.setCellValue(bom.getSubModel());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(9);
                    cell.setCellValue(bom.getUnit()==null?"":bom.getUnit());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(10);
                    cell.setCellValue(bom.getQuantity()==null?"": bom.getQuantity().toString());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(11);
                    cell.setCellValue(bom.getProducer()==null?"":bom.getProducer());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(12);
                    cell.setCellValue(bom.getMaterial());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(12);
                    cell.setCellValue(bom.getThickness());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(13);
                    cell.setCellValue(bom.getThickness());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(14);
                    cell.setCellValue(bom.getSurfaceDetail());
                    cell.setCellStyle(cellStyle);
                    cell=row.createCell(15);
                    cell.setCellValue(bom.getNotes());
                    cell.setCellStyle(cellStyle);
                }else{
                    row.createCell(1).setCellValue(i+1);
                    row.createCell(2).setCellValue(product.getParentCode());
                    row.createCell(3).setCellValue(product.getParentName());
                    row.createCell(4).setCellValue(product.getParentModel());
                    row.createCell(5).setCellValue(bom.getCategory());
                    row.createCell(6).setCellValue(bom.getSubCode());
                    row.createCell(7).setCellValue(bom.getSubName());
                    row.createCell(8).setCellValue(bom.getSubModel());
                    row.createCell(9).setCellValue(bom.getUnit()==null?"":bom.getUnit());
                    row.createCell(10).setCellValue(bom.getQuantity()==null?"": bom.getQuantity().toString());
                    row.createCell(11).setCellValue(bom.getProducer()==null?"":bom.getProducer());
                    row.createCell(12).setCellValue(bom.getMaterial());
                    row.createCell(13).setCellValue(bom.getThickness());
                    row.createCell(14).setCellValue(bom.getSurfaceDetail());
                    row.createCell(15).setCellValue(bom.getNotes());
                }
                if((bom.getPictureUrl()!=null)&&(!bom.getPictureUrl().isEmpty())){
                    ByteArrayOutputStream byteArrayOut = null;
                    try {
@@ -339,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));
@@ -379,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;
@@ -399,11 +505,14 @@
        } 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) {
        List<ProductBom> list=mapper.getAllSubWithOutMaterial(materialId);
        return new Response().setII(1,list.size()>0?true:false,list,"返回数据");
        return new Response().setII(1,list.size()>0,list,"返回数据");
    }
   //根据母料型号查询子件信息及有最新版本关联的散装件信息
     /*public Response getSubByMaterialProduct(String parentModel) {
@@ -426,33 +535,48 @@
                    }
            );
        }
        return new Response().setII(1,list.size()>0?true:false,list,"返回数据");
        return new Response().setII(1,list.size()>0,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,"返回物料信息及关联物料");
        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,"返回物料信息及关联物料");
    }
    //将物料存放为一个bom为下载做准备
    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.setQuantity(m.getQuantity());
        //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());
        bom.setCreateDate(m.getCreateDate());
        bom.setConnFlag(1);
        return  bom;
    }
@@ -471,4 +595,17 @@
        update.eq("product_id",productId);
        mapper.delete(update);
    }
    public List<ProductBom> getListByCodeAndModelList2(List<ProductBom> bomList) {
        return mapper.getListByCodeAndModelList2(bomList);
    }
    public List<ProductBom> getEnabledBomListByParentCodeAndCustomCode(String parentCode, String customCode) {
        return mapper.getEnabledBomListByParentCodeAndCustomCode(parentCode,customCode);
    }
    public void updateMaterialField2Bom() {
        List<ProductBom> bomList = mapper.selectListWithMaterialField();
        mapper.updateMaterialField2BomBatch(bomList);
    }
}