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

import (
	"context"
	"easyaudioencode/internal/core/audioencode"
	"git.lnton.com/lnton/pkg/orm"
)

var _ audioencode.AudioTaskStorer = AudioTask{}

// AudioTask Related business namespaces
type AudioTask DB

// FindAll implements audioencode.AudioTaskStorer.
func (d AudioTask) FindAll(bs *[]*audioencode.AudioTask) (int64, error) {
	db := d.db.Model(&audioencode.AudioTask{})
	var total int64
	if err := db.Count(&total).Error; err != nil || total <= 0 {
		// 如果统计失败或者数量为0，则返回错误
		return 0, err
	}
	return total, db.Find(bs).Error
}

// Find implements audioencode.AudioTaskStorer.
func (d AudioTask) Find(ctx context.Context, bs *[]*audioencode.AudioTask, page orm.Pager, opts ...orm.QueryOption) (int64, error) {
	return orm.FindWithContext(ctx, d.db, bs, page, opts...)
}

// Get implements audioencode.AudioTaskStorer.
func (d AudioTask) Get(ctx context.Context, model *audioencode.AudioTask, opts ...orm.QueryOption) error {
	return orm.FirstWithContext(ctx, d.db, model, opts...)
}

// Add implements audioencode.AudioTaskStorer.
func (d AudioTask) Add(ctx context.Context, model *audioencode.AudioTask) error {
	return d.db.WithContext(ctx).Create(model).Error
}

// Edit implements audioencode.AudioTaskStorer.
func (d AudioTask) Edit(ctx context.Context, model *audioencode.AudioTask, changeFn func(*audioencode.AudioTask), opts ...orm.QueryOption) error {
	return orm.UpdateWithContext(ctx, d.db, model, changeFn, opts...)
}

// Del implements audioencode.AudioTaskStorer.
func (d AudioTask) Del(ctx context.Context, model *audioencode.AudioTask, opts ...orm.QueryOption) error {
	return orm.DeleteWithContext(ctx, d.db, model, opts...)
}

// EditStatus implements audioencode.AudioTaskStorer.
func (d AudioTask) EditStatus(status int, id int) error {
	if err := d.db.Model(&audioencode.AudioTask{}).Where(`id = ?`, id).Update("task_status", status).Error; err != nil {
		return err
	}
	return nil
}

// EditStatusError implements audioencode.AudioTaskStorer.
func (d AudioTask) EditStatusError(id, status int, s string) error {
	if err := d.db.Model(&audioencode.AudioTask{}).Where(`id = ?`, id).Updates(map[string]any{
		"task_status": status,
		"error_msg":   s,
	}).Error; err != nil {
		return err
	}
	return nil
}
