Newer
Older
huludao / src / test / java / com / GisTest.java
//package com;
//
//import cn.hutool.poi.excel.ExcelReader;
//import com.alibaba.fastjson.JSONObject;
//import com.newfiber.api.Application;
//import com.newfiber.api.pc.model.gis.PipelineFlaw;
//import com.newfiber.api.pc.service.gis.PipelineFlawService;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.ArrayList;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Objects;
//import java.util.stream.Collectors;
//import javax.annotation.Resource;
//import javax.imageio.stream.FileImageOutputStream;
//import lombok.Data;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections.CollectionUtils;
//import org.apache.poi.xwpf.usermodel.XWPFDocument;
//import org.apache.poi.xwpf.usermodel.XWPFParagraph;
//import org.apache.poi.xwpf.usermodel.XWPFPicture;
//import org.apache.poi.xwpf.usermodel.XWPFRun;
//import org.apache.poi.xwpf.usermodel.XWPFTable;
//import org.apache.poi.xwpf.usermodel.XWPFTableCell;
//import org.apache.poi.xwpf.usermodel.XWPFTableRow;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//
//@Slf4j
//@RunWith(SpringJUnit4ClassRunner.class)
//@SpringBootTest(classes = {Application.class})
//public class GisTest {
//
//    @Resource
//    private PipelineFlawService pipelineFlawService;
//
//    private Integer imageCount = 1;
//
//    @Test
//    public void importTest(){
//        String fileName = "C:\\Users\\Administrator\\Downloads\\需修复结构性缺陷统计表6.01.xls";
//        List<PipelineFlaw> pipelineFlawList = new ArrayList<>();
//
//        for(ExcelSheet excelSheet : ExcelSheet.values()){
//
//            ExcelReader excelReader = new ExcelReader(fileName, excelSheet.index);
//            List<List<Object>> excelData = excelReader.read(2);
//
//            for(List<Object> excelRowData : excelData){
//                pipelineFlawList.add(PipelineFlaw.build(excelSheet.category, excelRowData));
//            }
//        }
//
//        pipelineFlawService.insertBatch(pipelineFlawList);
//    }
//
//    @Test
//    public void importPicture() throws Exception{
//        String filePath = "C:\\Users\\Administrator\\Downloads\\市政管网严重结构性缺陷清单";
//        String imagePath = "C:\\Users\\Administrator\\Downloads\\images\\";
//        File[] files = new File(filePath).listFiles();
//        if(null == files || files.length == 0){
//            log.error("文件不存在");
//            return;
//        }
//
//        List<WordImage> wordImageList = new ArrayList<>();
//
//        for(File file : files){
//            InputStream inputStream = new FileInputStream(file);
//            XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
//            Iterator<XWPFTable> tablesIterator = xwpfDocument.getTablesIterator();
//            while(tablesIterator.hasNext()){
//                XWPFTable table = tablesIterator.next();
//                XWPFTableRow keyTableRow = table.getRow(0);
//                String key = normizal(keyTableRow.getCell(3).getText()) + "-" + normizal(keyTableRow.getCell(5).getText());
//
//                XWPFTableRow pictureTableRow = table.getRow(7);
//                List<String> pictureName = new ArrayList<>();
//
//                pictureName.add(saveCellPicture(pictureTableRow.getCell(0), imagePath));
//                pictureName.add(saveCellPicture(pictureTableRow.getCell(1), imagePath));
//
//                wordImageList.add(new WordImage(key, pictureName));
//
//            }
//        }
//
//        for(WordImage wordImage : wordImageList){
//            pipelineFlawService.refreshPicture(wordImage.key, String.join("|", wordImage.pictureName));
//            System.out.println(JSONObject.toJSONString(wordImage));
//        }
//    }
//
//    private String saveCellPicture(XWPFTableCell pictureTableCell, String path) {
//        XWPFParagraph xwpfParagraph = pictureTableCell.getParagraphs().get(0);
//
//        String pictureName = null;
//        List<XWPFRun> xwpfRunList = xwpfParagraph.getRuns();
//        if(CollectionUtils.isNotEmpty(xwpfRunList)){
//            XWPFRun xwpfRun = xwpfRunList.get(0);
//            List<XWPFPicture> xwpfPictureList = xwpfRun.getEmbeddedPictures();
//            if(CollectionUtils.isNotEmpty(xwpfPictureList)){
//                XWPFPicture xwpfPicture = xwpfPictureList.get(0);
//                pictureName = String.format("image%s.png", imageCount++);
//                byte2image(xwpfPicture.getPictureData().getData(), path.concat(pictureName));
//            }
//        }
//        return pictureName;
//    }
//
//    public static  void byte2image(byte[] data,String path){
//        if(data.length<3||path.equals("")) return;
//        FileImageOutputStream imageOutput = null;
//        try{
//            imageOutput = new FileImageOutputStream(new File(path));
//            imageOutput.write(data, 0, data.length);
//        } catch(Exception ex) {
//            ex.printStackTrace();
//        }finally {
//            try {
//                if(null != imageOutput){
//                    imageOutput.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    private String normizal(String cellString){
////        if(StringUtil.isEmpty(cellString)){
////            return "";
////        }
////        return cellString.substring(0, cellString.length() - 1);
//        return cellString;
//    }
//
//    @Test
//    public void getTest(){
//        String number = "HS0030726-HS0030804";
//        PipelineFlaw pipelineFlaw = pipelineFlawService.infoByNumber(number);
//        System.out.println(pipelineFlaw.toString());
//    }
//
//    @Data
//    static class WordImage{
//        String key;
//        List<String> pictureName;
//
//        public WordImage(String key, List<String> pictureName) {
//            this.key = key;
//            this.pictureName = pictureName.stream().filter(Objects::nonNull).collect(Collectors.toList());
//        }
//    }
//
//    enum ExcelSheet{
//        /**
//         *
//         */
//        破裂缺陷(0, "破裂缺陷", "破裂缺陷"),
//        变形及起伏缺陷(1, "变形及起伏缺陷", "变形及起伏缺陷"),
//        错口缺陷(2, "错口缺陷", "错口缺陷"),
//        脱节缺陷(3, "脱节缺陷", "脱节缺陷"),;
//
//        ExcelSheet(Integer index, String name, String category) {
//            this.index = index;
//            this.name = name;
//            this.category = category;
//        }
//
//        Integer index;
//        String name;
//        String category;
//    }
//}