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

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

// VqdPollingStorer Instantiation interface
type VqdPollingStorer interface {
	Find(context.Context, *[]*VqdPolling, orm.Pager, ...orm.QueryOption) (int64, error)
	FindAll(dp *[]*VqdPolling) (int64, error)
	Get(context.Context, *VqdPolling, ...orm.QueryOption) error
	Add(context.Context, *VqdPolling) error
	Edit(context.Context, *VqdPolling, func(*VqdPolling), ...orm.QueryOption) error
	Del(context.Context, *VqdPolling, ...orm.QueryOption) error
}

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

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

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

// GetPollingChannelID Query a single object
func (c Core) GetPollingChannelID(ctx context.Context, chnId string) (*VqdPolling, error) {
	var out VqdPolling
	if err := c.store.VqdPolling().Get(ctx, &out, orm.Where("channel_id=?", chnId)); 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
}

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

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

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

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