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 TaskStatusConvertor 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 "重复入库" -> -2;
            case "暂存任务" -> -1;
            case "任务新建，待下发" -> 0;
            case "任务已下发" -> 1;
            case "任务开始执行" -> 2;
            case "出库完成" -> 3;
            case "环线运输" -> 4;
            case "拣选中" -> 8;
            case "盘点中" -> 9;
            case "任务完成" -> 100;
            case "任务取消" -> 998;
            default -> 999;
        };
    }

    /**
     * 这里是写的时候会调用
     *
     * @return 转换后的任务状态
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) {
        Integer value = context.getValue();
        if (value == -2) {
            return new WriteCellData<>("重复入库");
        } else if (value == -1) {
            return new WriteCellData<>("暂存任务");
        } else if (value == 0) {
            return new WriteCellData<>("任务新建，待下发");
        } else if (value == 1) {
            return new WriteCellData<>("任务已下发");
        } else if (value == 2) {
            return new WriteCellData<>("任务开始执行");
        } else if (value == 3) {
            return new WriteCellData<>("出库完成");
        } else if (value == 4) {
            return new WriteCellData<>("环线运输");
        } else if (value == 8) {
            return new WriteCellData<>("拣选中");
        } else if (value == 9) {
            return new WriteCellData<>("盘点中");
        } else if (value == 100) {
            return new WriteCellData<>("任务完成");
        } else if (value == 998) {
            return new WriteCellData<>("任务取消");
        } else {
            return new WriteCellData<>("任务异常");
        }
    }
}
