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

// PostCategory is the model entity for the PostCategory schema.
type PostCategory 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"`
	// 该分类下的文章数量
	Count int `json:"count,omitempty"`
	// 是否为系列
	IsSeries bool `json:"is_series,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the PostCategoryQuery when eager-loading is set.
	Edges        PostCategoryEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

// ArticlesOrErr returns the Articles value or an error if the edge
// was not loaded in eager-loading.
func (e PostCategoryEdges) ArticlesOrErr() ([]*Article, error) {
	if e.loadedTypes[0] {
		return e.Articles, nil
	}
	return nil, &NotLoadedError{edge: "articles"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*PostCategory) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case postcategory.FieldIsSeries:
			values[i] = new(sql.NullBool)
		case postcategory.FieldID, postcategory.FieldCount:
			values[i] = new(sql.NullInt64)
		case postcategory.FieldName, postcategory.FieldDescription:
			values[i] = new(sql.NullString)
		case postcategory.FieldDeletedAt, postcategory.FieldCreatedAt, postcategory.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 PostCategory fields.
func (_m *PostCategory) 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 postcategory.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 postcategory.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 postcategory.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 postcategory.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 postcategory.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 postcategory.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 postcategory.FieldCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field count", values[i])
			} else if value.Valid {
				_m.Count = int(value.Int64)
			}
		case postcategory.FieldIsSeries:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field is_series", values[i])
			} else if value.Valid {
				_m.IsSeries = value.Bool
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryArticles queries the "articles" edge of the PostCategory entity.
func (_m *PostCategory) QueryArticles() *ArticleQuery {
	return NewPostCategoryClient(_m.config).QueryArticles(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *PostCategory) String() string {
	var builder strings.Builder
	builder.WriteString("PostCategory(")
	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("count=")
	builder.WriteString(fmt.Sprintf("%v", _m.Count))
	builder.WriteString(", ")
	builder.WriteString("is_series=")
	builder.WriteString(fmt.Sprintf("%v", _m.IsSeries))
	builder.WriteByte(')')
	return builder.String()
}

// PostCategories is a parsable slice of PostCategory.
type PostCategories []*PostCategory
