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.pojo.po.DictData;
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 DicDataFeignClientFallbackFactory implements FallbackFactory<DicDataFeignClient> {
    @Override
    public DicDataFeignClient 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 DicDataFeignClient() {
            @Override
            public HttpResult<DictData> getDicDataById(String dicIndex) {
                log.error("{}异常，降级处理，异常为：{}","根据字典索引获取字典数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DictData>> getDicDataByTypeName(String dictTypeName) {
                log.error("{}异常，降级处理，异常为：{}","根据字典类型名称获取字典数据集合",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

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

            @Override
            public HttpResult<DictData> getDicDataByNameAndType(String dicName, String typeName) {
                log.error("{}异常，降级处理，异常为：{}","根据字典名称获取字典数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

            @Override
            public HttpResult<List<DictData>> getLoadTypeBySys() {
                log.error("{}异常，降级处理，异常为：{}","根据系统类型获取不同指标参数",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<DictData> addDicData(String dicTypeName, String dicDataName) {
                log.error("{}异常，降级处理，异常为：{}","后台新增字典数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<DictData> getDicDataByNameAndTypeName(String typeName, String dicDataName) {
                log.error("{}异常，降级处理，异常为：{}","根据字典类型名称&数据名称获取字典数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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