///////////////////////////////////////////////////////////
// THIS FILE IS AUTO GENERATED by gormgen, DON'T EDIT IT //
//        ANY CHANGES DONE HERE WILL BE LOST             //
///////////////////////////////////////////////////////////

package menu_action_repo

import (
	"fmt"
	"time"

	"github.com/xinliangnote/go-gin-api/internal/api/repository/db_repo"

	"github.com/pkg/errors"
	"gorm.io/gorm"
)

func NewModel() *MenuAction {
	return new(MenuAction)
}

func NewQueryBuilder() *menuActionRepoQueryBuilder {
	return new(menuActionRepoQueryBuilder)
}

func (t *MenuAction) Create(db *gorm.DB) (id int32, err error) {
	if err = db.Create(t).Error; err != nil {
		return 0, errors.Wrap(err, "create err")
	}
	return t.Id, nil
}

type menuActionRepoQueryBuilder struct {
	order []string
	where []struct {
		prefix string
		value  interface{}
	}
	limit  int
	offset int
}

func (qb *menuActionRepoQueryBuilder) buildQuery(db *gorm.DB) *gorm.DB {
	ret := db
	for _, where := range qb.where {
		ret = ret.Where(where.prefix, where.value)
	}
	for _, order := range qb.order {
		ret = ret.Order(order)
	}
	ret = ret.Limit(qb.limit).Offset(qb.offset)
	return ret
}

func (qb *menuActionRepoQueryBuilder) Updates(db *gorm.DB, m map[string]interface{}) (err error) {
	db = db.Model(&MenuAction{})

	for _, where := range qb.where {
		db.Where(where.prefix, where.value)
	}

	if err = db.Updates(m).Error; err != nil {
		return errors.Wrap(err, "updates err")
	}
	return nil
}

func (qb *menuActionRepoQueryBuilder) Delete(db *gorm.DB) (err error) {
	for _, where := range qb.where {
		db = db.Where(where.prefix, where.value)
	}

	if err = db.Delete(&MenuAction{}).Error; err != nil {
		return errors.Wrap(err, "delete err")
	}
	return nil
}

func (qb *menuActionRepoQueryBuilder) Count(db *gorm.DB) (int64, error) {
	var c int64
	res := qb.buildQuery(db).Model(&MenuAction{}).Count(&c)
	if res.Error != nil && res.Error == gorm.ErrRecordNotFound {
		c = 0
	}
	return c, res.Error
}

func (qb *menuActionRepoQueryBuilder) First(db *gorm.DB) (*MenuAction, error) {
	ret := &MenuAction{}
	res := qb.buildQuery(db).First(ret)
	if res.Error != nil && res.Error == gorm.ErrRecordNotFound {
		ret = nil
	}
	return ret, res.Error
}

func (qb *menuActionRepoQueryBuilder) QueryOne(db *gorm.DB) (*MenuAction, error) {
	qb.limit = 1
	ret, err := qb.QueryAll(db)
	if len(ret) > 0 {
		return ret[0], err
	}
	return nil, err
}

func (qb *menuActionRepoQueryBuilder) QueryAll(db *gorm.DB) ([]*MenuAction, error) {
	var ret []*MenuAction
	err := qb.buildQuery(db).Find(&ret).Error
	return ret, err
}

func (qb *menuActionRepoQueryBuilder) Limit(limit int) *menuActionRepoQueryBuilder {
	qb.limit = limit
	return qb
}

func (qb *menuActionRepoQueryBuilder) Offset(offset int) *menuActionRepoQueryBuilder {
	qb.offset = offset
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereId(p db_repo.Predicate, value int32) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "id", p),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereIdIn(value []int32) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "id", "IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereIdNotIn(value []int32) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "id", "NOT IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) OrderById(asc bool) *menuActionRepoQueryBuilder {
	order := "DESC"
	if asc {
		order = "ASC"
	}

	qb.order = append(qb.order, "id "+order)
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereMenuId(p db_repo.Predicate, value int32) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "menu_id", p),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereMenuIdIn(value []int32) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "menu_id", "IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereMenuIdNotIn(value []int32) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "menu_id", "NOT IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) OrderByMenuId(asc bool) *menuActionRepoQueryBuilder {
	order := "DESC"
	if asc {
		order = "ASC"
	}

	qb.order = append(qb.order, "menu_id "+order)
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereMethod(p db_repo.Predicate, value string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "method", p),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereMethodIn(value []string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "method", "IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereMethodNotIn(value []string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "method", "NOT IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) OrderByMethod(asc bool) *menuActionRepoQueryBuilder {
	order := "DESC"
	if asc {
		order = "ASC"
	}

	qb.order = append(qb.order, "method "+order)
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereApi(p db_repo.Predicate, value string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "api", p),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereApiIn(value []string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "api", "IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereApiNotIn(value []string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "api", "NOT IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) OrderByApi(asc bool) *menuActionRepoQueryBuilder {
	order := "DESC"
	if asc {
		order = "ASC"
	}

	qb.order = append(qb.order, "api "+order)
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereIsDeleted(p db_repo.Predicate, value int32) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "is_deleted", p),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereIsDeletedIn(value []int32) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "is_deleted", "IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereIsDeletedNotIn(value []int32) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "is_deleted", "NOT IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) OrderByIsDeleted(asc bool) *menuActionRepoQueryBuilder {
	order := "DESC"
	if asc {
		order = "ASC"
	}

	qb.order = append(qb.order, "is_deleted "+order)
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereCreatedAt(p db_repo.Predicate, value time.Time) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "created_at", p),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereCreatedAtIn(value []time.Time) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "created_at", "IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereCreatedAtNotIn(value []time.Time) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "created_at", "NOT IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) OrderByCreatedAt(asc bool) *menuActionRepoQueryBuilder {
	order := "DESC"
	if asc {
		order = "ASC"
	}

	qb.order = append(qb.order, "created_at "+order)
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereCreatedUser(p db_repo.Predicate, value string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "created_user", p),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereCreatedUserIn(value []string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "created_user", "IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereCreatedUserNotIn(value []string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "created_user", "NOT IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) OrderByCreatedUser(asc bool) *menuActionRepoQueryBuilder {
	order := "DESC"
	if asc {
		order = "ASC"
	}

	qb.order = append(qb.order, "created_user "+order)
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereUpdatedAt(p db_repo.Predicate, value time.Time) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "updated_at", p),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereUpdatedAtIn(value []time.Time) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "updated_at", "IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereUpdatedAtNotIn(value []time.Time) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "updated_at", "NOT IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) OrderByUpdatedAt(asc bool) *menuActionRepoQueryBuilder {
	order := "DESC"
	if asc {
		order = "ASC"
	}

	qb.order = append(qb.order, "updated_at "+order)
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereUpdatedUser(p db_repo.Predicate, value string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "updated_user", p),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereUpdatedUserIn(value []string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "updated_user", "IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) WhereUpdatedUserNotIn(value []string) *menuActionRepoQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "updated_user", "NOT IN"),
		value,
	})
	return qb
}

func (qb *menuActionRepoQueryBuilder) OrderByUpdatedUser(asc bool) *menuActionRepoQueryBuilder {
	order := "DESC"
	if asc {
		order = "ASC"
	}

	qb.order = append(qb.order, "updated_user "+order)
	return qb
}
