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

// VisitorLog is the model entity for the VisitorLog schema.
type VisitorLog 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"`
	// 访客唯一标识（基于IP+UA生成的hash）
	VisitorID string `json:"visitor_id,omitempty"`
	// 会话ID
	SessionID *string `json:"session_id,omitempty"`
	// 访客IP地址
	IPAddress string `json:"ip_address,omitempty"`
	// 用户代理字符串
	UserAgent *string `json:"user_agent,omitempty"`
	// 来源页面
	Referer *string `json:"referer,omitempty"`
	// 访问的URL路径
	URLPath string `json:"url_path,omitempty"`
	// 访客所在国家
	Country *string `json:"country,omitempty"`
	// 访客所在地区
	Region *string `json:"region,omitempty"`
	// 访客所在城市
	City *string `json:"city,omitempty"`
	// 浏览器类型
	Browser *string `json:"browser,omitempty"`
	// 操作系统
	Os *string `json:"os,omitempty"`
	// 设备类型
	Device *string `json:"device,omitempty"`
	// 页面停留时间（秒）
	Duration int `json:"duration,omitempty"`
	// 是否为跳出访问
	IsBounce     bool `json:"is_bounce,omitempty"`
	selectValues sql.SelectValues
}

// scanValues returns the types for scanning values from sql.Rows.
func (*VisitorLog) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case visitorlog.FieldIsBounce:
			values[i] = new(sql.NullBool)
		case visitorlog.FieldID, visitorlog.FieldDuration:
			values[i] = new(sql.NullInt64)
		case visitorlog.FieldVisitorID, visitorlog.FieldSessionID, visitorlog.FieldIPAddress, visitorlog.FieldUserAgent, visitorlog.FieldReferer, visitorlog.FieldURLPath, visitorlog.FieldCountry, visitorlog.FieldRegion, visitorlog.FieldCity, visitorlog.FieldBrowser, visitorlog.FieldOs, visitorlog.FieldDevice:
			values[i] = new(sql.NullString)
		case visitorlog.FieldCreatedAt:
			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 VisitorLog fields.
func (_m *VisitorLog) 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 visitorlog.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 visitorlog.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 visitorlog.FieldVisitorID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field visitor_id", values[i])
			} else if value.Valid {
				_m.VisitorID = value.String
			}
		case visitorlog.FieldSessionID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field session_id", values[i])
			} else if value.Valid {
				_m.SessionID = new(string)
				*_m.SessionID = value.String
			}
		case visitorlog.FieldIPAddress:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field ip_address", values[i])
			} else if value.Valid {
				_m.IPAddress = value.String
			}
		case visitorlog.FieldUserAgent:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field user_agent", values[i])
			} else if value.Valid {
				_m.UserAgent = new(string)
				*_m.UserAgent = value.String
			}
		case visitorlog.FieldReferer:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field referer", values[i])
			} else if value.Valid {
				_m.Referer = new(string)
				*_m.Referer = value.String
			}
		case visitorlog.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 visitorlog.FieldCountry:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field country", values[i])
			} else if value.Valid {
				_m.Country = new(string)
				*_m.Country = value.String
			}
		case visitorlog.FieldRegion:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field region", values[i])
			} else if value.Valid {
				_m.Region = new(string)
				*_m.Region = value.String
			}
		case visitorlog.FieldCity:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field city", values[i])
			} else if value.Valid {
				_m.City = new(string)
				*_m.City = value.String
			}
		case visitorlog.FieldBrowser:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field browser", values[i])
			} else if value.Valid {
				_m.Browser = new(string)
				*_m.Browser = value.String
			}
		case visitorlog.FieldOs:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field os", values[i])
			} else if value.Valid {
				_m.Os = new(string)
				*_m.Os = value.String
			}
		case visitorlog.FieldDevice:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field device", values[i])
			} else if value.Valid {
				_m.Device = new(string)
				*_m.Device = value.String
			}
		case visitorlog.FieldDuration:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field duration", values[i])
			} else if value.Valid {
				_m.Duration = int(value.Int64)
			}
		case visitorlog.FieldIsBounce:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field is_bounce", values[i])
			} else if value.Valid {
				_m.IsBounce = 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 VisitorLog.
// This includes values selected through modifiers, order, etc.
func (_m *VisitorLog) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

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

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

// String implements the fmt.Stringer.
func (_m *VisitorLog) String() string {
	var builder strings.Builder
	builder.WriteString("VisitorLog(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("visitor_id=")
	builder.WriteString(_m.VisitorID)
	builder.WriteString(", ")
	if v := _m.SessionID; v != nil {
		builder.WriteString("session_id=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("ip_address=")
	builder.WriteString(_m.IPAddress)
	builder.WriteString(", ")
	if v := _m.UserAgent; v != nil {
		builder.WriteString("user_agent=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.Referer; v != nil {
		builder.WriteString("referer=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("url_path=")
	builder.WriteString(_m.URLPath)
	builder.WriteString(", ")
	if v := _m.Country; v != nil {
		builder.WriteString("country=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.Region; v != nil {
		builder.WriteString("region=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.City; v != nil {
		builder.WriteString("city=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.Browser; v != nil {
		builder.WriteString("browser=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.Os; v != nil {
		builder.WriteString("os=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.Device; v != nil {
		builder.WriteString("device=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("duration=")
	builder.WriteString(fmt.Sprintf("%v", _m.Duration))
	builder.WriteString(", ")
	builder.WriteString("is_bounce=")
	builder.WriteString(fmt.Sprintf("%v", _m.IsBounce))
	builder.WriteByte(')')
	return builder.String()
}

// VisitorLogs is a parsable slice of VisitorLog.
type VisitorLogs []*VisitorLog
