// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/cloudwego/biz-demo/open-payment-platform/internal/payment/infrastructure/ent/order"
)

// OrderCreate is the builder for creating a Order entity.
type OrderCreate struct {
	config
	mutation *OrderMutation
	hooks    []Hook
}

// SetMerchantID sets the "merchant_id" field.
func (oc *OrderCreate) SetMerchantID(s string) *OrderCreate {
	oc.mutation.SetMerchantID(s)
	return oc
}

// SetChannel sets the "channel" field.
func (oc *OrderCreate) SetChannel(s string) *OrderCreate {
	oc.mutation.SetChannel(s)
	return oc
}

// SetPayWay sets the "pay_way" field.
func (oc *OrderCreate) SetPayWay(s string) *OrderCreate {
	oc.mutation.SetPayWay(s)
	return oc
}

// SetOutOrderNo sets the "out_order_no" field.
func (oc *OrderCreate) SetOutOrderNo(s string) *OrderCreate {
	oc.mutation.SetOutOrderNo(s)
	return oc
}

// SetTotalAmount sets the "total_amount" field.
func (oc *OrderCreate) SetTotalAmount(u uint64) *OrderCreate {
	oc.mutation.SetTotalAmount(u)
	return oc
}

// SetBody sets the "body" field.
func (oc *OrderCreate) SetBody(s string) *OrderCreate {
	oc.mutation.SetBody(s)
	return oc
}

// SetOrderStatus sets the "order_status" field.
func (oc *OrderCreate) SetOrderStatus(i int8) *OrderCreate {
	oc.mutation.SetOrderStatus(i)
	return oc
}

// SetAuthCode sets the "auth_code" field.
func (oc *OrderCreate) SetAuthCode(s string) *OrderCreate {
	oc.mutation.SetAuthCode(s)
	return oc
}

// SetWxAppid sets the "wx_appid" field.
func (oc *OrderCreate) SetWxAppid(s string) *OrderCreate {
	oc.mutation.SetWxAppid(s)
	return oc
}

// SetSubOpenid sets the "sub_openid" field.
func (oc *OrderCreate) SetSubOpenid(s string) *OrderCreate {
	oc.mutation.SetSubOpenid(s)
	return oc
}

// SetJumpURL sets the "jump_url" field.
func (oc *OrderCreate) SetJumpURL(s string) *OrderCreate {
	oc.mutation.SetJumpURL(s)
	return oc
}

// SetNotifyURL sets the "notify_url" field.
func (oc *OrderCreate) SetNotifyURL(s string) *OrderCreate {
	oc.mutation.SetNotifyURL(s)
	return oc
}

// SetClientIP sets the "client_ip" field.
func (oc *OrderCreate) SetClientIP(s string) *OrderCreate {
	oc.mutation.SetClientIP(s)
	return oc
}

// SetAttach sets the "attach" field.
func (oc *OrderCreate) SetAttach(s string) *OrderCreate {
	oc.mutation.SetAttach(s)
	return oc
}

// SetOrderExpiration sets the "order_expiration" field.
func (oc *OrderCreate) SetOrderExpiration(s string) *OrderCreate {
	oc.mutation.SetOrderExpiration(s)
	return oc
}

// SetExtendParams sets the "extend_params" field.
func (oc *OrderCreate) SetExtendParams(s string) *OrderCreate {
	oc.mutation.SetExtendParams(s)
	return oc
}

// Mutation returns the OrderMutation object of the builder.
func (oc *OrderCreate) Mutation() *OrderMutation {
	return oc.mutation
}

// Save creates the Order in the database.
func (oc *OrderCreate) Save(ctx context.Context) (*Order, error) {
	var (
		err  error
		node *Order
	)
	if len(oc.hooks) == 0 {
		if err = oc.check(); err != nil {
			return nil, err
		}
		node, err = oc.sqlSave(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*OrderMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			if err = oc.check(); err != nil {
				return nil, err
			}
			oc.mutation = mutation
			if node, err = oc.sqlSave(ctx); err != nil {
				return nil, err
			}
			mutation.id = &node.ID
			mutation.done = true
			return node, err
		})
		for i := len(oc.hooks) - 1; i >= 0; i-- {
			if oc.hooks[i] == nil {
				return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
			}
			mut = oc.hooks[i](mut)
		}
		v, err := mut.Mutate(ctx, oc.mutation)
		if err != nil {
			return nil, err
		}
		nv, ok := v.(*Order)
		if !ok {
			return nil, fmt.Errorf("unexpected node type %T returned from OrderMutation", v)
		}
		node = nv
	}
	return node, err
}

