118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"dianshang/internal/model"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// PointsRepository 积分数据访问层
|
||
|
|
type PointsRepository struct {
|
||
|
|
db *gorm.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewPointsRepository 创建积分数据访问层
|
||
|
|
func NewPointsRepository(db *gorm.DB) *PointsRepository {
|
||
|
|
return &PointsRepository{db: db}
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetUserPoints 获取用户积分
|
||
|
|
func (r *PointsRepository) GetUserPoints(userID uint) (int, error) {
|
||
|
|
var user model.User
|
||
|
|
err := r.db.Select("points").Where("id = ?", userID).First(&user).Error
|
||
|
|
if err != nil {
|
||
|
|
return 0, err
|
||
|
|
}
|
||
|
|
return user.Points, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// UpdateUserPoints 更新用户积分
|
||
|
|
func (r *PointsRepository) UpdateUserPoints(userID uint, points int) error {
|
||
|
|
return r.db.Model(&model.User{}).Where("id = ?", userID).Update("points", points).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// CreatePointsHistory 创建积分历史记录
|
||
|
|
func (r *PointsRepository) CreatePointsHistory(history *model.PointsHistory) error {
|
||
|
|
return r.db.Create(history).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetPointsHistory 获取积分历史记录
|
||
|
|
func (r *PointsRepository) GetPointsHistory(userID uint, page, pageSize int) ([]model.PointsHistory, int64, error) {
|
||
|
|
var histories []model.PointsHistory
|
||
|
|
var total int64
|
||
|
|
|
||
|
|
offset := (page - 1) * pageSize
|
||
|
|
|
||
|
|
// 获取总数
|
||
|
|
err := r.db.Model(&model.PointsHistory{}).Where("user_id = ?", userID).Count(&total).Error
|
||
|
|
if err != nil {
|
||
|
|
return nil, 0, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取分页数据
|
||
|
|
err = r.db.Where("user_id = ?", userID).
|
||
|
|
Order("created_at DESC").
|
||
|
|
Offset(offset).
|
||
|
|
Limit(pageSize).
|
||
|
|
Find(&histories).Error
|
||
|
|
|
||
|
|
return histories, total, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetPointsRules 获取积分规则列表
|
||
|
|
func (r *PointsRepository) GetPointsRules() ([]model.PointsRule, error) {
|
||
|
|
var rules []model.PointsRule
|
||
|
|
err := r.db.Where("status = ?", 1).Order("sort ASC, id ASC").Find(&rules).Error
|
||
|
|
return rules, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetPointsExchangeList 获取积分兑换商品列表
|
||
|
|
func (r *PointsRepository) GetPointsExchangeList() ([]model.PointsExchange, error) {
|
||
|
|
var exchanges []model.PointsExchange
|
||
|
|
err := r.db.Where("status = ?", 1).Order("sort ASC, id ASC").Find(&exchanges).Error
|
||
|
|
return exchanges, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetPointsExchangeByID 根据ID获取积分兑换商品
|
||
|
|
func (r *PointsRepository) GetPointsExchangeByID(id uint) (*model.PointsExchange, error) {
|
||
|
|
var exchange model.PointsExchange
|
||
|
|
err := r.db.Where("id = ? AND status = ?", id, 1).First(&exchange).Error
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &exchange, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// CreatePointsExchangeRecord 创建积分兑换记录
|
||
|
|
func (r *PointsRepository) CreatePointsExchangeRecord(record *model.PointsExchangeRecord) error {
|
||
|
|
return r.db.Create(record).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// UpdatePointsExchangeCount 更新兑换商品的兑换次数
|
||
|
|
func (r *PointsRepository) UpdatePointsExchangeCount(id uint) error {
|
||
|
|
return r.db.Model(&model.PointsExchange{}).Where("id = ?", id).
|
||
|
|
UpdateColumn("exchange_count", gorm.Expr("exchange_count + ?", 1)).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetUserExchangeRecords 获取用户兑换记录
|
||
|
|
func (r *PointsRepository) GetUserExchangeRecords(userID uint, page, pageSize int) ([]model.PointsExchangeRecord, int64, error) {
|
||
|
|
var records []model.PointsExchangeRecord
|
||
|
|
var total int64
|
||
|
|
|
||
|
|
offset := (page - 1) * pageSize
|
||
|
|
|
||
|
|
// 获取总数
|
||
|
|
err := r.db.Model(&model.PointsExchangeRecord{}).Where("user_id = ?", userID).Count(&total).Error
|
||
|
|
if err != nil {
|
||
|
|
return nil, 0, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取分页数据,预加载关联数据
|
||
|
|
err = r.db.Preload("PointsExchange").
|
||
|
|
Where("user_id = ?", userID).
|
||
|
|
Order("created_at DESC").
|
||
|
|
Offset(offset).
|
||
|
|
Limit(pageSize).
|
||
|
|
Find(&records).Error
|
||
|
|
|
||
|
|
return records, total, err
|
||
|
|
}
|