package com.njcn.event.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.event.api.EventDetailFeignClient;
import com.njcn.event.pojo.param.EventCountParam;
import com.njcn.event.pojo.po.EventDetail;
import com.njcn.event.pojo.po.RmpEventDetailPO;
import com.njcn.event.pojo.vo.GeneralVO;
import com.njcn.event.utils.EventlEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;

/**
 * @author denghuajun
 * @version 1.0.0
 * @date 2022/3/16
 */
@Slf4j
@Component
public class EventDetailFeignClientFallbackFactory implements FallbackFactory<EventDetailFeignClient> {
    @Override
    public EventDetailFeignClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = EventlEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new EventDetailFeignClient() {
            @Override
            public HttpResult<List<EventDetail>> getEventDetailData(String id, String startTime, String endTime) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点Id获取暂降事件信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<EventDetail>> getEventDetail(List<String> lineIndexes, String startTime, String endTime) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点集合获取暂降事件", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<EventDetail>> getEventDetailLimit(List<String> lineIndexes, String startTime, String endTime, Integer pageSize, Integer pageNum) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点集合获取暂降事件", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<GeneralVO>> getEventDetailCount(Map<String, Object> condMap) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点集合获取短时终端，电压暂升，电压暂降数量", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<RmpEventDetailPO>> getEventDetailByIdsList(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}", "根据暂态id集合获取暂降列表", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Integer> getEventDetailByLineCount(EventCountParam param) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点id集合,统计时间范围内暂态超标次数", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<RmpEventDetailPO>> getAppEventDetailLtAmplitude(EventCountParam param) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点id集合,统计时间范围内特征增幅小于0.9的暂态信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<RmpEventDetailPO>> getEventDetailByEventType(EventCountParam param) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点id集合和统计类型,查询暂态信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<RmpEventDetailPO>> getNewEventDetailByTime(EventCountParam param) {
                log.error("{}异常，降级处理，异常为：{}", "根据开始时间及结束时间获取暂态事件信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
