package cn.pojo.vo;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import cn.util.core.IsKit;
import cn.util.core.TypeKit;
import cn.util.string.JsonKit;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginUser implements UserDetails
{	
	private static final long serialVersionUID = 1L;

	//必须， 否则redis报错
	public LoginUser() {}

	/**用户唯一标识*/
	private String token;

	/**登陆时间*/
	private Long loginTime;

	/**过期时间*/
	private Long expireTime;

	/**登录IP地址*/
	private String ipaddr;

	/**登录地点*/
	private String loginLocation;

	/**浏览器类型*/
	private String browser;

	/**操作系统*/
	private String os;

	public String getToken() {
		return token;
	}
	public void setToken(String token) {
		this.token = token;
	}
	public Long getLoginTime() {
		return loginTime;
	}
	public void setLoginTime(Long loginTime) {
		this.loginTime = loginTime;
	}
	public Long getExpireTime() {
		return expireTime;
	}
	public void setExpireTime(Long expireTime) {
		this.expireTime = expireTime;
	}
	public String getIpaddr() {
		return ipaddr;
	}
	public void setIpaddr(String ipaddr) {
		this.ipaddr = ipaddr;
	}
	public String getLoginLocation() {
		return loginLocation;
	}
	public void setLoginLocation(String loginLocation) {
		this.loginLocation = loginLocation;
	}
	public String getBrowser() {
		return browser;
	}
	public void setBrowser(String browser) {
		this.browser = browser;
	}
	public String getOs() {
		return os;
	}
	public void setOs(String os) {
		this.os = os;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities(){
		//return authorities;
		return null;
	}
	/**指定用户是否解锁,锁定的用户无法进行身份验证 */
	@JsonIgnore
	@Override
	public boolean isAccountNonLocked(){
		//return CommonConstant.USER_STATUS_LOCK.equals(this.getStatus()) ? false : true;
		return true;
	}
	/**是否可用 ,禁用的用户不能身份验证*/
	@JsonIgnore
	@Override
	public boolean isEnabled(){
		//return CommonConstant.USER_STATUS_NORMAL.equals(this.getStatus()) ? true : false;
		return true;
	}
	/**指示是否已过期的用户的凭据(密码),过期的凭据防止认证*/
	@JsonIgnore
	@Override
	public boolean isCredentialsNonExpired(){
		return true;
	}
	/**账户是否未过期,过期无法验证 */
	@JsonIgnore
	@Override
	public boolean isAccountNonExpired(){
		return true;
	}
	/**springsecurity要求用户字段名必须为：username; 密码的字段为password*/
	@JsonIgnore
	@Override
	public String getPassword(){
		return TypeKit.objToStr(userMap.get("password"));
	}
	@Override
	public String getUsername(){
		return TypeKit.objToStr(userMap.get("userName"));
	}

	/** 用户信息 */
	/***cn.service.security.auth.UserDetailsServiceImpl调用*/
	public LoginUser(Map<String, Object> userMap)
	{
		this.userMap = userMap;
	}

	public void setUser(Map<String, Object> userMap) {
		this.userMap = userMap;
	}
	/**user所包含信息：
	 * 1）基本信息对应表：t_sys_user
	 * 2）部门信息dept对应表：t_sys_dept
	 * */
	private Map<String, Object> userMap;
	
	/**返回user所包含信息：*/
	public Map<String, Object> getUserMap() {
		if(IsKit.isNull(userMap)) {
			return new HashMap<String, Object>();
		}
		return userMap;
	}

	/**获取用户所在部门信息*/
	@JsonIgnore
	public Map<String, Object> getDept() 
	{
		Map<String, Object> deptMap = new HashMap<>();
		if (IsKit.isNotNull(userMap)){
			deptMap = JsonKit.jsonObjToMap(userMap.get("dept"));
		}
		return deptMap;
	}
	@JsonIgnore
	public String getDeptName()
	{
		Map<String, Object> deptMap = new HashMap<>();
		if (IsKit.isNotNull(userMap)){
			deptMap = JsonKit.jsonObjToMap(userMap.get("dept"));
		}
		return TypeKit.objToStr(deptMap.get("deptName"));
	}

	/**设备id uniapp推送用*/
	private String clientId;

	/**
	 * @return the clientId
	 */
	public String getClientId() {
		return clientId;
	}
	/**
	 * @param clientId the clientId to set
	 */
	public void setClientId(String clientId) {
		this.clientId = clientId;
	}
}
