// 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"
)

// 系统用户登录记录表
type LoginRecord struct {
	config `json:"-"`
	// ID of the ent.
	// Primary Key | 主键
	ID int64 `json:"id,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 ID | 用户ID
	UserID int64 `json:"user_id,omitempty"`
	// LastLogin Time | 最近一次登录时间
	LastLoginAt time.Time `json:"last_login_at,omitempty"`
	// LastLogin IPv4 | 最近一次登录IPv4
	LastLoginIpv4 string `json:"last_login_ipv4,omitempty"`
	// LastLogin Device | 最近一次登录设备
	LastLoginDevice string `json:"last_login_device,omitempty"`
	// LastLogin UA | 最近一次登录浏览器UA
	LastLoginUA string `json:"last_login_UA,omitempty"`
	// Login Count | 总登录次数
	LoginCount uint64 `json:"login_count,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the LoginRecordQuery when eager-loading is set.
	Edges        LoginRecordEdges `json:"edges"`
	selectValues sql.SelectValues
}

// LoginRecordEdges holds the relations/edges for other nodes in the graph.
type LoginRecordEdges struct {
	// User holds the value of the user edge.
	User *User `json:"user,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
}

// UserOrErr returns the User value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e LoginRecordEdges) UserOrErr() (*User, error) {
	if e.loadedTypes[0] {
		if e.User == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: user.Label}
		}
		return e.User, nil
	}
	return nil, &NotLoadedError{edge: "user"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*LoginRecord) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case loginrecord.FieldID, loginrecord.FieldVersion, loginrecord.FieldUserID, loginrecord.FieldLoginCount:
			values[i] = new(sql.NullInt64)
		case loginrecord.FieldLastLoginIpv4, loginrecord.FieldLastLoginDevice, loginrecord.FieldLastLoginUA:
			values[i] = new(sql.NullString)
		case loginrecord.FieldCreatedAt, loginrecord.FieldUpdatedAt, loginrecord.FieldLastLoginAt:
			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 LoginRecord fields.
func (lr *LoginRecord) 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 loginrecord.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			lr.ID = int64(value.Int64)
		case loginrecord.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 {
				lr.CreatedAt = value.Time
			}
		case loginrecord.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 {
				lr.UpdatedAt = value.Time
			}
		case loginrecord.FieldVersion:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field version", values[i])
			} else if value.Valid {
				lr.Version = value.Int64
			}
		case loginrecord.FieldUserID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field user_id", values[i])
			} else if value.Valid {
				lr.UserID = value.Int64
			}
		case loginrecord.FieldLastLoginAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field last_login_at", values[i])
			} else if value.Valid {
				lr.LastLoginAt = value.Time
			}
		case loginrecord.FieldLastLoginIpv4:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field last_login_ipv4", values[i])
			} else if value.Valid {
				lr.LastLoginIpv4 = value.String
			}
		case loginrecord.FieldLastLoginDevice:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field last_login_device", values[i])
			} else if value.Valid {
				lr.LastLoginDevice = value.String
			}
		case loginrecord.FieldLastLoginUA:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field last_login_UA", values[i])
			} else if value.Valid {
				lr.LastLoginUA = value.String
			}
		case loginrecord.FieldLoginCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field login_count", values[i])
			} else if value.Valid {
				lr.LoginCount = uint64(value.Int64)
			}
		default:
			lr.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryUser queries the "user" edge of the LoginRecord entity.
func (lr *LoginRecord) QueryUser() *UserQuery {
	return NewLoginRecordClient(lr.config).QueryUser(lr)
}

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

// Unwrap unwraps the LoginRecord 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 (lr *LoginRecord) Unwrap() *LoginRecord {
	_tx, ok := lr.config.driver.(*txDriver)
	if !ok {
		panic("ent: LoginRecord is not a transactional entity")
	}
	lr.config.driver = _tx.drv
	return lr
}

// String implements the fmt.Stringer.
func (lr *LoginRecord) String() string {
	var builder strings.Builder
	builder.WriteString("LoginRecord(")
	builder.WriteString(fmt.Sprintf("id=%v, ", lr.ID))
	builder.WriteString("created_at=")
	builder.WriteString(lr.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(lr.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("version=")
	builder.WriteString(fmt.Sprintf("%v", lr.Version))
	builder.WriteString(", ")
	builder.WriteString("user_id=")
	builder.WriteString(fmt.Sprintf("%v", lr.UserID))
	builder.WriteString(", ")
	builder.WriteString("last_login_at=")
	builder.WriteString(lr.LastLoginAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("last_login_ipv4=")
	builder.WriteString(lr.LastLoginIpv4)
	builder.WriteString(", ")
	builder.WriteString("last_login_device=")
	builder.WriteString(lr.LastLoginDevice)
	builder.WriteString(", ")
	builder.WriteString("last_login_UA=")
	builder.WriteString(lr.LastLoginUA)
	builder.WriteString(", ")
	builder.WriteString("login_count=")
	builder.WriteString(fmt.Sprintf("%v", lr.LoginCount))
	builder.WriteByte(')')
	return builder.String()
}

// LoginRecords is a parsable slice of LoginRecord.
type LoginRecords []*LoginRecord
