// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"
	"wechat-api/ent/contactfieldtemplate"
	"wechat-api/ent/custom_types"

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

// ContactFieldTemplate is the model entity for the ContactFieldTemplate schema.
type ContactFieldTemplate 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
	OrganizationID uint64 `json:"organization_id,omitempty"`
	// 模板
	Template     []custom_types.ContactFieldTemplate `json:"template,omitempty"`
	selectValues sql.SelectValues
}

// scanValues returns the types for scanning values from sql.Rows.
func (*ContactFieldTemplate) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case contactfieldtemplate.FieldTemplate:
			values[i] = new([]byte)
		case contactfieldtemplate.FieldID, contactfieldtemplate.FieldStatus, contactfieldtemplate.FieldOrganizationID:
			values[i] = new(sql.NullInt64)
		case contactfieldtemplate.FieldCreatedAt, contactfieldtemplate.FieldUpdatedAt, contactfieldtemplate.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 ContactFieldTemplate fields.
func (cft *ContactFieldTemplate) 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 contactfieldtemplate.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			cft.ID = uint64(value.Int64)
		case contactfieldtemplate.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 {
				cft.CreatedAt = value.Time
			}
		case contactfieldtemplate.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 {
				cft.UpdatedAt = value.Time
			}
		case contactfieldtemplate.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				cft.Status = uint8(value.Int64)
			}
		case contactfieldtemplate.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 {
				cft.DeletedAt = value.Time
			}
		case contactfieldtemplate.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 {
				cft.OrganizationID = uint64(value.Int64)
			}
		case contactfieldtemplate.FieldTemplate:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field template", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &cft.Template); err != nil {
					return fmt.Errorf("unmarshal field template: %w", err)
				}
			}
		default:
			cft.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

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

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

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

// ContactFieldTemplates is a parsable slice of ContactFieldTemplate.
type ContactFieldTemplates []*ContactFieldTemplate
