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.AreaFeignClient;
import com.njcn.system.pojo.dto.AreaTreeDTO;
import com.njcn.system.pojo.po.Area;
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 denghuajun
 * @version 1.0.0
 * @date 2022年01月05日 15:08
 */
@Slf4j
@Component
public class AreaFeignClientFallbackFactory implements FallbackFactory<AreaFeignClient> {


    /**
     * 输出远程请求接口异常日志
     * @param cause RPC请求异常
     */
    @Override
    public AreaFeignClient 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 AreaFeignClient() {
            @Override
            public HttpResult<Area> selectIdArea(String id) {
                log.error("{}异常，降级处理，异常为：{}","根据行政区域id查询详情",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Area>> areaNameByList(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}","根据行政区域id集合查询名称",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<AreaTreeDTO>> areaDeptTree(String id, Integer type) {
                log.error("{}异常，降级处理，异常为：{}","查询区域详情",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Area> areaPro(String id, Integer type) {
                log.error("{}异常，降级处理，异常为：{}","根据区域id查询省份信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Area> areaDeptPro(String id) {
                log.error("{}异常，降级处理，异常为：{}","根据部门id查询省份信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Area> selectAreaByName(String name) {
                log.error("{}异常，降级处理，异常为：{}","根据行政区域名称查询详细",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Area>> getPidAreaList(String areaId, Integer type) {
                log.error("{}异常，降级处理，异常为：{}","获取指定区域父级的子级区域集合",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };
    }
}
