package com.bonus.system.api.factory;

import com.bonus.common.core.constant.HttpStatus;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.system.api.RemoteRoleService;
import com.bonus.system.api.domain.SysRole;
import com.bonus.system.api.domain.SysUser;
import com.bonus.system.api.domain.SysUserRole;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

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

    @Override
    public RemoteRoleService create(Throwable throwable)
    {
        log.error("用户服务调用失败:{}", throwable.getMessage());
        return new RemoteRoleService()
        {
            @Override
            public TableDataInfo list(SysRole role, String source) {
                return getDataTableError(new ArrayList<SysRole>());
            }

            @Override
            public AjaxResult getInfo(Long roleId, String source) {
                return AjaxResult.error("根据角色id获取角色信息失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult add(SysRole role, String source) {
                return AjaxResult.error("新增角色失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult edit(SysRole role, String source) {
                return AjaxResult.error("修改角色失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult dataScope(SysRole role, String source) {
                return AjaxResult.error("修改角色数据权限失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult changeStatus(SysRole role, String source) {
                return AjaxResult.error("修改角色状态失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult remove(Long[] roleIds, String source) {
                return AjaxResult.error("删除角色失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult optionselect(String source) {
                return AjaxResult.error("获取角色下拉列表失败:" + throwable.getMessage());
            }

            @Override
            public TableDataInfo allocatedList(SysUser user, String source) {
                return getDataTableError(new ArrayList<SysUser>());
            }

            @Override
            public TableDataInfo unallocatedList(SysUser user, String source) {
                return getDataTableError(new ArrayList<SysUser>());
            }

            @Override
            public AjaxResult cancelAuthUser(SysUserRole userRole, String source) {
                return AjaxResult.error("取消授权失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds, String source) {
                return AjaxResult.error("批量取消授权失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds, String source) {
                return AjaxResult.error("批量选择授权失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult deptTree(Long roleId, String source) {
                return AjaxResult.error("获取角色部门树失败:" + throwable.getMessage());
            }

            private TableDataInfo getDataTableError(List<?> list)
            {
                TableDataInfo rspData = new TableDataInfo();
                rspData.setCode(HttpStatus.ERROR);
                rspData.setRows(list);
                rspData.setMsg("系统错误,请联系管理员");
                rspData.setTotal(new PageInfo(list).getTotal());
                return rspData;
            }
//            @Override
//            public R<LoginUser> getUserInfo(String username, String source)
//            {
//                return R.fail("获取用户失败:" + throwable.getMessage());
//            }
//
//            @Override
//            public R<Boolean> registerUserInfo(SysUser sysUser, String source)
//            {
//                return R.fail("注册用户失败:" + throwable.getMessage());
//            }
        };
    }
}
