package com.pj.project.sys_user_acc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pj.current.satoken.StpUserUtil;
import com.pj.project.sys_user.SysUserUtil;
import com.pj.utils.sg.NbUtil;

/**
 * sys_user登录相关工具类
 *
 * @author : kong
 * @date : 2020-06-29 14:06
 */
@Component
public class SysUserAccUtil {

    static SysUserAccMapper sysUserAccMapper;
    @Autowired
    public void setSysUserAccMapper(SysUserAccMapper sysUserAccMapper) {
        SysUserAccUtil.sysUserAccMapper = sysUserAccMapper;
    }

    /**
     * 返回当前登录者的信息
     */
    public static SysUserAcc getCurrSysUserAcc() {
        long userId = StpUserUtil.getLoginIdAsLong();
        return sysUserAccMapper.getById(userId);
    }

    /**
     * 根据id查询用户 
     * @param id
     * @return
     */
    public static Object getUserById(long id) {
        return SysUserUtil.getById(id);	
    }

    // 返回一个可用的随机username 
 	public static String getKyUsername(){
 		return getKyUsername(null);
 	}
 	public static String getKyUsername(String value){
 		String new_value = value;
 		if(new_value == null) {
 			new_value = "r_" + NbUtil.getRandomString(8);
 		}
 		for (int i = 2; ; i++) {
 			if(isKyUsername(new_value)) {
 				return new_value;
 			}
 			new_value = value + "__" + i;
 		}
 	}
 	
 	// 返回一个昵称是否可用 
 	public static boolean isKyUsername(String username) {
 		return sysUserAccMapper.getByUsername(username) == null;
 	}
}
