package com.cxy.classroll.student.util;

import com.cxy.classroll.common.execption.CxyException;
import com.cxy.classroll.model.vo.CsvData;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class CsvUtil {
    public <T> List<CsvData> getCsvDataManually(MultipartFile file) {
        List<CsvData> csvDataList = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8))) {
            String headerLine = reader.readLine(); // 读取并跳过第一行
            String line;
            while ((line = reader.readLine()) != null) {
                String[] values = line.split(",");
                CsvData csvData = new CsvData();
                csvData.setStudent_no(values[0]);
                csvData.setName(values[1]);
                csvDataList.add(csvData);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return csvDataList;
    }


}
