package com.njcn.device.pms.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.device.pms.api.PmsGeneralDeviceInfoClient;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.param.PwPmsMonitorParam;
import com.njcn.device.pms.utils.PmsDeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年10月18日 10:09
 */
@Slf4j
@Component
public class PmsGeneralDeviceInfoClientFallbackFactory implements FallbackFactory<PmsGeneralDeviceInfoClient> {


    @Override
    public PmsGeneralDeviceInfoClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = PmsDeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new PmsGeneralDeviceInfoClient()
        {
            @Override
            public HttpResult<List<PmsGeneralDeviceDTO>> getPmsDeviceInfoWithInOrg(PmsDeviceInfoParam pmsDeviceInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取指定组织下台账综合信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PmsGeneralDeviceDTO>> getPwPmsDeviceInfoWithOrgId(PmsDeviceInfoParam pmsDeviceInfoParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取指定组织下配网台账综合信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getPwPmsMonitorIds(PwPmsMonitorParam pwPmsMonitorParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取指定组织下配网监测点索引", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
