package com.microservices.system.api.factory;

import com.microservices.common.core.domain.R;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.system.api.RemoteUserService;
import com.microservices.system.api.domain.SysRole;
import com.microservices.system.api.domain.SysUser;
import com.microservices.system.api.domain.SysUserDeptRole;
import com.microservices.system.api.model.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

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

/**
 * 用户服务降级处理
 *
 * @author microservices
 */
@Component
public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserService>
{
    private static final Logger log = LoggerFactory.getLogger(RemoteUserFallbackFactory.class);

    @Override
    public RemoteUserService create(Throwable throwable)
    {
        log.error("用户服务调用失败:{}", throwable.getMessage());
        return new RemoteUserService()
        {
            @Override
            public R<LoginUser> getUserInfo(String username, String source)
            {
                return R.fail("获取用户失败:" + throwable.getMessage());
            }

            @Override
            public R<SysRole> getRoleInfoByInner(Long roleId, String source) {
                return R.fail("获取角色失败:" + throwable.getMessage());
            }

            @Override
            public R<SysUser> getSysUserByUserName(String username, String source) {
                return R.fail("获取用户失败:" + throwable.getMessage());
            }

            @Override
            public R<SysUser> getSysUserByUserId(Long userId, String source) {
                return R.fail("获取用户失败:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> addUserIdentify(Long deptId, Long userId, String roleKey, String source) {
                return R.fail("组织下新增用户身份失败:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> deleteUserIdentify(Long deptId, Long userId, String roleKey, String source) {

                return R.fail("组织下删除用户身份失败:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> registerUserInfo(SysUser sysUser, String source) {
                return R.fail("注册用户失败:" + throwable.getMessage());
            }

            @Override
            public R<String> getSysUserTokenByGitLinkCookie(String cookie, String source) {
                return R.fail("获取用户Token失败:" + throwable.getMessage());
            }

            @Override
            public R<List<SysUserDeptRole>> getSysUserDeptRoleListByUserName(String userName, String source) {
                return R.fail("查询用户下组织及组织对应角色列表失败:" + throwable.getMessage());
            }

            @Override
            public R<SysUserDeptRole> getFirstUserIdentity(String userName, String source) {
                return R.fail("查询用户下第一个身份失败:" + throwable.getMessage());
            }

            @Override
            public R<GenericsTableDataInfo<SysUserDeptRole>> searchUserIdentifierListByDept(Long deptId, String search, Integer pageNum, Integer pageSize, String source) {
                return R.fail("模糊查询组织下身份列表失败:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> checkGitLinkUserLogin(String token, String source) {
                return R.fail("查询GitLink用户登录状态失败:" + throwable.getMessage());
            }

            @Override
            public R<String> getTokenByGitLinkUserId(Long gitLinkUserId, String source) {
                return R.fail("获取GitLink用户Token失败:" + throwable.getMessage());
            }

            @Override
            public R<String> getUserNameByGitLinkToken(String gitLinkToken, String source) {
                return R.fail("通过GitLink用户Token获取系统用户名失败:" + throwable.getMessage());
            }

            @Override
            public R<SysUser> getSysUserByGitLinkUserId(Long gitLinkUserId, String source) {
                return R.fail("通过GitLink用户Id获取系统用户失败:" + throwable.getMessage());
            }

            @Override
            public R<SysUser> createUserByGitLinkUserId(Long gitLinkUserId, String source) {
                return R.fail("通过GitLink用户Id创建用户失败:" + throwable.getMessage());
            }

            @Override
            public R<Set<String>> selectMenuPermissionByRoleKey(String roleKey, String source) {
                return R.fail("根据角色Key获取菜单权限集合失败:" + throwable.getMessage());
            }

            @Override
            public R<SysUserDeptRole> getAllStatusUserIdentityByDeptIdAndUserId(Long deptId, Long userId, String source) {
                return R.fail("根据部门Id和用户Id获取所有状态的用户身份失败:" + throwable.getMessage());
            }
        };
    }
}
