package com.njcn.supervision.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.supervision.api.UserLedgerOldFeignClient;
import com.njcn.supervision.pojo.param.user.UserReportParam;
import com.njcn.supervision.pojo.vo.user.NewUserReportVO;
import com.njcn.supervision.pojo.vo.user.UserLedgerVO;
import com.njcn.supervision.utils.SupervisionEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;


@Slf4j
@Component
public class UserLedgerOldFeignClientFallbackFactory implements FallbackFactory<UserLedgerOldFeignClient> {
    @Override
    public UserLedgerOldFeignClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = SupervisionEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new UserLedgerOldFeignClient() {

            @Override
            public HttpResult<List<UserLedgerVO>> selectUserList(UserReportParam userReportParam) {
                log.error("{}异常，降级处理，异常为：{}", "查询用户台账", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<UserLedgerVO> selectUserInfo(String id) {
                log.error("{}异常，降级处理，异常为：{}", "查询用户台账详情", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<UserLedgerVO>> bindUserStation(String userId, String stationId) {
                log.error("{}异常，降级处理，异常为：{}", "用户电站信息绑定", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<NewUserReportVO>> getUserReportByIds(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}", "根据ids获取非电网侧用户信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
