package com.njcn.user.api.fallback;

import cn.hutool.core.lang.tree.Tree;
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.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.user.pojo.po.Dept;
import com.njcn.user.pojo.vo.PvTerminalTreeVO;
import com.njcn.user.utils.UserEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author denghuajun
 * @version 1.0.0
 * @date 2022年01月11日 10:21
 */
@Slf4j
@Component
public class DeptFeignClientFallbackFactory implements FallbackFactory<DeptFeignClient> {


    /**
     * 输出远程请求接口异常日志
     *
     * @param cause RPC请求异常
     */
    @Override
    public DeptFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (cause.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) cause.getCause();
            exceptionEnum = UserEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new DeptFeignClient() {

            @Override
            public HttpResult<List<DeptDTO>> getDeptDescendantIndexes(String id, List<Integer> type) {
                log.error("{}异常，降级处理，异常为：{}", "根据条件获取后代部门索引", cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> getDeptIdByArea(String area) {
                log.error("{}异常，降级处理，异常为：{}", "根据区域索引获取部门索引", cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> getAreaIdByDeptId(String deptId) {
                log.error("{}异常，降级处理，异常为：{}", "根据部门索引获取区域索引", cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<PvTerminalTreeVO>> allDeptList() {
                log.error("{}异常，降级处理，异常为：{}","查询所有部门异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Dept> getDeptById(String deptId) {
                log.error("{}异常，降级处理，异常为：{}","查询部门信息异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Dept> getDeptByCode(String deptCode) {
                log.error("{}异常，降级处理，异常为：{}","查询部门信息异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getDepSonIdtByDeptId(String deptId) {
                log.error("{}异常，降级处理，异常为：{}","查询所有子孙部门异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getDepSonSelfCodetByDeptId(String deptId) {
                log.error("{}异常，降级处理，异常为：{}","查询所有子孙部门异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getDepSonSelfCodetByCode(String code) {
                log.error("{}异常，降级处理，异常为：{}","查询所有子孙部门异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getDepSonIdByDeptId(String deptId) {
                log.error("{}异常，降级处理，异常为：{}","查询所有子孙部门异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Dept>> getDirectSonSelf(String deptId) {
                log.error("{}异常，降级处理，异常为：{}","查询所有子部门以及自身异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Dept>> getSpecialDeptList() {
                log.error("{}异常，降级处理，异常为：{}","查询所有本部异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Dept> getRootDept() {
                log.error("{}异常，降级处理，异常为：{}","查询根部门异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
            @Override
            public HttpResult<List<DeptDTO>> getDepSonDetailByDeptId(String deptId) {
                log.error("{}异常，降级处理，异常为：{}","查询前部门的的下级子部门list，(如果没子部门就是返回当前部门)",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getUpByDeptIds(String deptId) {
                log.error("{}异常，降级处理，异常为：{}","根据部门id查询当前部门所有父级部门id",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Dept>> getDeptByCodeList(List<String> list) {
                log.error("{}异常，降级处理，异常为：{}","根据单位code集合查询对应部门:",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Dept>> getDeptInfoListByIds(List<String> deptIds) {
                log.error("{}异常，降级处理，异常为：{}","根据部门ids获取部门集合:",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Dept>> getAllDept() {
                log.error("{}异常，降级处理，异常为：{}","获取所有单位:",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Tree<String>>> orgTreeSelector() {
                log.error("{}异常，降级处理，异常为：{}","获取所有单位树:",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}

