package com.njcn.device.biz.commApi.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.biz.commApi.CommLedgerDeptClient;
import com.njcn.device.biz.utils.DeviceEnumUtil;
import com.njcn.user.pojo.po.Dept;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 公共
 * @author cdf
 * @date 2023/5/17
 */
@Slf4j
@Component
public class CommLedgerDeptClientFallbackFactory implements FallbackFactory<CommLedgerDeptClient> {
    @Override
    public CommLedgerDeptClient 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 CommLedgerDeptClient() {
            @Override
            public HttpResult<Object> update(Dept dept) {
                log.error("{}异常，降级处理，异常为：{}", "通过部门获取所有子级部门详情", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Dept>> existMonitorDeptTree() {
                log.error("{}异常，降级处理，异常为：{}", "获取存在投运监测点的部门id集合", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }


        };
    }
}

