package models

import (
	"errors"
	"time"
	"zhenyihuibao/conf"
	"zhenyihuibao/pkg/logging"
)

var(
	UserStatusOn 	= "normal"
	UserStatusOff 	= "hidden"
)

//用户表
type FaUser struct {
	Id        		int   		`xorm:"not null pk autoincr INT(10)" json:"id"`
	//GroupId			int			`xorm:"not null comment('用户组ID') INT(11)" json:"groupId"`
	SchoolNumber	string		`xorm:"not null comment('学号') index VARCHAR(255)" json:"schoolNumber"`
	Username		string		`xorm:"not null comment('登录名称') index VARCHAR(20)" json:"username"`
	Nickname		string		`xorm:"not null comment('用户昵称') VARCHAR(20)" json:"nickname"`
	//Password		string		`xorm:"not null comment('密码') VARCHAR(255)"`
	//Salt			string		`xorm:"not null comment('盐') index VARCHAR(20)"`
	Email			string		`xorm:"not null comment('email') VARCHAR(255)" json:"email"`
	Mobile			string		`xorm:"not null comment('mobile') index VARCHAR(255)" json:"mobile"`
	Avatar			string		`xorm:"not null comment('头像') VARCHAR(255)" json:"avatar"`
	//Level			int			`xorm:"not null default 0 comment('等级') INT(11)"`
	Gender			int			`xorm:"not null default 0 comment('性别') TINYINT(1)" json:"gender"`
	Status			string		`xorm:"not null comment('状态') VARCHAR(30)" json:"status"`
	//birthday		int		`json:group_id`
	//bio		int		`json:group_id`
	//money		int		`json:group_id`
	//score		int		`json:group_id`
	//successions		int		`json:group_id`
	//maxsuccessions		int		`json:group_id`
	//prevtime		int		`json:group_id`
	//logintime		int		`json:group_id`
	//loginip		int		`json:group_id`
	//loginfailure		int		`json:group_id`
	//joinip		int		`json:group_id`
	//jointime		int		`json:group_id`
	//createtime		int		`json:group_id`
	//updatetime		int		`json:group_id`
	//token		int		`json:group_id`

	//verification		int		`json:group_id`
}

func (this *FaUser) GetUserById(userId int) (user *FaUser, err error){
	user = &FaUser{Id: userId}
	has, err := conf.SqlServer.Get(user)
	if err != nil || has == false {
		err =  errors.New("通过userId无法找到该用户!")
		return
	}
	return
}

func (this *FaUser) GetUserByToken(token string) (user *FaUser, err error){
	userToken := &FaUserToken{Token:token}
	has, err := conf.SqlServer.Get(userToken)
	if err != nil || has == false {
		err =  errors.New("找不到令牌!")
		return
	}
	user = &FaUser{Id: userToken.UserId}
	has, err = conf.SqlServer.Get(user)
	if err != nil || has == false {
		err =  errors.New("通过userId无法找到该用户!")
		return
	}
	return
}

func (this *FaUser) UpdateProfile(id int) ( err error){
	affected, err := conf.SqlServer.Where("id = ?", id).Update(this)
	if err != nil {
		logging.MyLogger.Infof("更改用户影响行数", affected)
		return
	}
	return
}

func (this *FaUser) CheckIfExistsMobile(id int, mobile string) (res bool, err error){
	faUser := &FaUser{Mobile:mobile}
	has, err := conf.SqlServer.Where("id != ?", id).Get(faUser)
	if err != nil{
		return
	}
	res = has
	return
}

func (this *FaUser) CheckIfExistsEmail(id int, email string) (res bool, err error){
	faUser := &FaUser{Email:email}
	has, err := conf.SqlServer.Where("id != ?", id).Get(faUser)
	if err != nil{
		return
	}
	res = has
	return
}

type FaUserToken struct {
	Token			string		`xorm:"not null comment('Token') index VARCHAR(255)"`
	UserId			int			`xorm:"not null comment('用户组ID') INT(11)"`
	Createtime      time.Time 	`xorm:"created not null default 'CURRENT_TIMESTAMP' TIMESTAMP" json:"createTime,omitempty"`
	Expiretime      int64 		`xorm:"updated not null default 'CURRENT_TIMESTAMP' TIMESTAMP" json:"expiretime,omitempty"`
}

func (this *FaUserToken) GetTokenByToken(token string) (utoken *FaUserToken, err error){
	userToken := &FaUserToken{Token:token}
	has, err := conf.SqlServer.Get(userToken)
	if err != nil || has == false {
		err =  errors.New("找不到该令牌!")
		return
	}
	utoken = userToken
	return
}


