package models

import (
	orm "go-admin/common/global"
	"go-admin/logger"
)

type ShopperPromotionCode struct {
	Model

	Uid     uint32 `json:"uid"`
	State   int8   `json:"state"`
	Code    string `json:"code"`
	StoreId uint32 `json:"store_id"`

	User *UserInfo `json:"user" gorm:"foreignKey:uid;references:uid"`
}

func (s ShopperPromotionCode) TableName() string {
	return "shopper_promotion_code"
}

func CheckCode(code string) bool {
	var c int64
	err := orm.Eloquent.
		Table("shopper_promotion_code").
		Where("code = ?", code).
		Count(&c).
		Error
	if err != nil {
		logger.Error("check code err", logger.Field("err", err))
		return false
	}
	return c > 0
}
