feat(subscription): add admin quota reset actions (#5952)
* feat(subscription): add admin quota reset actions * fix(subscription): keep quota reset in plan row actions * refactor(subscription): move user subscription actions into menu
This commit is contained in:
@@ -296,6 +296,16 @@ type SubscriptionSummary struct {
|
||||
Subscription *UserSubscription `json:"subscription"`
|
||||
}
|
||||
|
||||
type SubscriptionResetResult struct {
|
||||
PlanId int `json:"plan_id"`
|
||||
MatchedCount int `json:"matched_count"`
|
||||
ResetCount int `json:"reset_count"`
|
||||
UserCount int `json:"user_count"`
|
||||
AdvanceResetTime bool `json:"advance_reset_time"`
|
||||
PlanTitle string `json:"-"`
|
||||
AffectedUserIds []int `json:"-"`
|
||||
}
|
||||
|
||||
func calcPlanEndTime(start time.Time, plan *SubscriptionPlan) (int64, error) {
|
||||
if plan == nil {
|
||||
return 0, errors.New("plan is nil")
|
||||
@@ -974,6 +984,125 @@ func AdminDeleteUserSubscription(userSubscriptionId int) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func resetUserSubscriptionTx(tx *gorm.DB, sub *UserSubscription, plan *SubscriptionPlan, now int64, advanceResetTime bool) error {
|
||||
if tx == nil || sub == nil || plan == nil {
|
||||
return errors.New("invalid reset args")
|
||||
}
|
||||
sub.AmountUsed = 0
|
||||
if advanceResetTime {
|
||||
nextReset := calcNextResetTime(time.Unix(now, 0), plan, sub.EndTime)
|
||||
sub.NextResetTime = nextReset
|
||||
if nextReset > 0 {
|
||||
sub.LastResetTime = now
|
||||
} else {
|
||||
sub.LastResetTime = 0
|
||||
}
|
||||
}
|
||||
return tx.Save(sub).Error
|
||||
}
|
||||
|
||||
func buildSubscriptionResetResult(plan *SubscriptionPlan, subs []UserSubscription, advanceResetTime bool) *SubscriptionResetResult {
|
||||
userIds := make([]int, 0, len(subs))
|
||||
seenUsers := make(map[int]struct{}, len(subs))
|
||||
for _, sub := range subs {
|
||||
if _, ok := seenUsers[sub.UserId]; ok {
|
||||
continue
|
||||
}
|
||||
seenUsers[sub.UserId] = struct{}{}
|
||||
userIds = append(userIds, sub.UserId)
|
||||
}
|
||||
return &SubscriptionResetResult{
|
||||
PlanId: plan.Id,
|
||||
MatchedCount: len(subs),
|
||||
ResetCount: len(subs),
|
||||
UserCount: len(userIds),
|
||||
AdvanceResetTime: advanceResetTime,
|
||||
PlanTitle: plan.Title,
|
||||
AffectedUserIds: userIds,
|
||||
}
|
||||
}
|
||||
|
||||
func adminResetUserSubscriptionsByPlanTx(tx *gorm.DB, userId int, plan *SubscriptionPlan, now int64, advanceResetTime bool) (*SubscriptionResetResult, error) {
|
||||
if tx == nil || plan == nil {
|
||||
return nil, errors.New("invalid reset args")
|
||||
}
|
||||
var subs []UserSubscription
|
||||
if err := tx.Set("gorm:query_option", "FOR UPDATE").
|
||||
Where("user_id = ? AND plan_id = ? AND status = ? AND end_time > ?", userId, plan.Id, "active", now).
|
||||
Order("end_time asc, id asc").
|
||||
Find(&subs).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(subs) == 0 {
|
||||
return nil, errors.New("该用户没有有效的此套餐订阅")
|
||||
}
|
||||
for i := range subs {
|
||||
if err := resetUserSubscriptionTx(tx, &subs[i], plan, now, advanceResetTime); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return buildSubscriptionResetResult(plan, subs, advanceResetTime), nil
|
||||
}
|
||||
|
||||
func adminResetPlanSubscriptionsTx(tx *gorm.DB, plan *SubscriptionPlan, now int64, advanceResetTime bool) (*SubscriptionResetResult, error) {
|
||||
if tx == nil || plan == nil {
|
||||
return nil, errors.New("invalid reset args")
|
||||
}
|
||||
var subs []UserSubscription
|
||||
if err := tx.Set("gorm:query_option", "FOR UPDATE").
|
||||
Where("plan_id = ? AND status = ? AND end_time > ?", plan.Id, "active", now).
|
||||
Order("user_id asc, end_time asc, id asc").
|
||||
Find(&subs).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range subs {
|
||||
if err := resetUserSubscriptionTx(tx, &subs[i], plan, now, advanceResetTime); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return buildSubscriptionResetResult(plan, subs, advanceResetTime), nil
|
||||
}
|
||||
|
||||
func AdminResetUserSubscriptionsByPlan(userId int, planId int, advanceResetTime bool) (*SubscriptionResetResult, error) {
|
||||
if userId <= 0 || planId <= 0 {
|
||||
return nil, errors.New("invalid userId or planId")
|
||||
}
|
||||
var result *SubscriptionResetResult
|
||||
now := GetDBTimestamp()
|
||||
err := DB.Transaction(func(tx *gorm.DB) error {
|
||||
plan, err := getSubscriptionPlanByIdTx(tx, planId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result, err = adminResetUserSubscriptionsByPlanTx(tx, userId, plan, now, advanceResetTime)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func AdminResetPlanSubscriptions(planId int, advanceResetTime bool) (*SubscriptionResetResult, error) {
|
||||
if planId <= 0 {
|
||||
return nil, errors.New("invalid planId")
|
||||
}
|
||||
var result *SubscriptionResetResult
|
||||
now := GetDBTimestamp()
|
||||
err := DB.Transaction(func(tx *gorm.DB) error {
|
||||
plan, err := getSubscriptionPlanByIdTx(tx, planId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result, err = adminResetPlanSubscriptionsTx(tx, plan, now, advanceResetTime)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type SubscriptionPreConsumeResult struct {
|
||||
UserSubscriptionId int
|
||||
PreConsumed int64
|
||||
|
||||
Reference in New Issue
Block a user