package com.njcn.prepare.harmonic.api.liteflow.fallback;

import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.prepare.harmonic.api.liteflow.LiteFlowFeignClient;
import com.njcn.prepare.harmonic.pojo.bo.BaseParam;
import com.njcn.prepare.harmonic.utils.PrepareEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LiteFlowFeignClientFallbackFactory implements FallbackFactory<LiteFlowFeignClient> {
    @Override
    public LiteFlowFeignClient create(Throwable throwable) {
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException)throwable.getCause();
            exceptionEnum = PrepareEnumUtil.getExceptionEnum(businessException.getResult());
        }

        Enum<?> finalExceptionEnum = exceptionEnum;
        return new LiteFlowFeignClient() {
            @Override
            public void measurementPointExecutor(BaseParam baseParam) {
                log.error("{}异常，降级处理，异常为：{}", "监测点算法执行链:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public void measurementPointExecutorByHour(BaseParam baseParam) {
                log.error("{}异常，降级处理，异常为：{}", "监测点算法(按小时)执行链:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public void orgPointExecutor(BaseParam baseParam) {
                log.error("{}异常，降级处理，异常为：{}", "单位监测点算法执行链:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public void uploadOrgExecutor(BaseParam baseParam) {
                log.error("{}异常，降级处理，异常为：{}", "pms国网上送单位层级算法执行链:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public void substationExecutor(BaseParam baseParam) {
                log.error("{}异常，降级处理，异常为：{}", "变电站算法执行链:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public void deviceExecutor(BaseParam baseParam) {
                log.error("{}异常，降级处理，异常为：{}", "装置算法执行链:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public void pmsDimExecutor(BaseParam baseParam) {
                log.error("{}异常，降级处理，异常为：{}", "pms变电站母线算法执行链:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public void generaTrixExecutor(BaseParam baseParam) {
                log.error("{}异常，降级处理，异常为：{}", "母线算法执行链(主网测点):  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public void orgSubStationExecutor(BaseParam baseParam) {
                log.error("{}异常，降级处理，异常为：{}", "单位变电站算法执行链:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
