package com.njcn.device.pq.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.pq.api.TerminalBaseClient;
import com.njcn.device.pq.pojo.param.SyncTerminalParam;
import com.njcn.device.pq.pojo.po.Line;
import com.njcn.device.biz.utils.DeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年02月14日 14:03
 */
@Slf4j
@Component
public class TerminalBaseClientFallbackFactory implements FallbackFactory<TerminalBaseClient> {
    @Override
    public TerminalBaseClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = DeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new TerminalBaseClient()
        {
            @Override
            public HttpResult<List<Line>> getSubstationById(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}", "根据变电站id获取变电站详情", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> terminalSync(SyncTerminalParam syncTerminalParam) {
                log.error("{}异常，降级处理，异常为：{}", "台账同步", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> terminalSyncRunFly(String lineId) {
                log.error("{}异常，降级处理，异常为：{}", "监测点和装置同步成运行状态", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> terminalSyncDeleteFly(String lineId) {
                log.error("{}异常，降级处理，异常为：{}", "监测点信息删除", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };


    }
}
