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.domain.RoleParams;
import com.bonus.system.api.domain.SysDept;
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 com.bonus.common.core.domain.R;
import com.bonus.system.api.RemoteUserService;
import com.bonus.system.api.domain.SysUser;
import com.bonus.system.api.model.LoginUser;

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

/**
 * 用户服务降级处理
 * 
 * @author bonus
 */
@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 TableDataInfo list(SysUser user, String source) {
                return getDataTableError(new ArrayList<SysUser>());
            }

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

            /**
             * 通过用户名查询用户信息
             *
             * @param phone  手机号
             * @param source 请求来源
             * @return 结果
             */
            @Override
            public R<LoginUser> getUserInfoByPhone(String phone, String source) {
                return R.fail("获取用户失败:" + throwable.getMessage());
            }

            /**
             * 通过用户名查询用户信息
             *
             * @param email  邮箱
             * @param source 请求来源
             * @return 结果
             */
            @Override
            public R<LoginUser> getUserInfoByEmail(String email, String source) {
                return R.fail("获取用户失败:" + throwable.getMessage());
            }

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

            @Override
            public AjaxResult getInfo(String source) {
                return AjaxResult.error("获取当前登录用户权限内的角色和权限集合失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult getInfo(Long userId, String source) {
                return AjaxResult.error("根据用户编号获取详细信息,用户权限内的角色和权限集合失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult getUsers(Long[] userIds, String source) {
                return AjaxResult.error("根据用户编号列表获取用户列表,用户权限内的角色和权限集合失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult add(SysUser user, String source) {
                return AjaxResult.error("新增用户失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult edit(SysUser user, String source) {
                return AjaxResult.error("修改用户失败:" + throwable.getMessage());
            }

            /**
             * 修改用户
             *
             * @param user   修改用户的用户信息
             * @param source 请求来源
             * @return 修改用户影响的行数或错误信息
             */
            @Override
            public AjaxResult systemUpdateUser(SysUser user, String source) {
                return AjaxResult.error("修改用户失败:" + throwable.getMessage());
            }

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

            @Override
            public AjaxResult changeStatus(SysUser user, String source) {
                return AjaxResult.error("修改用户状态失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult authRole(Long userId, String source) {
                return AjaxResult.error("根据用户编号获取授权角色信息失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult insertAuthRole(Long userId, Long[] roleIds, String source) {
                return AjaxResult.error("给定用户id给用户授权角色失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult insertRole(RoleParams roleParams, String source) {
                return AjaxResult.error("给定用户id给用户授权角色失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult deptTree(SysDept dept, String source) {
                return AjaxResult.error("获取部门权限下的部门树列表:" + throwable.getMessage());
            }

            @Override
            public AjaxResult deptUserTree(SysUser sysUser, String source) {
                return AjaxResult.error("查询部门人员树列表:" + throwable.getMessage());
            }

            @Override
            public AjaxResult approvalStatus(SysUser user, 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;
            }
        };
    }
}
