// Code generated by ent, DO NOT EDIT.

package ent

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

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

// MessageRecords is the model entity for the MessageRecords schema.
type MessageRecords 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"`
	// 机器人微信 id
	BotWxid string `json:"bot_wxid,omitempty"`
	// 联系人 id
	ContactID uint64 `json:"contact_id,omitempty"`
	// 类型:1好友,2群组,3企业微信联系人
	ContactType int `json:"contact_type,omitempty"`
	// 接收方微信 id
	ContactWxid string `json:"contact_wxid,omitempty"`
	// 内容类型 1 文本 2 文件
	ContentType int `json:"content_type,omitempty"`
	// 发送内容
	Content string `json:"content,omitempty"`
	// 元数据
	Meta custom_types.Meta `json:"meta,omitempty"`
	// 异常原因
	ErrorDetail string `json:"error_detail,omitempty"`
	// 发送时间
	SendTime time.Time `json:"send_time,omitempty"`
	// 源类型 1 点发 2 群发 3 SOP
	SourceType int `json:"source_type,omitempty"`
	// 源 ID
	SourceID uint64 `json:"source_id,omitempty"`
	// 次源 ID
	SubSourceID uint64 `json:"sub_source_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 MessageRecordsQuery when eager-loading is set.
	Edges        MessageRecordsEdges `json:"edges"`
	selectValues sql.SelectValues
}

// MessageRecordsEdges holds the relations/edges for other nodes in the graph.
type MessageRecordsEdges struct {
	// SopStage holds the value of the sop_stage edge.
	SopStage *SopStage `json:"sop_stage,omitempty"`
	// SopNode holds the value of the sop_node edge.
	SopNode *SopNode `json:"sop_node,omitempty"`
	// MessageContact holds the value of the message_contact edge.
	MessageContact *Contact `json:"message_contact,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]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 MessageRecordsEdges) 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"}
}

// SopNodeOrErr returns the SopNode value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e MessageRecordsEdges) SopNodeOrErr() (*SopNode, error) {
	if e.SopNode != nil {
		return e.SopNode, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: sopnode.Label}
	}
	return nil, &NotLoadedError{edge: "sop_node"}
}

// MessageContactOrErr returns the MessageContact value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e MessageRecordsEdges) MessageContactOrErr() (*Contact, error) {
	if e.MessageContact != nil {
		return e.MessageContact, nil
	} else if e.loadedTypes[2] {
		return nil, &NotFoundError{label: contact.Label}
	}
	return nil, &NotLoadedError{edge: "message_contact"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*MessageRecords) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case messagerecords.FieldMeta:
			values[i] = new([]byte)
		case messagerecords.FieldID, messagerecords.FieldStatus, messagerecords.FieldContactID, messagerecords.FieldContactType, messagerecords.FieldContentType, messagerecords.FieldSourceType, messagerecords.FieldSourceID, messagerecords.FieldSubSourceID, messagerecords.FieldOrganizationID:
			values[i] = new(sql.NullInt64)
		case messagerecords.FieldBotWxid, messagerecords.FieldContactWxid, messagerecords.FieldContent, messagerecords.FieldErrorDetail:
			values[i] = new(sql.NullString)
		case messagerecords.FieldCreatedAt, messagerecords.FieldUpdatedAt, messagerecords.FieldSendTime:
			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 MessageRecords fields.
