package com.njcn.device.pms.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.device.pms.api.MonitorClient;
import com.njcn.device.pms.pojo.dto.PmsMonitorDTO;
import com.njcn.device.pms.pojo.dto.PmsMonitorInfoDTO;
import com.njcn.device.pms.pojo.param.MonitorTerminalParam;
import com.njcn.device.pms.pojo.param.PmsMonitorInfoParam;
import com.njcn.device.pms.pojo.param.PmsMonitorParam;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.device.pms.pojo.vo.MonitorVO;
import com.njcn.device.pms.utils.PmsDeviceEnumUtil;
import com.njcn.device.biz.pojo.po.Overlimit;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author yzh
 * @date 2022/10/18
 */
@Slf4j
@Component
public class MonitorClientFallbackFactory implements FallbackFactory<MonitorClient> {

    @Override
    public MonitorClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = PmsDeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new MonitorClient()
        {
            @Override
            public HttpResult<List<PmsMonitorInfoDTO>> getMonitorInfo(PmsMonitorInfoParam pmsMonitorInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取指定的监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PmsMonitorDTO>> getMonitorInfoListByCond(PmsMonitorParam pmsMonitorParam) {
                log.error("{}异常，降级处理，异常为：{}", "根据条件获取监测点信息列表", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Overlimit> getOverLimitData(String id) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点Id获取限值  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Monitor>> getMonitorList(List<String> monitorIds) {
                log.error("{}异常，降级处理，异常为：{}", "获取主网监测点数据  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<MonitorVO> getMonitorTerminal(MonitorTerminalParam param) {
                log.error("{}异常，降级处理，异常为：{}", "获取台账和终端信息(波形专用)  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
