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

package sms_coupon_history

import (
	"fmt"
	"time"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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