| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package models
- import "time"
- // PointsConsumptionLog 积分消费记录
- type PointsConsumptionLog struct {
- ID uint `gorm:"primarykey;autoIncrement" json:"id"`
- UserID string `gorm:"type:varchar(255);not null;index" json:"user_id"`
- FileName string `gorm:"type:varchar(500);not null" json:"file_name"`
- FileURL string `gorm:"type:text" json:"file_url"`
- PointsConsumed int `gorm:"not null;default:10" json:"points_consumed"`
- BalanceAfter int `gorm:"not null" json:"balance_after"`
- CreatedAt *time.Time `gorm:"column:created_at" json:"created_at"`
- }
- // TableName 指定表名
- func (PointsConsumptionLog) TableName() string {
- return "points_consumption_log"
- }
- // CreateConsumptionLog 创建消费记录
- func CreateConsumptionLog(log *PointsConsumptionLog) error {
- // 使用原始SQL插入,避免GORM的时间字段处理问题
- result := DB.Exec(
- "INSERT INTO points_consumption_log (user_id, file_name, file_url, points_consumed, balance_after) VALUES (?, ?, ?, ?, ?)",
- log.UserID, log.FileName, log.FileURL, log.PointsConsumed, log.BalanceAfter,
- )
- return result.Error
- }
- // GetConsumptionHistory 获取用户消费记录(按时间倒序)
- func GetConsumptionHistory(userID string, page, pageSize int) ([]PointsConsumptionLog, int64, error) {
- var logs []PointsConsumptionLog
- var total int64
- DB.Model(&PointsConsumptionLog{}).Where("user_id = ?", userID).Count(&total)
- offset := (page - 1) * pageSize
- err := DB.Where("user_id = ?", userID).
- Order("created_at DESC").
- Offset(offset).
- Limit(pageSize).
- Find(&logs).Error
- return logs, total, err
- }
|