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

// VqdAlarmStorer Instantiation interface
type VqdAlarmStorer interface {
	Find(context.Context, *[]*VqdAlarm, orm.Pager, ...orm.QueryOption) (int64, error)
	FindAll(dp *[]*VqdAlarm) (int64, error)
	Get(context.Context, *VqdAlarm, ...orm.QueryOption) error
	Add(context.Context, *VqdAlarm) error
	Edit(context.Context, *VqdAlarm, func(*VqdAlarm), ...orm.QueryOption) error
	Del(context.Context, *VqdAlarm, ...orm.QueryOption) error
	DelAll(expireTime time.Time, batchSize int) (int, error)
}

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

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

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

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

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

// DelVqdAlarm Delete object
func (c Core) DelVqdAlarm(ctx context.Context, id int) (*VqdAlarm, error) {
	var out VqdAlarm
	if err := c.store.VqdAlarm().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) DelVqdAlarmAll(ctx context.Context, ids []int) (*VqdAlarm, error) {
	var out VqdAlarm
	if err := c.store.VqdAlarm().Del(ctx, &out, orm.Where("id in (?)", ids)); err != nil {
		return nil, reason.ErrDB.Withf(`Del ids err[%s]`, err.Error())
	}
	return &out, nil
}

// DelVqdTaskAlarmAll Delete object
func (c Core) DelVqdTaskAlarmAll(expireTime time.Time, batchSize int) (int, error) {
	return c.store.VqdAlarm().DelAll(expireTime, batchSize)
}
