// 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"
)

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

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

// FindAudioEncode Paginated search
func (c Core) FindAudioEncode(ctx context.Context, in *FindAudioEncodeInput) ([]*AudioEncode, int64, error) {
	items := make([]*AudioEncode, 0)
	if in.Name != "" {
		query := orm.NewQuery(8).
			Where("name like ? ", "%"+in.Name+"%").OrderBy("created_at DESC")
		total, err := c.store.AudioEncode().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.AudioEncode().Find(ctx, &items, in, query.Encode()...)
		if err != nil {
			return nil, 0, reason.ErrDB.Withf(`Find err[%s]`, err.Error())
		}
		return items, total, nil
	}
}

// GetAudioEncode Query a single object
func (c Core) GetAudioEncode(ctx context.Context, id int) (*AudioEncode, error) {
	var out AudioEncode
	if err := c.store.AudioEncode().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) GetNameAudioEncode(ctx context.Context, name string) (*AudioEncode, error) {
	var out AudioEncode
	if err := c.store.AudioEncode().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
}

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

// EditAudioEncode Update object information
func (c Core) EditAudioEncode(ctx context.Context, in *EditAudioEncodeInput, id int) (*AudioEncode, error) {
	var out AudioEncode
	if err := c.store.AudioEncode().Edit(ctx, &out, func(b *AudioEncode) {
		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
}

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

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