|
|
package purchase
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
// ContentType 内容类型
|
|
|
type ContentType int8
|
|
|
|
|
|
const (
|
|
|
ContentTypeArticle ContentType = 1 // 文章
|
|
|
ContentTypeColumn ContentType = 2 // 专栏
|
|
|
)
|
|
|
|
|
|
// ContentSource 内容来源
|
|
|
type ContentSource int8
|
|
|
|
|
|
const (
|
|
|
ContentSourceBuy ContentSource = 1 // 购买
|
|
|
ContentSourceGift ContentSource = 2 // 赠送
|
|
|
)
|
|
|
|
|
|
// Purchase 内容购买记录
|
|
|
type Purchase struct {
|
|
|
Id uint64 `xorm:"pk autoincr 'id'" json:"id"`
|
|
|
UserId uint64 `xorm:"notnull 'user_id'" json:"userId"`
|
|
|
ContentId uint64 `xorm:"notnull 'content_id'" json:"contentId"`
|
|
|
ContentType ContentType `xorm:"tinyint(1) notnull 'content_type'" `
|
|
|
Price float64 `xorm:"decimal(10,2) notnull 'price'" json:"price"`
|
|
|
Duration int `xorm:"notnull 'duration'" json:"duration"` // 购买时长(月),文章为0表示永久
|
|
|
OrderNo string
|
|
|
ExpiredAt time.Time `xorm:"notnull 'expired_at'" json:"expiredAt"` // 过期时间,文章为null表示永久
|
|
|
ContentSource ContentSource `xorm:"tinyint(1) notnull 'content_source'" `
|
|
|
Status int8 `xorm:"tinyint(1) default 1 'status'" json:"status"`
|
|
|
CreatedAt time.Time `xorm:"datetime created 'created_at'" json:"createdAt"`
|
|
|
UpdatedAt time.Time `xorm:"datetime updated 'updated_at'" json:"updatedAt"`
|
|
|
}
|
|
|
|
|
|
// IsValid 检查购买记录是否有效
|
|
|
func (p *Purchase) IsValid() bool {
|
|
|
if p.Status != 1 {
|
|
|
return false
|
|
|
}
|
|
|
if p.ContentType == ContentTypeArticle {
|
|
|
return true // 文章永久有效
|
|
|
}
|
|
|
return time.Now().Before(p.ExpiredAt)
|
|
|
}
|
|
|
|
|
|
// NewPurchase 创建购买记录
|
|
|
func NewPurchase(userId, contentId uint64, contentType ContentType, price float64, duration int) *Purchase {
|
|
|
purchase := &Purchase{
|
|
|
UserId: userId,
|
|
|
ContentId: contentId,
|
|
|
ContentType: contentType,
|
|
|
Price: price,
|
|
|
Duration: duration,
|
|
|
Status: 1,
|
|
|
}
|
|
|
|
|
|
// 设置过期时间
|
|
|
if contentType == ContentTypeColumn {
|
|
|
purchase.ExpiredAt = time.Now().AddDate(0, duration, 0)
|
|
|
}
|
|
|
|
|
|
return purchase
|
|
|
}
|