// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/loginrecord"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/user"
	"git.noahlan.cn/noahlan/ntool-biz/core/orm/nent/types"
)

// 系统用户表
type User struct {
	config `json:"-"`
	// ID of the ent.
	// Primary Key | 主键
	ID int64 `json:"id,omitempty"`
	// Status | 状态
	Status types.Status `json:"status,omitempty"`
	// Create Time | 创建时间
	CreatedAt time.Time `json:"created_at,omitempty"`
	// Update Time | 更新时间
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// optimistic lock | 乐观锁
	Version int64 `json:"version,omitempty"`
	// User's login name | 登录名
	Username string `json:"username,omitempty"`
	// Phone number | 手机号
	PhoneNumber *string `json:"phone_number,omitempty"`
	// Email | 邮箱号
	Email *string `json:"email,omitempty"`
	// Password | 密码
	Password string `json:"password,omitempty"`
	// Nickname | 昵称
	Nickname *string `json:"nickname,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the UserQuery when eager-loading is set.
	Edges        UserEdges `json:"edges"`
	selectValues sql.SelectValues
}

// UserEdges holds the relations/edges for other nodes in the graph.
type UserEdges struct {
	// Metas holds the value of the metas edge.
	Metas []*UserMeta `json:"metas,omitempty"`
	// Roles holds the value of the roles edge.
	Roles []*Role `json:"roles,omitempty"`
	// Socials holds the value of the socials edge.
	Socials []*UserSocial `json:"socials,omitempty"`
	// Departments holds the value of the departments edge.
	Departments []*Department `json:"departments,omitempty"`
	// Token holds the value of the token edge.
	Token []*Token `json:"token,omitempty"`
	// LoginRecord holds the value of the loginRecord edge.
	LoginRecord *LoginRecord `json:"loginRecord,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [6]bool
}

// MetasOrErr returns the Metas value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) MetasOrErr() ([]*UserMeta, error) {
	if e.loadedTypes[0] {
		return e.Metas, nil
	}
	return nil, &NotLoadedError{edge: "metas"}
}

// RolesOrErr returns the Roles value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) RolesOrErr() ([]*Role, error) {
	if e.loadedTypes[1] {
		return e.Roles, nil
	}
	return nil, &NotLoadedError{edge: "roles"}
}

// SocialsOrErr returns the Socials value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) SocialsOrErr() ([]*UserSocial, error) {
	if e.loadedTypes[2] {
		return e.Socials, nil
	}
	return nil, &NotLoadedError{edge: "socials"}
}

// DepartmentsOrErr returns the Departments value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) DepartmentsOrErr() ([]*Department, error) {
	if e.loadedTypes[3] {
		return e.Departments, nil
	}
	return nil, &NotLoadedError{edge: "departments"}
}

// TokenOrErr returns the Token value or an error if the edge
// was not loaded in eager-loading.
func (e UserEdges) TokenOrErr() ([]*Token, error) {
	if e.loadedTypes[4] {
		return e.Token, nil
	}
	return nil, &NotLoadedError{edge: "token"}
}

// LoginRecordOrErr returns the LoginRecord value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e UserEdges) LoginRecordOrErr() (*LoginRecord, error) {
	if e.loadedTypes[5] {
		if e.LoginRecord == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: loginrecord.Label}
		}
		return e.LoginRecord, nil
	}
	return nil, &NotLoadedError{edge: "loginRecord"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*User) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case user.FieldID, user.FieldVersion:
			values[i] = new(sql.NullInt64)
		case user.FieldStatus, user.FieldUsername, user.FieldPhoneNumber, user.FieldEmail, user.FieldPassword, user.FieldNickname:
			values[i] = new(sql.NullString)
		case user.FieldCreatedAt, user.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the User fields.
