// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/anzhiyu-c/anheyu-app/ent/article"
)

// Article is the model entity for the Article schema.
type Article 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"`
	// 文章标题
	Title string `json:"title,omitempty"`
	// 文章的 Markdown 原文
	ContentMd string `json:"content_md,omitempty"`
	// 由 content_md 解析和净化后的 HTML
	ContentHTML string `json:"content_html,omitempty"`
	// 封面图URL
	CoverURL string `json:"cover_url,omitempty"`
	// Status holds the value of the "status" field.
	Status article.Status `json:"status,omitempty"`
	// 浏览次数
	ViewCount int `json:"view_count,omitempty"`
	// 总字数
	WordCount int `json:"word_count,omitempty"`
	// 阅读时长(分钟)
	ReadingTime int `json:"reading_time,omitempty"`
	// 作者IP属地
	IPLocation string `json:"ip_location,omitempty"`
	// 主色调，取自 top_img_url 或 cover_url
	PrimaryColor string `json:"primary_color,omitempty"`
	// 主色调是否为手动设置
	IsPrimaryColorManual bool `json:"is_primary_color_manual,omitempty"`
	// 首页推荐文章排序，0 表示不展示，>0 表示展示，数值越小越靠前
	HomeSort int `json:"home_sort,omitempty"`
	// 置顶排序，0 表示不置顶，>0 表示置顶，数值越小越靠前
	PinSort int `json:"pin_sort,omitempty"`
	// 顶部图URL，可选。若不填，则在保存时自动使用封面图URL
	TopImgURL string `json:"top_img_url,omitempty"`
	// 文章摘要列表，用于随机摘要功能
	Summaries []string `json:"summaries,omitempty"`
	// 永久链接，用于替换ID，需要保证唯一性
	Abbrlink *string `json:"abbrlink,omitempty"`
	// 是否显示版权信息
	Copyright bool `json:"copyright,omitempty"`
	// 版权作者
	CopyrightAuthor string `json:"copyright_author,omitempty"`
	// 版权作者链接
	CopyrightAuthorHref string `json:"copyright_author_href,omitempty"`
	// 版权来源链接
	CopyrightURL string `json:"copyright_url,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the ArticleQuery when eager-loading is set.
	Edges        ArticleEdges `json:"edges"`
	selectValues sql.SelectValues
}

// ArticleEdges holds the relations/edges for other nodes in the graph.
type ArticleEdges struct {
	// PostTags holds the value of the post_tags edge.
	PostTags []*PostTag `json:"post_tags,omitempty"`
	// PostCategories holds the value of the post_categories edge.
	PostCategories []*PostCategory `json:"post_categories,omitempty"`
	// Comments holds the value of the comments edge.
	Comments []*Comment `json:"comments,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
}

// PostTagsOrErr returns the PostTags value or an error if the edge
// was not loaded in eager-loading.
func (e ArticleEdges) PostTagsOrErr() ([]*PostTag, error) {
	if e.loadedTypes[0] {
		return e.PostTags, nil
	}
	return nil, &NotLoadedError{edge: "post_tags"}
}

// PostCategoriesOrErr returns the PostCategories value or an error if the edge
// was not loaded in eager-loading.
func (e ArticleEdges) PostCategoriesOrErr() ([]*PostCategory, error) {
	if e.loadedTypes[1] {
		return e.PostCategories, nil
	}
	return nil, &NotLoadedError{edge: "post_categories"}
}

