// 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/entity"
	"github.com/anzhiyu-c/anheyu-app/ent/file"
	"github.com/anzhiyu-c/anheyu-app/ent/fileentity"
)

// FileEntity is the model entity for the FileEntity schema.
type FileEntity 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"`
	// 关联的逻辑文件ID
	FileID uint `json:"file_id,omitempty"`
	// 关联的物理存储实体ID
	EntityID uint `json:"entity_id,omitempty"`
	// 文件版本标识
	Version *string `json:"version,omitempty"`
	// 是否是逻辑文件的当前激活版本
	IsCurrent bool `json:"is_current,omitempty"`
	// 此版本上传用户ID
	UploadedByUserID *uint64 `json:"uploaded_by_user_id,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the FileEntityQuery when eager-loading is set.
	Edges        FileEntityEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

// FileOrErr returns the File value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e FileEntityEdges) FileOrErr() (*File, error) {
	if e.File != nil {
		return e.File, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: file.Label}
	}
	return nil, &NotLoadedError{edge: "file"}
}

// EntityOrErr returns the Entity value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e FileEntityEdges) EntityOrErr() (*Entity, error) {
	if e.Entity != nil {
		return e.Entity, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: entity.Label}
	}
	return nil, &NotLoadedError{edge: "entity"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*FileEntity) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case fileentity.FieldIsCurrent:
			values[i] = new(sql.NullBool)
		case fileentity.FieldID, fileentity.FieldFileID, fileentity.FieldEntityID, fileentity.FieldUploadedByUserID:
			values[i] = new(sql.NullInt64)
		case fileentity.FieldVersion:
			values[i] = new(sql.NullString)
		case fileentity.FieldDeletedAt, fileentity.FieldCreatedAt, fileentity.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 FileEntity fields.
func (_m *FileEntity) 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 fileentity.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 fileentity.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 fileentity.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 fileentity.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 fileentity.FieldFileID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field file_id", values[i])
			} else if value.Valid {
				_m.FileID = uint(value.Int64)
			}
		case fileentity.FieldEntityID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field entity_id", values[i])
			} else if value.Valid {
				_m.EntityID = uint(value.Int64)
			}
		case fileentity.FieldVersion:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field version", values[i])
			} else if value.Valid {
				_m.Version = new(string)
				*_m.Version = value.String
			}
		case fileentity.FieldIsCurrent:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field is_current", values[i])
			} else if value.Valid {
				_m.IsCurrent = value.Bool
			}
		case fileentity.FieldUploadedByUserID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field uploaded_by_user_id", values[i])
			} else if value.Valid {
				_m.UploadedByUserID = new(uint64)
				*_m.UploadedByUserID = uint64(value.Int64)
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryFile queries the "file" edge of the FileEntity entity.
func (_m *FileEntity) QueryFile() *FileQuery {
	return NewFileEntityClient(_m.config).QueryFile(_m)
}

// QueryEntity queries the "entity" edge of the FileEntity entity.
func (_m *FileEntity) QueryEntity() *EntityQuery {
	return NewFileEntityClient(_m.config).QueryEntity(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *FileEntity) String() string {
	var builder strings.Builder
	builder.WriteString("FileEntity(")
	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("file_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.FileID))
	builder.WriteString(", ")
	builder.WriteString("entity_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.EntityID))
	builder.WriteString(", ")
	if v := _m.Version; v != nil {
		builder.WriteString("version=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("is_current=")
	builder.WriteString(fmt.Sprintf("%v", _m.IsCurrent))
	builder.WriteString(", ")
	if v := _m.UploadedByUserID; v != nil {
		builder.WriteString("uploaded_by_user_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteByte(')')
	return builder.String()
}

// FileEntities is a parsable slice of FileEntity.
type FileEntities []*FileEntity
