Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 39s
feat(抽奖动态): 修复抽奖动态未渲染问题并优化文案展示 fix(用户概览): 修复用户概览无数据显示问题 feat(新用户列表): 在新用户列表显示称号明细 refactor(待办事项): 移除代办模块并全宽展示实时动态 feat(批量操作): 限制为单用户操作并在批量时提醒 fix(称号分配): 防重复分配称号的改造计划 perf(接口性能): 优化新用户和抽奖动态接口性能 feat(订单漏斗): 优化订单转化漏斗指标计算 docs(测试计划): 完善盲盒运营API核查与闭环测试计划
57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
package user
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
|
||
"bindbox-game/internal/repository/mysql/model"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
func (s *service) AddCoupon(ctx context.Context, userID int64, couponID int64) error {
|
||
tpl, err := s.readDB.SystemCoupons.WithContext(ctx).Where(s.readDB.SystemCoupons.ID.Eq(couponID)).First()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if tpl == nil || tpl.Status != 1 {
|
||
return nil
|
||
}
|
||
// 配额检查:若 TotalQuantity > 0 则限制发放总量
|
||
if tpl.TotalQuantity > 0 {
|
||
issued, ierr := s.readDB.UserCoupons.WithContext(ctx).Where(s.readDB.UserCoupons.CouponID.Eq(couponID)).Count()
|
||
if ierr != nil {
|
||
return ierr
|
||
}
|
||
if issued >= tpl.TotalQuantity {
|
||
return gorm.ErrInvalidData
|
||
}
|
||
}
|
||
// 用户持有上限:同模板未使用的数量最多1张
|
||
exist, eerr := s.readDB.UserCoupons.WithContext(ctx).
|
||
Where(s.readDB.UserCoupons.UserID.Eq(userID)).
|
||
Where(s.readDB.UserCoupons.CouponID.Eq(couponID)).
|
||
Where(s.readDB.UserCoupons.Status.Eq(1)).
|
||
Count()
|
||
if eerr != nil {
|
||
return eerr
|
||
}
|
||
if exist > 0 {
|
||
return gorm.ErrInvalidData
|
||
}
|
||
item := &model.UserCoupons{UserID: userID, CouponID: couponID, Status: 1}
|
||
if !tpl.ValidStart.IsZero() {
|
||
item.ValidStart = tpl.ValidStart
|
||
} else {
|
||
item.ValidStart = time.Now()
|
||
}
|
||
if !tpl.ValidEnd.IsZero() {
|
||
item.ValidEnd = tpl.ValidEnd
|
||
}
|
||
do := s.writeDB.UserCoupons.WithContext(ctx).Omit(s.readDB.UserCoupons.UsedAt, s.readDB.UserCoupons.UsedOrderID)
|
||
if tpl.ValidEnd.IsZero() {
|
||
do = do.Omit(s.writeDB.UserCoupons.ValidEnd)
|
||
}
|
||
return do.Create(item)
|
||
}
|
||
|