package com.njcn.system.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.system.api.DicDataFeignClient;
import com.njcn.system.api.DictTreeFeignClient;
import com.njcn.system.pojo.po.DictData;
import com.njcn.system.pojo.po.SysDicTreePO;
import com.njcn.system.pojo.vo.DictTreeVO;
import com.njcn.system.utils.SystemEnumUtil;
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 2021年09月09日 15:19
 */
@Slf4j
@Component
public class DictTreeFeignClientFallbackFactory implements FallbackFactory<DictTreeFeignClient> {
    @Override
    public DictTreeFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (cause.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) cause.getCause();
            exceptionEnum = SystemEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new DictTreeFeignClient() {
            @Override
            public HttpResult<SysDicTreePO> queryById(String id) {
                log.error("{}异常，降级处理，异常为：{}", "根据字典id获取字典数据", cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<DictTreeVO> queryByCode(String code) {
                log.error("{}异常，降级处理，异常为：{}", "根据字典code获取字典树数据", cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DictTreeVO>> query(String pid) {
                log.error("{}异常，降级处理，异常为：{}", "根据字典pid获取字典树数据", cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<SysDicTreePO>> queryByCodeList(String code) {
                log.error("{}异常，降级处理，异常为：{}", "根据字典code获取字典树数据", cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<SysDicTreePO>> queryAll() {
                log.error("{}异常，降级处理，异常为：{}", "获取所有字典树数据", cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<SysDicTreePO>> queryAllByType(Integer type) {
                log.error("{}异常，降级处理，异常为：{}", "分类获取所有字典树数据", cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<SysDicTreePO>> queryTree() {
                log.error("{}异常，降级处理，异常为：{}", "获取树形数据", cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
