package com.njcn.device.pq.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.pq.api.GeneralDeviceInfoClient;
import com.njcn.device.pq.pojo.bo.BaseLineInfo;
import com.njcn.device.pq.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.param.OnlineRateParam;
import com.njcn.device.pq.pojo.po.TopMsgPO;
import com.njcn.device.pq.pojo.vo.DevDetail;
import com.njcn.device.pq.pojo.vo.RStatOnlineRateVO;
import com.njcn.device.biz.utils.DeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年02月14日 14:03
 */
@Slf4j
@Component
public class GeneralDeviceInfoClientFallbackFactory implements FallbackFactory<GeneralDeviceInfoClient> {
    @Override
    public GeneralDeviceInfoClient 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 GeneralDeviceInfoClient()
        {
            @Override
            public HttpResult<List<GeneralDeviceDTO>> getPracticalRunDeviceInfoAsDept(DeviceInfoParam deviceInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取按部门分类的实际运行终端综合信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
            @Override
            public HttpResult<List<GeneralDeviceDTO>> getOfflineRunDeviceInfoAsDept(DeviceInfoParam deviceInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取按部门分类的实际便携式终端综合信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
            @Override
            public HttpResult<List<GeneralDeviceDTO>> getPracticalAllDeviceInfoAsDept(DeviceInfoParam deviceInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取按部门分类的实际所有终端综合信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<GeneralDeviceDTO>> getPracticalRunDeviceInfoAsSubstation(DeviceInfoParam deviceInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取按变电站分类的实际运行终端综合信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<GeneralDeviceDTO>> getPracticalAllDeviceInfoAsSubstation(DeviceInfoParam deviceInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取变电站分类的所有终端综合信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<GeneralDeviceDTO>> getPracticalRunDeviceInfo(DeviceInfoParam deviceInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取实际运行终端综合信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<GeneralDeviceDTO>> getOfflineRunDeviceInfo(DeviceInfoParam deviceInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取实际便携式终端综合信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<GeneralDeviceDTO>> getPracticalAllDeviceInfo(DeviceInfoParam deviceInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取实际所有终端综合信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<BaseLineInfo>> getBaseLineInfo(List<String> lineIndex) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点及以上层的基础信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<RStatOnlineRateVO>> getOnlineRateByDevIds(OnlineRateParam param) {
                log.error("{}异常，降级处理，异常为：{}", "获取终端在线率（谐波专用）", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> deptGetRunLine(String deptId) {
                log.error("{}异常，降级处理，异常为：{}", "获取单位投运监测点", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> deptGetRunLineEvent(String deptId) {
                log.error("{}异常，降级处理，异常为：{}", "获取单位暂态系统投运监测点", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> deptGetRunDev(String deptId) {
                log.error("{}异常，降级处理，异常为：{}", "根据单位获取终端集合", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DevDetail>> getDevInfoByIds(List<String> devIds) {
                log.error("{}异常，降级处理，异常为：{}", "根据终端id集合获取终端信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<TopMsgPO>> getTopMsgInfoByIds(OnlineRateParam param) {
                log.error("{}异常，降级处理，异常为：{}", "根据topIds集合获取终端信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Float> getTotalOnlineRates(OnlineRateParam param) {
                log.error("{}异常，降级处理，异常为：{}", "根据终端id集合获取总终端在线率", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };


    }
}
