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.EquipmentFeignClient;
import com.njcn.csdevice.pojo.dto.CsEquipmentDeliveryDTO;
import com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO;
import com.njcn.csdevice.pojo.vo.CsEquipmentDeliveryVO;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author xy
 */
@Slf4j
@Component
public class EquipmentFeignClientFallbackFactory implements FallbackFactory<EquipmentFeignClient> {
    @Override
    public EquipmentFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (cause.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) cause.getCause();
//            exceptionEnum = UserEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new EquipmentFeignClient() {

            @Override
            public HttpResult<CsEquipmentDeliveryVO> queryEquipmentByndid(String ndid) {
                log.error("{}异常，降级处理，异常为：{}","通过ndid查询出厂设备异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> updateStatusBynDid(String nDid, Integer status) {
                log.error("{}异常，降级处理，异常为：{}","通过ndid修改装置的状态",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsEquipmentDeliveryDTO>> queryDeviceById(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}","查询出厂设备详情",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> updateSoftInfoBynDid(String nDid, String id, Integer module) {
                log.error("{}异常，降级处理，异常为：{}","根据网关id调整软件信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsEquipmentDeliveryPO> findDevByNDid(String nDid) {
                log.error("{}异常，降级处理，异常为：{}","通过nDid查询设备信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> updateSoftInfo(String nDid, String id) {
                log.error("{}异常，降级处理，异常为：{}","更新设备软件信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> updateModuleNumber(String nDid, Integer number) {
                log.error("{}异常，降级处理，异常为：{}","更新设备模块个数",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsEquipmentDeliveryPO>> getAll() {
                log.error("{}异常，降级处理，异常为：{}","获取所有装置",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> judgeDevModel(String nDid) {
                log.error("{}异常，降级处理，异常为：{}","判断设备型号",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsEquipmentDeliveryPO> getDevByLineId(String lineId) {
                log.error("{}异常，降级处理，异常为：{}","根据监测点id查询装置信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> updateCldDevStatus(String nodeId, Integer processNo) {
                log.error("{}异常，降级处理，异常为：{}","云前置心跳丢失切换设备状态",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> flipCldDevStatus(String date, String devId, Integer status) {
                log.error("{}异常，降级处理，异常为：{}","云前置设备状态翻转",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };
    }
}
