package com.qmth.boot.tools.excel.model;

import org.apache.poi.ss.usermodel.Cell;

import java.util.Date;

/**
 * 写入Excel时多类型单元格数据封装类
 */
public class CellValue {

    public static final int TYPE_STRING = 1;

    public static final int TYPE_DOUBLE = 2;

    public static final int TYPE_DATE = 3;

    private Object value;

    private int type;

    private CellValue(int type, Object value) {
        this.type = type;
        this.value = value;
    }

    public static CellValue of(String value) {
        return new CellValue(TYPE_STRING, value);
    }

    public static CellValue of(double value) {
        return new CellValue(TYPE_DOUBLE, value);
    }

    public static CellValue of(Date value) {
        return new CellValue(TYPE_DATE, value);
    }

    public static CellValue[] of(String[] array) {
        CellValue[] valueArray = new CellValue[array.length];
        for (int i = 0; i < array.length; i++) {
            valueArray[i] = CellValue.of(array[i]);
        }
        return valueArray;
    }

    public void writeToCell(Cell cell) {
        switch (type) {
        case TYPE_DATE:
            cell.setCellValue((Date) value);
            break;
        case TYPE_DOUBLE:
            cell.setCellValue((Double) value);
            break;
        default:
            cell.setCellValue((String) value);
            break;
        }
    }
}
