package models

import (
	"github.com/jinzhu/gorm"
	"bwallet/pkg/setting"
)

type Auth struct {
	Uid        int    `gorm:"primary_key" json:"uid"`
	Username   string `json:"username"`
	Password   string `json:"password"`
	Group      string `json:"group"`
	PlatformId int    `json:"platform_id"`
}

func (a Auth) TableName() string {
	return setting.DatabaseSetting.Name_Manage + ".gli_admin"
}

func CheckAuth(username, password string) (bool, error) {
	var auth Auth
	err := db.Select("uid").Where(Auth{Username: username, Password: password}).First(&auth).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return false, err
	}

	if auth.Uid > 0 {
		return true, nil
	}

	return false, nil
}

func CheckToken(token string) (bool, error) {
	var auth Auth
	err := db.Select("uid").Where("access_token = ?", token).First(&auth).Error
	if err != nil {
		return false, err
	}

	return true, nil
}

func GetUserInfo(token string) (*Auth, error) {
	var auth Auth
	if err := db.Where("access_token = ?", token).First(&auth).Error; err != nil {
		return nil, err
	}

	return &auth, nil
}