func (u *User) assignValues(columns []string, values []any) error {
	if m, n := len(values), len(columns); m < n {
		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
	}
	for i := range columns {
		switch columns[i] {
		case user.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			u.ID = int64(value.Int64)
		case user.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				u.Status = types.Status(value.String)
			}
		case user.FieldCreatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field created_at", values[i])
			} else if value.Valid {
				u.CreatedAt = value.Time
			}
		case user.FieldUpdatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field updated_at", values[i])
			} else if value.Valid {
				u.UpdatedAt = value.Time
			}
		case user.FieldVersion:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field version", values[i])
			} else if value.Valid {
				u.Version = value.Int64
			}
		case user.FieldUsername:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field username", values[i])
			} else if value.Valid {
				u.Username = value.String
			}
		case user.FieldPhoneNumber:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field phone_number", values[i])
			} else if value.Valid {
				u.PhoneNumber = new(string)
				*u.PhoneNumber = value.String
			}
		case user.FieldEmail:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field email", values[i])
			} else if value.Valid {
				u.Email = new(string)
				*u.Email = value.String
			}
		case user.FieldPassword:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field password", values[i])
			} else if value.Valid {
				u.Password = value.String
			}
		case user.FieldNickname:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field nickname", values[i])
			} else if value.Valid {
				u.Nickname = new(string)
				*u.Nickname = value.String
			}
		default:
			u.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

// Value returns the ent.Value that was dynamically selected and assigned to the User.
// This includes values selected through modifiers, order, etc.
func (u *User) Value(name string) (ent.Value, error) {
	return u.selectValues.Get(name)
}

// QueryMetas queries the "metas" edge of the User entity.
func (u *User) QueryMetas() *UserMetaQuery {
	return NewUserClient(u.config).QueryMetas(u)
}

// QueryRoles queries the "roles" edge of the User entity.
func (u *User) QueryRoles() *RoleQuery {
	return NewUserClient(u.config).QueryRoles(u)
}

// QuerySocials queries the "socials" edge of the User entity.
func (u *User) QuerySocials() *UserSocialQuery {
	return NewUserClient(u.config).QuerySocials(u)
}

// QueryDepartments queries the "departments" edge of the User entity.
func (u *User) QueryDepartments() *DepartmentQuery {
	return NewUserClient(u.config).QueryDepartments(u)
}

// QueryToken queries the "token" edge of the User entity.
func (u *User) QueryToken() *TokenQuery {
	return NewUserClient(u.config).QueryToken(u)
}

// QueryLoginRecord queries the "loginRecord" edge of the User entity.
func (u *User) QueryLoginRecord() *LoginRecordQuery {
	return NewUserClient(u.config).QueryLoginRecord(u)
}

// Update returns a builder for updating this User.
// Note that you need to call User.Unwrap() before calling this method if this User
// was returned from a transaction, and the transaction was committed or rolled back.
func (u *User) Update() *UserUpdateOne {
	return NewUserClient(u.config).UpdateOne(u)
}

// Unwrap unwraps the User entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (u *User) Unwrap() *User {
	_tx, ok := u.config.driver.(*txDriver)
	if !ok {
		panic("ent: User is not a transactional entity")
	}
	u.config.driver = _tx.drv
	return u
}

// String implements the fmt.Stringer.
func (u *User) String() string {
	var builder strings.Builder
	builder.WriteString("User(")
	builder.WriteString(fmt.Sprintf("id=%v, ", u.ID))
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", u.Status))
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(u.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(u.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("version=")
	builder.WriteString(fmt.Sprintf("%v", u.Version))
	builder.WriteString(", ")
	builder.WriteString("username=")
	builder.WriteString(u.Username)
	builder.WriteString(", ")
	if v := u.PhoneNumber; v != nil {
		builder.WriteString("phone_number=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := u.Email; v != nil {
		builder.WriteString("email=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("password=")
	builder.WriteString(u.Password)
	builder.WriteString(", ")
	if v := u.Nickname; v != nil {
		builder.WriteString("nickname=")
		builder.WriteString(*v)
	}
	builder.WriteByte(')')
	return builder.String()
}

// Users is a parsable slice of User.
type Users []*User
