// Code generated by ent, DO NOT EDIT.

package ent

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

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

// SopNode is the model entity for the SopNode schema.
type SopNode 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"`
	// 阶段 ID
	StageID uint64 `json:"stage_id,omitempty"`
	// 父节点 ID
	ParentID uint64 `json:"parent_id,omitempty"`
	// 节点名称
	Name string `json:"name,omitempty"`
	// 触发条件类型 1 客户回复后触发 2 超时后触发
	ConditionType int `json:"condition_type,omitempty"`
	// 触发语义列表 当为空时则代表用户回复任意内容后触发
	ConditionList []string `json:"condition_list,omitempty"`
	// 超时触发时间（分钟）
	NoReplyCondition uint64 `json:"no_reply_condition,omitempty"`
	// 超时触发时间单位
	NoReplyUnit string `json:"no_reply_unit,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"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the SopNodeQuery when eager-loading is set.
	Edges        SopNodeEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

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

// NodeMessagesOrErr returns the NodeMessages value or an error if the edge
// was not loaded in eager-loading.
func (e SopNodeEdges) NodeMessagesOrErr() ([]*MessageRecords, error) {
	if e.loadedTypes[1] {
		return e.NodeMessages, nil
	}
	return nil, &NotLoadedError{edge: "node_messages"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*SopNode) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case sopnode.FieldConditionList, sopnode.FieldActionMessage, sopnode.FieldActionLabelAdd, sopnode.FieldActionLabelDel, sopnode.FieldActionForward:
			values[i] = new([]byte)
		case sopnode.FieldID, sopnode.FieldStatus, sopnode.FieldStageID, sopnode.FieldParentID, sopnode.FieldConditionType, sopnode.FieldNoReplyCondition:
			values[i] = new(sql.NullInt64)
		case sopnode.FieldName, sopnode.FieldNoReplyUnit:
			values[i] = new(sql.NullString)
		case sopnode.FieldCreatedAt, sopnode.FieldUpdatedAt, sopnode.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 SopNode fields.
func (sn *SopNode) 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 sopnode.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			sn.ID = uint64(value.Int64)
		case sopnode.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 {
				sn.CreatedAt = value.Time
			}
		case sopnode.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 {
				sn.UpdatedAt = value.Time
			}
		case sopnode.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				sn.Status = uint8(value.Int64)
			}
		case sopnode.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 {
				sn.DeletedAt = value.Time
			}
		case sopnode.FieldStageID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field stage_id", values[i])
			} else if value.Valid {
				sn.StageID = uint64(value.Int64)
			}
		case sopnode.FieldParentID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field parent_id", values[i])
			} else if value.Valid {
				sn.ParentID = uint64(value.Int64)
			}
		case sopnode.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				sn.Name = value.String
			}
		case sopnode.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 {
				sn.ConditionType = int(value.Int64)
			}
		case sopnode.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, &sn.ConditionList); err != nil {
					return fmt.Errorf("unmarshal field condition_list: %w", err)
				}
			}
		case sopnode.FieldNoReplyCondition:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field no_reply_condition", values[i])
			} else if value.Valid {
				sn.NoReplyCondition = uint64(value.Int64)
			}
		case sopnode.FieldNoReplyUnit:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field no_reply_unit", values[i])
			} else if value.Valid {
				sn.NoReplyUnit = value.String
			}
		case sopnode.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, &sn.ActionMessage); err != nil {
					return fmt.Errorf("unmarshal field action_message: %w", err)
				}
			}
		case sopnode.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, &sn.ActionLabelAdd); err != nil {
					return fmt.Errorf("unmarshal field action_label_add: %w", err)
				}
			}
		case sopnode.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, &sn.ActionLabelDel); err != nil {
					return fmt.Errorf("unmarshal field action_label_del: %w", err)
				}
			}
		case sopnode.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, &sn.ActionForward); err != nil {
					return fmt.Errorf("unmarshal field action_forward: %w", err)
				}
			}
		default:
			sn.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QuerySopStage queries the "sop_stage" edge of the SopNode entity.
func (sn *SopNode) QuerySopStage() *SopStageQuery {
	return NewSopNodeClient(sn.config).QuerySopStage(sn)
}

// QueryNodeMessages queries the "node_messages" edge of the SopNode entity.
func (sn *SopNode) QueryNodeMessages() *MessageRecordsQuery {
	return NewSopNodeClient(sn.config).QueryNodeMessages(sn)
}

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

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

// String implements the fmt.Stringer.
func (sn *SopNode) String() string {
	var builder strings.Builder
	builder.WriteString("SopNode(")
	builder.WriteString(fmt.Sprintf("id=%v, ", sn.ID))
	builder.WriteString("created_at=")
	builder.WriteString(sn.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(sn.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", sn.Status))
	builder.WriteString(", ")
	builder.WriteString("deleted_at=")
	builder.WriteString(sn.DeletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("stage_id=")
	builder.WriteString(fmt.Sprintf("%v", sn.StageID))
	builder.WriteString(", ")
	builder.WriteString("parent_id=")
	builder.WriteString(fmt.Sprintf("%v", sn.ParentID))
	builder.WriteString(", ")
	builder.WriteString("name=")
	builder.WriteString(sn.Name)
	builder.WriteString(", ")
	builder.WriteString("condition_type=")
	builder.WriteString(fmt.Sprintf("%v", sn.ConditionType))
	builder.WriteString(", ")
	builder.WriteString("condition_list=")
	builder.WriteString(fmt.Sprintf("%v", sn.ConditionList))
	builder.WriteString(", ")
	builder.WriteString("no_reply_condition=")
	builder.WriteString(fmt.Sprintf("%v", sn.NoReplyCondition))
	builder.WriteString(", ")
	builder.WriteString("no_reply_unit=")
	builder.WriteString(sn.NoReplyUnit)
	builder.WriteString(", ")
	builder.WriteString("action_message=")
	builder.WriteString(fmt.Sprintf("%v", sn.ActionMessage))
	builder.WriteString(", ")
	builder.WriteString("action_label_add=")
	builder.WriteString(fmt.Sprintf("%v", sn.ActionLabelAdd))
	builder.WriteString(", ")
	builder.WriteString("action_label_del=")
	builder.WriteString(fmt.Sprintf("%v", sn.ActionLabelDel))
	builder.WriteString(", ")
	builder.WriteString("action_forward=")
	builder.WriteString(fmt.Sprintf("%v", sn.ActionForward))
	builder.WriteByte(')')
	return builder.String()
}

// SopNodes is a parsable slice of SopNode.
type SopNodes []*SopNode
