package com.bs.mes.schedule.api;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class CalendarManagerTest {
    @Test
    public void addOaccupiedCalendar() {
        List<Map<String, String>> list = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\1\\Desktop\\default.md"))) {

//            int row = 0;
            String line;
            Boolean flag1 = null;
            Map<String, String> map = null;

            while ((line = reader.readLine()) != null) {


                if (flag1 == null || flag1) {
                    map = new HashMap<>();
                    flag1 = false;
                }

                if (line.contains("接口地址")) {
                    String replace = line.replace("**接口地址**:", "").replace("<p>", "").replace("</p>", "").replace("`", "");
                    map.put("apiaddr", replace);
                }

                if (line.contains("**请求方式**:")) {
                    String replace = line.replace("**请求方式**:", "").replace("<p>", "").replace("</p>", "").replace("`", "");
                    map.put("apimethod", replace);
                }


                if (line.contains("**接口描述**:")) {
                    String replace = line.replace("**接口描述**:", "").replace("<p>", "").replace("<p />", "").replace("</p>", "").replace("`", "");
                    map.put("apidesc", replace);
                }


                if (map.size() == 3) {
                    flag1 = true;
                    list.add(map);
                }
//                row++;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        excel(list);
    }


    public void excel(List<Map<String, String>> list) {
        Set<String> objects = new HashSet<>();

        List<Map<String, String>> objects1 = new ArrayList<>();
        for (Map<String, String> entry : list) {
            if (objects.add(entry.get("apiaddr"))) {
                objects1.add(entry);
            }
        }

        // 创建一个新的工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建一个工作表
        Sheet sheet = workbook.createSheet("接口");

        // 创建行（0基索引）
        Row row = sheet.createRow(0);

        // 创建单元格并设置值
        Cell cell = row.createCell(0);
        cell.setCellValue("服务启用");
        cell = row.createCell(3);
        cell.setCellValue("接口服务描述");
        cell = row.createCell(1);
        cell.setCellValue("接口地址");
        cell = row.createCell(2);
        cell.setCellValue("api地址");

        int rowIndex = 1;
        for (Map<String, String> dataRow : objects1) {
            // 创建行（0基索引）
            row = sheet.createRow(rowIndex);


            // 创建单元格并设置值
            cell = row.createCell(0);
            cell.setCellValue("是");
            cell = row.createCell(3);
            cell.setCellValue(dataRow.get("apidesc"));
            cell = row.createCell(1);
            cell.setCellValue("http://192.168.3.91:8080");
            cell = row.createCell(2);
            cell.setCellValue(dataRow.get("apiaddr"));
            rowIndex++;
        }

        // 写入到文件
        try (FileOutputStream outputStream = new FileOutputStream("C:\\Users\\1\\Desktop\\对外提供接口地址.xlsx")) {
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 清理资源
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}