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.StatationStatClient;
import com.njcn.device.pms.pojo.dto.PmsStatationStatInfoDTO;
import com.njcn.device.pms.pojo.param.PmsBaseParam;
import com.njcn.device.pms.pojo.param.PmsStatationStatInfoParam;
import com.njcn.device.pms.pojo.po.StatationStat;
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/18
 */

@Slf4j
@Component
public class StatationStatClientFallbackFactory implements FallbackFactory<StatationStatClient> {

    @Override
    public StatationStatClient 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 StatationStatClient() {
            @Override
            public HttpResult<List<PmsStatationStatInfoDTO>> getStatationStatInfo(PmsStatationStatInfoParam pmsStatationStatInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取指定的变电站信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<StatationStat>> getPowerInfo(PmsStatationStatInfoParam param) {
                log.error("{}异常，降级处理，异常为：{}", "获取变电站信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<StatationStat>> getstatationStatList(PmsBaseParam pmsBaseParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取变电站信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
