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.CsLedgerFeignClient;
import com.njcn.csdevice.pojo.dto.DevDetailDTO;
import com.njcn.csdevice.pojo.dto.LineParamDTO;
import com.njcn.csdevice.pojo.param.CsLedgerParam;
import com.njcn.csdevice.pojo.po.CsLedger;
import com.njcn.csdevice.pojo.vo.CsLedgerVO;
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 CsLedgerFeignClientFallbackFactory implements FallbackFactory<CsLedgerFeignClient> {
    @Override
    public CsLedgerFeignClient 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 CsLedgerFeignClient() {

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

            @Override
            public HttpResult<CsLedger> add(CsLedgerParam csLedgerParam) {
                log.error("{}异常，降级处理，异常为：{}","新增台账数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsLedger>> queryLine(LineParamDTO lineParamdto) {
                log.error("{}异常，降级处理，异常为：{}","查询监测点",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

            @Override
            public HttpResult<List<CsLedgerVO>> lineTree() {
                log.error("{}异常，降级处理，异常为：{}","查询监测点数数据异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsLedgerVO>> getDeviceTree() {
                log.error("{}异常，降级处理，异常为：{}","查询设备树",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsLedgerVO>> getAllLedger() {
                log.error("{}异常，降级处理，异常为：{}","获取台账树所有数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DevDetailDTO>> getInfoByIds(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}","根据设备集合查询工程，设备信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DevDetailDTO>> getDevInfoByEngineerIds(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}","根据工程获取设备信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DevDetailDTO>> getEngineeringHaveDevs(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}","根据工程获取设备信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
