package com.njcn.harmonic.api.fallback;

import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.device.biz.utils.DeviceEnumUtil;
import com.njcn.harmonic.api.HarmDataFeignClient;
import com.njcn.harmonic.pojo.param.HistoryHarmParam;
import com.njcn.harmonic.pojo.param.StatSubstationBizBaseParam;
import com.njcn.harmonic.pojo.po.day.RStatLimitRateDPO;
import com.njcn.harmonic.pojo.vo.AssessEvaluation;
import com.njcn.influx.pojo.dto.HarmHistoryDataDTO;
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 2023年07月21日 14:31
 */
@Slf4j
@Component
public class HarmDataFeignClientFallbackFactory implements FallbackFactory<HarmDataFeignClient> {
    @Override
    public HarmDataFeignClient 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 HarmDataFeignClient() {
            @Override
            public HttpResult<HarmHistoryDataDTO> getHistoryHarmData(HistoryHarmParam historyHarmParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取谐波历史数据", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getLinesTarget(StatSubstationBizBaseParam param) {
                log.error("{}异常，降级处理，异常为：{}", "获取越线监测点", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<RStatLimitRateDPO>> getLinesRate(StatSubstationBizBaseParam param) {
                log.error("{}异常，降级处理，异常为：{}", "获取越线监测点越线次数", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<AssessEvaluation> getHarmIAndHarmVData(StatisticsBizBaseParam param) {
                log.error("{}异常，降级处理，异常为：{}", "评估结果获取谐波电压和谐波电流含有率、三项不平横度和负序电流", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
