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.CsCommTerminalFeignClient;
import com.njcn.csdevice.pojo.dto.DevDetailDTO;
import com.njcn.device.biz.pojo.po.PqsDeviceUnit;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

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


            @Override
            public HttpResult<PqsDeviceUnit> lineUnitDetail(String lineId) {
                log.error("{}异常，降级处理，异常为：{}","查询分组",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getDevIdsByUser(String userId) {
                log.error("{}异常，降级处理，异常为：{}","根据登录用户id获取用户所有设备id集合",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getLineIdsByUser(String userId) {
                log.error("{}异常，降级处理，异常为：{}","根据登录用户id获取用户所有监测点id集合",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getPqUserIdsByUser(String userId) {
                log.error("{}异常，降级处理，异常为：{}","根据登录用户id获取电能质量用户id集合",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DevDetailDTO>> getLedgerByLineId(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}","根据监测点id集合获取所有台账信息异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