// SaveX calls Save and panics if Save returns an error.
func (oc *OrderCreate) SaveX(ctx context.Context) *Order {
	v, err := oc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (oc *OrderCreate) Exec(ctx context.Context) error {
	_, err := oc.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (oc *OrderCreate) ExecX(ctx context.Context) {
	if err := oc.Exec(ctx); err != nil {
		panic(err)
	}
}

// check runs all checks and user-defined validators on the builder.
func (oc *OrderCreate) check() error {
	if _, ok := oc.mutation.MerchantID(); !ok {
		return &ValidationError{Name: "merchant_id", err: errors.New(`ent: missing required field "Order.merchant_id"`)}
	}
	if _, ok := oc.mutation.Channel(); !ok {
		return &ValidationError{Name: "channel", err: errors.New(`ent: missing required field "Order.channel"`)}
	}
	if _, ok := oc.mutation.PayWay(); !ok {
		return &ValidationError{Name: "pay_way", err: errors.New(`ent: missing required field "Order.pay_way"`)}
	}
	if _, ok := oc.mutation.OutOrderNo(); !ok {
		return &ValidationError{Name: "out_order_no", err: errors.New(`ent: missing required field "Order.out_order_no"`)}
	}
	if _, ok := oc.mutation.TotalAmount(); !ok {
		return &ValidationError{Name: "total_amount", err: errors.New(`ent: missing required field "Order.total_amount"`)}
	}
	if _, ok := oc.mutation.Body(); !ok {
		return &ValidationError{Name: "body", err: errors.New(`ent: missing required field "Order.body"`)}
	}
	if _, ok := oc.mutation.OrderStatus(); !ok {
		return &ValidationError{Name: "order_status", err: errors.New(`ent: missing required field "Order.order_status"`)}
	}
	if _, ok := oc.mutation.AuthCode(); !ok {
		return &ValidationError{Name: "auth_code", err: errors.New(`ent: missing required field "Order.auth_code"`)}
	}
	if _, ok := oc.mutation.WxAppid(); !ok {
		return &ValidationError{Name: "wx_appid", err: errors.New(`ent: missing required field "Order.wx_appid"`)}
	}
	if _, ok := oc.mutation.SubOpenid(); !ok {
		return &ValidationError{Name: "sub_openid", err: errors.New(`ent: missing required field "Order.sub_openid"`)}
	}
	if _, ok := oc.mutation.JumpURL(); !ok {
		return &ValidationError{Name: "jump_url", err: errors.New(`ent: missing required field "Order.jump_url"`)}
	}
	if _, ok := oc.mutation.NotifyURL(); !ok {
		return &ValidationError{Name: "notify_url", err: errors.New(`ent: missing required field "Order.notify_url"`)}
	}
	if _, ok := oc.mutation.ClientIP(); !ok {
		return &ValidationError{Name: "client_ip", err: errors.New(`ent: missing required field "Order.client_ip"`)}
	}
	if _, ok := oc.mutation.Attach(); !ok {
		return &ValidationError{Name: "attach", err: errors.New(`ent: missing required field "Order.attach"`)}
	}
	if _, ok := oc.mutation.OrderExpiration(); !ok {
		return &ValidationError{Name: "order_expiration", err: errors.New(`ent: missing required field "Order.order_expiration"`)}
	}
	if _, ok := oc.mutation.ExtendParams(); !ok {
		return &ValidationError{Name: "extend_params", err: errors.New(`ent: missing required field "Order.extend_params"`)}
	}
	return nil
}

func (oc *OrderCreate) sqlSave(ctx context.Context) (*Order, error) {
	_node, _spec := oc.createSpec()
	if err := sqlgraph.CreateNode(ctx, oc.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)
	return _node, nil
}

func (oc *OrderCreate) createSpec() (*Order, *sqlgraph.CreateSpec) {
	var (
		_node = &Order{config: oc.config}
		_spec = &sqlgraph.CreateSpec{
			Table: order.Table,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: order.FieldID,
			},
		}
	)
	if value, ok := oc.mutation.MerchantID(); ok {
		_spec.SetField(order.FieldMerchantID, field.TypeString, value)
		_node.MerchantID = value
	}
	if value, ok := oc.mutation.Channel(); ok {
		_spec.SetField(order.FieldChannel, field.TypeString, value)
		_node.Channel = value
	}
	if value, ok := oc.mutation.PayWay(); ok {
		_spec.SetField(order.FieldPayWay, field.TypeString, value)
		_node.PayWay = value
	}
	if value, ok := oc.mutation.OutOrderNo(); ok {
		_spec.SetField(order.FieldOutOrderNo, field.TypeString, value)
		_node.OutOrderNo = value
	}
	if value, ok := oc.mutation.TotalAmount(); ok {
		_spec.SetField(order.FieldTotalAmount, field.TypeUint64, value)
		_node.TotalAmount = value
	}
	if value, ok := oc.mutation.Body(); ok {
		_spec.SetField(order.FieldBody, field.TypeString, value)
		_node.Body = value
	}
	if value, ok := oc.mutation.OrderStatus(); ok {
		_spec.SetField(order.FieldOrderStatus, field.TypeInt8, value)
		_node.OrderStatus = value
	}
	if value, ok := oc.mutation.AuthCode(); ok {
		_spec.SetField(order.FieldAuthCode, field.TypeString, value)
		_node.AuthCode = value
	}
	if value, ok := oc.mutation.WxAppid(); ok {
		_spec.SetField(order.FieldWxAppid, field.TypeString, value)
		_node.WxAppid = value
	}
	if value, ok := oc.mutation.SubOpenid(); ok {
		_spec.SetField(order.FieldSubOpenid, field.TypeString, value)
		_node.SubOpenid = value
	}
	if value, ok := oc.mutation.JumpURL(); ok {
		_spec.SetField(order.FieldJumpURL, field.TypeString, value)
		_node.JumpURL = value
	}
	if value, ok := oc.mutation.NotifyURL(); ok {
		_spec.SetField(order.FieldNotifyURL, field.TypeString, value)
		_node.NotifyURL = value
	}
	if value, ok := oc.mutation.ClientIP(); ok {
		_spec.SetField(order.FieldClientIP, field.TypeString, value)
		_node.ClientIP = value
	}
	if value, ok := oc.mutation.Attach(); ok {
		_spec.SetField(order.FieldAttach, field.TypeString, value)
		_node.Attach = value
	}
	if value, ok := oc.mutation.OrderExpiration(); ok {
		_spec.SetField(order.FieldOrderExpiration, field.TypeString, value)
		_node.OrderExpiration = value
	}
	if value, ok := oc.mutation.ExtendParams(); ok {
		_spec.SetField(order.FieldExtendParams, field.TypeString, value)
		_node.ExtendParams = value
	}
	return _node, _spec
}

// OrderCreateBulk is the builder for creating many Order entities in bulk.
type OrderCreateBulk struct {
	config
	builders []*OrderCreate
}

// Save creates the Order entities in the database.
func (ocb *OrderCreateBulk) Save(ctx context.Context) ([]*Order, error) {
	specs := make([]*sqlgraph.CreateSpec, len(ocb.builders))
	nodes := make([]*Order, len(ocb.builders))
	mutators := make([]Mutator, len(ocb.builders))
	for i := range ocb.builders {
		func(i int, root context.Context) {
			builder := ocb.builders[i]
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*OrderMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				nodes[i], specs[i] = builder.createSpec()
				var err error
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, ocb.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, ocb.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, ocb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (ocb *OrderCreateBulk) SaveX(ctx context.Context) []*Order {
	v, err := ocb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (ocb *OrderCreateBulk) Exec(ctx context.Context) error {
	_, err := ocb.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (ocb *OrderCreateBulk) ExecX(ctx context.Context) {
	if err := ocb.Exec(ctx); err != nil {
		panic(err)
	}
}
