// Code generated by ent, DO NOT EDIT.

package ent

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

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

// SopStage is the model entity for the SopStage schema.
type SopStage 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 任务 ID
	TaskID uint64 `json:"task_id,omitempty"`
	// 阶段名称
	Name string `json:"name,omitempty"`
	// 客群筛选条件类型  1 按标签筛选 2 按客户基本信息筛选
	ConditionType int `json:"condition_type,omitempty"`
	// 筛选条件关系  1 满足所有条件（and） 2 满足任意条件（or）
	ConditionOperator int `json:"condition_operator,omitempty"`
	// 筛选条件列表
	ConditionList []custom_types.Condition `json:"condition_list,omitempty"`
	// 命中后发送的消息内容
	ActionMessage []custom_types.Action `json:"action_message,omitempty"`
	// 命中后需要打的标签
	ActionLabelAdd []uint64 `json:"action_label_add,omitempty"`
	// 命中后需要移除的标签
	ActionLabelDel []uint64 `json:"action_label_del,omitempty"`
	// 命中后转发的消息
	ActionForward *custom_types.ActionForward `json:"action_forward,omitempty"`
	// 阶段顺序
	IndexSort int `json:"index_sort,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the SopStageQuery when eager-loading is set.
	Edges        SopStageEdges `json:"edges"`
	selectValues sql.SelectValues
}

// SopStageEdges holds the relations/edges for other nodes in the graph.
type SopStageEdges struct {
	// SopTask holds the value of the sop_task edge.
	SopTask *SopTask `json:"sop_task,omitempty"`
	// StageNodes holds the value of the stage_nodes edge.
	StageNodes []*SopNode `json:"stage_nodes,omitempty"`
	// StageMessages holds the value of the stage_messages edge.
	StageMessages []*MessageRecords `json:"stage_messages,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
}

// SopTaskOrErr returns the SopTask value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e SopStageEdges) SopTaskOrErr() (*SopTask, error) {
	if e.SopTask != nil {
		return e.SopTask, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: soptask.Label}
	}
	return nil, &NotLoadedError{edge: "sop_task"}
}

// StageNodesOrErr returns the StageNodes value or an error if the edge
// was not loaded in eager-loading.
func (e SopStageEdges) StageNodesOrErr() ([]*SopNode, error) {
	if e.loadedTypes[1] {
		return e.StageNodes, nil
	}
	return nil, &NotLoadedError{edge: "stage_nodes"}
}

// StageMessagesOrErr returns the StageMessages value or an error if the edge
// was not loaded in eager-loading.
func (e SopStageEdges) StageMessagesOrErr() ([]*MessageRecords, error) {
	if e.loadedTypes[2] {
		return e.StageMessages, nil
	}
	return nil, &NotLoadedError{edge: "stage_messages"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*SopStage) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case sopstage.FieldConditionList, sopstage.FieldActionMessage, sopstage.FieldActionLabelAdd, sopstage.FieldActionLabelDel, sopstage.FieldActionForward:
			values[i] = new([]byte)
		case sopstage.FieldID, sopstage.FieldStatus, sopstage.FieldTaskID, sopstage.FieldConditionType, sopstage.FieldConditionOperator, sopstage.FieldIndexSort:
			values[i] = new(sql.NullInt64)
		case sopstage.FieldName:
			values[i] = new(sql.NullString)
		case sopstage.FieldCreatedAt, sopstage.FieldUpdatedAt, sopstage.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 SopStage fields.
func (ss *SopStage) 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 sopstage.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			ss.ID = uint64(value.Int64)
		case sopstage.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 {
				ss.CreatedAt = value.Time
			}
		case sopstage.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 {
				ss.UpdatedAt = value.Time
			}
		case sopstage.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				ss.Status = uint8(value.Int64)
			}
		case sopstage.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 {
				ss.DeletedAt = value.Time
			}
		case sopstage.FieldTaskID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field task_id", values[i])
			} else if value.Valid {
				ss.TaskID = uint64(value.Int64)
			}
		case sopstage.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				ss.Name = value.String
			}
		case sopstage.FieldConditionType:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field condition_type", values[i])
			} else if value.Valid {
				ss.ConditionType = int(value.Int64)
			}
		case sopstage.FieldConditionOperator:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field condition_operator", values[i])
			} else if value.Valid {
				ss.ConditionOperator = int(value.Int64)
			}
		case sopstage.FieldConditionList:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field condition_list", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &ss.ConditionList); err != nil {
					return fmt.Errorf("unmarshal field condition_list: %w", err)
				}
			}
		case sopstage.FieldActionMessage:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field action_message", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &ss.ActionMessage); err != nil {
					return fmt.Errorf("unmarshal field action_message: %w", err)
				}
			}
		case sopstage.FieldActionLabelAdd:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field action_label_add", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &ss.ActionLabelAdd); err != nil {
					return fmt.Errorf("unmarshal field action_label_add: %w", err)
				}
			}
		case sopstage.FieldActionLabelDel:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field action_label_del", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &ss.ActionLabelDel); err != nil {
					return fmt.Errorf("unmarshal field action_label_del: %w", err)
				}
			}
		case sopstage.FieldActionForward:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field action_forward", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &ss.ActionForward); err != nil {
					return fmt.Errorf("unmarshal field action_forward: %w", err)
				}
			}
		case sopstage.FieldIndexSort:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field index_sort", values[i])
			} else if value.Valid {
				ss.IndexSort = int(value.Int64)
			}
		default:
			ss.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QuerySopTask queries the "sop_task" edge of the SopStage entity.
