package com.wms_main.excel.easyexcel.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.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * excel任务类型转换
 */
public class TaskTypeConvertor implements Converter<Integer> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

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

    /**
     * 这里是读的时候会调用
     *
     * @return 转换后的任务类型
     */
    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        String cellValue = cellData.getStringValue();
        return switch (cellValue.trim()) {
            case "入库" -> 1;
            case "出库" -> 2;
            case "盘点" -> 10;
            case "移库" -> 9;
            default -> -1;
        };
    }

    /**
     * 这里是写的时候会调用
     *
     * @return 转换后的任务类型
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) {
        Integer value = context.getValue();
        if (value == 1) {
            return new WriteCellData<>("入库");
        } else if (value == 2) {
            return new WriteCellData<>("出库");
        } else if (value == 10) {
            return new WriteCellData<>("盘点");
        } else if (value == 9) {
            return new WriteCellData<>("移库");
        } else {
            return new WriteCellData<>("未知");
        }
    }
}
