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.DataArrayFeignClient;
import com.njcn.csdevice.pojo.param.DataArrayParam;
import com.njcn.csdevice.pojo.po.CsDataArray;
import com.njcn.csdevice.pojo.vo.DataArrayTreeVO;
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 DataArrayFeignClientFallbackFactory implements FallbackFactory<DataArrayFeignClient> {
    @Override
    public DataArrayFeignClient 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 DataArrayFeignClient() {
            @Override
            public HttpResult<List<CsDataArray>> getArrayBySet(String dataSet) {
                log.error("{}异常，降级处理，异常为：{}","根据数据集获取详细数据失败",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DataArrayTreeVO>> getDataArray(List<String> dataSetList) {
                log.error("{}异常，降级处理，异常为：{}","获取详细数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsDataArray>> getDataArrayById(String pid, String name) {
                log.error("{}异常，降级处理，异常为：{}","根据数据集id和名称获取详细数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

            @Override
            public HttpResult<List<CsDataArray>> findListByParam(DataArrayParam param) {
                log.error("{}异常，降级处理，异常为：{}","根据条件查询详细数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
