// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/suyuan32/simple-admin-job/ent/custom_types" "github.com/suyuan32/simple-admin-job/ent/messagerecords" "github.com/suyuan32/simple-admin-job/ent/sopnode" "github.com/suyuan32/simple-admin-job/ent/sopstage" ) // SopNodeCreate is the builder for creating a SopNode entity. type SopNodeCreate struct { config mutation *SopNodeMutation hooks []Hook } // SetCreatedAt sets the "created_at" field. func (snc *SopNodeCreate) SetCreatedAt(t time.Time) *SopNodeCreate { snc.mutation.SetCreatedAt(t) return snc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (snc *SopNodeCreate) SetNillableCreatedAt(t *time.Time) *SopNodeCreate { if t != nil { snc.SetCreatedAt(*t) } return snc } // SetUpdatedAt sets the "updated_at" field. func (snc *SopNodeCreate) SetUpdatedAt(t time.Time) *SopNodeCreate { snc.mutation.SetUpdatedAt(t) return snc } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (snc *SopNodeCreate) SetNillableUpdatedAt(t *time.Time) *SopNodeCreate { if t != nil { snc.SetUpdatedAt(*t) } return snc } // SetStatus sets the "status" field. func (snc *SopNodeCreate) SetStatus(u uint8) *SopNodeCreate { snc.mutation.SetStatus(u) return snc } // SetNillableStatus sets the "status" field if the given value is not nil. func (snc *SopNodeCreate) SetNillableStatus(u *uint8) *SopNodeCreate { if u != nil { snc.SetStatus(*u) } return snc } // SetStageID sets the "stage_id" field. func (snc *SopNodeCreate) SetStageID(u uint64) *SopNodeCreate { snc.mutation.SetStageID(u) return snc } // SetParentID sets the "parent_id" field. func (snc *SopNodeCreate) SetParentID(u uint64) *SopNodeCreate { snc.mutation.SetParentID(u) return snc } // SetName sets the "name" field. func (snc *SopNodeCreate) SetName(s string) *SopNodeCreate { snc.mutation.SetName(s) return snc } // SetNillableName sets the "name" field if the given value is not nil. func (snc *SopNodeCreate) SetNillableName(s *string) *SopNodeCreate { if s != nil { snc.SetName(*s) } return snc } // SetConditionType sets the "condition_type" field. func (snc *SopNodeCreate) SetConditionType(i int) *SopNodeCreate { snc.mutation.SetConditionType(i) return snc } // SetNillableConditionType sets the "condition_type" field if the given value is not nil. func (snc *SopNodeCreate) SetNillableConditionType(i *int) *SopNodeCreate { if i != nil { snc.SetConditionType(*i) } return snc } // SetConditionList sets the "condition_list" field. func (snc *SopNodeCreate) SetConditionList(s []string) *SopNodeCreate { snc.mutation.SetConditionList(s) return snc } // SetNoReplyCondition sets the "no_reply_condition" field. func (snc *SopNodeCreate) SetNoReplyCondition(u uint64) *SopNodeCreate { snc.mutation.SetNoReplyCondition(u) return snc } // SetNillableNoReplyCondition sets the "no_reply_condition" field if the given value is not nil. func (snc *SopNodeCreate) SetNillableNoReplyCondition(u *uint64) *SopNodeCreate { if u != nil { snc.SetNoReplyCondition(*u) } return snc } // SetActionMessage sets the "action_message" field. func (snc *SopNodeCreate) SetActionMessage(ct []custom_types.Action) *SopNodeCreate { snc.mutation.SetActionMessage(ct) return snc } // SetActionLabel sets the "action_label" field. func (snc *SopNodeCreate) SetActionLabel(u []uint64) *SopNodeCreate { snc.mutation.SetActionLabel(u) return snc } // SetDeletedAt sets the "deleted_at" field. func (snc *SopNodeCreate) SetDeletedAt(t time.Time) *SopNodeCreate { snc.mutation.SetDeletedAt(t) return snc } // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil. func (snc *SopNodeCreate) SetNillableDeletedAt(t *time.Time) *SopNodeCreate { if t != nil { snc.SetDeletedAt(*t) } return snc } // SetID sets the "id" field. func (snc *SopNodeCreate) SetID(u uint64) *SopNodeCreate { snc.mutation.SetID(u) return snc } // SetSopStageID sets the "sop_stage" edge to the SopStage entity by ID. func (snc *SopNodeCreate) SetSopStageID(id uint64) *SopNodeCreate { snc.mutation.SetSopStageID(id) return snc } // SetSopStage sets the "sop_stage" edge to the SopStage entity. func (snc *SopNodeCreate) SetSopStage(s *SopStage) *SopNodeCreate { return snc.SetSopStageID(s.ID) } // AddNodeMessageIDs adds the "node_messages" edge to the MessageRecords entity by IDs. func (snc *SopNodeCreate) AddNodeMessageIDs(ids ...uint64) *SopNodeCreate { snc.mutation.AddNodeMessageIDs(ids...) return snc } // AddNodeMessages adds the "node_messages" edges to the MessageRecords entity. func (snc *SopNodeCreate) AddNodeMessages(m ...*MessageRecords) *SopNodeCreate { ids := make([]uint64, len(m)) for i := range m { ids[i] = m[i].ID } return snc.AddNodeMessageIDs(ids...) } // Mutation returns the SopNodeMutation object of the builder. func (snc *SopNodeCreate) Mutation() *SopNodeMutation { return snc.mutation } // Save creates the SopNode in the database. func (snc *SopNodeCreate) Save(ctx context.Context) (*SopNode, error) { snc.defaults() return withHooks(ctx, snc.sqlSave, snc.mutation, snc.hooks) } // SaveX calls Save and panics if Save returns an error. func (snc *SopNodeCreate) SaveX(ctx context.Context) *SopNode { v, err := snc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (snc *SopNodeCreate) Exec(ctx context.Context) error { _, err := snc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (snc *SopNodeCreate) ExecX(ctx context.Context) { if err := snc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (snc *SopNodeCreate) defaults() { if _, ok := snc.mutation.CreatedAt(); !ok { v := sopnode.DefaultCreatedAt() snc.mutation.SetCreatedAt(v) } if _, ok := snc.mutation.UpdatedAt(); !ok { v := sopnode.DefaultUpdatedAt() snc.mutation.SetUpdatedAt(v) } if _, ok := snc.mutation.Status(); !ok { v := sopnode.DefaultStatus snc.mutation.SetStatus(v) } if _, ok := snc.mutation.Name(); !ok { v := sopnode.DefaultName snc.mutation.SetName(v) } if _, ok := snc.mutation.ConditionType(); !ok { v := sopnode.DefaultConditionType snc.mutation.SetConditionType(v) } if _, ok := snc.mutation.NoReplyCondition(); !ok { v := sopnode.DefaultNoReplyCondition snc.mutation.SetNoReplyCondition(v) } } // check runs all checks and user-defined validators on the builder. func (snc *SopNodeCreate) check() error { if _, ok := snc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "SopNode.created_at"`)} } if _, ok := snc.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "SopNode.updated_at"`)} } if _, ok := snc.mutation.StageID(); !ok { return &ValidationError{Name: "stage_id", err: errors.New(`ent: missing required field "SopNode.stage_id"`)} } if _, ok := snc.mutation.ParentID(); !ok { return &ValidationError{Name: "parent_id", err: errors.New(`ent: missing required field "SopNode.parent_id"`)} } if _, ok := snc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "SopNode.name"`)} } if _, ok := snc.mutation.ConditionType(); !ok { return &ValidationError{Name: "condition_type", err: errors.New(`ent: missing required field "SopNode.condition_type"`)} } if _, ok := snc.mutation.NoReplyCondition(); !ok { return &ValidationError{Name: "no_reply_condition", err: errors.New(`ent: missing required field "SopNode.no_reply_condition"`)} } if _, ok := snc.mutation.SopStageID(); !ok { return &ValidationError{Name: "sop_stage", err: errors.New(`ent: missing required edge "SopNode.sop_stage"`)} } return nil } func (snc *SopNodeCreate) sqlSave(ctx context.Context) (*SopNode, error) { if err := snc.check(); err != nil { return nil, err } _node, _spec := snc.createSpec() if err := sqlgraph.CreateNode(ctx, snc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } if _spec.ID.Value != _node.ID { id := _spec.ID.Value.(int64) _node.ID = uint64(id) } snc.mutation.id = &_node.ID snc.mutation.done = true return _node, nil } func (snc *SopNodeCreate) createSpec() (*SopNode, *sqlgraph.CreateSpec) { var ( _node = &SopNode{config: snc.config} _spec = sqlgraph.NewCreateSpec(sopnode.Table, sqlgraph.NewFieldSpec(sopnode.FieldID, field.TypeUint64)) ) if id, ok := snc.mutation.ID(); ok { _node.ID = id _spec.ID.Value = id } if value, ok := snc.mutation.CreatedAt(); ok { _spec.SetField(sopnode.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := snc.mutation.UpdatedAt(); ok { _spec.SetField(sopnode.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } if value, ok := snc.mutation.Status(); ok { _spec.SetField(sopnode.FieldStatus, field.TypeUint8, value) _node.Status = value } if value, ok := snc.mutation.ParentID(); ok { _spec.SetField(sopnode.FieldParentID, field.TypeUint64, value) _node.ParentID = value } if value, ok := snc.mutation.Name(); ok { _spec.SetField(sopnode.FieldName, field.TypeString, value) _node.Name = value } if value, ok := snc.mutation.ConditionType(); ok { _spec.SetField(sopnode.FieldConditionType, field.TypeInt, value) _node.ConditionType = value } if value, ok := snc.mutation.ConditionList(); ok { _spec.SetField(sopnode.FieldConditionList, field.TypeJSON, value) _node.ConditionList = value } if value, ok := snc.mutation.NoReplyCondition(); ok { _spec.SetField(sopnode.FieldNoReplyCondition, field.TypeUint64, value) _node.NoReplyCondition = value } if value, ok := snc.mutation.ActionMessage(); ok { _spec.SetField(sopnode.FieldActionMessage, field.TypeJSON, value) _node.ActionMessage = value } if value, ok := snc.mutation.ActionLabel(); ok { _spec.SetField(sopnode.FieldActionLabel, field.TypeJSON, value) _node.ActionLabel = value } if value, ok := snc.mutation.DeletedAt(); ok { _spec.SetField(sopnode.FieldDeletedAt, field.TypeTime, value) _node.DeletedAt = value } if nodes := snc.mutation.SopStageIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: sopnode.SopStageTable, Columns: []string{sopnode.SopStageColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(sopstage.FieldID, field.TypeUint64), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.StageID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := snc.mutation.NodeMessagesIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: sopnode.NodeMessagesTable, Columns: []string{sopnode.NodeMessagesColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // SopNodeCreateBulk is the builder for creating many SopNode entities in bulk. type SopNodeCreateBulk struct { config err error builders []*SopNodeCreate } // Save creates the SopNode entities in the database. func (sncb *SopNodeCreateBulk) Save(ctx context.Context) ([]*SopNode, error) { if sncb.err != nil { return nil, sncb.err } specs := make([]*sqlgraph.CreateSpec, len(sncb.builders)) nodes := make([]*SopNode, len(sncb.builders)) mutators := make([]Mutator, len(sncb.builders)) for i := range sncb.builders { func(i int, root context.Context) { builder := sncb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*SopNodeMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err := builder.check(); err != nil { return nil, err } builder.mutation = mutation var err error nodes[i], specs[i] = builder.createSpec() if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, sncb.builders[i+1].mutation) } else { spec := &sqlgraph.BatchCreateSpec{Nodes: specs} // Invoke the actual operation on the latest mutation in the chain. if err = sqlgraph.BatchCreate(ctx, sncb.driver, spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } } } if err != nil { return nil, err } mutation.id = &nodes[i].ID if specs[i].ID.Value != nil && nodes[i].ID == 0 { id := specs[i].ID.Value.(int64) nodes[i].ID = uint64(id) } mutation.done = true return nodes[i], nil }) for i := len(builder.hooks) - 1; i >= 0; i-- { mut = builder.hooks[i](mut) } mutators[i] = mut }(i, ctx) } if len(mutators) > 0 { if _, err := mutators[0].Mutate(ctx, sncb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (sncb *SopNodeCreateBulk) SaveX(ctx context.Context) []*SopNode { v, err := sncb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (sncb *SopNodeCreateBulk) Exec(ctx context.Context) error { _, err := sncb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (sncb *SopNodeCreateBulk) ExecX(ctx context.Context) { if err := sncb.Exec(ctx); err != nil { panic(err) } }