package com.njcn.user.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.user.api.UserFeignClient;
import com.njcn.user.pojo.dto.UserDTO;
import com.njcn.user.pojo.po.User;
import com.njcn.user.pojo.vo.UserVO;
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 hongawen
 * @version 1.0.0
 * @date 2021年08月24日 10:21
 */
@Slf4j
@Component
public class UserFeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {


    /**
     * 输出远程请求接口异常日志
     * @param cause RPC请求异常
     */
    @Override
    public UserFeignClient 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 UserFeignClient() {
            @Override
            public HttpResult<UserDTO> getUserByName(String loginName) {
                log.error("{}异常，降级处理，异常为：{}","根据登录名查询用户信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<UserDTO> getUserByPhone(String phone) {
                log.error("{}异常，降级处理，异常为：{}","根据手机号查询用户信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> judgeUserStatus(String loginName) {
                log.error("{}异常，降级处理，异常为：{}","认证后根据用户名判断用户状态",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> updateUserLoginErrorTimes(String loginName) {
                log.error("{}异常，降级处理，异常为：{}","更新用户登录认证密码错误次数",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

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

            @Override
            public HttpResult<List<User>> getUserByDeptIds(List<String> deptId) {
                log.error("{}异常，降级处理，异常为：{}","根据部门ids查询接收短信通知的用户信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<User>> getUserInfoByDeptIds(List<String> deptId) {
                log.error("{}异常，降级处理，异常为：{}","根据部门ids查询用户信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getUserIdByRoleId(List<String> roleId) {
                log.error("{}异常，降级处理，异常为：{}","根据角色ids查询用户id",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<User>> getUserListByRoleCode(String roleCode) {
                log.error("{}异常，降级处理，异常为：{}","根据角色Code集合查询用户信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<UserVO> getUserById(String id) {
                log.error("{}异常，降级处理，异常为：{}","根据用户id获取用户详情",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<User>> getUserListByIds(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}","根据用户id集合获取用户集合",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<UserVO>> getUserVOByIdList(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}","根据用户id集合获取用户集合",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getColleaguesIdByUserId(String id) {
                log.error("{}异常，降级处理，异常为：{}","根据用户id获取该用户同部门下所有的所有用户id",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> userRoleList() {
                log.error("{}异常，降级处理，异常为：{}","获取所有用户角色",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };
    }
}
