// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/compapiasynctask"
	"wechat-api/ent/predicate"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// CompapiAsynctaskUpdate is the builder for updating CompapiAsynctask entities.
type CompapiAsynctaskUpdate struct {
	config
	hooks    []Hook
	mutation *CompapiAsynctaskMutation
}

// Where appends a list predicates to the CompapiAsynctaskUpdate builder.
func (cau *CompapiAsynctaskUpdate) Where(ps ...predicate.CompapiAsynctask) *CompapiAsynctaskUpdate {
	cau.mutation.Where(ps...)
	return cau
}

// SetUpdatedAt sets the "updated_at" field.
func (cau *CompapiAsynctaskUpdate) SetUpdatedAt(t time.Time) *CompapiAsynctaskUpdate {
	cau.mutation.SetUpdatedAt(t)
	return cau
}

// SetAuthToken sets the "auth_token" field.
func (cau *CompapiAsynctaskUpdate) SetAuthToken(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetAuthToken(s)
	return cau
}

// SetNillableAuthToken sets the "auth_token" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableAuthToken(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetAuthToken(*s)
	}
	return cau
}

// SetEventType sets the "event_type" field.
func (cau *CompapiAsynctaskUpdate) SetEventType(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetEventType(s)
	return cau
}

// SetNillableEventType sets the "event_type" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableEventType(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetEventType(*s)
	}
	return cau
}

// SetChatID sets the "chat_id" field.
func (cau *CompapiAsynctaskUpdate) SetChatID(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetChatID(s)
	return cau
}

// SetNillableChatID sets the "chat_id" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableChatID(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetChatID(*s)
	}
	return cau
}

// ClearChatID clears the value of the "chat_id" field.
func (cau *CompapiAsynctaskUpdate) ClearChatID() *CompapiAsynctaskUpdate {
	cau.mutation.ClearChatID()
	return cau
}

// SetResponseChatItemID sets the "response_chat_item_id" field.
func (cau *CompapiAsynctaskUpdate) SetResponseChatItemID(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetResponseChatItemID(s)
	return cau
}

// SetNillableResponseChatItemID sets the "response_chat_item_id" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableResponseChatItemID(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetResponseChatItemID(*s)
	}
	return cau
}

// ClearResponseChatItemID clears the value of the "response_chat_item_id" field.
func (cau *CompapiAsynctaskUpdate) ClearResponseChatItemID() *CompapiAsynctaskUpdate {
	cau.mutation.ClearResponseChatItemID()
	return cau
}

// SetOrganizationID sets the "organization_id" field.
func (cau *CompapiAsynctaskUpdate) SetOrganizationID(u uint64) *CompapiAsynctaskUpdate {
	cau.mutation.ResetOrganizationID()
	cau.mutation.SetOrganizationID(u)
	return cau
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableOrganizationID(u *uint64) *CompapiAsynctaskUpdate {
	if u != nil {
		cau.SetOrganizationID(*u)
	}
	return cau
}

// AddOrganizationID adds u to the "organization_id" field.
func (cau *CompapiAsynctaskUpdate) AddOrganizationID(u int64) *CompapiAsynctaskUpdate {
	cau.mutation.AddOrganizationID(u)
	return cau
}

// SetOpenaiBase sets the "openai_base" field.
func (cau *CompapiAsynctaskUpdate) SetOpenaiBase(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetOpenaiBase(s)
	return cau
}

// SetNillableOpenaiBase sets the "openai_base" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableOpenaiBase(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetOpenaiBase(*s)
	}
	return cau
}

// SetOpenaiKey sets the "openai_key" field.
func (cau *CompapiAsynctaskUpdate) SetOpenaiKey(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetOpenaiKey(s)
	return cau
}

// SetNillableOpenaiKey sets the "openai_key" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableOpenaiKey(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetOpenaiKey(*s)
	}
	return cau
}

// SetRequestRaw sets the "request_raw" field.
func (cau *CompapiAsynctaskUpdate) SetRequestRaw(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetRequestRaw(s)
	return cau
}

// SetNillableRequestRaw sets the "request_raw" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableRequestRaw(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetRequestRaw(*s)
	}
	return cau
}

// SetResponseRaw sets the "response_raw" field.
func (cau *CompapiAsynctaskUpdate) SetResponseRaw(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetResponseRaw(s)
	return cau
}

