package com.njcn.device.pms.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.pms.api.MonitorSendClient;
import com.njcn.device.pms.pojo.param.MonitorParam;
import com.njcn.device.pms.utils.PmsDeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * @author wr
 * @description
 * @date 2023/12/25 9:17
 */
@Slf4j
@Component
public class MonitorSendClientFallbackFactory implements FallbackFactory<MonitorSendClient> {

    @Override
    public MonitorSendClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = PmsDeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new MonitorSendClient()
        {

            @Override
            public HttpResult<String> windSend(MonitorParam.Info param) {
                log.error("{}异常，降级处理，异常为：{}", "国网五类台账上送接口", throwable.toString());
                return new HttpResult<>(CommonResponseEnum.FAIL.getCode(),CommonResponseEnum.FAIL.getMessage());
            }
        };
    }
}