package entity

import (
	"context"
	"gorm.io/gorm"
	"yx-dataset-server/app/schema"
)

// GetUserDB 获取User存储
func GetUserDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return getDBWithModel(ctx, defDB, User{})
}

// SchemaUser User对象
type SchemaUser schema.User

// ToUser 转换为User实体
func (a SchemaUser) ToUser() *User {
	item := &User{
		RecordID:      a.RecordID,
		UserName:      a.UserName,
		RealName:      a.RealName,
		OrgId:         a.OrgId,
		RoleId:        a.RoleId,
		Phone:         a.Phone,
		Password:      a.Password,
		Photo:         a.Photo,
		Status:        a.Status,
		CreatorId:     a.CreatorId,
		H5AssistantId: a.H5AssistantId,
	}
	return item
}

// ToSchemaUser 转换为User对象
func (a User) ToSchemaUser() *schema.User {
	item := &schema.User{
		RecordID:      a.RecordID,
		UserName:      a.UserName,
		RealName:      a.RealName,
		OrgId:         a.OrgId,
		RoleId:        a.RoleId,
		Phone:         a.Phone,
		Password:      a.Password,
		Photo:         a.Photo,
		Status:        a.Status,
		CreatedAt:     a.CreatedAt,
		CreatorId:     a.CreatorId,
		H5AssistantId: a.H5AssistantId,
	}
	return item
}

// User User实体
type User struct {
	gorm.Model
	RecordID      string `gorm:"column:record_id;size:32;index;"`
	UserName      string `gorm:"column:user_name;size:20;"`
	RealName      string `gorm:"column:real_name;size:20;"`
	RoleId        string `gorm:"column:role_id;size:32;index;"`
	OrgId         string `gorm:"column:org_id;size:32;index;"`
	Phone         string `gorm:"column:phone;size:20;"`
	Password      string `gorm:"column:password;size:100;"`
	Photo         string `gorm:"column:photo;size:200;"`
	Status        bool   `gorm:"column:status;"`
	CreatorId     string `gorm:"column:creator_id;size:32;"`
	H5AssistantId string `gorm:"column:h5_assistant_id;size:32;"`
}

// Users User列表
type Users []*User

// ToSchemaUsers 转换为User对象列表
func (a Users) ToSchemaUsers() []*schema.User {
	list := make([]*schema.User, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaUser()
	}
	return list
}