// SetNillableResponseRaw sets the "response_raw" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableResponseRaw(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetResponseRaw(*s)
	}
	return cau
}

// ClearResponseRaw clears the value of the "response_raw" field.
func (cau *CompapiAsynctaskUpdate) ClearResponseRaw() *CompapiAsynctaskUpdate {
	cau.mutation.ClearResponseRaw()
	return cau
}

// SetCallbackURL sets the "callback_url" field.
func (cau *CompapiAsynctaskUpdate) SetCallbackURL(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetCallbackURL(s)
	return cau
}

// SetNillableCallbackURL sets the "callback_url" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableCallbackURL(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetCallbackURL(*s)
	}
	return cau
}

// SetCallbackResponseRaw sets the "callback_response_raw" field.
func (cau *CompapiAsynctaskUpdate) SetCallbackResponseRaw(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetCallbackResponseRaw(s)
	return cau
}

// SetNillableCallbackResponseRaw sets the "callback_response_raw" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableCallbackResponseRaw(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetCallbackResponseRaw(*s)
	}
	return cau
}

// ClearCallbackResponseRaw clears the value of the "callback_response_raw" field.
func (cau *CompapiAsynctaskUpdate) ClearCallbackResponseRaw() *CompapiAsynctaskUpdate {
	cau.mutation.ClearCallbackResponseRaw()
	return cau
}

// SetModel sets the "model" field.
func (cau *CompapiAsynctaskUpdate) SetModel(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetModel(s)
	return cau
}

// SetNillableModel sets the "model" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableModel(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetModel(*s)
	}
	return cau
}

// ClearModel clears the value of the "model" field.
func (cau *CompapiAsynctaskUpdate) ClearModel() *CompapiAsynctaskUpdate {
	cau.mutation.ClearModel()
	return cau
}

// SetTaskStatus sets the "task_status" field.
func (cau *CompapiAsynctaskUpdate) SetTaskStatus(i int8) *CompapiAsynctaskUpdate {
	cau.mutation.ResetTaskStatus()
	cau.mutation.SetTaskStatus(i)
	return cau
}

// SetNillableTaskStatus sets the "task_status" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableTaskStatus(i *int8) *CompapiAsynctaskUpdate {
	if i != nil {
		cau.SetTaskStatus(*i)
	}
	return cau
}

// AddTaskStatus adds i to the "task_status" field.
func (cau *CompapiAsynctaskUpdate) AddTaskStatus(i int8) *CompapiAsynctaskUpdate {
	cau.mutation.AddTaskStatus(i)
	return cau
}

// ClearTaskStatus clears the value of the "task_status" field.
func (cau *CompapiAsynctaskUpdate) ClearTaskStatus() *CompapiAsynctaskUpdate {
	cau.mutation.ClearTaskStatus()
	return cau
}

// SetRetryCount sets the "retry_count" field.
func (cau *CompapiAsynctaskUpdate) SetRetryCount(i int8) *CompapiAsynctaskUpdate {
	cau.mutation.ResetRetryCount()
	cau.mutation.SetRetryCount(i)
	return cau
}

// SetNillableRetryCount sets the "retry_count" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableRetryCount(i *int8) *CompapiAsynctaskUpdate {
	if i != nil {
		cau.SetRetryCount(*i)
	}
	return cau
}

// AddRetryCount adds i to the "retry_count" field.
func (cau *CompapiAsynctaskUpdate) AddRetryCount(i int8) *CompapiAsynctaskUpdate {
	cau.mutation.AddRetryCount(i)
	return cau
}

// ClearRetryCount clears the value of the "retry_count" field.
func (cau *CompapiAsynctaskUpdate) ClearRetryCount() *CompapiAsynctaskUpdate {
	cau.mutation.ClearRetryCount()
	return cau
}

// SetLastError sets the "last_error" field.
func (cau *CompapiAsynctaskUpdate) SetLastError(s string) *CompapiAsynctaskUpdate {
	cau.mutation.SetLastError(s)
	return cau
}

// SetNillableLastError sets the "last_error" field if the given value is not nil.
func (cau *CompapiAsynctaskUpdate) SetNillableLastError(s *string) *CompapiAsynctaskUpdate {
	if s != nil {
		cau.SetLastError(*s)
	}
	return cau
}

// ClearLastError clears the value of the "last_error" field.
func (cau *CompapiAsynctaskUpdate) ClearLastError() *CompapiAsynctaskUpdate {
	cau.mutation.ClearLastError()
	return cau
}

