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.EpdFeignClient;
import com.njcn.system.pojo.dto.EpdDTO;
import com.njcn.system.pojo.param.EleEpdPqdParam;
import com.njcn.system.pojo.po.EleEpdPqd;
import com.njcn.system.pojo.vo.EleEpdPqdListVO;
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 EpdFeignClientFallbackFactory implements FallbackFactory<EpdFeignClient> {
    @Override
    public EpdFeignClient 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 EpdFeignClient() {
            @Override
            public HttpResult<String> update(EleEpdPqdParam.EleEpdPqdUpdateParam updateParam) {
                log.error("{}异常，降级处理，异常为：{}","更新字典数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

            @Override
            public HttpResult<EleEpdPqd> add(EleEpdPqdParam eleEpdPqdParam) {
                log.error("{}异常，降级处理，异常为：{}","新增字典数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<EleEpdPqd>> dictMarkByDataType(String dataType) {
                log.error("{}异常，降级处理，异常为：{}","通过数据模型获取字典数据组装唯一标识",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Map<String, String>> addEvt(List<EleEpdPqdParam> eleEpdPqdParam) {
                log.error("{}异常，降级处理，异常为：{}","录入事件字典",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

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

            @Override
            public HttpResult<List<EleEpdPqd>> judgeExist(String name, String dataType) {
                log.error("{}异常，降级处理，异常为：{}","校验字典是否存在",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<EleEpdPqd> findByParam(String name, String dataType, String phase) {
                log.error("{}异常，降级处理，异常为：{}","根据条件查询字典数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

            @Override
            public HttpResult<EleEpdPqd> findByName(String name) {
                log.error("{}异常，降级处理，异常为：{}","根据名称查询字典信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<EleEpdPqd>> findListByShowName(String name) {
                log.error("{}异常，降级处理，异常为：{}","根据名称查询字典信息集合",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<EleEpdPqd>> selectByClassId(String classId) {
                log.error("{}异常，降级处理，异常为：{}","根据数据分类查询字典信息集合",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<EleEpdPqdListVO>> selectAll() {
                log.error("{}异常，降级处理，异常为：{}","根据数据分类查询字典信息集合",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<EleEpdPqd>> findListByCode(String code) {
                log.error("{}异常，降级处理，异常为：{}","根据告警码获取数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
