package com.njcn.device.pq.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.biz.pojo.dto.PollutionLineDTO;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.pq.pojo.dto.*;
import com.njcn.device.pq.pojo.param.DataParam;
import com.njcn.device.pq.pojo.param.LineBaseQueryParam;
import com.njcn.device.pq.pojo.param.OnlineRateParam;
import com.njcn.device.pq.pojo.po.Line;
import com.njcn.device.pq.pojo.po.LineDetail;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.pq.pojo.po.line.LineInfoVO;
import com.njcn.device.pq.pojo.vo.*;
import com.njcn.device.biz.utils.DeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

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

/**
 * @author denghuajun
 * @date 2022/2/28
 */
@Slf4j
@Component
public class LineFeignClientFallbackFactory implements FallbackFactory<LineFeignClient> {
    @Override
    public LineFeignClient 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 LineFeignClient() {

            @Override
            public HttpResult<CommunicateVO> getComFlagInfoData(String id, String searchBeginTime, String searchEndTime) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点Id获取终端状态统计", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> getLineIdByDevId(String id) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点Id获取终端id", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Overlimit> getOverLimitData(String id) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点Id获取限值  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<LineDetailDataVO> getLineDetailData(String id) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点Id获取监测点详情  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<DeviceVO> getDeviceDetailData(String id) {
                log.error("{}异常，降级处理，异常为：{}", "根据装置Id获取装置详情  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<LineOverLimitVO> getLineOverLimitData(String id) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点Id获取监测点越限  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

            @Override
            public HttpResult<List<SubstationDetailVO>> getSubstationData(List<String> id) {
                log.error("{}异常，降级处理，异常为：{}", "获取变电站信息  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Line>> getSubIndexLineDetail(String id) {
                log.error("{}异常，降级处理，异常为：{}", "获取所有信息  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineDeviceStateVO>> getAllLine(List<String> id) {
                log.error("{}异常，降级处理，异常为：{}", "获取终端信息  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Integer> getDeviceState(String id) {
                log.error("{}异常，降级处理，异常为：{}", "获取终端状态  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<PollutionSubstationDTO> getSubstationInfo(String id) {
                log.error("{}异常，降级处理，异常为：{}", "变电站信息  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PollutionLineDTO>> getLineInfo(PollutionParamDTO pollutionParamDTO) {
                log.error("{}异常，降级处理，异常为：{}", "监测点信息  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

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

            @Override
            public HttpResult<List<WarningSubstationDTO>> getWarningSub(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}", "变电站集合为  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<OverLimitLineDTO>> getOverLimitLineInfo(PollutionParamDTO pollutionParamDTO) {
                log.error("{}异常，降级处理，异常为：{}", "监测点集合为  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getLineList() {
                log.error("{}异常，降级处理，异常为：{}", "监测点集合为  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Overlimit>> getOverLimitByList(PollutionParamDTO pollutionParamDTO) {
                log.error("{}异常，降级处理，异常为：{}", "监测点集合为  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

            @Override
            public HttpResult<List<AreaLineInfoVO>> getRichLineInfo(LineBaseQueryParam lineBaseQueryParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点详情异常  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

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

            @Override
            public HttpResult<List<String>> getDeviceList() {
                log.error("{}异常，降级处理，异常为：{}", "获取装置Id集合:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

            @Override
            public HttpResult<Integer> getOnLineCount(List<String> lineIds) {
                log.error("{}异常，降级处理，异常为：{}", "获取在线监测点数量异常:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getOnOrUnLine( LineBaseQueryParam lineBaseQueryParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取当前状态在线和离线的监测点异常:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }


            @Override
            public HttpResult<List<OnlineLineDTO>> getOnLineDevLine(List<String> lineIds) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点设备id异常:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Map<String,List<String>>> getLineBySubstationRelation(@RequestParam("devDataType") Integer devDataType) {
                log.error("{}异常，降级处理，异常为：{}", "获取变电站和监测点的关系（分稳态暂态）:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineDetailDataVO>> getLineDetailList(@RequestBody List<String> lineIds) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点基础详情异常:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Integer> getDeviceCountBySubstation(@RequestParam("subIndex") String subIndex){
                log.error("{}异常，降级处理，异常为：{}", "获取变电站下终端数:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Integer> getLineCountBySubstation(@RequestParam("subIndex") String subIndex) {
                log.error("{}异常，降级处理，异常为：{}", "获取变电站下监测点数:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<RStatIntegrityVO>> getOnIntegrityByIds(OnlineRateParam param) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点数据完整率（谐波专用）:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<RStatIntegrityVO>> getOnIntegrityByIdsAndTime(LineBaseQueryParam param) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点数据完整率:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineDetailVO.Detail>> getLineDetailByIds(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点id获取监测点集合:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public void updateDeviceRunFlag(String deviceId, Integer status) {
                log.error("{}异常，降级处理，异常为：{}", "更新监测点运行状态:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public void updateLineRunFlag(String lineId, Integer status) {
                log.error("{}异常，降级处理，异常为：{}", "更新监测点运行状态:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineDetailVO.Detail>> getDeptDeviceDetailData(DataParam param) {
                log.error("{}异常，降级处理，异常为：{}", "更新监测点运行状态:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineDetailVO.noDataLineInfo>> getNoDataLine() {
                log.error("{}异常，降级处理，异常为：{}", "获取当日无数据的监测点:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<ReportLineInfoVo>> getReportLineInfo(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点id获取预告警单基础信息:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineDetail>> getByName(LineBaseQueryParam param) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点名称和电网侧变电站名称模糊搜索:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getLevelMonitorIdByDept(String level, String deptId) {
                log.error("{}异常，降级处理，异常为：{}", "根据部门查询重要测点id:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineDetail>> getMonitorByObjId(String objId) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测对象id查测点集合信息:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PollutionSubstationDTO>> getPowerStationByMonitorIds(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测id集合查询场站用户信息:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Set<String>> getDevListByLineIdList(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点id集合获取装置id集合:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
