package com.njcn.prepare.harmonic.api.event.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.harmonic.pojo.vo.PowerStatisticsTargetVO;
import com.njcn.prepare.harmonic.api.event.RActivePowerRangeFeignClient;
import com.njcn.prepare.harmonic.pojo.param.RActivePowerRangeParam;
import com.njcn.prepare.harmonic.pojo.po.RActivePowerRangePO;
import com.njcn.prepare.harmonic.utils.PrepareEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * 有功功率趋势熔断降级
 * @author guofeihu
 * @date 2024-08-22
 */
@Slf4j
@Component
public class RActivePowerRangeFeignClientFallbackFactory implements FallbackFactory<RActivePowerRangeFeignClient> {

    @Override
    public RActivePowerRangeFeignClient create(Throwable throwable) {
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException)throwable.getCause();
            exceptionEnum = PrepareEnumUtil.getExceptionEnum(businessException.getResult());
        }

        Enum<?> finalExceptionEnum = exceptionEnum;
        return new RActivePowerRangeFeignClient() {
            @Override
            public HttpResult<List<PowerStatisticsTargetVO>> record(RActivePowerRangeParam rActivePowerRangeParam) {
                log.error("{}异常，降级处理，异常为：{}", "有功功率趋势记录：", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<RActivePowerRangePO> getDataByLineId(String lineId,String startTime,String endTime) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点ID获取有功功率趋势信息：", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
