package model

import (
	"gorm.io/gorm"
	"time"
)

type User struct {
	ID        uint   `gorm:"primaryKey" json:"id"`
	Uuid      string `json:"uuid" gorm:"index"`
	UserId    string `json:"user_id"`
	Email     string `json:"email" gorm:"index"`
	Username  string `json:"username" gorm:"index"`
	LinuxName string `json:"linux_name" gorm:"index"`
	Phone     string `json:"phone" gorm:"index"`
	//RoleId   string `json:"role_id"`
	//Role             Role        `json:"role" gorm:"foreignkey:RoleId;references:RoleId"`
	Role             []*Role       `json:"role" gorm:"many2many:user_role;foreignkey:UserId;references:RoleId"`
	DepartmentId     string        `json:"department_id"`
	Department       Departments   `json:"department" gorm:"foreignkey:DepartmentId;references:DepartmentId"`
	Departments      []Departments `json:"departments" gorm:"many2many:user_department_relations;foreignkey:UserId;references:DepartmentId"`
	PositionId       string        `json:"position_id"`
	Position         Position      `json:"position" gorm:"foreignkey:PositionId;references:PositionId"`
	LastLoginAt      string        `json:"last_login_at"`
	LastLoginIp      string        `json:"last_login_ip"`
	LastLoginAddress string        `json:"last_login_address"`
	Password         string        `json:"-"`
	Gender           string        `json:"gender"`
	Active           bool          `json:"active"`
	IsManager        bool          `json:"is_manager" gorm:"default:false"`
	IsRestrict       bool          `json:"is_restrict" gorm:"default:false"`
	IpRange          string        `json:"ip_range"`
	// 补充字段
	Company        string         `json:"company" gorm:"comment:归属分公司"`
	DefaultShell   string         `json:"default_shell" gorm:"用户默认shell"`
	Home           string         `json:"home" gorm:"comment:用户home目录路径"`
	Uid            string         `json:"uid" gorm:"comment:用户在Nis中的uid"`
	Salt           string         `json:"-" gorm:"comment:密码盐"`
	Iv             string         `json:"-"`
	SosManager     bool           `json:"sos_manager" gorm:"comment:为true,默认加载到sos配置文件的admin中"`
	ProjectManager bool           `json:"project_manager" gorm:"comment:为true,能查看所有项目"`
	CompanyManager bool           `json:"company_manager" gorm:"comment:为true，可管理用户,用户组"`
	Groups         []*Group       `json:"groups" gorm:"many2many:user_group;"`
	DN             string         `json:"dn"`
	CreatedAt      time.Time      `json:"created_at"`
	UpdatedAt      time.Time      `json:"-"`
	DeletedAt      gorm.DeletedAt `gorm:"index" json:"-"`
}

func (User) TableName() string {
	return "users"
}

type LoginRecord struct {
	ID        uint           `gorm:"primaryKey" json:"id"`
	Uuid      string         `json:"uuid" gorm:"index"`
	Username  string         `json:"username"`
	IP        string         `json:"ip"`
	Address   string         `json:"address"`
	Msg       string         `json:"msg"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"-"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}

func (LoginRecord) TableName() string {
	return "login_Record"
}

//
// Group
/*  @Description: 根据项目，将项目内成员分组，如TP0001S  */
type Group struct {
	ID        uint           `gorm:"primaryKey" json:"id"`
	Name      string         `json:"name" gorm:"comment:组名"`
	Users     []*User        `json:"users" gorm:"many2many:user_group;"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"-"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}

func (Group) TableName() string {
	return "group"
}