// Mutation returns the CompapiAsynctaskMutation object of the builder.
func (cau *CompapiAsynctaskUpdate) Mutation() *CompapiAsynctaskMutation {
	return cau.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (cau *CompapiAsynctaskUpdate) Save(ctx context.Context) (int, error) {
	cau.defaults()
	return withHooks(ctx, cau.sqlSave, cau.mutation, cau.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (cau *CompapiAsynctaskUpdate) SaveX(ctx context.Context) int {
	affected, err := cau.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

// defaults sets the default values of the builder before save.
func (cau *CompapiAsynctaskUpdate) defaults() {
	if _, ok := cau.mutation.UpdatedAt(); !ok {
		v := compapiasynctask.UpdateDefaultUpdatedAt()
		cau.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (cau *CompapiAsynctaskUpdate) check() error {
	if v, ok := cau.mutation.OrganizationID(); ok {
		if err := compapiasynctask.OrganizationIDValidator(v); err != nil {
			return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "CompapiAsynctask.organization_id": %w`, err)}
		}
	}
	if v, ok := cau.mutation.CallbackURL(); ok {
		if err := compapiasynctask.CallbackURLValidator(v); err != nil {
			return &ValidationError{Name: "callback_url", err: fmt.Errorf(`ent: validator failed for field "CompapiAsynctask.callback_url": %w`, err)}
		}
	}
	return nil
}

func (cau *CompapiAsynctaskUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := cau.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(compapiasynctask.Table, compapiasynctask.Columns, sqlgraph.NewFieldSpec(compapiasynctask.FieldID, field.TypeUint64))
	if ps := cau.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := cau.mutation.UpdatedAt(); ok {
		_spec.SetField(compapiasynctask.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := cau.mutation.AuthToken(); ok {
		_spec.SetField(compapiasynctask.FieldAuthToken, field.TypeString, value)
	}
	if value, ok := cau.mutation.EventType(); ok {
		_spec.SetField(compapiasynctask.FieldEventType, field.TypeString, value)
	}
	if value, ok := cau.mutation.ChatID(); ok {
		_spec.SetField(compapiasynctask.FieldChatID, field.TypeString, value)
	}
	if cau.mutation.ChatIDCleared() {
		_spec.ClearField(compapiasynctask.FieldChatID, field.TypeString)
	}
	if value, ok := cau.mutation.ResponseChatItemID(); ok {
		_spec.SetField(compapiasynctask.FieldResponseChatItemID, field.TypeString, value)
	}
	if cau.mutation.ResponseChatItemIDCleared() {
		_spec.ClearField(compapiasynctask.FieldResponseChatItemID, field.TypeString)
	}
	if value, ok := cau.mutation.OrganizationID(); ok {
		_spec.SetField(compapiasynctask.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := cau.mutation.AddedOrganizationID(); ok {
		_spec.AddField(compapiasynctask.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := cau.mutation.OpenaiBase(); ok {
		_spec.SetField(compapiasynctask.FieldOpenaiBase, field.TypeString, value)
	}
	if value, ok := cau.mutation.OpenaiKey(); ok {
		_spec.SetField(compapiasynctask.FieldOpenaiKey, field.TypeString, value)
	}
	if value, ok := cau.mutation.RequestRaw(); ok {
		_spec.SetField(compapiasynctask.FieldRequestRaw, field.TypeString, value)
	}
	if value, ok := cau.mutation.ResponseRaw(); ok {
		_spec.SetField(compapiasynctask.FieldResponseRaw, field.TypeString, value)
	}
	if cau.mutation.ResponseRawCleared() {
		_spec.ClearField(compapiasynctask.FieldResponseRaw, field.TypeString)
	}
	if value, ok := cau.mutation.CallbackURL(); ok {
		_spec.SetField(compapiasynctask.FieldCallbackURL, field.TypeString, value)
	}
	if value, ok := cau.mutation.CallbackResponseRaw(); ok {
		_spec.SetField(compapiasynctask.FieldCallbackResponseRaw, field.TypeString, value)
	}
	if cau.mutation.CallbackResponseRawCleared() {
		_spec.ClearField(compapiasynctask.FieldCallbackResponseRaw, field.TypeString)
	}
	if value, ok := cau.mutation.Model(); ok {
		_spec.SetField(compapiasynctask.FieldModel, field.TypeString, value)
	}
	if cau.mutation.ModelCleared() {
		_spec.ClearField(compapiasynctask.FieldModel, field.TypeString)
	}
	if value, ok := cau.mutation.TaskStatus(); ok {
		_spec.SetField(compapiasynctask.FieldTaskStatus, field.TypeInt8, value)
	}
	if value, ok := cau.mutation.AddedTaskStatus(); ok {
		_spec.AddField(compapiasynctask.FieldTaskStatus, field.TypeInt8, value)
	}
	if cau.mutation.TaskStatusCleared() {
		_spec.ClearField(compapiasynctask.FieldTaskStatus, field.TypeInt8)
	}
	if value, ok := cau.mutation.RetryCount(); ok {
		_spec.SetField(compapiasynctask.FieldRetryCount, field.TypeInt8, value)
	}
	if value, ok := cau.mutation.AddedRetryCount(); ok {
		_spec.AddField(compapiasynctask.FieldRetryCount, field.TypeInt8, value)
	}
	if cau.mutation.RetryCountCleared() {
		_spec.ClearField(compapiasynctask.FieldRetryCount, field.TypeInt8)
	}
	if value, ok := cau.mutation.LastError(); ok {
		_spec.SetField(compapiasynctask.FieldLastError, field.TypeString, value)
	}
	if cau.mutation.LastErrorCleared() {
		_spec.ClearField(compapiasynctask.FieldLastError, field.TypeString)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, cau.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{compapiasynctask.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	cau.mutation.done = true
	return n, nil
}

// CompapiAsynctaskUpdateOne is the builder for updating a single CompapiAsynctask entity.
type CompapiAsynctaskUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *CompapiAsynctaskMutation
}

// SetUpdatedAt sets the "updated_at" field.
func (cauo *CompapiAsynctaskUpdateOne) SetUpdatedAt(t time.Time) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetUpdatedAt(t)
	return cauo
}

// SetAuthToken sets the "auth_token" field.
func (cauo *CompapiAsynctaskUpdateOne) SetAuthToken(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetAuthToken(s)
	return cauo
}

// SetNillableAuthToken sets the "auth_token" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableAuthToken(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetAuthToken(*s)
	}
	return cauo
}

// SetEventType sets the "event_type" field.
func (cauo *CompapiAsynctaskUpdateOne) SetEventType(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetEventType(s)
	return cauo
}

// SetNillableEventType sets the "event_type" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableEventType(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetEventType(*s)
	}
	return cauo
}

// SetChatID sets the "chat_id" field.
func (cauo *CompapiAsynctaskUpdateOne) SetChatID(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetChatID(s)
	return cauo
}

// SetNillableChatID sets the "chat_id" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableChatID(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetChatID(*s)
	}
	return cauo
}

// ClearChatID clears the value of the "chat_id" field.
func (cauo *CompapiAsynctaskUpdateOne) ClearChatID() *CompapiAsynctaskUpdateOne {
	cauo.mutation.ClearChatID()
	return cauo
}

// SetResponseChatItemID sets the "response_chat_item_id" field.
func (cauo *CompapiAsynctaskUpdateOne) SetResponseChatItemID(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetResponseChatItemID(s)
	return cauo
}

// SetNillableResponseChatItemID sets the "response_chat_item_id" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableResponseChatItemID(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetResponseChatItemID(*s)
	}
	return cauo
}

// ClearResponseChatItemID clears the value of the "response_chat_item_id" field.
func (cauo *CompapiAsynctaskUpdateOne) ClearResponseChatItemID() *CompapiAsynctaskUpdateOne {
	cauo.mutation.ClearResponseChatItemID()
	return cauo
}

// SetOrganizationID sets the "organization_id" field.
func (cauo *CompapiAsynctaskUpdateOne) SetOrganizationID(u uint64) *CompapiAsynctaskUpdateOne {
	cauo.mutation.ResetOrganizationID()
	cauo.mutation.SetOrganizationID(u)
	return cauo
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableOrganizationID(u *uint64) *CompapiAsynctaskUpdateOne {
	if u != nil {
		cauo.SetOrganizationID(*u)
	}
	return cauo
}

// AddOrganizationID adds u to the "organization_id" field.
func (cauo *CompapiAsynctaskUpdateOne) AddOrganizationID(u int64) *CompapiAsynctaskUpdateOne {
	cauo.mutation.AddOrganizationID(u)
	return cauo
}

// SetOpenaiBase sets the "openai_base" field.
func (cauo *CompapiAsynctaskUpdateOne) SetOpenaiBase(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetOpenaiBase(s)
	return cauo
}

// SetNillableOpenaiBase sets the "openai_base" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableOpenaiBase(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetOpenaiBase(*s)
	}
	return cauo
}

// SetOpenaiKey sets the "openai_key" field.
func (cauo *CompapiAsynctaskUpdateOne) SetOpenaiKey(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetOpenaiKey(s)
	return cauo
}

// SetNillableOpenaiKey sets the "openai_key" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableOpenaiKey(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetOpenaiKey(*s)
	}
	return cauo
}

// SetRequestRaw sets the "request_raw" field.
func (cauo *CompapiAsynctaskUpdateOne) SetRequestRaw(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetRequestRaw(s)
	return cauo
}

// SetNillableRequestRaw sets the "request_raw" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableRequestRaw(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetRequestRaw(*s)
	}
	return cauo
}

// SetResponseRaw sets the "response_raw" field.
func (cauo *CompapiAsynctaskUpdateOne) SetResponseRaw(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetResponseRaw(s)
	return cauo
}

// SetNillableResponseRaw sets the "response_raw" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableResponseRaw(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetResponseRaw(*s)
	}
	return cauo
}

// ClearResponseRaw clears the value of the "response_raw" field.
func (cauo *CompapiAsynctaskUpdateOne) ClearResponseRaw() *CompapiAsynctaskUpdateOne {
	cauo.mutation.ClearResponseRaw()
	return cauo
}

// SetCallbackURL sets the "callback_url" field.
func (cauo *CompapiAsynctaskUpdateOne) SetCallbackURL(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetCallbackURL(s)
	return cauo
}

// SetNillableCallbackURL sets the "callback_url" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableCallbackURL(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetCallbackURL(*s)
	}
	return cauo
}

// SetCallbackResponseRaw sets the "callback_response_raw" field.
func (cauo *CompapiAsynctaskUpdateOne) SetCallbackResponseRaw(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetCallbackResponseRaw(s)
	return cauo
}

// SetNillableCallbackResponseRaw sets the "callback_response_raw" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableCallbackResponseRaw(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetCallbackResponseRaw(*s)
	}
	return cauo
}

// ClearCallbackResponseRaw clears the value of the "callback_response_raw" field.
func (cauo *CompapiAsynctaskUpdateOne) ClearCallbackResponseRaw() *CompapiAsynctaskUpdateOne {
	cauo.mutation.ClearCallbackResponseRaw()
	return cauo
}

// SetModel sets the "model" field.
func (cauo *CompapiAsynctaskUpdateOne) SetModel(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetModel(s)
	return cauo
}

// SetNillableModel sets the "model" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableModel(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetModel(*s)
	}
	return cauo
}

// ClearModel clears the value of the "model" field.
func (cauo *CompapiAsynctaskUpdateOne) ClearModel() *CompapiAsynctaskUpdateOne {
	cauo.mutation.ClearModel()
	return cauo
}

// SetTaskStatus sets the "task_status" field.
func (cauo *CompapiAsynctaskUpdateOne) SetTaskStatus(i int8) *CompapiAsynctaskUpdateOne {
	cauo.mutation.ResetTaskStatus()
	cauo.mutation.SetTaskStatus(i)
	return cauo
}

// SetNillableTaskStatus sets the "task_status" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableTaskStatus(i *int8) *CompapiAsynctaskUpdateOne {
	if i != nil {
		cauo.SetTaskStatus(*i)
	}
	return cauo
}

// AddTaskStatus adds i to the "task_status" field.
func (cauo *CompapiAsynctaskUpdateOne) AddTaskStatus(i int8) *CompapiAsynctaskUpdateOne {
	cauo.mutation.AddTaskStatus(i)
	return cauo
}

// ClearTaskStatus clears the value of the "task_status" field.
func (cauo *CompapiAsynctaskUpdateOne) ClearTaskStatus() *CompapiAsynctaskUpdateOne {
	cauo.mutation.ClearTaskStatus()
	return cauo
}

// SetRetryCount sets the "retry_count" field.
func (cauo *CompapiAsynctaskUpdateOne) SetRetryCount(i int8) *CompapiAsynctaskUpdateOne {
	cauo.mutation.ResetRetryCount()
	cauo.mutation.SetRetryCount(i)
	return cauo
}

// SetNillableRetryCount sets the "retry_count" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableRetryCount(i *int8) *CompapiAsynctaskUpdateOne {
	if i != nil {
		cauo.SetRetryCount(*i)
	}
	return cauo
}

// AddRetryCount adds i to the "retry_count" field.
func (cauo *CompapiAsynctaskUpdateOne) AddRetryCount(i int8) *CompapiAsynctaskUpdateOne {
	cauo.mutation.AddRetryCount(i)
	return cauo
}

// ClearRetryCount clears the value of the "retry_count" field.
func (cauo *CompapiAsynctaskUpdateOne) ClearRetryCount() *CompapiAsynctaskUpdateOne {
	cauo.mutation.ClearRetryCount()
	return cauo
}

// SetLastError sets the "last_error" field.
func (cauo *CompapiAsynctaskUpdateOne) SetLastError(s string) *CompapiAsynctaskUpdateOne {
	cauo.mutation.SetLastError(s)
	return cauo
}

// SetNillableLastError sets the "last_error" field if the given value is not nil.
func (cauo *CompapiAsynctaskUpdateOne) SetNillableLastError(s *string) *CompapiAsynctaskUpdateOne {
	if s != nil {
		cauo.SetLastError(*s)
	}
	return cauo
}

// ClearLastError clears the value of the "last_error" field.
func (cauo *CompapiAsynctaskUpdateOne) ClearLastError() *CompapiAsynctaskUpdateOne {
	cauo.mutation.ClearLastError()
	return cauo
}

// Mutation returns the CompapiAsynctaskMutation object of the builder.
func (cauo *CompapiAsynctaskUpdateOne) Mutation() *CompapiAsynctaskMutation {
	return cauo.mutation
}

// Where appends a list predicates to the CompapiAsynctaskUpdate builder.
func (cauo *CompapiAsynctaskUpdateOne) Where(ps ...predicate.CompapiAsynctask) *CompapiAsynctaskUpdateOne {
	cauo.mutation.Where(ps...)
	return cauo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (cauo *CompapiAsynctaskUpdateOne) Select(field string, fields ...string) *CompapiAsynctaskUpdateOne {
	cauo.fields = append([]string{field}, fields...)
	return cauo
}

// Save executes the query and returns the updated CompapiAsynctask entity.
func (cauo *CompapiAsynctaskUpdateOne) Save(ctx context.Context) (*CompapiAsynctask, error) {
	cauo.defaults()
	return withHooks(ctx, cauo.sqlSave, cauo.mutation, cauo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (cauo *CompapiAsynctaskUpdateOne) SaveX(ctx context.Context) *CompapiAsynctask {
	node, err := cauo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (cauo *CompapiAsynctaskUpdateOne) Exec(ctx context.Context) error {
	_, err := cauo.Save(ctx)
	return err
}

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

// defaults sets the default values of the builder before save.
func (cauo *CompapiAsynctaskUpdateOne) defaults() {
	if _, ok := cauo.mutation.UpdatedAt(); !ok {
		v := compapiasynctask.UpdateDefaultUpdatedAt()
		cauo.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (cauo *CompapiAsynctaskUpdateOne) check() error {
	if v, ok := cauo.mutation.OrganizationID(); ok {
		if err := compapiasynctask.OrganizationIDValidator(v); err != nil {
			return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "CompapiAsynctask.organization_id": %w`, err)}
		}
	}
	if v, ok := cauo.mutation.CallbackURL(); ok {
		if err := compapiasynctask.CallbackURLValidator(v); err != nil {
			return &ValidationError{Name: "callback_url", err: fmt.Errorf(`ent: validator failed for field "CompapiAsynctask.callback_url": %w`, err)}
		}
	}
	return nil
}

func (cauo *CompapiAsynctaskUpdateOne) sqlSave(ctx context.Context) (_node *CompapiAsynctask, err error) {
	if err := cauo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(compapiasynctask.Table, compapiasynctask.Columns, sqlgraph.NewFieldSpec(compapiasynctask.FieldID, field.TypeUint64))
	id, ok := cauo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "CompapiAsynctask.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := cauo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, compapiasynctask.FieldID)
		for _, f := range fields {
			if !compapiasynctask.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != compapiasynctask.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := cauo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := cauo.mutation.UpdatedAt(); ok {
		_spec.SetField(compapiasynctask.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := cauo.mutation.AuthToken(); ok {
		_spec.SetField(compapiasynctask.FieldAuthToken, field.TypeString, value)
	}
	if value, ok := cauo.mutation.EventType(); ok {
		_spec.SetField(compapiasynctask.FieldEventType, field.TypeString, value)
	}
	if value, ok := cauo.mutation.ChatID(); ok {
		_spec.SetField(compapiasynctask.FieldChatID, field.TypeString, value)
	}
	if cauo.mutation.ChatIDCleared() {
		_spec.ClearField(compapiasynctask.FieldChatID, field.TypeString)
	}
	if value, ok := cauo.mutation.ResponseChatItemID(); ok {
		_spec.SetField(compapiasynctask.FieldResponseChatItemID, field.TypeString, value)
	}
	if cauo.mutation.ResponseChatItemIDCleared() {
		_spec.ClearField(compapiasynctask.FieldResponseChatItemID, field.TypeString)
	}
	if value, ok := cauo.mutation.OrganizationID(); ok {
		_spec.SetField(compapiasynctask.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := cauo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(compapiasynctask.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := cauo.mutation.OpenaiBase(); ok {
		_spec.SetField(compapiasynctask.FieldOpenaiBase, field.TypeString, value)
	}
	if value, ok := cauo.mutation.OpenaiKey(); ok {
		_spec.SetField(compapiasynctask.FieldOpenaiKey, field.TypeString, value)
	}
	if value, ok := cauo.mutation.RequestRaw(); ok {
		_spec.SetField(compapiasynctask.FieldRequestRaw, field.TypeString, value)
	}
	if value, ok := cauo.mutation.ResponseRaw(); ok {
		_spec.SetField(compapiasynctask.FieldResponseRaw, field.TypeString, value)
	}
	if cauo.mutation.ResponseRawCleared() {
		_spec.ClearField(compapiasynctask.FieldResponseRaw, field.TypeString)
	}
	if value, ok := cauo.mutation.CallbackURL(); ok {
		_spec.SetField(compapiasynctask.FieldCallbackURL, field.TypeString, value)
	}
	if value, ok := cauo.mutation.CallbackResponseRaw(); ok {
		_spec.SetField(compapiasynctask.FieldCallbackResponseRaw, field.TypeString, value)
	}
	if cauo.mutation.CallbackResponseRawCleared() {
		_spec.ClearField(compapiasynctask.FieldCallbackResponseRaw, field.TypeString)
	}
	if value, ok := cauo.mutation.Model(); ok {
		_spec.SetField(compapiasynctask.FieldModel, field.TypeString, value)
	}
	if cauo.mutation.ModelCleared() {
		_spec.ClearField(compapiasynctask.FieldModel, field.TypeString)
	}
	if value, ok := cauo.mutation.TaskStatus(); ok {
		_spec.SetField(compapiasynctask.FieldTaskStatus, field.TypeInt8, value)
	}
	if value, ok := cauo.mutation.AddedTaskStatus(); ok {
		_spec.AddField(compapiasynctask.FieldTaskStatus, field.TypeInt8, value)
	}
	if cauo.mutation.TaskStatusCleared() {
		_spec.ClearField(compapiasynctask.FieldTaskStatus, field.TypeInt8)
	}
	if value, ok := cauo.mutation.RetryCount(); ok {
		_spec.SetField(compapiasynctask.FieldRetryCount, field.TypeInt8, value)
	}
	if value, ok := cauo.mutation.AddedRetryCount(); ok {
		_spec.AddField(compapiasynctask.FieldRetryCount, field.TypeInt8, value)
	}
	if cauo.mutation.RetryCountCleared() {
		_spec.ClearField(compapiasynctask.FieldRetryCount, field.TypeInt8)
	}
	if value, ok := cauo.mutation.LastError(); ok {
		_spec.SetField(compapiasynctask.FieldLastError, field.TypeString, value)
	}
	if cauo.mutation.LastErrorCleared() {
		_spec.ClearField(compapiasynctask.FieldLastError, field.TypeString)
	}
	_node = &CompapiAsynctask{config: cauo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, cauo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{compapiasynctask.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	cauo.mutation.done = true
	return _node, nil
}