func (ss *SopStage) QuerySopTask() *SopTaskQuery {
	return NewSopStageClient(ss.config).QuerySopTask(ss)
}

// QueryStageNodes queries the "stage_nodes" edge of the SopStage entity.
func (ss *SopStage) QueryStageNodes() *SopNodeQuery {
	return NewSopStageClient(ss.config).QueryStageNodes(ss)
}

// QueryStageMessages queries the "stage_messages" edge of the SopStage entity.
func (ss *SopStage) QueryStageMessages() *MessageRecordsQuery {
	return NewSopStageClient(ss.config).QueryStageMessages(ss)
}

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

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

// String implements the fmt.Stringer.
func (ss *SopStage) String() string {
	var builder strings.Builder
	builder.WriteString("SopStage(")
	builder.WriteString(fmt.Sprintf("id=%v, ", ss.ID))
	builder.WriteString("created_at=")
	builder.WriteString(ss.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(ss.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", ss.Status))
	builder.WriteString(", ")
	builder.WriteString("deleted_at=")
	builder.WriteString(ss.DeletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("task_id=")
	builder.WriteString(fmt.Sprintf("%v", ss.TaskID))
	builder.WriteString(", ")
	builder.WriteString("name=")
	builder.WriteString(ss.Name)
	builder.WriteString(", ")
	builder.WriteString("condition_type=")
	builder.WriteString(fmt.Sprintf("%v", ss.ConditionType))
	builder.WriteString(", ")
	builder.WriteString("condition_operator=")
	builder.WriteString(fmt.Sprintf("%v", ss.ConditionOperator))
	builder.WriteString(", ")
	builder.WriteString("condition_list=")
	builder.WriteString(fmt.Sprintf("%v", ss.ConditionList))
	builder.WriteString(", ")
	builder.WriteString("action_message=")
	builder.WriteString(fmt.Sprintf("%v", ss.ActionMessage))
	builder.WriteString(", ")
	builder.WriteString("action_label_add=")
	builder.WriteString(fmt.Sprintf("%v", ss.ActionLabelAdd))
	builder.WriteString(", ")
	builder.WriteString("action_label_del=")
	builder.WriteString(fmt.Sprintf("%v", ss.ActionLabelDel))
	builder.WriteString(", ")
	builder.WriteString("action_forward=")
	builder.WriteString(fmt.Sprintf("%v", ss.ActionForward))
	builder.WriteString(", ")
	builder.WriteString("index_sort=")
	builder.WriteString(fmt.Sprintf("%v", ss.IndexSort))
	builder.WriteByte(')')
	return builder.String()
}

// SopStages is a parsable slice of SopStage.
type SopStages []*SopStage
