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.CommTerminalGeneralClient;
import com.njcn.device.biz.pojo.dto.*;
import com.njcn.device.biz.pojo.param.DeptGetLineParam;
import com.njcn.device.biz.utils.DeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 公共台账
 * @author cdf
 * @date 2022/9/19
 */
@Slf4j
@Component
public class CommTerminalGeneralClientFallbackFactory implements FallbackFactory<CommTerminalGeneralClient> {
    @Override
    public CommTerminalGeneralClient 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 CommTerminalGeneralClient() {
            @Override
            public HttpResult<List<DeptGetBase>> getDeptChildrenByParent(DeptGetLineParam deptGetLineParam) {
                log.error("{}异常，降级处理，异常为：{}", "通过部门获取所有子级部门详情", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DeptGetChildrenMoreDTO>> deptGetLine(DeptGetLineParam deptGetLineParam) {
                log.error("{}异常，降级处理，异常为：{}", "根据单位获取监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DeptGetSubStationDTO>> deptSubStation(DeptGetLineParam deptGetLineParam) {
                log.error("{}异常，降级处理，异常为：{}", "根据单位获取所有变电站", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DeptGetBusBarDTO>> deptBusBar(DeptGetLineParam deptGetLineParam) {
                log.error("{}异常，降级处理，异常为：{}", "根据单位获取所有变电站", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DeptGetDeviceDTO>> deptGetDevice(DeptGetLineParam deptGetLineParam) {
                log.error("{}异常，降级处理，异常为：{}", "根据单位获取所有装置", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<LineDevGetBandDTO> substationGetLine(String substationId) {
                log.error("{}异常，降级处理，异常为：{}", "根据单站id获取监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineDevGetDTO>> busBarGetLine(String busBarId) {
                log.error("{}异常，降级处理，异常为：{}", "根据母线id获取监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DeptGetChildrenDTO>> deptGetLineList(DeptGetLineParam deptGetLineParam) {
                log.error("{}异常，降级处理，异常为：{}", "通过部门获取所有子集部门所拥有的监测点", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> isPqOrPms() {
                log.error("{}异常，降级处理，异常为：{}", "返回pq还是pms系统", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };
    }
}

