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

package admin_menu_repo

import (
	"fmt"
	"time"

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

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

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

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

func (t *AdminMenu) 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 adminMenuRepoQueryBuilder struct {
	order []string
	where []struct {
		prefix string
		value  interface{}
	}
	limit  int
	offset int
}

func (qb *adminMenuRepoQueryBuilder) 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 *adminMenuRepoQueryBuilder) Updates(db *gorm.DB, m map[string]interface{}) (err error) {
	db = db.Model(&AdminMenu{})

	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 *adminMenuRepoQueryBuilder) Delete(db *gorm.DB) (err error) {
	for _, where := range qb.where {
		db = db.Where(where.prefix, where.value)
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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