package models

import (
	orm "go-admin/common/global"
	"go-admin/tools"
)

type Login struct {
	Username string `form:"UserName" json:"username" binding:"required"`
	Password string `form:"Password" json:"password" binding:"required"`
	Code     string `form:"Code" json:"code" binding:"required"`
	UUID     string `form:"UUID" json:"uuid" binding:"required"`
	SysType  uint32 `form:"SysType"  json:"sys_type" binding:"required"` // 1-管理系统 2-合作商系统
	Uid      uint32 `json:"uid"`
}

func (u *Login) GetUser() (user SysUser, role SysRole, e error) {
	if u.Uid != 0 {
		e = orm.Eloquent.Table("sys_user").Where("uid = ? ", u.Uid).Find(&user).Error
		if e != nil {
			return
		}
	} else {
		e = orm.Eloquent.Table("sys_user").Where("username = ? ", u.Username).Find(&user).Error
		if e != nil {
			return
		}

		_, e = tools.CompareHashAndPassword(user.Password, u.Password)
		if e != nil {
			return
		}
	}

	e = orm.Eloquent.Table("sys_role").Where("role_id = ? ", user.RoleId).First(&role).Error
	if e != nil {
		return
	}
	return
}
