// Code generated by ent, DO NOT EDIT. package ent import ( "bj_power_mes/ent/lossledger" "context" "errors" "fmt" "time" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // LossLedgerCreate is the builder for creating a LossLedger entity. type LossLedgerCreate struct { config mutation *LossLedgerMutation hooks []Hook } // SetStationNo sets the "stationNo" field. func (_c *LossLedgerCreate) SetStationNo(v int) *LossLedgerCreate { _c.mutation.SetStationNo(v) return _c } // SetMaterialCode sets the "materialCode" field. func (_c *LossLedgerCreate) SetMaterialCode(v string) *LossLedgerCreate { _c.mutation.SetMaterialCode(v) return _c } // SetMaterialName sets the "materialName" field. func (_c *LossLedgerCreate) SetMaterialName(v string) *LossLedgerCreate { _c.mutation.SetMaterialName(v) return _c } // SetNillableMaterialName sets the "materialName" field if the given value is not nil. func (_c *LossLedgerCreate) SetNillableMaterialName(v *string) *LossLedgerCreate { if v != nil { _c.SetMaterialName(*v) } return _c } // SetTheoryQty sets the "theoryQty" field. func (_c *LossLedgerCreate) SetTheoryQty(v float64) *LossLedgerCreate { _c.mutation.SetTheoryQty(v) return _c } // SetActualQty sets the "actualQty" field. func (_c *LossLedgerCreate) SetActualQty(v float64) *LossLedgerCreate { _c.mutation.SetActualQty(v) return _c } // SetLossQty sets the "lossQty" field. func (_c *LossLedgerCreate) SetLossQty(v float64) *LossLedgerCreate { _c.mutation.SetLossQty(v) return _c } // SetReason sets the "reason" field. func (_c *LossLedgerCreate) SetReason(v string) *LossLedgerCreate { _c.mutation.SetReason(v) return _c } // SetReturnNo sets the "returnNo" field. func (_c *LossLedgerCreate) SetReturnNo(v string) *LossLedgerCreate { _c.mutation.SetReturnNo(v) return _c } // SetNillableReturnNo sets the "returnNo" field if the given value is not nil. func (_c *LossLedgerCreate) SetNillableReturnNo(v *string) *LossLedgerCreate { if v != nil { _c.SetReturnNo(*v) } return _c } // SetWorkOrderNo sets the "workOrderNo" field. func (_c *LossLedgerCreate) SetWorkOrderNo(v string) *LossLedgerCreate { _c.mutation.SetWorkOrderNo(v) return _c } // SetNillableWorkOrderNo sets the "workOrderNo" field if the given value is not nil. func (_c *LossLedgerCreate) SetNillableWorkOrderNo(v *string) *LossLedgerCreate { if v != nil { _c.SetWorkOrderNo(*v) } return _c } // SetStatus sets the "status" field. func (_c *LossLedgerCreate) SetStatus(v string) *LossLedgerCreate { _c.mutation.SetStatus(v) return _c } // SetNillableStatus sets the "status" field if the given value is not nil. func (_c *LossLedgerCreate) SetNillableStatus(v *string) *LossLedgerCreate { if v != nil { _c.SetStatus(*v) } return _c } // SetComment sets the "comment" field. func (_c *LossLedgerCreate) SetComment(v string) *LossLedgerCreate { _c.mutation.SetComment(v) return _c } // SetNillableComment sets the "comment" field if the given value is not nil. func (_c *LossLedgerCreate) SetNillableComment(v *string) *LossLedgerCreate { if v != nil { _c.SetComment(*v) } return _c } // SetCommenter sets the "commenter" field. func (_c *LossLedgerCreate) SetCommenter(v string) *LossLedgerCreate { _c.mutation.SetCommenter(v) return _c } // SetNillableCommenter sets the "commenter" field if the given value is not nil. func (_c *LossLedgerCreate) SetNillableCommenter(v *string) *LossLedgerCreate { if v != nil { _c.SetCommenter(*v) } return _c } // SetCommentedAt sets the "commentedAt" field. func (_c *LossLedgerCreate) SetCommentedAt(v int64) *LossLedgerCreate { _c.mutation.SetCommentedAt(v) return _c } // SetNillableCommentedAt sets the "commentedAt" field if the given value is not nil. func (_c *LossLedgerCreate) SetNillableCommentedAt(v *int64) *LossLedgerCreate { if v != nil { _c.SetCommentedAt(*v) } return _c } // SetOperator sets the "operator" field. func (_c *LossLedgerCreate) SetOperator(v string) *LossLedgerCreate { _c.mutation.SetOperator(v) return _c } // SetNillableOperator sets the "operator" field if the given value is not nil. func (_c *LossLedgerCreate) SetNillableOperator(v *string) *LossLedgerCreate { if v != nil { _c.SetOperator(*v) } return _c } // SetCreatedAt sets the "createdAt" field. func (_c *LossLedgerCreate) SetCreatedAt(v time.Time) *LossLedgerCreate { _c.mutation.SetCreatedAt(v) return _c } // SetNillableCreatedAt sets the "createdAt" field if the given value is not nil. func (_c *LossLedgerCreate) SetNillableCreatedAt(v *time.Time) *LossLedgerCreate { if v != nil { _c.SetCreatedAt(*v) } return _c } // Mutation returns the LossLedgerMutation object of the builder. func (_c *LossLedgerCreate) Mutation() *LossLedgerMutation { return _c.mutation } // Save creates the LossLedger in the database. func (_c *LossLedgerCreate) Save(ctx context.Context) (*LossLedger, error) { _c.defaults() return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *LossLedgerCreate) SaveX(ctx context.Context) *LossLedger { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *LossLedgerCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *LossLedgerCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (_c *LossLedgerCreate) defaults() { if _, ok := _c.mutation.MaterialName(); !ok { v := lossledger.DefaultMaterialName _c.mutation.SetMaterialName(v) } if _, ok := _c.mutation.ReturnNo(); !ok { v := lossledger.DefaultReturnNo _c.mutation.SetReturnNo(v) } if _, ok := _c.mutation.WorkOrderNo(); !ok { v := lossledger.DefaultWorkOrderNo _c.mutation.SetWorkOrderNo(v) } if _, ok := _c.mutation.Status(); !ok { v := lossledger.DefaultStatus _c.mutation.SetStatus(v) } if _, ok := _c.mutation.Comment(); !ok { v := lossledger.DefaultComment _c.mutation.SetComment(v) } if _, ok := _c.mutation.Commenter(); !ok { v := lossledger.DefaultCommenter _c.mutation.SetCommenter(v) } if _, ok := _c.mutation.CommentedAt(); !ok { v := lossledger.DefaultCommentedAt _c.mutation.SetCommentedAt(v) } if _, ok := _c.mutation.Operator(); !ok { v := lossledger.DefaultOperator _c.mutation.SetOperator(v) } if _, ok := _c.mutation.CreatedAt(); !ok { v := lossledger.DefaultCreatedAt() _c.mutation.SetCreatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (_c *LossLedgerCreate) check() error { if _, ok := _c.mutation.StationNo(); !ok { return &ValidationError{Name: "stationNo", err: errors.New(`ent: missing required field "LossLedger.stationNo"`)} } if _, ok := _c.mutation.MaterialCode(); !ok { return &ValidationError{Name: "materialCode", err: errors.New(`ent: missing required field "LossLedger.materialCode"`)} } if v, ok := _c.mutation.MaterialCode(); ok { if err := lossledger.MaterialCodeValidator(v); err != nil { return &ValidationError{Name: "materialCode", err: fmt.Errorf(`ent: validator failed for field "LossLedger.materialCode": %w`, err)} } } if _, ok := _c.mutation.MaterialName(); !ok { return &ValidationError{Name: "materialName", err: errors.New(`ent: missing required field "LossLedger.materialName"`)} } if v, ok := _c.mutation.MaterialName(); ok { if err := lossledger.MaterialNameValidator(v); err != nil { return &ValidationError{Name: "materialName", err: fmt.Errorf(`ent: validator failed for field "LossLedger.materialName": %w`, err)} } } if _, ok := _c.mutation.TheoryQty(); !ok { return &ValidationError{Name: "theoryQty", err: errors.New(`ent: missing required field "LossLedger.theoryQty"`)} } if _, ok := _c.mutation.ActualQty(); !ok { return &ValidationError{Name: "actualQty", err: errors.New(`ent: missing required field "LossLedger.actualQty"`)} } if _, ok := _c.mutation.LossQty(); !ok { return &ValidationError{Name: "lossQty", err: errors.New(`ent: missing required field "LossLedger.lossQty"`)} } if _, ok := _c.mutation.Reason(); !ok { return &ValidationError{Name: "reason", err: errors.New(`ent: missing required field "LossLedger.reason"`)} } if v, ok := _c.mutation.Reason(); ok { if err := lossledger.ReasonValidator(v); err != nil { return &ValidationError{Name: "reason", err: fmt.Errorf(`ent: validator failed for field "LossLedger.reason": %w`, err)} } } if _, ok := _c.mutation.ReturnNo(); !ok { return &ValidationError{Name: "returnNo", err: errors.New(`ent: missing required field "LossLedger.returnNo"`)} } if v, ok := _c.mutation.ReturnNo(); ok { if err := lossledger.ReturnNoValidator(v); err != nil { return &ValidationError{Name: "returnNo", err: fmt.Errorf(`ent: validator failed for field "LossLedger.returnNo": %w`, err)} } } if _, ok := _c.mutation.WorkOrderNo(); !ok { return &ValidationError{Name: "workOrderNo", err: errors.New(`ent: missing required field "LossLedger.workOrderNo"`)} } if v, ok := _c.mutation.WorkOrderNo(); ok { if err := lossledger.WorkOrderNoValidator(v); err != nil { return &ValidationError{Name: "workOrderNo", err: fmt.Errorf(`ent: validator failed for field "LossLedger.workOrderNo": %w`, err)} } } if _, ok := _c.mutation.Status(); !ok { return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "LossLedger.status"`)} } if v, ok := _c.mutation.Status(); ok { if err := lossledger.StatusValidator(v); err != nil { return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "LossLedger.status": %w`, err)} } } if _, ok := _c.mutation.Comment(); !ok { return &ValidationError{Name: "comment", err: errors.New(`ent: missing required field "LossLedger.comment"`)} } if v, ok := _c.mutation.Comment(); ok { if err := lossledger.CommentValidator(v); err != nil { return &ValidationError{Name: "comment", err: fmt.Errorf(`ent: validator failed for field "LossLedger.comment": %w`, err)} } } if _, ok := _c.mutation.Commenter(); !ok { return &ValidationError{Name: "commenter", err: errors.New(`ent: missing required field "LossLedger.commenter"`)} } if v, ok := _c.mutation.Commenter(); ok { if err := lossledger.CommenterValidator(v); err != nil { return &ValidationError{Name: "commenter", err: fmt.Errorf(`ent: validator failed for field "LossLedger.commenter": %w`, err)} } } if _, ok := _c.mutation.CommentedAt(); !ok { return &ValidationError{Name: "commentedAt", err: errors.New(`ent: missing required field "LossLedger.commentedAt"`)} } if _, ok := _c.mutation.Operator(); !ok { return &ValidationError{Name: "operator", err: errors.New(`ent: missing required field "LossLedger.operator"`)} } if v, ok := _c.mutation.Operator(); ok { if err := lossledger.OperatorValidator(v); err != nil { return &ValidationError{Name: "operator", err: fmt.Errorf(`ent: validator failed for field "LossLedger.operator": %w`, err)} } } if _, ok := _c.mutation.CreatedAt(); !ok { return &ValidationError{Name: "createdAt", err: errors.New(`ent: missing required field "LossLedger.createdAt"`)} } return nil } func (_c *LossLedgerCreate) sqlSave(ctx context.Context) (*LossLedger, error) { if err := _c.check(); err != nil { return nil, err } _node, _spec := _c.createSpec() if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } id := _spec.ID.Value.(int64) _node.ID = int(id) _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *LossLedgerCreate) createSpec() (*LossLedger, *sqlgraph.CreateSpec) { var ( _node = &LossLedger{config: _c.config} _spec = sqlgraph.NewCreateSpec(lossledger.Table, sqlgraph.NewFieldSpec(lossledger.FieldID, field.TypeInt)) ) if value, ok := _c.mutation.StationNo(); ok { _spec.SetField(lossledger.FieldStationNo, field.TypeInt, value) _node.StationNo = value } if value, ok := _c.mutation.MaterialCode(); ok { _spec.SetField(lossledger.FieldMaterialCode, field.TypeString, value) _node.MaterialCode = value } if value, ok := _c.mutation.MaterialName(); ok { _spec.SetField(lossledger.FieldMaterialName, field.TypeString, value) _node.MaterialName = value } if value, ok := _c.mutation.TheoryQty(); ok { _spec.SetField(lossledger.FieldTheoryQty, field.TypeFloat64, value) _node.TheoryQty = value } if value, ok := _c.mutation.ActualQty(); ok { _spec.SetField(lossledger.FieldActualQty, field.TypeFloat64, value) _node.ActualQty = value } if value, ok := _c.mutation.LossQty(); ok { _spec.SetField(lossledger.FieldLossQty, field.TypeFloat64, value) _node.LossQty = value } if value, ok := _c.mutation.Reason(); ok { _spec.SetField(lossledger.FieldReason, field.TypeString, value) _node.Reason = value } if value, ok := _c.mutation.ReturnNo(); ok { _spec.SetField(lossledger.FieldReturnNo, field.TypeString, value) _node.ReturnNo = value } if value, ok := _c.mutation.WorkOrderNo(); ok { _spec.SetField(lossledger.FieldWorkOrderNo, field.TypeString, value) _node.WorkOrderNo = value } if value, ok := _c.mutation.Status(); ok { _spec.SetField(lossledger.FieldStatus, field.TypeString, value) _node.Status = value } if value, ok := _c.mutation.Comment(); ok { _spec.SetField(lossledger.FieldComment, field.TypeString, value) _node.Comment = value } if value, ok := _c.mutation.Commenter(); ok { _spec.SetField(lossledger.FieldCommenter, field.TypeString, value) _node.Commenter = value } if value, ok := _c.mutation.CommentedAt(); ok { _spec.SetField(lossledger.FieldCommentedAt, field.TypeInt64, value) _node.CommentedAt = value } if value, ok := _c.mutation.Operator(); ok { _spec.SetField(lossledger.FieldOperator, field.TypeString, value) _node.Operator = value } if value, ok := _c.mutation.CreatedAt(); ok { _spec.SetField(lossledger.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } return _node, _spec } // LossLedgerCreateBulk is the builder for creating many LossLedger entities in bulk. type LossLedgerCreateBulk struct { config err error builders []*LossLedgerCreate } // Save creates the LossLedger entities in the database. func (_c *LossLedgerCreateBulk) Save(ctx context.Context) ([]*LossLedger, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*LossLedger, len(_c.builders)) mutators := make([]Mutator, len(_c.builders)) for i := range _c.builders { func(i int, root context.Context) { builder := _c.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*LossLedgerMutation) 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, _c.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, _c.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 { id := specs[i].ID.Value.(int64) nodes[i].ID = int(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, _c.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (_c *LossLedgerCreateBulk) SaveX(ctx context.Context) []*LossLedger { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *LossLedgerCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *LossLedgerCreateBulk) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } }