// Code generated by gowebx, DO AVOID EDIT.
package audioencode

import (
	"context"
	"git.lnton.com/lnton/pkg/orm"
	"git.lnton.com/lnton/pkg/reason"
	"github.com/jinzhu/copier"
	"log/slog"
)

// AudioTaskStorer Instantiation interface
type AudioTaskStorer interface {
	Find(context.Context, *[]*AudioTask, orm.Pager, ...orm.QueryOption) (int64, error)
	FindAll(dp *[]*AudioTask) (int64, error)
	Get(context.Context, *AudioTask, ...orm.QueryOption) error
	Add(context.Context, *AudioTask) error
	Edit(context.Context, *AudioTask, func(*AudioTask), ...orm.QueryOption) error
	Del(context.Context, *AudioTask, ...orm.QueryOption) error
	EditStatus(status int, id int) error
	EditStatusError(id, status int, s string) error
}

// FindAudioTaskAll Paginated search
func (c Core) FindAudioTaskAll() ([]*AudioTask, int64, error) {
	items := make([]*AudioTask, 0)
	total, err := c.store.AudioTask().FindAll(&items)
	if err != nil {
		return nil, 0, reason.ErrDB.Withf(`Find err[%s]`, err.Error())
	}
	return items, total, nil
}

// FindAudioTask Paginated search
func (c Core) FindAudioTask(ctx context.Context, in *FindAudioTaskInput) ([]*AudioTask, int64, error) {
	items := make([]*AudioTask, 0)
	if in.Name != "" {
		query := orm.NewQuery(8).
			Where("audio_name like ? OR  channel_id like ? OR channel_name like ?", "%"+in.Name+"%", "%"+in.Name+"%", "%"+in.Name+"%").OrderBy("created_at DESC")
		total, err := c.store.AudioTask().Find(ctx, &items, in, query.Encode()...)
		if err != nil {
			return nil, 0, reason.ErrDB.Withf(`Find err[%s]`, err.Error())
		}
		return items, total, nil
	} else {
		query := orm.NewQuery(2).OrderBy("created_at DESC")
		total, err := c.store.AudioTask().Find(ctx, &items, in, query.Encode()...)
		if err != nil {
			return nil, 0, reason.ErrDB.Withf(`Find err[%s]`, err.Error())
		}
		return items, total, nil
	}
}

// GetAudioTask Query a single object
func (c Core) GetAudioTask(ctx context.Context, id int) (*AudioTask, error) {
	var out AudioTask
	if err := c.store.AudioTask().Get(ctx, &out, orm.Where("id=?", id)); err != nil {
		if orm.IsErrRecordNotFound(err) {
			return nil, reason.ErrNotFound.Withf(`Get err[%s]`, err.Error())
		}
		return nil, reason.ErrDB.Withf(`Get err[%s]`, err.Error())
	}
	return &out, nil
}
func (c Core) GetNameAudioTask(ctx context.Context, name string) (*AudioTask, error) {
	var out AudioTask
	if err := c.store.AudioTask().Get(ctx, &out, orm.Where("name=?", name)); err != nil {
		if orm.IsErrRecordNotFound(err) {
			return nil, reason.ErrNotFound.Withf(`Get err[%s]`, err.Error())
		}
		return nil, reason.ErrDB.Withf(`Get err[%s]`, err.Error())
	}
	return &out, nil
}

// AddAudioTask Insert into database
func (c Core) AddAudioTask(ctx context.Context, in *AddAudioTask) (*AudioTask, error) {
	var out AudioTask
	if err := copier.Copy(&out, in); err != nil {
		slog.Error("Copy", "err", err)
	}
	if err := c.store.AudioTask().Add(ctx, &out); err != nil {
		return nil, reason.ErrDB.Withf(`Add err[%s]`, err.Error())
	}
	return &out, nil
}

// EditAudioTask Update object information
func (c Core) EditAudioTask(ctx context.Context, in *EditAudioTaskInput, id int) (*AudioTask, error) {
	var out AudioTask
	if err := c.store.AudioTask().Edit(ctx, &out, func(b *AudioTask) {
		if err := copier.Copy(b, in); err != nil {
			slog.Error("Copy", "err", err)
		}
	}, orm.Where("id=?", id)); err != nil {
		return nil, reason.ErrDB.Withf(`Edit err[%s]`, err.Error())
	}
	return &out, nil
}

// DelAudioTask Delete object
func (c Core) DelAudioTask(ctx context.Context, id int) (*AudioTask, error) {
	var out AudioTask
	if err := c.store.AudioTask().Del(ctx, &out, orm.Where("id=?", id)); err != nil {
		return nil, reason.ErrDB.Withf(`Del err[%s]`, err.Error())
	}
	return &out, nil
}

func (c Core) DelAudioTaskAll(ctx context.Context, ids []int) (*AudioTask, error) {
	var out AudioTask
	if err := c.store.AudioTask().Del(ctx, &out, orm.Where("id in (?)", ids)); err != nil {
		return nil, reason.ErrDB.Withf(`Del ids err[%s]`, err.Error())
	}
	return &out, nil
}

// AudioTaskStatus Update
func (c Core) AudioTaskStatus(status int, id int) error {
	if err := c.store.AudioTask().EditStatus(status, id); err != nil {
		return reason.ErrDB.Withf(`Status err[%s]`, err.Error())
	}
	return nil
}

// AudioTaskStatus Update
func (c Core) AudioTaskStatusError(id, status int, s string) error {
	if err := c.store.AudioTask().EditStatusError(id, status, s); err != nil {
		return reason.ErrDB.Withf(`StatusError err[%s]`, err.Error())
	}
	return nil
}
