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

package cms_prefrence_area

import (
	"fmt"

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

	"github.com/ChangSZ/mall-go/internal/repository/mysql"
)

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

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

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

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

func (qb *cmsPrefrenceAreaQueryBuilder) 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)
	}
	if qb.limit != 0 {
		ret = ret.Limit(qb.limit)
	}
	ret = ret.Offset(qb.offset)
	return ret
}

func (qb *cmsPrefrenceAreaQueryBuilder) Updates(db *gorm.DB, m map[string]interface{}) (int64, error) {
	db = db.Model(&CmsPrefrenceArea{})

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

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

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

	ret := db.Delete(&CmsPrefrenceArea{})
	err := ret.Error
	if err != nil {
		return 0, errors.Wrap(err, "delete err")
	}
	return ret.RowsAffected, nil
}

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

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

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

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

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

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

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

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

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

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

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

func (qb *cmsPrefrenceAreaQueryBuilder) WhereName(p mysql.Predicate, value string) *cmsPrefrenceAreaQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "name", p),
		value,
	})
	return qb
}

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

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

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

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

func (qb *cmsPrefrenceAreaQueryBuilder) WhereSubTitle(p mysql.Predicate, value string) *cmsPrefrenceAreaQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "sub_title", p),
		value,
	})
	return qb
}

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

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

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

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

func (qb *cmsPrefrenceAreaQueryBuilder) WherePic(p mysql.Predicate, value []byte) *cmsPrefrenceAreaQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "pic", p),
		value,
	})
	return qb
}

func (qb *cmsPrefrenceAreaQueryBuilder) WherePicIn(value [][]byte) *cmsPrefrenceAreaQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "pic", "IN"),
		value,
	})
	return qb
}

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

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

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

func (qb *cmsPrefrenceAreaQueryBuilder) WhereSort(p mysql.Predicate, value int32) *cmsPrefrenceAreaQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "sort", p),
		value,
	})
	return qb
}

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

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

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

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

func (qb *cmsPrefrenceAreaQueryBuilder) WhereShowStatus(p mysql.Predicate, value int32) *cmsPrefrenceAreaQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "show_status", p),
		value,
	})
	return qb
}

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

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

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

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