package com.bonus.canteen.core.common.converter;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.converters.Converter;
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 java.util.Objects;

public abstract class LeExcelFieldConvertor<T extends LeExcelFieldEnum<?>> implements Converter<Integer> {
   private final Class<T> clz;

   protected LeExcelFieldConvertor(Class<T> clz) {
      this.clz = clz;
   }

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

   @Override
   public CellDataTypeEnum supportExcelTypeKey() {
      return null;
   }

   @Override
   public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
      if (Objects.isNull(value)) {
         return new WriteCellData("");
      } else {
         String desc = "";
         T[] enumConstants = (T[]) this.clz.getEnumConstants();
         if (enumConstants != null) {
            LeExcelFieldEnum[] var6 = enumConstants;
            int var7 = enumConstants.length;

            for(int var8 = 0; var8 < var7; ++var8) {
               T t = (T) var6[var8];
               if (ObjectUtil.equals(value, t.getKey())) {
                  desc = t.getDesc();
                  break;
               }
            }
         }

         return new WriteCellData(desc);
      }
   }
}
