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

package pms_product_operate_log

import (
	"fmt"
	"time"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func (qb *pmsProductOperateLogQueryBuilder) WherePriceOld(p mysql.Predicate, value float64) *pmsProductOperateLogQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "price_old", p),
		value,
	})
	return qb
}

func (qb *pmsProductOperateLogQueryBuilder) WherePriceOldIn(value []float64) *pmsProductOperateLogQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "price_old", "IN"),
		value,
	})
	return qb
}

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

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

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

func (qb *pmsProductOperateLogQueryBuilder) WherePriceNew(p mysql.Predicate, value float64) *pmsProductOperateLogQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "price_new", p),
		value,
	})
	return qb
}

func (qb *pmsProductOperateLogQueryBuilder) WherePriceNewIn(value []float64) *pmsProductOperateLogQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "price_new", "IN"),
		value,
	})
	return qb
}

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

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

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

func (qb *pmsProductOperateLogQueryBuilder) WhereSalePriceOld(p mysql.Predicate, value float64) *pmsProductOperateLogQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "sale_price_old", p),
		value,
	})
	return qb
}

func (qb *pmsProductOperateLogQueryBuilder) WhereSalePriceOldIn(value []float64) *pmsProductOperateLogQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "sale_price_old", "IN"),
		value,
	})
	return qb
}

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

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

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

func (qb *pmsProductOperateLogQueryBuilder) WhereSalePriceNew(p mysql.Predicate, value float64) *pmsProductOperateLogQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "sale_price_new", p),
		value,
	})
	return qb
}

func (qb *pmsProductOperateLogQueryBuilder) WhereSalePriceNewIn(value []float64) *pmsProductOperateLogQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "sale_price_new", "IN"),
		value,
	})
	return qb
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func (qb *pmsProductOperateLogQueryBuilder) WhereCreateTime(p mysql.Predicate, value time.Time) *pmsProductOperateLogQueryBuilder {
	qb.where = append(qb.where, struct {
		prefix string
		value  interface{}
	}{
		fmt.Sprintf("%v %v ?", "create_time", p),
		value,
	})
	return qb
}

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

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

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

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