You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.0 KiB
Go

1 month ago
package purchase
import "time"
// ContentType 内容类型
type ContentType int8
const (
ContentTypeArticle ContentType = 1 // 文章
ContentTypeColumn ContentType = 2 // 专栏
)
// ContentSource 内容来源
1 month ago
type ContentSource int8
const (
ContentSourceBuy ContentSource = 1 // 购买
ContentSourceGift ContentSource = 2 // 赠送
1 month ago
)
// 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表示永久
ExpiredAt time.Time `xorm:"notnull 'expired_at'" json:"expiredAt"` // 过期时间文章为null表示永久
1 month ago
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)
1 month ago
}
// NewPurchase 创建购买记录
func NewPurchase(userId, contentId uint64, contentType ContentType, price float64, duration int) *Purchase {
purchase := &Purchase{
1 month ago
UserId: userId,
ContentId: contentId,
ContentType: contentType,
Price: price,
Duration: duration,
1 month ago
Status: 1,
}
// 设置过期时间
if contentType == ContentTypeColumn {
purchase.ExpiredAt = time.Now().AddDate(0, duration, 0)
}
return purchase
1 month ago
}