package com.njcn.csharmonic.api.fallback;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.pojo.vo.DataGroupEventVO;
import com.njcn.csharmonic.api.EventFeignClient;
import com.njcn.csharmonic.param.CsEventUserQueryPage;
import com.njcn.csharmonic.param.CsEventUserQueryParam;
import com.njcn.csharmonic.pojo.po.CsEventPO;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author gfh
 */
@Slf4j
@Component
public class EventFeignClientFallbackFactory implements FallbackFactory<EventFeignClient> {
    @Override
    public EventFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (cause.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) cause.getCause();
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new EventFeignClient() {
            @Override
            public HttpResult<String> saveBatchEventList(List<CsEventPO> csEventPOS) {
                log.error("{}异常，降级处理，异常为：{}","获取当天事件未读消息未读消息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Page<DataGroupEventVO>> pageQueryByLineId(CsEventUserQueryPage csEventUserQueryPage) {
                log.error("{}异常，降级处理，异常为：{}","根据监测点分页查询暂降事件",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsEventPO> queryByIndex( CsEventPO csEventPO) {
                log.error("{}异常，降级处理，异常为：{}","根据根据表唯一索引查询(用于校验是否存在该事件)s",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsEventPO> queryById(String eventId) {
                log.error("{}异常，降级处理，异常为：{}","根据事件id查询数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsEventPO>> getEventByTime(List<String> lineList, String startTime, String endTime) {
                log.error("{}异常，降级处理，异常为：{}","根据时间获取无波形的暂态事件",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsEventPO>> getDevAlarmList(CsEventUserQueryParam param) {
                log.error("{}异常，降级处理，异常为：{}","获取设备运行告警事件数据异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsEventPO>> getEventByIdList(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}","根据事件id集合获取事件详情异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
