package com.miyzh.utils;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

import java.io.*;
import java.util.*;

/**
 * description: ExceptionMsgUtil
 * date: 2020/6/23 9:18
 * author: SZQ
 */
public class CsvUtil {

    public static List<String> readCsv(String rootDir, String filePath) {
        Reader reader = null;
        List<String> list = new ArrayList<>();
        if(null == filePath || "".equals(filePath)) {
            return null;
        }
        filePath = rootDir + filePath;
        try {
            reader = new FileReader(filePath);
            Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(reader);
            for (CSVRecord csvRecord : records) {
                list.add(csvRecord.get(2));
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                if(null != reader) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    public static List<String> readCsvCode(String rootDir, String filePath) {
        Reader reader = null;
        List<String> list = new ArrayList<>();
        if(null == filePath || "".equals(filePath)) {
            return null;
        }
        filePath = rootDir + filePath;
        try {
            reader = new FileReader(filePath);
            Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(reader);
            for (CSVRecord csvRecord : records) {
                Document doc = DocumentHelper.parseText(csvRecord.get(2));
                list.add(doc.getRootElement().element("SERIAL_NUMBER").getTextTrim());
            }
            return list;
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        } finally {
            try{
                if(null != reader) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }


    public static void writeCsv(String rootDir, String filePath, String clinicCode, List<String> list, String tradeCode) {
        try {
            File fileDir = new File(rootDir);
            if(!fileDir.exists()) {
                fileDir.mkdir();
            }
            int startIndex = 0;
            int endIndex;
            int limit = 1000;
            for(int j = 0; j <= list.size() / limit; j++) {
                List<String> newList = new ArrayList<>();
                CollectionUtils.addAll(newList, new Object[list.size()]);
                Collections.copy(newList, list);
                endIndex = startIndex + limit;
                if(list.size() / limit == j && list.size() % limit > 0) {
                    endIndex = list.size();
                }
                List<String> subList = newList.subList(startIndex, endIndex);
                startIndex = endIndex;
                int i= 1;
                FileOutputStream fos = new FileOutputStream(rootDir + filePath + "-" + j + ".csv");
                OutputStreamWriter osw = new OutputStreamWriter(fos);
                CSVFormat csvFormat = CSVFormat.DEFAULT.withIgnoreHeaderCase();
                CSVPrinter csvPrinter = new CSVPrinter(osw, csvFormat);
                for (String param : subList) {
                    System.out.println("开始写入===" + i);
                    List<String> headerList = new ArrayList<>();
                    headerList.add(clinicCode);
                    headerList.add(tradeCode);
                    headerList.add(param);
                    csvPrinter.printRecord(headerList);
                    i++;
                }
                csvPrinter.flush();
                csvPrinter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
