// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/anzhiyu-c/anheyu-app/ent/usergroup"
	"github.com/anzhiyu-c/anheyu-app/pkg/domain/model"
)

// UserGroup is the model entity for the UserGroup schema.
type UserGroup struct {
	config `json:"-"`
	// ID of the ent.
	ID uint `json:"id,omitempty"`
	// DeletedAt holds the value of the "deleted_at" field.
	DeletedAt *time.Time `json:"deleted_at,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// UpdatedAt holds the value of the "updated_at" field.
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// 用户组名称/角色名称
	Name string `json:"name,omitempty"`
	// 用户组描述/角色描述
	Description string `json:"description,omitempty"`
	// 权限集, Base64编码的字节
	Permissions model.Boolset `json:"permissions,omitempty"`
	// 用户组的最大存储容量（字节）, 0为不限制
	MaxStorage int64 `json:"max_storage,omitempty"`
	// 用户组的最大上传速度（字节/秒）, 0为不限制
	SpeedLimit int64 `json:"speed_limit,omitempty"`
	// 用户组的特定JSON配置
	Settings *model.GroupSettings `json:"settings,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the UserGroupQuery when eager-loading is set.
	Edges        UserGroupEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

// UsersOrErr returns the Users value or an error if the edge
// was not loaded in eager-loading.
func (e UserGroupEdges) UsersOrErr() ([]*User, error) {
	if e.loadedTypes[0] {
		return e.Users, nil
	}
	return nil, &NotLoadedError{edge: "users"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*UserGroup) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case usergroup.FieldPermissions:
			values[i] = new(model.Boolset)
		case usergroup.FieldSettings:
			values[i] = new(model.GroupSettings)
		case usergroup.FieldID, usergroup.FieldMaxStorage, usergroup.FieldSpeedLimit:
			values[i] = new(sql.NullInt64)
		case usergroup.FieldName, usergroup.FieldDescription:
			values[i] = new(sql.NullString)
		case usergroup.FieldDeletedAt, usergroup.FieldCreatedAt, usergroup.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 UserGroup fields.
func (_m *UserGroup) 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 usergroup.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			_m.ID = uint(value.Int64)
		case usergroup.FieldDeletedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field deleted_at", values[i])
			} else if value.Valid {
				_m.DeletedAt = new(time.Time)
				*_m.DeletedAt = value.Time
			}
		case usergroup.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 {
				_m.CreatedAt = value.Time
			}
		case usergroup.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 {
				_m.UpdatedAt = value.Time
			}
		case usergroup.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				_m.Name = value.String
			}
		case usergroup.FieldDescription:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field description", values[i])
			} else if value.Valid {
				_m.Description = value.String
			}
		case usergroup.FieldPermissions:
			if value, ok := values[i].(*model.Boolset); !ok {
				return fmt.Errorf("unexpected type %T for field permissions", values[i])
			} else if value != nil {
				_m.Permissions = *value
			}
		case usergroup.FieldMaxStorage:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field max_storage", values[i])
			} else if value.Valid {
				_m.MaxStorage = value.Int64
			}
		case usergroup.FieldSpeedLimit:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field speed_limit", values[i])
			} else if value.Valid {
				_m.SpeedLimit = value.Int64
			}
		case usergroup.FieldSettings:
			if value, ok := values[i].(*model.GroupSettings); !ok {
				return fmt.Errorf("unexpected type %T for field settings", values[i])
			} else if value != nil {
				_m.Settings = value
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryUsers queries the "users" edge of the UserGroup entity.
func (_m *UserGroup) QueryUsers() *UserQuery {
	return NewUserGroupClient(_m.config).QueryUsers(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *UserGroup) String() string {
	var builder strings.Builder
	builder.WriteString("UserGroup(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	if v := _m.DeletedAt; v != nil {
		builder.WriteString("deleted_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("name=")
	builder.WriteString(_m.Name)
	builder.WriteString(", ")
	builder.WriteString("description=")
	builder.WriteString(_m.Description)
	builder.WriteString(", ")
	builder.WriteString("permissions=")
	builder.WriteString(fmt.Sprintf("%v", _m.Permissions))
	builder.WriteString(", ")
	builder.WriteString("max_storage=")
	builder.WriteString(fmt.Sprintf("%v", _m.MaxStorage))
	builder.WriteString(", ")
	builder.WriteString("speed_limit=")
	builder.WriteString(fmt.Sprintf("%v", _m.SpeedLimit))
	builder.WriteString(", ")
	builder.WriteString("settings=")
	builder.WriteString(fmt.Sprintf("%v", _m.Settings))
	builder.WriteByte(')')
	return builder.String()
}

// UserGroups is a parsable slice of UserGroup.
type UserGroups []*UserGroup
