// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/Wei-Shaw/sub2api/ent/announcement"
	"github.com/Wei-Shaw/sub2api/ent/announcementread"
	"github.com/Wei-Shaw/sub2api/ent/predicate"
	"github.com/Wei-Shaw/sub2api/internal/domain"
)

// AnnouncementUpdate is the builder for updating Announcement entities.
type AnnouncementUpdate struct {
	config
	hooks    []Hook
	mutation *AnnouncementMutation
}

// Where appends a list predicates to the AnnouncementUpdate builder.
func (_u *AnnouncementUpdate) Where(ps ...predicate.Announcement) *AnnouncementUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetTitle sets the "title" field.
func (_u *AnnouncementUpdate) SetTitle(v string) *AnnouncementUpdate {
	_u.mutation.SetTitle(v)
	return _u
}

// SetNillableTitle sets the "title" field if the given value is not nil.
func (_u *AnnouncementUpdate) SetNillableTitle(v *string) *AnnouncementUpdate {
	if v != nil {
		_u.SetTitle(*v)
	}
	return _u
}

// SetContent sets the "content" field.
func (_u *AnnouncementUpdate) SetContent(v string) *AnnouncementUpdate {
	_u.mutation.SetContent(v)
	return _u
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (_u *AnnouncementUpdate) SetNillableContent(v *string) *AnnouncementUpdate {
	if v != nil {
		_u.SetContent(*v)
	}
	return _u
}

// SetStatus sets the "status" field.
func (_u *AnnouncementUpdate) SetStatus(v string) *AnnouncementUpdate {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *AnnouncementUpdate) SetNillableStatus(v *string) *AnnouncementUpdate {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetNotifyMode sets the "notify_mode" field.
func (_u *AnnouncementUpdate) SetNotifyMode(v string) *AnnouncementUpdate {
	_u.mutation.SetNotifyMode(v)
	return _u
}

// SetNillableNotifyMode sets the "notify_mode" field if the given value is not nil.
func (_u *AnnouncementUpdate) SetNillableNotifyMode(v *string) *AnnouncementUpdate {
	if v != nil {
		_u.SetNotifyMode(*v)
	}
	return _u
}

// SetTargeting sets the "targeting" field.
func (_u *AnnouncementUpdate) SetTargeting(v domain.AnnouncementTargeting) *AnnouncementUpdate {
	_u.mutation.SetTargeting(v)
	return _u
}

// SetNillableTargeting sets the "targeting" field if the given value is not nil.
func (_u *AnnouncementUpdate) SetNillableTargeting(v *domain.AnnouncementTargeting) *AnnouncementUpdate {
	if v != nil {
		_u.SetTargeting(*v)
	}
	return _u
}

// ClearTargeting clears the value of the "targeting" field.
func (_u *AnnouncementUpdate) ClearTargeting() *AnnouncementUpdate {
	_u.mutation.ClearTargeting()
	return _u
}

// SetStartsAt sets the "starts_at" field.
func (_u *AnnouncementUpdate) SetStartsAt(v time.Time) *AnnouncementUpdate {
	_u.mutation.SetStartsAt(v)
	return _u
}

// SetNillableStartsAt sets the "starts_at" field if the given value is not nil.
func (_u *AnnouncementUpdate) SetNillableStartsAt(v *time.Time) *AnnouncementUpdate {
	if v != nil {
		_u.SetStartsAt(*v)
	}
	return _u
}

// ClearStartsAt clears the value of the "starts_at" field.
func (_u *AnnouncementUpdate) ClearStartsAt() *AnnouncementUpdate {
	_u.mutation.ClearStartsAt()
	return _u
}

// SetEndsAt sets the "ends_at" field.
func (_u *AnnouncementUpdate) SetEndsAt(v time.Time) *AnnouncementUpdate {
	_u.mutation.SetEndsAt(v)
	return _u
}

// SetNillableEndsAt sets the "ends_at" field if the given value is not nil.
func (_u *AnnouncementUpdate) SetNillableEndsAt(v *time.Time) *AnnouncementUpdate {
	if v != nil {
		_u.SetEndsAt(*v)
	}
	return _u
}

// ClearEndsAt clears the value of the "ends_at" field.
func (_u *AnnouncementUpdate) ClearEndsAt() *AnnouncementUpdate {
	_u.mutation.ClearEndsAt()
	return _u
}

// SetCreatedBy sets the "created_by" field.
func (_u *AnnouncementUpdate) SetCreatedBy(v int64) *AnnouncementUpdate {
	_u.mutation.ResetCreatedBy()
	_u.mutation.SetCreatedBy(v)
	return _u
}

// SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
func (_u *AnnouncementUpdate) SetNillableCreatedBy(v *int64) *AnnouncementUpdate {
	if v != nil {
		_u.SetCreatedBy(*v)
	}
	return _u
}

// AddCreatedBy adds value to the "created_by" field.
func (_u *AnnouncementUpdate) AddCreatedBy(v int64) *AnnouncementUpdate {
	_u.mutation.AddCreatedBy(v)
	return _u
}

// ClearCreatedBy clears the value of the "created_by" field.
func (_u *AnnouncementUpdate) ClearCreatedBy() *AnnouncementUpdate {
	_u.mutation.ClearCreatedBy()
	return _u
}

// SetUpdatedBy sets the "updated_by" field.
func (_u *AnnouncementUpdate) SetUpdatedBy(v int64) *AnnouncementUpdate {
	_u.mutation.ResetUpdatedBy()
	_u.mutation.SetUpdatedBy(v)
	return _u
}

// SetNillableUpdatedBy sets the "updated_by" field if the given value is not nil.
func (_u *AnnouncementUpdate) SetNillableUpdatedBy(v *int64) *AnnouncementUpdate {
	if v != nil {
		_u.SetUpdatedBy(*v)
	}
	return _u
}

// AddUpdatedBy adds value to the "updated_by" field.
func (_u *AnnouncementUpdate) AddUpdatedBy(v int64) *AnnouncementUpdate {
	_u.mutation.AddUpdatedBy(v)
	return _u
}

// ClearUpdatedBy clears the value of the "updated_by" field.
func (_u *AnnouncementUpdate) ClearUpdatedBy() *AnnouncementUpdate {
	_u.mutation.ClearUpdatedBy()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *AnnouncementUpdate) SetUpdatedAt(v time.Time) *AnnouncementUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// AddReadIDs adds the "reads" edge to the AnnouncementRead entity by IDs.
func (_u *AnnouncementUpdate) AddReadIDs(ids ...int64) *AnnouncementUpdate {
	_u.mutation.AddReadIDs(ids...)
	return _u
}

// AddReads adds the "reads" edges to the AnnouncementRead entity.
func (_u *AnnouncementUpdate) AddReads(v ...*AnnouncementRead) *AnnouncementUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddReadIDs(ids...)
}

// Mutation returns the AnnouncementMutation object of the builder.
func (_u *AnnouncementUpdate) Mutation() *AnnouncementMutation {
	return _u.mutation
}

// ClearReads clears all "reads" edges to the AnnouncementRead entity.
func (_u *AnnouncementUpdate) ClearReads() *AnnouncementUpdate {
	_u.mutation.ClearReads()
	return _u
}

// RemoveReadIDs removes the "reads" edge to AnnouncementRead entities by IDs.
func (_u *AnnouncementUpdate) RemoveReadIDs(ids ...int64) *AnnouncementUpdate {
	_u.mutation.RemoveReadIDs(ids...)
	return _u
}

// RemoveReads removes "reads" edges to AnnouncementRead entities.
func (_u *AnnouncementUpdate) RemoveReads(v ...*AnnouncementRead) *AnnouncementUpdate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveReadIDs(ids...)
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *AnnouncementUpdate) check() error {
	if v, ok := _u.mutation.Title(); ok {
		if err := announcement.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Announcement.title": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Content(); ok {
		if err := announcement.ContentValidator(v); err != nil {
			return &ValidationError{Name: "content", err: fmt.Errorf(`ent: validator failed for field "Announcement.content": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := announcement.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Announcement.status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.NotifyMode(); ok {
		if err := announcement.NotifyModeValidator(v); err != nil {
			return &ValidationError{Name: "notify_mode", err: fmt.Errorf(`ent: validator failed for field "Announcement.notify_mode": %w`, err)}
		}
	}
	return nil
}

func (_u *AnnouncementUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(announcement.Table, announcement.Columns, sqlgraph.NewFieldSpec(announcement.FieldID, field.TypeInt64))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Title(); ok {
		_spec.SetField(announcement.FieldTitle, field.TypeString, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(announcement.FieldContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(announcement.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.NotifyMode(); ok {
		_spec.SetField(announcement.FieldNotifyMode, field.TypeString, value)
	}
	if value, ok := _u.mutation.Targeting(); ok {
		_spec.SetField(announcement.FieldTargeting, field.TypeJSON, value)
	}
	if _u.mutation.TargetingCleared() {
		_spec.ClearField(announcement.FieldTargeting, field.TypeJSON)
	}
	if value, ok := _u.mutation.StartsAt(); ok {
		_spec.SetField(announcement.FieldStartsAt, field.TypeTime, value)
	}
	if _u.mutation.StartsAtCleared() {
		_spec.ClearField(announcement.FieldStartsAt, field.TypeTime)
	}
	if value, ok := _u.mutation.EndsAt(); ok {
		_spec.SetField(announcement.FieldEndsAt, field.TypeTime, value)
	}
	if _u.mutation.EndsAtCleared() {
		_spec.ClearField(announcement.FieldEndsAt, field.TypeTime)
	}
	if value, ok := _u.mutation.CreatedBy(); ok {
		_spec.SetField(announcement.FieldCreatedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedCreatedBy(); ok {
		_spec.AddField(announcement.FieldCreatedBy, field.TypeInt64, value)
	}
	if _u.mutation.CreatedByCleared() {
		_spec.ClearField(announcement.FieldCreatedBy, field.TypeInt64)
	}
	if value, ok := _u.mutation.UpdatedBy(); ok {
		_spec.SetField(announcement.FieldUpdatedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUpdatedBy(); ok {
		_spec.AddField(announcement.FieldUpdatedBy, field.TypeInt64, value)
	}
	if _u.mutation.UpdatedByCleared() {
		_spec.ClearField(announcement.FieldUpdatedBy, field.TypeInt64)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(announcement.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.ReadsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   announcement.ReadsTable,
			Columns: []string{announcement.ReadsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedReadsIDs(); len(nodes) > 0 && !_u.mutation.ReadsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   announcement.ReadsTable,
			Columns: []string{announcement.ReadsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.ReadsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   announcement.ReadsTable,
			Columns: []string{announcement.ReadsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{announcement.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// AnnouncementUpdateOne is the builder for updating a single Announcement entity.
type AnnouncementUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *AnnouncementMutation
}

// SetTitle sets the "title" field.
func (_u *AnnouncementUpdateOne) SetTitle(v string) *AnnouncementUpdateOne {
	_u.mutation.SetTitle(v)
	return _u
}

// SetNillableTitle sets the "title" field if the given value is not nil.
func (_u *AnnouncementUpdateOne) SetNillableTitle(v *string) *AnnouncementUpdateOne {
	if v != nil {
		_u.SetTitle(*v)
	}
	return _u
}

// SetContent sets the "content" field.
func (_u *AnnouncementUpdateOne) SetContent(v string) *AnnouncementUpdateOne {
	_u.mutation.SetContent(v)
	return _u
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (_u *AnnouncementUpdateOne) SetNillableContent(v *string) *AnnouncementUpdateOne {
	if v != nil {
		_u.SetContent(*v)
	}
	return _u
}

// SetStatus sets the "status" field.
func (_u *AnnouncementUpdateOne) SetStatus(v string) *AnnouncementUpdateOne {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *AnnouncementUpdateOne) SetNillableStatus(v *string) *AnnouncementUpdateOne {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetNotifyMode sets the "notify_mode" field.
func (_u *AnnouncementUpdateOne) SetNotifyMode(v string) *AnnouncementUpdateOne {
	_u.mutation.SetNotifyMode(v)
	return _u
}

// SetNillableNotifyMode sets the "notify_mode" field if the given value is not nil.
func (_u *AnnouncementUpdateOne) SetNillableNotifyMode(v *string) *AnnouncementUpdateOne {
	if v != nil {
		_u.SetNotifyMode(*v)
	}
	return _u
}

// SetTargeting sets the "targeting" field.
func (_u *AnnouncementUpdateOne) SetTargeting(v domain.AnnouncementTargeting) *AnnouncementUpdateOne {
	_u.mutation.SetTargeting(v)
	return _u
}

// SetNillableTargeting sets the "targeting" field if the given value is not nil.
func (_u *AnnouncementUpdateOne) SetNillableTargeting(v *domain.AnnouncementTargeting) *AnnouncementUpdateOne {
	if v != nil {
		_u.SetTargeting(*v)
	}
	return _u
}

// ClearTargeting clears the value of the "targeting" field.
func (_u *AnnouncementUpdateOne) ClearTargeting() *AnnouncementUpdateOne {
	_u.mutation.ClearTargeting()
	return _u
}

// SetStartsAt sets the "starts_at" field.
func (_u *AnnouncementUpdateOne) SetStartsAt(v time.Time) *AnnouncementUpdateOne {
	_u.mutation.SetStartsAt(v)
	return _u
}

// SetNillableStartsAt sets the "starts_at" field if the given value is not nil.
func (_u *AnnouncementUpdateOne) SetNillableStartsAt(v *time.Time) *AnnouncementUpdateOne {
	if v != nil {
		_u.SetStartsAt(*v)
	}
	return _u
}

// ClearStartsAt clears the value of the "starts_at" field.
func (_u *AnnouncementUpdateOne) ClearStartsAt() *AnnouncementUpdateOne {
	_u.mutation.ClearStartsAt()
	return _u
}

// SetEndsAt sets the "ends_at" field.
func (_u *AnnouncementUpdateOne) SetEndsAt(v time.Time) *AnnouncementUpdateOne {
	_u.mutation.SetEndsAt(v)
	return _u
}

// SetNillableEndsAt sets the "ends_at" field if the given value is not nil.
func (_u *AnnouncementUpdateOne) SetNillableEndsAt(v *time.Time) *AnnouncementUpdateOne {
	if v != nil {
		_u.SetEndsAt(*v)
	}
	return _u
}

// ClearEndsAt clears the value of the "ends_at" field.
func (_u *AnnouncementUpdateOne) ClearEndsAt() *AnnouncementUpdateOne {
	_u.mutation.ClearEndsAt()
	return _u
}

// SetCreatedBy sets the "created_by" field.
func (_u *AnnouncementUpdateOne) SetCreatedBy(v int64) *AnnouncementUpdateOne {
	_u.mutation.ResetCreatedBy()
	_u.mutation.SetCreatedBy(v)
	return _u
}

// SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
func (_u *AnnouncementUpdateOne) SetNillableCreatedBy(v *int64) *AnnouncementUpdateOne {
	if v != nil {
		_u.SetCreatedBy(*v)
	}
	return _u
}

// AddCreatedBy adds value to the "created_by" field.
func (_u *AnnouncementUpdateOne) AddCreatedBy(v int64) *AnnouncementUpdateOne {
	_u.mutation.AddCreatedBy(v)
	return _u
}

// ClearCreatedBy clears the value of the "created_by" field.
func (_u *AnnouncementUpdateOne) ClearCreatedBy() *AnnouncementUpdateOne {
	_u.mutation.ClearCreatedBy()
	return _u
}

// SetUpdatedBy sets the "updated_by" field.
func (_u *AnnouncementUpdateOne) SetUpdatedBy(v int64) *AnnouncementUpdateOne {
	_u.mutation.ResetUpdatedBy()
	_u.mutation.SetUpdatedBy(v)
	return _u
}

// SetNillableUpdatedBy sets the "updated_by" field if the given value is not nil.
func (_u *AnnouncementUpdateOne) SetNillableUpdatedBy(v *int64) *AnnouncementUpdateOne {
	if v != nil {
		_u.SetUpdatedBy(*v)
	}
	return _u
}

// AddUpdatedBy adds value to the "updated_by" field.
func (_u *AnnouncementUpdateOne) AddUpdatedBy(v int64) *AnnouncementUpdateOne {
	_u.mutation.AddUpdatedBy(v)
	return _u
}

// ClearUpdatedBy clears the value of the "updated_by" field.
func (_u *AnnouncementUpdateOne) ClearUpdatedBy() *AnnouncementUpdateOne {
	_u.mutation.ClearUpdatedBy()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *AnnouncementUpdateOne) SetUpdatedAt(v time.Time) *AnnouncementUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// AddReadIDs adds the "reads" edge to the AnnouncementRead entity by IDs.
func (_u *AnnouncementUpdateOne) AddReadIDs(ids ...int64) *AnnouncementUpdateOne {
	_u.mutation.AddReadIDs(ids...)
	return _u
}

// AddReads adds the "reads" edges to the AnnouncementRead entity.
func (_u *AnnouncementUpdateOne) AddReads(v ...*AnnouncementRead) *AnnouncementUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddReadIDs(ids...)
}

// Mutation returns the AnnouncementMutation object of the builder.
func (_u *AnnouncementUpdateOne) Mutation() *AnnouncementMutation {
	return _u.mutation
}

// ClearReads clears all "reads" edges to the AnnouncementRead entity.
func (_u *AnnouncementUpdateOne) ClearReads() *AnnouncementUpdateOne {
	_u.mutation.ClearReads()
	return _u
}

// RemoveReadIDs removes the "reads" edge to AnnouncementRead entities by IDs.
func (_u *AnnouncementUpdateOne) RemoveReadIDs(ids ...int64) *AnnouncementUpdateOne {
	_u.mutation.RemoveReadIDs(ids...)
	return _u
}

// RemoveReads removes "reads" edges to AnnouncementRead entities.
func (_u *AnnouncementUpdateOne) RemoveReads(v ...*AnnouncementRead) *AnnouncementUpdateOne {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveReadIDs(ids...)
}

// Where appends a list predicates to the AnnouncementUpdate builder.
func (_u *AnnouncementUpdateOne) Where(ps ...predicate.Announcement) *AnnouncementUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *AnnouncementUpdateOne) check() error {
	if v, ok := _u.mutation.Title(); ok {
		if err := announcement.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Announcement.title": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Content(); ok {
		if err := announcement.ContentValidator(v); err != nil {
			return &ValidationError{Name: "content", err: fmt.Errorf(`ent: validator failed for field "Announcement.content": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := announcement.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Announcement.status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.NotifyMode(); ok {
		if err := announcement.NotifyModeValidator(v); err != nil {
			return &ValidationError{Name: "notify_mode", err: fmt.Errorf(`ent: validator failed for field "Announcement.notify_mode": %w`, err)}
		}
	}
	return nil
}

func (_u *AnnouncementUpdateOne) sqlSave(ctx context.Context) (_node *Announcement, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(announcement.Table, announcement.Columns, sqlgraph.NewFieldSpec(announcement.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Announcement.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, announcement.FieldID)
		for _, f := range fields {
			if !announcement.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != announcement.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.Title(); ok {
		_spec.SetField(announcement.FieldTitle, field.TypeString, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(announcement.FieldContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(announcement.FieldStatus, field.TypeString, value)
	}
	if value, ok := _u.mutation.NotifyMode(); ok {
		_spec.SetField(announcement.FieldNotifyMode, field.TypeString, value)
	}
	if value, ok := _u.mutation.Targeting(); ok {
		_spec.SetField(announcement.FieldTargeting, field.TypeJSON, value)
	}
	if _u.mutation.TargetingCleared() {
		_spec.ClearField(announcement.FieldTargeting, field.TypeJSON)
	}
	if value, ok := _u.mutation.StartsAt(); ok {
		_spec.SetField(announcement.FieldStartsAt, field.TypeTime, value)
	}
	if _u.mutation.StartsAtCleared() {
		_spec.ClearField(announcement.FieldStartsAt, field.TypeTime)
	}
	if value, ok := _u.mutation.EndsAt(); ok {
		_spec.SetField(announcement.FieldEndsAt, field.TypeTime, value)
	}
	if _u.mutation.EndsAtCleared() {
		_spec.ClearField(announcement.FieldEndsAt, field.TypeTime)
	}
	if value, ok := _u.mutation.CreatedBy(); ok {
		_spec.SetField(announcement.FieldCreatedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedCreatedBy(); ok {
		_spec.AddField(announcement.FieldCreatedBy, field.TypeInt64, value)
	}
	if _u.mutation.CreatedByCleared() {
		_spec.ClearField(announcement.FieldCreatedBy, field.TypeInt64)
	}
	if value, ok := _u.mutation.UpdatedBy(); ok {
		_spec.SetField(announcement.FieldUpdatedBy, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUpdatedBy(); ok {
		_spec.AddField(announcement.FieldUpdatedBy, field.TypeInt64, value)
	}
	if _u.mutation.UpdatedByCleared() {
		_spec.ClearField(announcement.FieldUpdatedBy, field.TypeInt64)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(announcement.FieldUpdatedAt, field.TypeTime, value)
	}
	if _u.mutation.ReadsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   announcement.ReadsTable,
			Columns: []string{announcement.ReadsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedReadsIDs(); len(nodes) > 0 && !_u.mutation.ReadsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   announcement.ReadsTable,
			Columns: []string{announcement.ReadsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.ReadsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   announcement.ReadsTable,
			Columns: []string{announcement.ReadsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Announcement{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{announcement.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
