// 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/sopstage" "github.com/suyuan32/simple-admin-job/ent/soptask" ) // SopTaskCreate is the builder for creating a SopTask entity. type SopTaskCreate struct { config mutation *SopTaskMutation hooks []Hook } // SetCreatedAt sets the "created_at" field. func (stc *SopTaskCreate) SetCreatedAt(t time.Time) *SopTaskCreate { stc.mutation.SetCreatedAt(t) return stc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (stc *SopTaskCreate) SetNillableCreatedAt(t *time.Time) *SopTaskCreate { if t != nil { stc.SetCreatedAt(*t) } return stc } // SetUpdatedAt sets the "updated_at" field. func (stc *SopTaskCreate) SetUpdatedAt(t time.Time) *SopTaskCreate { stc.mutation.SetUpdatedAt(t) return stc } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (stc *SopTaskCreate) SetNillableUpdatedAt(t *time.Time) *SopTaskCreate { if t != nil { stc.SetUpdatedAt(*t) } return stc } // SetStatus sets the "status" field. func (stc *SopTaskCreate) SetStatus(u uint8) *SopTaskCreate { stc.mutation.SetStatus(u) return stc } // SetNillableStatus sets the "status" field if the given value is not nil. func (stc *SopTaskCreate) SetNillableStatus(u *uint8) *SopTaskCreate { if u != nil { stc.SetStatus(*u) } return stc } // SetName sets the "name" field. func (stc *SopTaskCreate) SetName(s string) *SopTaskCreate { stc.mutation.SetName(s) return stc } // SetBotWxidList sets the "bot_wxid_list" field. func (stc *SopTaskCreate) SetBotWxidList(s []string) *SopTaskCreate { stc.mutation.SetBotWxidList(s) return stc } // SetType sets the "type" field. func (stc *SopTaskCreate) SetType(i int) *SopTaskCreate { stc.mutation.SetType(i) return stc } // SetNillableType sets the "type" field if the given value is not nil. func (stc *SopTaskCreate) SetNillableType(i *int) *SopTaskCreate { if i != nil { stc.SetType(*i) } return stc } // SetPlanStartTime sets the "plan_start_time" field. func (stc *SopTaskCreate) SetPlanStartTime(t time.Time) *SopTaskCreate { stc.mutation.SetPlanStartTime(t) return stc } // SetNillablePlanStartTime sets the "plan_start_time" field if the given value is not nil. func (stc *SopTaskCreate) SetNillablePlanStartTime(t *time.Time) *SopTaskCreate { if t != nil { stc.SetPlanStartTime(*t) } return stc } // SetPlanEndTime sets the "plan_end_time" field. func (stc *SopTaskCreate) SetPlanEndTime(t time.Time) *SopTaskCreate { stc.mutation.SetPlanEndTime(t) return stc } // SetNillablePlanEndTime sets the "plan_end_time" field if the given value is not nil. func (stc *SopTaskCreate) SetNillablePlanEndTime(t *time.Time) *SopTaskCreate { if t != nil { stc.SetPlanEndTime(*t) } return stc } // SetCreatorID sets the "creator_id" field. func (stc *SopTaskCreate) SetCreatorID(s string) *SopTaskCreate { stc.mutation.SetCreatorID(s) return stc } // SetNillableCreatorID sets the "creator_id" field if the given value is not nil. func (stc *SopTaskCreate) SetNillableCreatorID(s *string) *SopTaskCreate { if s != nil { stc.SetCreatorID(*s) } return stc } // SetDeletedAt sets the "deleted_at" field. func (stc *SopTaskCreate) SetDeletedAt(t time.Time) *SopTaskCreate { stc.mutation.SetDeletedAt(t) return stc } // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil. func (stc *SopTaskCreate) SetNillableDeletedAt(t *time.Time) *SopTaskCreate { if t != nil { stc.SetDeletedAt(*t) } return stc } // SetID sets the "id" field. func (stc *SopTaskCreate) SetID(u uint64) *SopTaskCreate { stc.mutation.SetID(u) return stc } // AddTaskStageIDs adds the "task_stages" edge to the SopStage entity by IDs. func (stc *SopTaskCreate) AddTaskStageIDs(ids ...uint64) *SopTaskCreate { stc.mutation.AddTaskStageIDs(ids...) return stc } // AddTaskStages adds the "task_stages" edges to the SopStage entity. func (stc *SopTaskCreate) AddTaskStages(s ...*SopStage) *SopTaskCreate { ids := make([]uint64, len(s)) for i := range s { ids[i] = s[i].ID } return stc.AddTaskStageIDs(ids...) } // Mutation returns the SopTaskMutation object of the builder. func (stc *SopTaskCreate) Mutation() *SopTaskMutation { return stc.mutation } // Save creates the SopTask in the database. func (stc *SopTaskCreate) Save(ctx context.Context) (*SopTask, error) { stc.defaults() return withHooks(ctx, stc.sqlSave, stc.mutation, stc.hooks) } // SaveX calls Save and panics if Save returns an error. func (stc *SopTaskCreate) SaveX(ctx context.Context) *SopTask { v, err := stc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (stc *SopTaskCreate) Exec(ctx context.Context) error { _, err := stc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (stc *SopTaskCreate) ExecX(ctx context.Context) { if err := stc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (stc *SopTaskCreate) defaults() { if _, ok := stc.mutation.CreatedAt(); !ok { v := soptask.DefaultCreatedAt() stc.mutation.SetCreatedAt(v) } if _, ok := stc.mutation.UpdatedAt(); !ok { v := soptask.DefaultUpdatedAt() stc.mutation.SetUpdatedAt(v) } if _, ok := stc.mutation.Status(); !ok { v := soptask.DefaultStatus stc.mutation.SetStatus(v) } if _, ok := stc.mutation.GetType(); !ok { v := soptask.DefaultType stc.mutation.SetType(v) } } // check runs all checks and user-defined validators on the builder. func (stc *SopTaskCreate) check() error { if _, ok := stc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "SopTask.created_at"`)} } if _, ok := stc.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "SopTask.updated_at"`)} } if _, ok := stc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "SopTask.name"`)} } if v, ok := stc.mutation.Name(); ok { if err := soptask.NameValidator(v); err != nil { return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "SopTask.name": %w`, err)} } } if _, ok := stc.mutation.GetType(); !ok { return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "SopTask.type"`)} } return nil } func (stc *SopTaskCreate) sqlSave(ctx context.Context) (*SopTask, error) { if err := stc.check(); err != nil { return nil, err } _node, _spec := stc.createSpec() if err := sqlgraph.CreateNode(ctx, stc.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) } stc.mutation.id = &_node.ID stc.mutation.done = true return _node, nil } func (stc *SopTaskCreate) createSpec() (*SopTask, *sqlgraph.CreateSpec) { var ( _node = &SopTask{config: stc.config} _spec = sqlgraph.NewCreateSpec(soptask.Table, sqlgraph.NewFieldSpec(soptask.FieldID, field.TypeUint64)) ) if id, ok := stc.mutation.ID(); ok { _node.ID = id _spec.ID.Value = id } if value, ok := stc.mutation.CreatedAt(); ok { _spec.SetField(soptask.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := stc.mutation.UpdatedAt(); ok { _spec.SetField(soptask.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } if value, ok := stc.mutation.Status(); ok { _spec.SetField(soptask.FieldStatus, field.TypeUint8, value) _node.Status = value } if value, ok := stc.mutation.Name(); ok { _spec.SetField(soptask.FieldName, field.TypeString, value) _node.Name = value } if value, ok := stc.mutation.BotWxidList(); ok { _spec.SetField(soptask.FieldBotWxidList, field.TypeJSON, value) _node.BotWxidList = value } if value, ok := stc.mutation.GetType(); ok { _spec.SetField(soptask.FieldType, field.TypeInt, value) _node.Type = value } if value, ok := stc.mutation.PlanStartTime(); ok { _spec.SetField(soptask.FieldPlanStartTime, field.TypeTime, value) _node.PlanStartTime = value } if value, ok := stc.mutation.PlanEndTime(); ok { _spec.SetField(soptask.FieldPlanEndTime, field.TypeTime, value) _node.PlanEndTime = value } if value, ok := stc.mutation.CreatorID(); ok { _spec.SetField(soptask.FieldCreatorID, field.TypeString, value) _node.CreatorID = value } if value, ok := stc.mutation.DeletedAt(); ok { _spec.SetField(soptask.FieldDeletedAt, field.TypeTime, value) _node.DeletedAt = value } if nodes := stc.mutation.TaskStagesIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: soptask.TaskStagesTable, Columns: []string{soptask.TaskStagesColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(sopstage.FieldID, field.TypeUint64), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // SopTaskCreateBulk is the builder for creating many SopTask entities in bulk. type SopTaskCreateBulk struct { config err error builders []*SopTaskCreate } // Save creates the SopTask entities in the database. func (stcb *SopTaskCreateBulk) Save(ctx context.Context) ([]*SopTask, error) { if stcb.err != nil { return nil, stcb.err } specs := make([]*sqlgraph.CreateSpec, len(stcb.builders)) nodes := make([]*SopTask, len(stcb.builders)) mutators := make([]Mutator, len(stcb.builders)) for i := range stcb.builders { func(i int, root context.Context) { builder := stcb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*SopTaskMutation) 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, stcb.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, stcb.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, stcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (stcb *SopTaskCreateBulk) SaveX(ctx context.Context) []*SopTask { v, err := stcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (stcb *SopTaskCreateBulk) Exec(ctx context.Context) error { _, err := stcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (stcb *SopTaskCreateBulk) ExecX(ctx context.Context) { if err := stcb.Exec(ctx); err != nil { panic(err) } }