package org.linlinjava.jz.admin.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.linlinjava.jz.core.util.BeanUtil;
import org.linlinjava.jz.db.domain.SysCity;
import org.linlinjava.jz.db.service.JzSysCityService;

/**
 * @author fuxubing
 */
public class CityConverter implements Converter<Long> {
    @Override
    public Class supportJavaTypeKey() {
        return null;
    }

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

    private final JzSysCityService jzSysCityService = BeanUtil.getBean(JzSysCityService.class);

    @Override
    public Long convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {

        SysCity sysCity = jzSysCityService.queryByCityName(cellData.getStringValue());
        return null != sysCity ? sysCity.getId() : 0;
    }

    @Override
    public CellData convertToExcelData(Long value, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        SysCity sysCity = jzSysCityService.queryById(value);
        return new CellData(null != sysCity ? sysCity.getCityName() : "");
    }
}