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.PqsTerminalLogsClient;
import com.njcn.device.pq.pojo.po.PqsTerminalLogs;
import com.njcn.device.biz.utils.DeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author zbj
 * @version 1.0.0
 * @date 2023年04月13日 13:34
 */
@Slf4j
@Component
public class PqsTerminalLogsClientFallbackFactory implements FallbackFactory<PqsTerminalLogsClient> {
    @Override
    public PqsTerminalLogsClient 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 PqsTerminalLogsClient()
        {
            @Override
            public HttpResult<Object> saveLogs(PqsTerminalLogs pqsTerminalLogs) {
                log.error("{}异常，降级处理，异常为：{}", "新增日志表数据", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };


    }
}
