// Code generated by entc, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"entgo.io/ent/dialect/sql"

	"github.com/pingcap/test-infra/common/model"
	"github.com/pingcap/test-infra/services/tcms/service/store/v2/ent/resourceduration"
)

// ResourceDuration is the model entity for the ResourceDuration schema.
type ResourceDuration struct {
	config `json:"-"`
	// ID of the ent.
	ID model.ID `json:"id,omitempty"`
	// CreatedAt holds the value of the "createdAt" field.
	CreatedAt time.Time `json:"createdAt,omitempty"`
	// UpdatedAt holds the value of the "updatedAt" field.
	UpdatedAt time.Time `json:"updatedAt,omitempty"`
	// PlanExecutionID holds the value of the "planExecutionID" field.
	PlanExecutionID model.ID `json:"planExecutionID,omitempty"`
	// Type holds the value of the "type" field.
	Type model.ResourceDurationType `json:"type,omitempty"`
	// CpuDuration holds the value of the "cpuDuration" field.
	CpuDuration uint64 `json:"cpuDuration,omitempty"`
	// MemoryDuration holds the value of the "memoryDuration" field.
	MemoryDuration uint64 `json:"memoryDuration,omitempty"`
}

// scanValues returns the types for scanning values from sql.Rows.
func (*ResourceDuration) scanValues(columns []string) ([]interface{}, error) {
	values := make([]interface{}, len(columns))
	for i := range columns {
		switch columns[i] {
		case resourceduration.FieldID, resourceduration.FieldPlanExecutionID, resourceduration.FieldCpuDuration, resourceduration.FieldMemoryDuration:
			values[i] = new(sql.NullInt64)
		case resourceduration.FieldType:
			values[i] = new(sql.NullString)
		case resourceduration.FieldCreatedAt, resourceduration.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		default:
			return nil, fmt.Errorf("unexpected column %q for type ResourceDuration", columns[i])
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the ResourceDuration fields.
func (rd *ResourceDuration) assignValues(columns []string, values []interface{}) 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 resourceduration.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			rd.ID = model.ID(value.Int64)
		case resourceduration.FieldCreatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field createdAt", values[i])
			} else if value.Valid {
				rd.CreatedAt = value.Time
			}
		case resourceduration.FieldUpdatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field updatedAt", values[i])
			} else if value.Valid {
				rd.UpdatedAt = value.Time
			}
		case resourceduration.FieldPlanExecutionID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field planExecutionID", values[i])
			} else if value.Valid {
				rd.PlanExecutionID = model.ID(value.Int64)
			}
		case resourceduration.FieldType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field type", values[i])
			} else if value.Valid {
				rd.Type = model.ResourceDurationType(value.String)
			}
		case resourceduration.FieldCpuDuration:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field cpuDuration", values[i])
			} else if value.Valid {
				rd.CpuDuration = uint64(value.Int64)
			}
		case resourceduration.FieldMemoryDuration:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field memoryDuration", values[i])
			} else if value.Valid {
				rd.MemoryDuration = uint64(value.Int64)
			}
		}
	}
	return nil
}

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

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

// String implements the fmt.Stringer.
func (rd *ResourceDuration) String() string {
	var builder strings.Builder
	builder.WriteString("ResourceDuration(")
	builder.WriteString(fmt.Sprintf("id=%v", rd.ID))
	builder.WriteString(", createdAt=")
	builder.WriteString(rd.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", updatedAt=")
	builder.WriteString(rd.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", planExecutionID=")
	builder.WriteString(fmt.Sprintf("%v", rd.PlanExecutionID))
	builder.WriteString(", type=")
	builder.WriteString(fmt.Sprintf("%v", rd.Type))
	builder.WriteString(", cpuDuration=")
	builder.WriteString(fmt.Sprintf("%v", rd.CpuDuration))
	builder.WriteString(", memoryDuration=")
	builder.WriteString(fmt.Sprintf("%v", rd.MemoryDuration))
	builder.WriteByte(')')
	return builder.String()
}

// ResourceDurations is a parsable slice of ResourceDuration.
type ResourceDurations []*ResourceDuration

func (rd ResourceDurations) config(cfg config) {
	for _i := range rd {
		rd[_i].config = cfg
	}
}
