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.pq.api.DeptLineFeignClient;
import com.njcn.device.biz.utils.DeviceEnumUtil;
import com.njcn.device.pq.pojo.po.DeptLine;
import feign.hystrix.FallbackFactory;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * @author denghuajun
 * @date 2022/1/13
 *
 */
@Slf4j
@Component
public class DeptLineFeignClientFallbackFactory implements FallbackFactory<DeptLineFeignClient> {
    @Override
    public DeptLineFeignClient 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 DeptLineFeignClient()
        {

            @Override
            public HttpResult<List<String>> getLineByDeptId(String id) {
                log.error("{}异常，降级处理，异常为：{}", "部门Id获取绑定监测点", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getLineByDeptIdAndNewStation(String id) {
                log.error("{}异常，降级处理，异常为：{}", "根据部门id获取绑定的监测点且再根据NewStation进行过滤", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> selectDeptBindLines(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}", "部门Ids集合查询是否绑定", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Integer> removeBind(String id) {
                log.error("{}异常，降级处理，异常为：{}", "部门解除绑定", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

            @Override
            public HttpResult<DeptLine> getLineByLineIds(String ids) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点id集合查询部门信息id", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

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

            @Override
            public HttpResult<List<DeptLine>> getAllData() {
                log.error("{}异常，降级处理，异常为：{}", "获取部门监测点表所有数据", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
