package models

import (
	"time"

	coreTypes "git.apinb.com/bsm-sdk/core/types"
)

// 用户模型
type MgtUser struct {
	coreTypes.Std_IICUDS                  // 标准字段
	Name                 string           `gorm:"column:name;type:varchar(255);" json:"name,omitempty"`                  // 姓名
	Account              string           `gorm:"column:account;type:varchar(255);uniqueIndex" json:"account,omitempty"` // 用户名
	Phone                string           `gorm:"column:phone;type:varchar(255);" json:"phone,omitempty"`                // 手机号
	Password             string           `gorm:"column:password;type:varchar(255);" json:"password,omitempty"`          // 密码
	Email                string           `gorm:"column:email;type:varchar(255);" json:"email,omitempty"`                // 邮箱
	Avatar               string           `gorm:"column:avatar;type:varchar(255);" json:"avatar,omitempty"`              // 头像
	Salt                 string           `gorm:"column:salt;type:varchar(255);" json:"salt,omitempty"`                  // 密码盐
	LastLoginAt          *time.Time       `gorm:"column:last_login_at;" json:"last_login_at,omitempty"`                  // 最后登录时间
	Departments          []MgtDepartment  `gorm:"many2many:mgt_link_user_dpt;foreignKey:ID;joinForeignKey:user_id;References:ID;joinReferences:dpt_id" json:"departments,omitempty"`
	Roles                []MgtRole        `gorm:"many2many:mgt_link_user_role;foreignKey:ID;joinForeignKey:user_id;References:ID;joinReferences:role_id" json:"roles,omitempty"`
	Application          []MgtApplication `gorm:"many2many:mgt_link_user_app;foreignKey:ID;joinForeignKey:user_id;References:ID;joinReferences:app_id" json:"application,omitempty"`     // 用户关联的角色
	PmnApplication       []MgtApplication `gorm:"many2many:mgt_link_user_pmn;foreignKey:ID;joinForeignKey:user_id;References:ID;joinReferences:app_id" json:"pmn_application,omitempty"` // 用户关联的角色
	Permissions          []MgtPermission  `gorm:"many2many:mgt_link_user_pmn;foreignKey:ID;joinForeignKey:user_id;References:ID;joinReferences:pmn_id" json:"permissions,omitempty"`     // 用户关联的权限
}

//	func init() {
//		database.AppendMigrate(&User{})
//	}

func UpdateLastLoginDate(id uint) error {
	now := time.Now()
	err := DBService.Model(&MgtUser{}).Where("id = ?", id).Update("last_login_at", now).Error
	if err != nil {
		return err
	}
	return nil
}
