// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"
	"wechat-api/ent/contact"
	"wechat-api/ent/contactfield"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
)

// ContactField is the model entity for the ContactField schema.
type ContactField struct {
	config `json:"-"`
	// ID of the ent.
	ID uint64 `json:"id,omitempty"`
	// Create Time | 创建日期
	CreatedAt time.Time `json:"created_at,omitempty"`
	// Update Time | 修改日期
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// Status 1: normal 2: ban | 状态 1 正常 2 禁用
	Status uint8 `json:"status,omitempty"`
	// Delete Time | 删除日期
	DeletedAt time.Time `json:"deleted_at,omitempty"`
	// 联系人 ID
	ContactID uint64 `json:"contact_id,omitempty"`
	// 表单 id
	FormID string `json:"form_id,omitempty"`
	// 表单值
	Value []string `json:"value,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the ContactFieldQuery when eager-loading is set.
	Edges        ContactFieldEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

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

// scanValues returns the types for scanning values from sql.Rows.
func (*ContactField) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case contactfield.FieldValue:
			values[i] = new([]byte)
		case contactfield.FieldID, contactfield.FieldStatus, contactfield.FieldContactID:
			values[i] = new(sql.NullInt64)
		case contactfield.FieldFormID:
			values[i] = new(sql.NullString)
		case contactfield.FieldCreatedAt, contactfield.FieldUpdatedAt, contactfield.FieldDeletedAt:
			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 ContactField fields.
func (cf *ContactField) 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 contactfield.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			cf.ID = uint64(value.Int64)
		case contactfield.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 {
				cf.CreatedAt = value.Time
			}
		case contactfield.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 {
				cf.UpdatedAt = value.Time
			}
		case contactfield.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				cf.Status = uint8(value.Int64)
			}
		case contactfield.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 {
				cf.DeletedAt = value.Time
			}
		case contactfield.FieldContactID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field contact_id", values[i])
			} else if value.Valid {
				cf.ContactID = uint64(value.Int64)
			}
		case contactfield.FieldFormID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field form_id", values[i])
			} else if value.Valid {
				cf.FormID = value.String
			}
		case contactfield.FieldValue:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field value", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &cf.Value); err != nil {
					return fmt.Errorf("unmarshal field value: %w", err)
				}
			}
		default:
			cf.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryFieldContact queries the "field_contact" edge of the ContactField entity.
func (cf *ContactField) QueryFieldContact() *ContactQuery {
	return NewContactFieldClient(cf.config).QueryFieldContact(cf)
}

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

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

// String implements the fmt.Stringer.
func (cf *ContactField) String() string {
	var builder strings.Builder
	builder.WriteString("ContactField(")
	builder.WriteString(fmt.Sprintf("id=%v, ", cf.ID))
	builder.WriteString("created_at=")
	builder.WriteString(cf.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(cf.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", cf.Status))
	builder.WriteString(", ")
	builder.WriteString("deleted_at=")
	builder.WriteString(cf.DeletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("contact_id=")
	builder.WriteString(fmt.Sprintf("%v", cf.ContactID))
	builder.WriteString(", ")
	builder.WriteString("form_id=")
	builder.WriteString(cf.FormID)
	builder.WriteString(", ")
	builder.WriteString("value=")
	builder.WriteString(fmt.Sprintf("%v", cf.Value))
	builder.WriteByte(')')
	return builder.String()
}

// ContactFields is a parsable slice of ContactField.
type ContactFields []*ContactField
