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.PwMonitorClient;
import com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO;
import com.njcn.device.pms.pojo.param.PwPmsMonitorParam;
import com.njcn.device.pms.utils.PmsDeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author yzh
 * @date 2022/10/28
 */

@Slf4j
@Component
public class PwMonitorClientFallbackFactory implements FallbackFactory<PwMonitorClient> {
    @Override
    public PwMonitorClient 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 PwMonitorClient() {
            @Override
            public HttpResult<List<PwPmsMonitorDTO>> getPwMonitorList(PwPmsMonitorParam pwPmsMonitorParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取配网所有监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PwPmsMonitorDTO>> getPwNewMonitorList(PwPmsMonitorParam pwPmsMonitorParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取配网所有监测点信息(新)", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PwPmsMonitorDTO>> getPwSubsetMonitorList(PwPmsMonitorParam pwPmsMonitorParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取子集配网所有监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PwPmsMonitorDTO>> getPwPhotovoltaicMonitorList(PwPmsMonitorParam pwPmsMonitorParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取分布式光伏配网所有监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
