package com.rehome.jpahefengweather.service.excel;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.rehome.jpahefengweather.entity.HefengCity;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class ReadCityExcelXls {

    private String filePath = null;
    //ReadCityExcelXls
    public ReadCityExcelXls(String dir, String fileName){
        if(dir!=null&&fileName!=null){
            this.filePath = dir+File.separator+fileName;
        }
    }


    public void testReadExcel() {
        // 要读取的Excel文件路径
        File file = new File(this.filePath);
        if (!file.exists()) {
            System.out.println("excel文件不存在");
            return;
        }
        List<HefengCity> excelList = this.readExcel(file);
        System.out.println("list中的数据打印出来");
        if(excelList!=null&&excelList.size()>0){
            System.out.println(String.valueOf(excelList.size()));
        }
        if(excelList!=null){
            for (int i = 0; i < excelList.size(); i++) {
                HefengCity city = excelList.get(i);
                Gson gson = new Gson();
                System.out.print(gson.toJson(city));
                System.out.println();
            }
        }
    }

    public List<HefengCity> getCityLists() {
        // 要读取的Excel文件路径
        File file = new File(this.filePath);
        if (!file.exists()) {
            System.out.println("excel文件不存在");
            return null;
        }
        List<HefengCity> excelList = this.readExcel(file);
       return excelList;
    }

    // 去读Excel的方法readExcel，该方法的入口参数为一个File对象
    public List<HefengCity> readExcel(File file) {
        try {
            //用流的方式先读取到你想要的excel的文件
            FileInputStream fis=new FileInputStream(file);
            //解析excel
            POIFSFileSystem pSystem=new POIFSFileSystem(fis);
            //获取整个excel
            HSSFWorkbook hb=new HSSFWorkbook(pSystem);
            // Excel的页签数量
            int sheet_size = hb.getNumberOfSheets();
            for (int index = 0; index < sheet_size; index++) {
                List<HefengCity> outerList = new ArrayList<HefengCity>();
                // 每个页签创建一个Sheet对象
                HSSFSheet sheet=hb.getSheetAt(index);
                //获取第一行
                int firstrow=    sheet.getFirstRowNum();
                //获取最后一行
                int lastrow=    sheet.getLastRowNum();
                //循环行数依次获取列数
                for (int i = firstrow; i < lastrow+1; i++) {
                    if(i==0){
                        continue;
                    }
                    HefengCity city = new HefengCity();
                    //获取哪一行i
                    Row row=sheet.getRow(i);
                    if (row!=null) {
                        //获取这一行的第一列
                        int firstcell=row.getFirstCellNum();
                        //获取这一行的最后一列
                        int lastcell=row.getLastCellNum();
                        for (int j = firstcell; j <lastcell; j++) {
                            //获取第j列
                            Cell cell=row.getCell(j);
                            cell.setCellType(Cell.CELL_TYPE_STRING);
                            if (cell!=null) {
                                switch (j){
                                    case 0:
                                        city.setLocationId(cell.getStringCellValue());
                                        break;
                                    case 1:
                                        city.setLocationNameEn(cell.getStringCellValue());
                                        break;
                                    case 2:
                                        city.setLocationNameZh(cell.getStringCellValue());
                                    case 3:
                                        city.setCountryCode(cell.getStringCellValue());
                                        break;
                                    case 4:
                                        city.setCountryNameEn(cell.getStringCellValue());
                                        break;
                                    case 5:
                                        city.setCountryNameZh(cell.getStringCellValue());
                                        break;
                                    case 6:
                                        city.setAdm1NameEn(cell.getStringCellValue());
                                        break;
                                    case 7:
                                        city.setAdm1NameZh(cell.getStringCellValue());
                                        break;
                                    case 8:
                                        city.setAdm2NameEn(cell.getStringCellValue());
                                        break;
                                    case 9:
                                        city.setAdm2NameZh(cell.getStringCellValue());
                                        break;
                                    case 10:
                                        city.setTimezone(cell.getStringCellValue());
                                        break;
                                    case 11:
                                        city.setLatitude(cell.getStringCellValue());
                                        break;
                                    case 12:
                                        city.setLongitude(cell.getStringCellValue());
                                        break;
                                    case 13:
                                        city.setAdcode(cell.getStringCellValue());
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                        outerList.add(city);
                    }
                }
                fis.close();
                return outerList;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
