package com.chinaweal.aiccs.common.util;


import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @since 2024年07月30日 11:47
 */
public class LocalDateTimeConverter implements Converter<LocalDateTime> {


    static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public Class<LocalDateTime> supportJavaTypeKey() {
        return LocalDateTime.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
                                           GlobalConfiguration globalConfiguration) {
        if (CellDataTypeEnum.NUMBER.equals(cellData.getType())) {
            Date date = HSSFDateUtil.getJavaDate(cellData.getNumberValue().doubleValue());
            return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        } else if (CellDataTypeEnum.STRING.equals(cellData.getType())) {
            String value = cellData.getStringValue().replace("/", "-");
            if (value.length() <= 10) {
                value = value + " 00:00:00";
            }
            LocalDateTime dateTime;
            try {
                dateTime = LocalDateTime.parse(value, FORMATTER);
            } catch (Exception e) {
                throw new RuntimeException("日期时间格式错误 请修改为 'yyyy-MM-dd HH:mm:ss'格式");
            }
            return dateTime;
        } else {
            return null;
        }
    }

    @Override
    public CellData<String> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        return new CellData<>(value.format(FORMATTER));
    }

}
