124 lines
3.7 KiB
Go
124 lines
3.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"dianshang/internal/model"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CouponRepository 优惠券仓储
|
|
type CouponRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewCouponRepository 创建优惠券仓储
|
|
func NewCouponRepository(db *gorm.DB) *CouponRepository {
|
|
return &CouponRepository{db: db}
|
|
}
|
|
|
|
// GetAvailableCoupons 获取可用优惠券列表
|
|
func (r *CouponRepository) GetAvailableCoupons() ([]model.Coupon, error) {
|
|
var coupons []model.Coupon
|
|
now := time.Now()
|
|
err := r.db.Where("status = ? AND start_time <= ? AND end_time >= ?", 1, now, now).
|
|
Where("total_count = 0 OR used_count < total_count").
|
|
Order("created_at DESC").
|
|
Find(&coupons).Error
|
|
return coupons, err
|
|
}
|
|
|
|
// GetUserCoupons 获取用户优惠券
|
|
func (r *CouponRepository) GetUserCoupons(userID uint, status int) ([]model.UserCoupon, error) {
|
|
var userCoupons []model.UserCoupon
|
|
query := r.db.Preload("Coupon").Where("user_id = ?", userID)
|
|
|
|
if status > 0 {
|
|
// API状态值到数据库状态值的映射
|
|
// API: 1=未使用,2=已使用,3=已过期
|
|
// DB: 0=未使用,1=已使用,2=已过期
|
|
dbStatus := status - 1
|
|
query = query.Where("status = ?", dbStatus)
|
|
}
|
|
|
|
err := query.Order("created_at DESC").Find(&userCoupons).Error
|
|
return userCoupons, err
|
|
}
|
|
|
|
// GetByID 根据ID获取优惠券
|
|
func (r *CouponRepository) GetByID(id uint) (*model.Coupon, error) {
|
|
var coupon model.Coupon
|
|
err := r.db.First(&coupon, id).Error
|
|
return &coupon, err
|
|
}
|
|
|
|
// CheckUserCouponExists 检查用户是否已领取优惠券
|
|
func (r *CouponRepository) CheckUserCouponExists(userID, couponID uint) (bool, error) {
|
|
var count int64
|
|
err := r.db.Model(&model.UserCoupon{}).
|
|
Where("user_id = ? AND coupon_id = ?", userID, couponID).
|
|
Count(&count).Error
|
|
return count > 0, err
|
|
}
|
|
|
|
// GetUserCouponCount 获取用户领取某优惠券的数量
|
|
func (r *CouponRepository) GetUserCouponCount(userID, couponID uint) (int, error) {
|
|
var count int64
|
|
err := r.db.Model(&model.UserCoupon{}).
|
|
Where("user_id = ? AND coupon_id = ?", userID, couponID).
|
|
Count(&count).Error
|
|
return int(count), err
|
|
}
|
|
|
|
// CreateUserCoupon 创建用户优惠券记录
|
|
func (r *CouponRepository) CreateUserCoupon(userCoupon *model.UserCoupon) error {
|
|
return r.db.Transaction(func(tx *gorm.DB) error {
|
|
// 创建用户优惠券记录
|
|
if err := tx.Create(userCoupon).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
// 更新优惠券使用数量
|
|
return tx.Model(&model.Coupon{}).
|
|
Where("id = ?", userCoupon.CouponID).
|
|
UpdateColumn("used_count", gorm.Expr("used_count + ?", 1)).Error
|
|
})
|
|
}
|
|
|
|
// GetUserCouponByID 根据ID获取用户优惠券
|
|
func (r *CouponRepository) GetUserCouponByID(id uint) (*model.UserCoupon, error) {
|
|
var userCoupon model.UserCoupon
|
|
err := r.db.Preload("Coupon").First(&userCoupon, id).Error
|
|
return &userCoupon, err
|
|
}
|
|
|
|
// GetUserCouponByOrderID 根据订单ID获取用户优惠券
|
|
func (r *CouponRepository) GetUserCouponByOrderID(orderID uint) (*model.UserCoupon, error) {
|
|
var userCoupon model.UserCoupon
|
|
err := r.db.Preload("Coupon").Where("order_id = ?", orderID).First(&userCoupon).Error
|
|
return &userCoupon, err
|
|
}
|
|
|
|
// UseCoupon 使用优惠券
|
|
func (r *CouponRepository) UseCoupon(userCouponID, orderID uint) error {
|
|
now := time.Now()
|
|
return r.db.Model(&model.UserCoupon{}).
|
|
Where("id = ?", userCouponID).
|
|
Updates(map[string]interface{}{
|
|
"status": 1, // 已使用
|
|
"order_id": orderID,
|
|
"used_time": &now,
|
|
}).Error
|
|
}
|
|
|
|
// RestoreCoupon 恢复优惠券(取消订单时使用)
|
|
func (r *CouponRepository) RestoreCoupon(userCouponID uint) error {
|
|
return r.db.Model(&model.UserCoupon{}).
|
|
Where("id = ?", userCouponID).
|
|
Updates(map[string]interface{}{
|
|
"status": 0, // 恢复为未使用
|
|
"order_id": nil, // 清除订单关联
|
|
"used_time": nil, // 清除使用时间
|
|
}).Error
|
|
}
|