package com.njcn.harmonic.api.fallback;

import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.device.biz.utils.DeviceEnumUtil;
import com.njcn.device.pq.pojo.param.OnlineRateParam;
import com.njcn.harmonic.api.PollutionSubstationClient;
import com.njcn.harmonic.pojo.excel.pollution.UserLinePollution;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2023年07月21日 14:31
 */
@Slf4j
@Component
public class PollutionSubstationClientFactory implements FallbackFactory<PollutionSubstationClient> {
    @Override
    public PollutionSubstationClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = DeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new PollutionSubstationClient() {


            @Override
            public HttpResult<Map<String, UserLinePollution>> getSumList(OnlineRateParam.Info param) {
                log.error("{}异常，降级处理，异常为：{}", "国网上送-典型源荷指标统计数据", throwable.toString());
                return new HttpResult<>(CommonResponseEnum.FAIL.getCode(),CommonResponseEnum.FAIL.getMessage());
            }
        };
    }
}
