// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"
	"wechat-api/ent/contact"
	"wechat-api/ent/label"
	"wechat-api/ent/labelrelationship"

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

// LabelRelationship is the model entity for the LabelRelationship schema.
type LabelRelationship 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
	LabelID uint64 `json:"label_id,omitempty"`
	// 联系人 ID
	ContactID uint64 `json:"contact_id,omitempty"`
	// 机构 ID
	OrganizationID uint64 `json:"organization_id,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the LabelRelationshipQuery when eager-loading is set.
	Edges        LabelRelationshipEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

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

// LabelsOrErr returns the Labels value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e LabelRelationshipEdges) LabelsOrErr() (*Label, error) {
	if e.Labels != nil {
		return e.Labels, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: label.Label}
	}
	return nil, &NotLoadedError{edge: "labels"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*LabelRelationship) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case labelrelationship.FieldID, labelrelationship.FieldStatus, labelrelationship.FieldLabelID, labelrelationship.FieldContactID, labelrelationship.FieldOrganizationID:
			values[i] = new(sql.NullInt64)
		case labelrelationship.FieldCreatedAt, labelrelationship.FieldUpdatedAt, labelrelationship.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 LabelRelationship fields.
func (lr *LabelRelationship) 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 labelrelationship.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			lr.ID = uint64(value.Int64)
		case labelrelationship.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 {
				lr.CreatedAt = value.Time
			}
		case labelrelationship.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 {
				lr.UpdatedAt = value.Time
			}
		case labelrelationship.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				lr.Status = uint8(value.Int64)
			}
		case labelrelationship.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 {
				lr.DeletedAt = value.Time
			}
		case labelrelationship.FieldLabelID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field label_id", values[i])
			} else if value.Valid {
				lr.LabelID = uint64(value.Int64)
			}
		case labelrelationship.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 {
				lr.ContactID = uint64(value.Int64)
			}
		case labelrelationship.FieldOrganizationID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field organization_id", values[i])
			} else if value.Valid {
				lr.OrganizationID = uint64(value.Int64)
			}
		default:
			lr.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryContacts queries the "contacts" edge of the LabelRelationship entity.
func (lr *LabelRelationship) QueryContacts() *ContactQuery {
	return NewLabelRelationshipClient(lr.config).QueryContacts(lr)
}

// QueryLabels queries the "labels" edge of the LabelRelationship entity.
func (lr *LabelRelationship) QueryLabels() *LabelQuery {
	return NewLabelRelationshipClient(lr.config).QueryLabels(lr)
}

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

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

// String implements the fmt.Stringer.
func (lr *LabelRelationship) String() string {
	var builder strings.Builder
	builder.WriteString("LabelRelationship(")
	builder.WriteString(fmt.Sprintf("id=%v, ", lr.ID))
	builder.WriteString("created_at=")
	builder.WriteString(lr.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(lr.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", lr.Status))
	builder.WriteString(", ")
	builder.WriteString("deleted_at=")
	builder.WriteString(lr.DeletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("label_id=")
	builder.WriteString(fmt.Sprintf("%v", lr.LabelID))
	builder.WriteString(", ")
	builder.WriteString("contact_id=")
	builder.WriteString(fmt.Sprintf("%v", lr.ContactID))
	builder.WriteString(", ")
	builder.WriteString("organization_id=")
	builder.WriteString(fmt.Sprintf("%v", lr.OrganizationID))
	builder.WriteByte(')')
	return builder.String()
}

// LabelRelationships is a parsable slice of LabelRelationship.
type LabelRelationships []*LabelRelationship