// CommentsOrErr returns the Comments value or an error if the edge
// was not loaded in eager-loading.
func (e ArticleEdges) CommentsOrErr() ([]*Comment, error) {
	if e.loadedTypes[2] {
		return e.Comments, nil
	}
	return nil, &NotLoadedError{edge: "comments"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Article) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case article.FieldSummaries:
			values[i] = new([]byte)
		case article.FieldIsPrimaryColorManual, article.FieldCopyright:
			values[i] = new(sql.NullBool)
		case article.FieldID, article.FieldViewCount, article.FieldWordCount, article.FieldReadingTime, article.FieldHomeSort, article.FieldPinSort:
			values[i] = new(sql.NullInt64)
		case article.FieldTitle, article.FieldContentMd, article.FieldContentHTML, article.FieldCoverURL, article.FieldStatus, article.FieldIPLocation, article.FieldPrimaryColor, article.FieldTopImgURL, article.FieldAbbrlink, article.FieldCopyrightAuthor, article.FieldCopyrightAuthorHref, article.FieldCopyrightURL:
			values[i] = new(sql.NullString)
		case article.FieldDeletedAt, article.FieldCreatedAt, article.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 Article fields.
func (_m *Article) 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 article.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 article.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 article.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 article.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 article.FieldTitle:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field title", values[i])
			} else if value.Valid {
				_m.Title = value.String
			}
		case article.FieldContentMd:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field content_md", values[i])
			} else if value.Valid {
				_m.ContentMd = value.String
			}
		case article.FieldContentHTML:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field content_html", values[i])
			} else if value.Valid {
				_m.ContentHTML = value.String
			}
		case article.FieldCoverURL:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field cover_url", values[i])
			} else if value.Valid {
				_m.CoverURL = value.String
			}
		case article.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				_m.Status = article.Status(value.String)
			}
		case article.FieldViewCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field view_count", values[i])
			} else if value.Valid {
				_m.ViewCount = int(value.Int64)
			}
		case article.FieldWordCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field word_count", values[i])
			} else if value.Valid {
				_m.WordCount = int(value.Int64)
			}
		case article.FieldReadingTime:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field reading_time", values[i])
			} else if value.Valid {
				_m.ReadingTime = int(value.Int64)
			}
		case article.FieldIPLocation:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field ip_location", values[i])
			} else if value.Valid {
				_m.IPLocation = value.String
			}
		case article.FieldPrimaryColor:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field primary_color", values[i])
			} else if value.Valid {
				_m.PrimaryColor = value.String
			}
		case article.FieldIsPrimaryColorManual:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field is_primary_color_manual", values[i])
			} else if value.Valid {
				_m.IsPrimaryColorManual = value.Bool
			}
		case article.FieldHomeSort:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field home_sort", values[i])
			} else if value.Valid {
				_m.HomeSort = int(value.Int64)
			}
		case article.FieldPinSort:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field pin_sort", values[i])
			} else if value.Valid {
				_m.PinSort = int(value.Int64)
			}
		case article.FieldTopImgURL:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field top_img_url", values[i])
			} else if value.Valid {
				_m.TopImgURL = value.String
			}
		case article.FieldSummaries:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field summaries", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.Summaries); err != nil {
					return fmt.Errorf("unmarshal field summaries: %w", err)
				}
			}
		case article.FieldAbbrlink:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field abbrlink", values[i])
			} else if value.Valid {
				_m.Abbrlink = new(string)
				*_m.Abbrlink = value.String
			}
		case article.FieldCopyright:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field copyright", values[i])
			} else if value.Valid {
				_m.Copyright = value.Bool
			}
		case article.FieldCopyrightAuthor:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field copyright_author", values[i])
			} else if value.Valid {
				_m.CopyrightAuthor = value.String
			}
		case article.FieldCopyrightAuthorHref:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field copyright_author_href", values[i])
			} else if value.Valid {
				_m.CopyrightAuthorHref = value.String
			}
		case article.FieldCopyrightURL:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field copyright_url", values[i])
			} else if value.Valid {
				_m.CopyrightURL = value.String
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryPostTags queries the "post_tags" edge of the Article entity.
func (_m *Article) QueryPostTags() *PostTagQuery {
	return NewArticleClient(_m.config).QueryPostTags(_m)
}

// QueryPostCategories queries the "post_categories" edge of the Article entity.
func (_m *Article) QueryPostCategories() *PostCategoryQuery {
	return NewArticleClient(_m.config).QueryPostCategories(_m)
}

// QueryComments queries the "comments" edge of the Article entity.
func (_m *Article) QueryComments() *CommentQuery {
	return NewArticleClient(_m.config).QueryComments(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *Article) String() string {
	var builder strings.Builder
	builder.WriteString("Article(")
	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("title=")
	builder.WriteString(_m.Title)
	builder.WriteString(", ")
	builder.WriteString("content_md=")
	builder.WriteString(_m.ContentMd)
	builder.WriteString(", ")
	builder.WriteString("content_html=")
	builder.WriteString(_m.ContentHTML)
	builder.WriteString(", ")
	builder.WriteString("cover_url=")
	builder.WriteString(_m.CoverURL)
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", _m.Status))
	builder.WriteString(", ")
	builder.WriteString("view_count=")
	builder.WriteString(fmt.Sprintf("%v", _m.ViewCount))
	builder.WriteString(", ")
	builder.WriteString("word_count=")
	builder.WriteString(fmt.Sprintf("%v", _m.WordCount))
	builder.WriteString(", ")
	builder.WriteString("reading_time=")
	builder.WriteString(fmt.Sprintf("%v", _m.ReadingTime))
	builder.WriteString(", ")
	builder.WriteString("ip_location=")
	builder.WriteString(_m.IPLocation)
	builder.WriteString(", ")
	builder.WriteString("primary_color=")
	builder.WriteString(_m.PrimaryColor)
	builder.WriteString(", ")
	builder.WriteString("is_primary_color_manual=")
	builder.WriteString(fmt.Sprintf("%v", _m.IsPrimaryColorManual))
	builder.WriteString(", ")
	builder.WriteString("home_sort=")
	builder.WriteString(fmt.Sprintf("%v", _m.HomeSort))
	builder.WriteString(", ")
	builder.WriteString("pin_sort=")
	builder.WriteString(fmt.Sprintf("%v", _m.PinSort))
	builder.WriteString(", ")
	builder.WriteString("top_img_url=")
	builder.WriteString(_m.TopImgURL)
	builder.WriteString(", ")
	builder.WriteString("summaries=")
	builder.WriteString(fmt.Sprintf("%v", _m.Summaries))
	builder.WriteString(", ")
	if v := _m.Abbrlink; v != nil {
		builder.WriteString("abbrlink=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("copyright=")
	builder.WriteString(fmt.Sprintf("%v", _m.Copyright))
	builder.WriteString(", ")
	builder.WriteString("copyright_author=")
	builder.WriteString(_m.CopyrightAuthor)
	builder.WriteString(", ")
	builder.WriteString("copyright_author_href=")
	builder.WriteString(_m.CopyrightAuthorHref)
	builder.WriteString(", ")
	builder.WriteString("copyright_url=")
	builder.WriteString(_m.CopyrightURL)
	builder.WriteByte(')')
	return builder.String()
}

// Articles is a parsable slice of Article.
type Articles []*Article
