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.EleEvtFeignClient;
import com.njcn.system.api.EpdFeignClient;
import com.njcn.system.pojo.param.EleEpdPqdParam;
import com.njcn.system.pojo.param.EleEvtParam;
import com.njcn.system.pojo.po.EleEpdPqd;
import com.njcn.system.pojo.po.EleEvtParm;
import com.njcn.system.utils.SystemEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2023/5/24 18:46
 */
@Slf4j
@Component
public class EleEvtFeignClientFallbackFactory implements FallbackFactory<EleEvtFeignClient> {
    @Override
    public EleEvtFeignClient 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 EleEvtFeignClient() {
            @Override
            public HttpResult<EleEpdPqd> add(EleEvtParam evtParam) {
                log.error("{}异常，降级处理，异常为：{}","新增事件拓展数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<EleEpdPqd> delete(String id) {
                log.error("{}异常，降级处理，异常为：{}","删除事件拓展数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<EleEvtParm>> queryByPid(String pid) {
                log.error("{}异常，降级处理，异常为：{}","查询事件拓展数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
