// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"galileo/ent/task"
	"strings"
	"time"

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

// Task is the model entity for the Task schema.
type Task struct {
	config `json:"-"`
	// ID of the ent.
	// 任务ID
	ID int64 `json:"id,omitempty"`
	// 任务名称
	Name string `json:"name,omitempty"`
	// 任务创建时间
	CreatedAt time.Time `json:"created_at,omitempty"`
	// 任务创建人
	CreatedBy uint32 `json:"created_by,omitempty"`
	// 任务经办人
	Assignee uint32 `json:"assignee,omitempty"`
	// 任务类型：实时任务 0、延时任务 1、定时任务 2、响应式任务 3
	Type int8 `json:"type,omitempty"`
	// 定时任务的频率，仅用于定时任务
	Frequency int8 `json:"frequency,omitempty"`
	// 预期调度时间: 实时任务为空，延时任务取年月日时分秒，定时任务取 时分秒+频率
	ScheduleTime time.Time `json:"schedule_time,omitempty"`
	// 任务优先级
	Rank int8 `json:"rank,omitempty"`
	// 任务状态
	Status int8 `json:"status,omitempty"`
	// 任务开始时间
	StartTime time.Time `json:"start_time,omitempty"`
	// 任务完成时间
	CompletedAt time.Time `json:"completed_at,omitempty"`
	// 数据库记录更新时间
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// 任务更新人
	UpdatedBy uint32 `json:"updated_by,omitempty"`
	// 任务状态更新时间
	StatusUpdatedAt time.Time `json:"status_updated_at,omitempty"`
	// 任务截止日期
	Deadline time.Time `json:"deadline,omitempty"`
	// 任务删除时间
	DeletedAt time.Time `json:"deleted_at,omitempty"`
	// 任务删除人
	DeletedBy uint32 `json:"deleted_by,omitempty"`
	// 任务描述
	Description string `json:"description,omitempty"`
	// 任务所属的测试计划
	TestplanID int64 `json:"testplan_id,omitempty"`
	// 测试用例集合
	TestcaseSuite []int64 `json:"testcase_suite,omitempty"`
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Task) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case task.FieldTestcaseSuite:
			values[i] = new([]byte)
		case task.FieldID, task.FieldCreatedBy, task.FieldAssignee, task.FieldType, task.FieldFrequency, task.FieldRank, task.FieldStatus, task.FieldUpdatedBy, task.FieldDeletedBy, task.FieldTestplanID:
			values[i] = new(sql.NullInt64)
		case task.FieldName, task.FieldDescription:
			values[i] = new(sql.NullString)
		case task.FieldCreatedAt, task.FieldScheduleTime, task.FieldStartTime, task.FieldCompletedAt, task.FieldUpdatedAt, task.FieldStatusUpdatedAt, task.FieldDeadline, task.FieldDeletedAt:
			values[i] = new(sql.NullTime)
		default:
			return nil, fmt.Errorf("unexpected column %q for type Task", columns[i])
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Task fields.
func (t *Task) 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 task.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			t.ID = int64(value.Int64)
		case task.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				t.Name = value.String
			}
		case task.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 {
				t.CreatedAt = value.Time
			}
		case task.FieldCreatedBy:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field created_by", values[i])
			} else if value.Valid {
				t.CreatedBy = uint32(value.Int64)
			}
		case task.FieldAssignee:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field assignee", values[i])
			} else if value.Valid {
				t.Assignee = uint32(value.Int64)
			}
		case task.FieldType:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field type", values[i])
			} else if value.Valid {
				t.Type = int8(value.Int64)
			}
		case task.FieldFrequency:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field frequency", values[i])
			} else if value.Valid {
				t.Frequency = int8(value.Int64)
			}
		case task.FieldScheduleTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field schedule_time", values[i])
			} else if value.Valid {
				t.ScheduleTime = value.Time
			}
		case task.FieldRank:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field rank", values[i])
			} else if value.Valid {
				t.Rank = int8(value.Int64)
			}
		case task.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				t.Status = int8(value.Int64)
			}
		case task.FieldStartTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field start_time", values[i])
			} else if value.Valid {
				t.StartTime = value.Time
			}
		case task.FieldCompletedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field completed_at", values[i])
			} else if value.Valid {
				t.CompletedAt = value.Time
			}
		case task.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 {
				t.UpdatedAt = value.Time
			}
		case task.FieldUpdatedBy:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field updated_by", values[i])
			} else if value.Valid {
				t.UpdatedBy = uint32(value.Int64)
			}
		case task.FieldStatusUpdatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field status_updated_at", values[i])
			} else if value.Valid {
				t.StatusUpdatedAt = value.Time
			}
		case task.FieldDeadline:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field deadline", values[i])
			} else if value.Valid {
				t.Deadline = value.Time
			}
		case task.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 {
				t.DeletedAt = value.Time
			}
		case task.FieldDeletedBy:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field deleted_by", values[i])
			} else if value.Valid {
				t.DeletedBy = uint32(value.Int64)
			}
		case task.FieldDescription:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field description", values[i])
			} else if value.Valid {
				t.Description = value.String
			}
		case task.FieldTestplanID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field testplan_id", values[i])
			} else if value.Valid {
				t.TestplanID = value.Int64
			}
		case task.FieldTestcaseSuite:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field testcase_suite", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &t.TestcaseSuite); err != nil {
					return fmt.Errorf("unmarshal field testcase_suite: %w", err)
				}
			}
		}
	}
	return nil
}

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

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

// String implements the fmt.Stringer.
func (t *Task) String() string {
	var builder strings.Builder
	builder.WriteString("Task(")
	builder.WriteString(fmt.Sprintf("id=%v, ", t.ID))
	builder.WriteString("name=")
	builder.WriteString(t.Name)
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(t.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("created_by=")
	builder.WriteString(fmt.Sprintf("%v", t.CreatedBy))
	builder.WriteString(", ")
	builder.WriteString("assignee=")
	builder.WriteString(fmt.Sprintf("%v", t.Assignee))
	builder.WriteString(", ")
	builder.WriteString("type=")
	builder.WriteString(fmt.Sprintf("%v", t.Type))
	builder.WriteString(", ")
	builder.WriteString("frequency=")
	builder.WriteString(fmt.Sprintf("%v", t.Frequency))
	builder.WriteString(", ")
	builder.WriteString("schedule_time=")
	builder.WriteString(t.ScheduleTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("rank=")
	builder.WriteString(fmt.Sprintf("%v", t.Rank))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", t.Status))
	builder.WriteString(", ")
	builder.WriteString("start_time=")
	builder.WriteString(t.StartTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("completed_at=")
	builder.WriteString(t.CompletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(t.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_by=")
	builder.WriteString(fmt.Sprintf("%v", t.UpdatedBy))
	builder.WriteString(", ")
	builder.WriteString("status_updated_at=")
	builder.WriteString(t.StatusUpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("deadline=")
	builder.WriteString(t.Deadline.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("deleted_at=")
	builder.WriteString(t.DeletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("deleted_by=")
	builder.WriteString(fmt.Sprintf("%v", t.DeletedBy))
	builder.WriteString(", ")
	builder.WriteString("description=")
	builder.WriteString(t.Description)
	builder.WriteString(", ")
	builder.WriteString("testplan_id=")
	builder.WriteString(fmt.Sprintf("%v", t.TestplanID))
	builder.WriteString(", ")
	builder.WriteString("testcase_suite=")
	builder.WriteString(fmt.Sprintf("%v", t.TestcaseSuite))
	builder.WriteByte(')')
	return builder.String()
}

// Tasks is a parsable slice of Task.
type Tasks []*Task
