package com.njcn.csdevice.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.csdevice.api.DataSetFeignClient;
import com.njcn.csdevice.pojo.po.CsDataSet;
import com.njcn.csdevice.pojo.vo.LineTargetVO;
import com.njcn.csdevice.utils.CsDeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2023/4/10 20:09
 */
@Slf4j
@Component
public class DataSetFeignClientFallbackFactory implements FallbackFactory<DataSetFeignClient> {
    @Override
    public DataSetFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (cause.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) cause.getCause();
            exceptionEnum = CsDeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new DataSetFeignClient() {
            @Override
            public HttpResult<List<CsDataSet>> getSetByModelId(String modelId,Integer clDev) {
                log.error("{}异常，降级处理，异常为：{}","根据模板id获取数据集",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineTargetVO>> getDataSet(String modelId,Integer clDev) {
                log.error("{}异常，降级处理，异常为：{}","获取数据集",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsDataSet>> getModuleDataSet(String modelId) {
                log.error("{}异常，降级处理，异常为：{}","获取子模块数据集",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsDataSet>> getDataSetBySetIds(List<String> lineIds) {
                log.error("{}异常，降级处理，异常为：{}","根据数据集ids获取数据集",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsDataSet> getBaseDataSet(String modelId, Integer clDev) {
                log.error("{}异常，降级处理，异常为：{}","获取实时数据-基础数据集",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsDataSet> getHarmonicDataSet(String modelId, Integer clDev, Integer target) {
                log.error("{}异常，降级处理，异常为：{}","获取实时数据-谐波数据集",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsDataSet> getDataSetByIdx(String modelId, Integer idx) {
                log.error("{}异常，降级处理，异常为：{}","根据idx获取数据集数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
