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.CsStatisticalSetFeignClient;
import com.njcn.system.pojo.po.CsStatisticalSetPO;
import com.njcn.system.pojo.po.EleEpdPqd;
import com.njcn.system.pojo.vo.CsStatisticalSetVO;
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 CsStatistiacalFeignClientFallbackFactory implements FallbackFactory<CsStatisticalSetFeignClient> {


    /**
     * 输出远程请求接口异常日志
     * @param cause RPC请求异常
     */
    @Override
    public CsStatisticalSetFeignClient 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 CsStatisticalSetFeignClient() {


            @Override
            public HttpResult<Boolean> addStatistical(String id, List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}","根据模板录入字典数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsStatisticalSetVO> queryStatistical(String id) {
                log.error("{}异常，降级处理，异常为：{}","根据统计类型id查询定指标",cause.toString());
                throw new BusinessException(finalExceptionEnum);            }

            @Override
            public HttpResult<List<EleEpdPqd>> queryStatisticalSelect(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}","根据统计类型id查询已绑定指标下拉框",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsStatisticalSetPO>> queryStatisticalById(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}","根据id查询数据异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
