package com.njcn.device.biz.commApi.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.biz.commApi.CommLineClient;
import com.njcn.device.biz.pojo.dto.LineALLInfoDTO;
import com.njcn.device.biz.pojo.dto.LineDTO;
import com.njcn.device.biz.pojo.dto.PollutionLineDTO;
import com.njcn.device.biz.pojo.dto.PollutionLineInfoDTO;
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 cdf
 * @date 2023/5/17
 */
@Slf4j
@Component
public class CommLineClientFallbackFactory implements FallbackFactory<CommLineClient> {
    @Override
    public CommLineClient 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 CommLineClient() {
            @Override
            public HttpResult<LineDTO> getLineDetail(String id) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PollutionLineInfoDTO>> getPollutionLineInfo(List<String> id) {
                log.error("{}异常，降级处理，异常为：{}", "获取污区值监测点相关信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineDTO>> getLineDetailBatch(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}", "批量获取监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<LineALLInfoDTO> getLineAllDetail(String id) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineALLInfoDTO>> getLineAllDetailList(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点列表信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PollutionLineDTO>> getLineInfo(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点列表信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };
    }
}

