package com.njcn.csdevice.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.csdevice.api.DevModelRelationFeignClient;
import com.njcn.csdevice.pojo.param.CsDevModelRelationAddParm;
import com.njcn.csdevice.pojo.po.CsDevModelRelationPO;
import com.njcn.csdevice.utils.CsDeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author xy
 */
@Slf4j
@Component
public class DevModelRelationFeignClientFallbackFactory implements FallbackFactory<DevModelRelationFeignClient> {
    @Override
    public DevModelRelationFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (cause.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) cause.getCause();
            exceptionEnum = CsDeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new DevModelRelationFeignClient() {
            @Override
            public HttpResult<List<CsDevModelRelationPO>> getModelByDevId(String devId) {
                log.error("{}异常，降级处理，异常为：{}","根据装置获取模板",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsDevModelRelationPO> addDevModelRelation(CsDevModelRelationAddParm addParm) {
                log.error("{}异常，降级处理，异常为：{}","绑定设备与模板",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> getModelByType(String devId, Integer type) {
                log.error("{}异常，降级处理，异常为：{}","根据装置类型查询模板",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> updateDataByList(List<String> list, String id) {
                log.error("{}异常，降级处理，异常为：{}","根据装置集合修改模板信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
