package com.xisc.pm.infra.feign.fallback;

import com.xisc.pm.api.dto.InterfaceReceiveDTO;
import com.xisc.pm.infra.feign.HeApiFeign;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

/**
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @date 2024/11/4 14:48
 * @description
 */
@Component
public class HeApiFeignFallbackFactory implements FallbackFactory<HeApiFeign> {
    private static final Logger LOGGER = LoggerFactory.getLogger(HeApiFeignFallbackFactory.class);

    @Override
    public HeApiFeign create(Throwable cause) {
        // 定义自定义的回退行为
        return new HeApiFeign() {
            @Override
            public ResponseEntity<String> secondPlanAssign(Long organizationId, InterfaceReceiveDTO dt) {
                // 记录异常堆栈跟踪信息以进行故障排除
                LOGGER.error("secondDataSend-error: {}", ExceptionUtils.getStackTrace(cause));
                // 返回HTTP 500状态码表示服务器错误
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
        };
    }
}
