// Code generated by ent, DO NOT EDIT.

package ent

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

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

// SopTask is the model entity for the SopTask schema.
type SopTask 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"`
	// SOP 任务名称
	Name string `json:"name,omitempty"`
	// 机器人微信 id 列表
	BotWxidList []string `json:"bot_wxid_list,omitempty"`
	// 标签类型:1好友,2群组,3企业微信联系人
	Type int `json:"type,omitempty"`
	// 任务计划开始时间
	PlanStartTime time.Time `json:"plan_start_time,omitempty"`
	// 任务计划结束时间
	PlanEndTime time.Time `json:"plan_end_time,omitempty"`
	// 创建者 id
	CreatorID string `json:"creator_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 SopTaskQuery when eager-loading is set.
	Edges        SopTaskEdges `json:"edges"`
	selectValues sql.SelectValues
}

// SopTaskEdges holds the relations/edges for other nodes in the graph.
type SopTaskEdges struct {
	// TaskStages holds the value of the task_stages edge.
	TaskStages []*SopStage `json:"task_stages,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
}

// TaskStagesOrErr returns the TaskStages value or an error if the edge
// was not loaded in eager-loading.
func (e SopTaskEdges) TaskStagesOrErr() ([]*SopStage, error) {
	if e.loadedTypes[0] {
		return e.TaskStages, nil
	}
	return nil, &NotLoadedError{edge: "task_stages"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*SopTask) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case soptask.FieldBotWxidList:
			values[i] = new([]byte)
		case soptask.FieldID, soptask.FieldStatus, soptask.FieldType, soptask.FieldOrganizationID:
			values[i] = new(sql.NullInt64)
		case soptask.FieldName, soptask.FieldCreatorID:
			values[i] = new(sql.NullString)
		case soptask.FieldCreatedAt, soptask.FieldUpdatedAt, soptask.FieldDeletedAt, soptask.FieldPlanStartTime, soptask.FieldPlanEndTime:
			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 SopTask fields.
func (st *SopTask) 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 soptask.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			st.ID = uint64(value.Int64)
		case soptask.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 {
				st.CreatedAt = value.Time
			}
		case soptask.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 {
				st.UpdatedAt = value.Time
			}
		case soptask.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				st.Status = uint8(value.Int64)
			}
		case soptask.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 {
				st.DeletedAt = value.Time
			}
		case soptask.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				st.Name = value.String
			}
		case soptask.FieldBotWxidList:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field bot_wxid_list", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &st.BotWxidList); err != nil {
					return fmt.Errorf("unmarshal field bot_wxid_list: %w", err)
				}
			}
		case soptask.FieldType:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field type", values[i])
			} else if value.Valid {
				st.Type = int(value.Int64)
			}
		case soptask.FieldPlanStartTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field plan_start_time", values[i])
			} else if value.Valid {
				st.PlanStartTime = value.Time
			}
		case soptask.FieldPlanEndTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field plan_end_time", values[i])
			} else if value.Valid {
				st.PlanEndTime = value.Time
			}
		case soptask.FieldCreatorID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field creator_id", values[i])
			} else if value.Valid {
				st.CreatorID = value.String
			}
		case soptask.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 {
				st.OrganizationID = uint64(value.Int64)
			}
		default:
			st.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryTaskStages queries the "task_stages" edge of the SopTask entity.
func (st *SopTask) QueryTaskStages() *SopStageQuery {
	return NewSopTaskClient(st.config).QueryTaskStages(st)
}

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

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

// String implements the fmt.Stringer.
func (st *SopTask) String() string {
	var builder strings.Builder
	builder.WriteString("SopTask(")
	builder.WriteString(fmt.Sprintf("id=%v, ", st.ID))
	builder.WriteString("created_at=")
	builder.WriteString(st.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(st.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", st.Status))
	builder.WriteString(", ")
	builder.WriteString("deleted_at=")
	builder.WriteString(st.DeletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("name=")
	builder.WriteString(st.Name)
	builder.WriteString(", ")
	builder.WriteString("bot_wxid_list=")
	builder.WriteString(fmt.Sprintf("%v", st.BotWxidList))
	builder.WriteString(", ")
	builder.WriteString("type=")
	builder.WriteString(fmt.Sprintf("%v", st.Type))
	builder.WriteString(", ")
	builder.WriteString("plan_start_time=")
	builder.WriteString(st.PlanStartTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("plan_end_time=")
	builder.WriteString(st.PlanEndTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("creator_id=")
	builder.WriteString(st.CreatorID)
	builder.WriteString(", ")
	builder.WriteString("organization_id=")
	builder.WriteString(fmt.Sprintf("%v", st.OrganizationID))
	builder.WriteByte(')')
	return builder.String()
}

// SopTasks is a parsable slice of SopTask.
type SopTasks []*SopTask