package com.qmth.sop.business.templete.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.qmth.sop.common.enums.DingSubmitStatusEnum;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class LongLocalDateTimeConverter implements Converter<Long> {

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

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

    @Override
    public WriteCellData<?> convertToExcelData(Long value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        //Long时间戳转换时间字符串
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(value/1000, 0, ZoneOffset.ofHours(8));
        return new WriteCellData<>(DATE_TIME_FORMATTER.format(localDateTime));
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Long> context) throws Exception {
      //Long时间戳转换时间字符串
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(context.getValue()/1000, 0, ZoneOffset.ofHours(8));
        return new WriteCellData<>(DATE_TIME_FORMATTER.format(localDateTime));
    }
}