package me.zhengjie.ykt.service.impl;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

public class OfficeUtils {

    private static final Logger logger = LoggerFactory.getLogger(OfficeUtils.class);

    public static List<Map<Object, Object>> readExcelContentz(MultipartFile file) {
        // 格式化时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 状态匹配字典
        Map<String, Object> m = new HashMap<>();
        m.put("禁用", "0");
        m.put("启用", "1");
        m.put("待维修", "2");
        List<Map<Object, Object>> list = new ArrayList<>();
        // 上传文件名
        Workbook wb = getWb(file);
        if(wb == null) {
            throw  new RuntimeException("文件上传失败");
        }
        Sheet sheet = wb.getSheetAt(0);
        // 获取总行数
        Integer rownum = sheet.getLastRowNum();
        Row row = sheet.getRow(0);
        int column = row.getPhysicalNumberOfCells();
        // 获取excel表头的所有列
        Row firstRowcell = sheet.getRow(0);
        // 获取excel表格的内容, 从第二行开始
        for(int i = 1;i <= rownum;i ++) {
            row = sheet.getRow(i);
            Map<Object, Object> rowMap = new HashMap<>();
            for(int j = 0;j < column;j ++) {
                Object obj = row.getCell(j);
                if("设备状态".equals(String.valueOf(firstRowcell.getCell(j)))) {
                    rowMap.put(getCellKey(String.valueOf(firstRowcell.getCell(j))), m.get(obj.toString()));
                }else if("注册时间".equals(String.valueOf(firstRowcell.getCell(j)))) {
                    Cell cell = row.getCell(j);
                    rowMap.put(getCellKey(String.valueOf(firstRowcell.getCell(j))), sdf.format(cell.getDateCellValue()));
                }else {
                    rowMap.put(getCellKey(String.valueOf(firstRowcell.getCell(j))), String.valueOf(obj));
                }
            }
            list.add(rowMap);
        }
        return list;
    }

    private static Object getCellKey(String text) {
        Object key = "";
        if(!"".equals(text)) {
            switch (text) {
                case "设备编号":
                    key = "code";
                    break;
                case "设备名称":
                    key = "name";
                    break;
                case "设备安装位置":
                    key = "buildAddress";
                    break;
                case "注册时间":
                    key = "registTime";
                    break;
                case "设备状态":
                    key = "status";
                    break;
                case "备注":
                    key = "remark";
                    break;
            }
        }
        return key;
    }

    private static Workbook getWb(MultipartFile file) {
        String filepath = file.getOriginalFilename();
        // 获取文件名后缀
        String ext = filepath.substring(filepath.lastIndexOf("."));
        Workbook wb = null;
        try {
            InputStream is = file.getInputStream();
            if(".xls".equals(ext)) {
                wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(ext)) {
                wb = new XSSFWorkbook(is);
            }else {
                wb = null;
            }
        }catch (IOException e) {
            logger.error(e.getMessage());
        }
        return wb;
    }
}
