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.param.StatisticsBizBaseParam;
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.pojo.param.MonitorGetParam;
import com.njcn.device.biz.pojo.param.SubstationParam;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.biz.pojo.po.PqsDeviceUnit;
import com.njcn.device.biz.utils.DeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

/**
 * 公共台账
 * @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<MonitorCommLedgerInfoDTO>> deptGetLineInfo(DeptGetLineParam deptGetLineParam) {
                log.error("{}异常，降级处理，异常为：{}", "根据部门获取监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<DeptGetChildrenMoreDTO>> deptGetAllLine(DeptGetLineParam deptGetLineParam) {
                log.error("{}异常，降级处理，异常为：{}", "根据单位获取all监测点信息", 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<DeptGetSubStationDTO.Info>> deptGetSubStationInfo(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<List<DeptGetDeviceDTO>> deptGetDeviceAndMonitor(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<Overlimit> getOverLimitData(String id) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点限值", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Overlimit>> getAllLineOverLimit() {
                log.error("{}异常，降级处理，异常为：{}", "获取所有监测点限值", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Overlimit>> getOverLimitDataByIds(@RequestBody List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}", "通过监测点集合获取监测点限值", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Map<String, List<String>>> getLineBySubstationRelation(Integer devDataType) {
                log.error("{}异常，降级处理，异常为：{}", "获取变电站下监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getRunMonitorIds() {
                log.error("{}异常，降级处理，异常为：{}", "获取投运监测点索引", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineDevGetDTO>> getMonitorDetailList(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点详细信息集合", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<PqsDeviceUnit> lineUnitDetail(String lineId) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点获取测点数据单位", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<LineDevGetDTO> getMonitorDetail(String lineId) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点详细信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<StatisticsMonitor>> getStatisticsMonitor(StatisticsBizBaseParam param) {
                log.error("{}异常，降级处理，异常为：{}", "获取专项分析台账统计", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

            @Override
            public HttpResult<List<BusBarAndHisMonitorDTO>> getBusBarAndHisMonitor() {
                log.error("{}异常，降级处理，异常为：{}", "pms获取指定单位下面的母线以及母线下面的监测点信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CommMonitorInfoDTO>> tagOrIdGetMonitorList(MonitorGetParam monitorGetParam) {
                log.error("{}异常，降级处理，异常为：{}", "根据对象标签，监测点，获取主网监测点", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<SubGetBase>> tagOrIdGetSub(SubstationParam substationParam) {
                log.error("{}异常，降级处理，异常为：{}", "根据获取变电站信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }


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

        };
    }
}

