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

// URLStat is the model entity for the URLStat schema.
type URLStat struct {
	config `json:"-"`
	// ID of the ent.
	ID uint `json:"id,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"`
	// URL路径
	URLPath string `json:"url_path,omitempty"`
	// 页面标题
	PageTitle *string `json:"page_title,omitempty"`
	// 总访问量
	TotalViews int64 `json:"total_views,omitempty"`
	// 独立访问量
	UniqueViews int64 `json:"unique_views,omitempty"`
	// 跳出次数
	BounceCount int64 `json:"bounce_count,omitempty"`
	// 平均停留时间（秒）
	AvgDuration float64 `json:"avg_duration,omitempty"`
	// 最后访问时间
	LastVisitedAt *time.Time `json:"last_visited_at,omitempty"`
	selectValues  sql.SelectValues
}

// scanValues returns the types for scanning values from sql.Rows.
func (*URLStat) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case urlstat.FieldAvgDuration:
			values[i] = new(sql.NullFloat64)
		case urlstat.FieldID, urlstat.FieldTotalViews, urlstat.FieldUniqueViews, urlstat.FieldBounceCount:
			values[i] = new(sql.NullInt64)
		case urlstat.FieldURLPath, urlstat.FieldPageTitle:
			values[i] = new(sql.NullString)
		case urlstat.FieldCreatedAt, urlstat.FieldUpdatedAt, urlstat.FieldLastVisitedAt:
			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 URLStat fields.
func (_m *URLStat) 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 urlstat.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 urlstat.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 urlstat.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 urlstat.FieldURLPath:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field url_path", values[i])
			} else if value.Valid {
				_m.URLPath = value.String
			}
		case urlstat.FieldPageTitle:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field page_title", values[i])
			} else if value.Valid {
				_m.PageTitle = new(string)
				*_m.PageTitle = value.String
			}
		case urlstat.FieldTotalViews:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field total_views", values[i])
			} else if value.Valid {
				_m.TotalViews = value.Int64
			}
		case urlstat.FieldUniqueViews:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field unique_views", values[i])
			} else if value.Valid {
				_m.UniqueViews = value.Int64
			}
		case urlstat.FieldBounceCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field bounce_count", values[i])
			} else if value.Valid {
				_m.BounceCount = value.Int64
			}
		case urlstat.FieldAvgDuration:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field avg_duration", values[i])
			} else if value.Valid {
				_m.AvgDuration = value.Float64
			}
		case urlstat.FieldLastVisitedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field last_visited_at", values[i])
			} else if value.Valid {
				_m.LastVisitedAt = new(time.Time)
				*_m.LastVisitedAt = value.Time
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

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

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

// String implements the fmt.Stringer.
func (_m *URLStat) String() string {
	var builder strings.Builder
	builder.WriteString("URLStat(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	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("url_path=")
	builder.WriteString(_m.URLPath)
	builder.WriteString(", ")
	if v := _m.PageTitle; v != nil {
		builder.WriteString("page_title=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("total_views=")
	builder.WriteString(fmt.Sprintf("%v", _m.TotalViews))
	builder.WriteString(", ")
	builder.WriteString("unique_views=")
	builder.WriteString(fmt.Sprintf("%v", _m.UniqueViews))
	builder.WriteString(", ")
	builder.WriteString("bounce_count=")
	builder.WriteString(fmt.Sprintf("%v", _m.BounceCount))
	builder.WriteString(", ")
	builder.WriteString("avg_duration=")
	builder.WriteString(fmt.Sprintf("%v", _m.AvgDuration))
	builder.WriteString(", ")
	if v := _m.LastVisitedAt; v != nil {
		builder.WriteString("last_visited_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteByte(')')
	return builder.String()
}

// URLStats is a parsable slice of URLStat.
type URLStats []*URLStat
