// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"
	"wechat-api/ent/creditusage"

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

// CreditUsage is the model entity for the CreditUsage schema.
type CreditUsage 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"`
	// Delete Time | 删除日期
	DeletedAt time.Time `json:"deleted_at,omitempty"`
	// user_id | 用户ID
	UserID string `json:"user_id,omitempty"`
	// number | 积分改变量
	Number float32 `json:"number,omitempty"`
	// status | 状态 1-正常 2-禁用
	Status int `json:"status,omitempty"`
	// ntype | 积分变化类型：1-消耗 2-增加
	Ntype int `json:"ntype,omitempty"`
	// table | 积分变化表名
	Table string `json:"table,omitempty"`
	// organization_id | 租户ID
	OrganizationID uint64 `json:"organization_id,omitempty"`
	// nid | 积分变化关联信息ID
	Nid uint64 `json:"nid,omitempty"`
	// reason | 积分变动原因
	Reason string `json:"reason,omitempty"`
	// operator | 积分变动人
	Operator     string `json:"operator,omitempty"`
	selectValues sql.SelectValues
}

// scanValues returns the types for scanning values from sql.Rows.
func (*CreditUsage) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case creditusage.FieldNumber:
			values[i] = new(sql.NullFloat64)
		case creditusage.FieldID, creditusage.FieldStatus, creditusage.FieldNtype, creditusage.FieldOrganizationID, creditusage.FieldNid:
			values[i] = new(sql.NullInt64)
		case creditusage.FieldUserID, creditusage.FieldTable, creditusage.FieldReason, creditusage.FieldOperator:
			values[i] = new(sql.NullString)
		case creditusage.FieldCreatedAt, creditusage.FieldUpdatedAt, creditusage.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 CreditUsage fields.
func (cu *CreditUsage) 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 creditusage.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			cu.ID = uint64(value.Int64)
		case creditusage.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 {
				cu.CreatedAt = value.Time
			}
		case creditusage.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 {
				cu.UpdatedAt = value.Time
			}
		case creditusage.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 {
				cu.DeletedAt = value.Time
			}
		case creditusage.FieldUserID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field user_id", values[i])
			} else if value.Valid {
				cu.UserID = value.String
			}
		case creditusage.FieldNumber:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field number", values[i])
			} else if value.Valid {
				cu.Number = float32(value.Float64)
			}
		case creditusage.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				cu.Status = int(value.Int64)
			}
		case creditusage.FieldNtype:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field ntype", values[i])
			} else if value.Valid {
				cu.Ntype = int(value.Int64)
			}
		case creditusage.FieldTable:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field table", values[i])
			} else if value.Valid {
				cu.Table = value.String
			}
		case creditusage.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 {
				cu.OrganizationID = uint64(value.Int64)
			}
		case creditusage.FieldNid:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field nid", values[i])
			} else if value.Valid {
				cu.Nid = uint64(value.Int64)
			}
		case creditusage.FieldReason:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field reason", values[i])
			} else if value.Valid {
				cu.Reason = value.String
			}
		case creditusage.FieldOperator:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field operator", values[i])
			} else if value.Valid {
				cu.Operator = value.String
			}
		default:
			cu.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

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

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

// String implements the fmt.Stringer.
func (cu *CreditUsage) String() string {
	var builder strings.Builder
	builder.WriteString("CreditUsage(")
	builder.WriteString(fmt.Sprintf("id=%v, ", cu.ID))
	builder.WriteString("created_at=")
	builder.WriteString(cu.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(cu.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("deleted_at=")
	builder.WriteString(cu.DeletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("user_id=")
	builder.WriteString(cu.UserID)
	builder.WriteString(", ")
	builder.WriteString("number=")
	builder.WriteString(fmt.Sprintf("%v", cu.Number))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", cu.Status))
	builder.WriteString(", ")
	builder.WriteString("ntype=")
	builder.WriteString(fmt.Sprintf("%v", cu.Ntype))
	builder.WriteString(", ")
	builder.WriteString("table=")
	builder.WriteString(cu.Table)
	builder.WriteString(", ")
	builder.WriteString("organization_id=")
	builder.WriteString(fmt.Sprintf("%v", cu.OrganizationID))
	builder.WriteString(", ")
	builder.WriteString("nid=")
	builder.WriteString(fmt.Sprintf("%v", cu.Nid))
	builder.WriteString(", ")
	builder.WriteString("reason=")
	builder.WriteString(cu.Reason)
	builder.WriteString(", ")
	builder.WriteString("operator=")
	builder.WriteString(cu.Operator)
	builder.WriteByte(')')
	return builder.String()
}

// CreditUsages is a parsable slice of CreditUsage.
type CreditUsages []*CreditUsage