func (mr *MessageRecords) 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 messagerecords.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			mr.ID = uint64(value.Int64)
		case messagerecords.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 {
				mr.CreatedAt = value.Time
			}
		case messagerecords.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 {
				mr.UpdatedAt = value.Time
			}
		case messagerecords.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				mr.Status = uint8(value.Int64)
			}
		case messagerecords.FieldBotWxid:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field bot_wxid", values[i])
			} else if value.Valid {
				mr.BotWxid = value.String
			}
		case messagerecords.FieldContactID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field contact_id", values[i])
			} else if value.Valid {
				mr.ContactID = uint64(value.Int64)
			}
		case messagerecords.FieldContactType:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field contact_type", values[i])
			} else if value.Valid {
				mr.ContactType = int(value.Int64)
			}
		case messagerecords.FieldContactWxid:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field contact_wxid", values[i])
			} else if value.Valid {
				mr.ContactWxid = value.String
			}
		case messagerecords.FieldContentType:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field content_type", values[i])
			} else if value.Valid {
				mr.ContentType = int(value.Int64)
			}
		case messagerecords.FieldContent:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field content", values[i])
			} else if value.Valid {
				mr.Content = value.String
			}
		case messagerecords.FieldMeta:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field meta", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &mr.Meta); err != nil {
					return fmt.Errorf("unmarshal field meta: %w", err)
				}
			}
		case messagerecords.FieldErrorDetail:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field error_detail", values[i])
			} else if value.Valid {
				mr.ErrorDetail = value.String
			}
		case messagerecords.FieldSendTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field send_time", values[i])
			} else if value.Valid {
				mr.SendTime = value.Time
			}
		case messagerecords.FieldSourceType:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field source_type", values[i])
			} else if value.Valid {
				mr.SourceType = int(value.Int64)
			}
		case messagerecords.FieldSourceID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field source_id", values[i])
			} else if value.Valid {
				mr.SourceID = uint64(value.Int64)
			}
		case messagerecords.FieldSubSourceID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field sub_source_id", values[i])
			} else if value.Valid {
				mr.SubSourceID = uint64(value.Int64)
			}
		case messagerecords.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 {
				mr.OrganizationID = uint64(value.Int64)
			}
		default:
			mr.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QuerySopStage queries the "sop_stage" edge of the MessageRecords entity.
func (mr *MessageRecords) QuerySopStage() *SopStageQuery {
	return NewMessageRecordsClient(mr.config).QuerySopStage(mr)
}

// QuerySopNode queries the "sop_node" edge of the MessageRecords entity.
func (mr *MessageRecords) QuerySopNode() *SopNodeQuery {
	return NewMessageRecordsClient(mr.config).QuerySopNode(mr)
}

// QueryMessageContact queries the "message_contact" edge of the MessageRecords entity.
func (mr *MessageRecords) QueryMessageContact() *ContactQuery {
	return NewMessageRecordsClient(mr.config).QueryMessageContact(mr)
}

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

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

// String implements the fmt.Stringer.
func (mr *MessageRecords) String() string {
	var builder strings.Builder
	builder.WriteString("MessageRecords(")
	builder.WriteString(fmt.Sprintf("id=%v, ", mr.ID))
	builder.WriteString("created_at=")
	builder.WriteString(mr.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(mr.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", mr.Status))
	builder.WriteString(", ")
	builder.WriteString("bot_wxid=")
	builder.WriteString(mr.BotWxid)
	builder.WriteString(", ")
	builder.WriteString("contact_id=")
	builder.WriteString(fmt.Sprintf("%v", mr.ContactID))
	builder.WriteString(", ")
	builder.WriteString("contact_type=")
	builder.WriteString(fmt.Sprintf("%v", mr.ContactType))
	builder.WriteString(", ")
	builder.WriteString("contact_wxid=")
	builder.WriteString(mr.ContactWxid)
	builder.WriteString(", ")
	builder.WriteString("content_type=")
	builder.WriteString(fmt.Sprintf("%v", mr.ContentType))
	builder.WriteString(", ")
	builder.WriteString("content=")
	builder.WriteString(mr.Content)
	builder.WriteString(", ")
	builder.WriteString("meta=")
	builder.WriteString(fmt.Sprintf("%v", mr.Meta))
	builder.WriteString(", ")
	builder.WriteString("error_detail=")
	builder.WriteString(mr.ErrorDetail)
	builder.WriteString(", ")
	builder.WriteString("send_time=")
	builder.WriteString(mr.SendTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("source_type=")
	builder.WriteString(fmt.Sprintf("%v", mr.SourceType))
	builder.WriteString(", ")
	builder.WriteString("source_id=")
	builder.WriteString(fmt.Sprintf("%v", mr.SourceID))
	builder.WriteString(", ")
	builder.WriteString("sub_source_id=")
	builder.WriteString(fmt.Sprintf("%v", mr.SubSourceID))
	builder.WriteString(", ")
	builder.WriteString("organization_id=")
	builder.WriteString(fmt.Sprintf("%v", mr.OrganizationID))
	builder.WriteByte(')')
	return builder.String()
}

// MessageRecordsSlice is a parsable slice of MessageRecords.
type MessageRecordsSlice []*MessageRecords