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

import com.xisc.pm.api.dto.ObtainWitnessPointDTO;
import com.xisc.pm.api.dto.PreOptimalChargingDTO;
import com.xisc.pm.api.dto.QdMatchParamDTO;
import com.xisc.pm.api.dto.QdSurplusLowMagnificationDTO;
import com.xisc.pm.infra.feign.QdOrderApiFeign;
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;

import java.util.List;

/**
 * 
 * @author jun.zeng
 *
 */
@Component
public class QdOrderApiFeignFallbackFactory implements FallbackFactory<QdOrderApiFeign> {
    private static final Logger LOGGER = LoggerFactory.getLogger(QdOrderApiFeignFallbackFactory.class);

    /**
     * 当发生异常时，创建一个CtEventFeign实例的回退机制。
     *
     * @param cause 引发失败的Throwable，用于创建回退实例。
     * @return 返回实现了特定回退行为的CtEventFeign实例。
     */
    @Override
    public QdOrderApiFeign create(Throwable cause) {
        // 定义自定义的回退行为
        return new QdOrderApiFeign() {

            @Override
            public ResponseEntity<String> orderApi(Long organizationId, List<String> orderNoList) {
                return getStringResponseEntity(cause);
            }

            private ResponseEntity<String> getStringResponseEntity(Throwable cause) {
                // 记录异常堆栈跟踪信息以进行故障排除
                LOGGER.error("eventExecute-error: {}", ExceptionUtils.getStackTrace(cause));
                // 返回HTTP 500状态码表示服务器错误
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }

            @Override
            public ResponseEntity<String> orderTechApi(Long organizationId, List<String> orderNoList) {
                return getStringResponseEntity(cause);
            }

            @Override
            public ResponseEntity<String> preOptimalChargingApi(Long organizationId, List<PreOptimalChargingDTO> dtoList) {
                // 记录异常堆栈跟踪信息以进行故障排除
                LOGGER.error("preOptimalChargingApi-error: {}", ExceptionUtils.getStackTrace(cause));
                // 返回HTTP 500状态码表示服务器错误
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }

            @Override
            public ResponseEntity<String> phyFurnaceChemicalMatchApi(Long organizationId,
                    List<QdMatchParamDTO> dtoList) {
                LOGGER.error("phyFurnaceChemicalMatchApi-error: {}", ExceptionUtils.getStackTrace(cause));
                // 返回HTTP 500状态码表示服务器错误
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }

            @Override
            public ResponseEntity<String> surplusLowMagnificationMatchApi(Long organizationId,
                    List<QdSurplusLowMagnificationDTO> dtoList) {
                LOGGER.error("surplusLowMagnificationMatchApi-error: {}", ExceptionUtils.getStackTrace(cause));
                // 返回HTTP 500状态码表示服务器错误
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }

            @Override
            public ResponseEntity<String> obtainWitnessPointApi(Long organizationId, List<ObtainWitnessPointDTO> dtoList) {
                LOGGER.error("obtainWitnessPointApi-error: {}", ExceptionUtils.getStackTrace(cause));
                // 返回HTTP 500状态码表示服务器错误
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
        };
    }

}
