feat(quality-owner): 增加优质号主自助取消与权益校验
This commit is contained in:
@@ -23,8 +23,21 @@ var (
|
||||
ErrDepositInsufficient = errors.New("quality owner deposit insufficient")
|
||||
ErrInvalidReason = errors.New("invalid reason")
|
||||
ErrPaymentPending = errors.New("quality owner payment pending")
|
||||
ErrActivePremiumListings = errors.New("active premium listings exist")
|
||||
)
|
||||
|
||||
type ActivePremiumListingsError struct {
|
||||
Count int64
|
||||
}
|
||||
|
||||
func (e ActivePremiumListingsError) Error() string {
|
||||
return ErrActivePremiumListings.Error()
|
||||
}
|
||||
|
||||
func (e ActivePremiumListingsError) Is(target error) bool {
|
||||
return target == ErrActivePremiumListings
|
||||
}
|
||||
|
||||
type AuditMeta = auditlog.Meta
|
||||
|
||||
type Repository struct {
|
||||
@@ -72,7 +85,11 @@ func (r *Repository) Status(ctx context.Context, userID uint64) (*StatusDTO, err
|
||||
if walletErr != nil && !errors.Is(walletErr, gorm.ErrRecordNotFound) {
|
||||
return nil, walletErr
|
||||
}
|
||||
return toStatusDTO(user, account, rules), nil
|
||||
activePremiumListings, err := activePremiumListingCount(r.db.WithContext(ctx), userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toStatusDTO(user, account, rules, activePremiumListings), nil
|
||||
}
|
||||
|
||||
// PaymentAmount 返回当前用户在线支付开通优质号主所需的保证金金额。
|
||||
@@ -254,6 +271,53 @@ func (r *Repository) Activate(ctx context.Context, userID uint64) (*StatusDTO, e
|
||||
return r.Status(ctx, userID)
|
||||
}
|
||||
|
||||
func (r *Repository) Cancel(ctx context.Context, userID uint64, meta AuditMeta) (*StatusDTO, error) {
|
||||
if r == nil || r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
if userID == 0 {
|
||||
return nil, ErrInvalidUser
|
||||
}
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if user.QualityOwnerStatus != StatusActive {
|
||||
return ErrInactive
|
||||
}
|
||||
beforeDepositCent := user.QualityOwnerDepositCent
|
||||
beforeSource := user.QualityOwnerDepositSource
|
||||
if err := cancelActiveQualityOwnerTx(tx, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Save(&user).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return auditlog.Append(tx, auditlog.Entry{
|
||||
ActorType: "user",
|
||||
ActorID: userID,
|
||||
Action: "quality_owner.cancel",
|
||||
BizType: "user",
|
||||
BizID: &userID,
|
||||
Meta: meta,
|
||||
Detail: map[string]any{
|
||||
"reason": "用户主动取消优质号主",
|
||||
"before_status": StatusActive,
|
||||
"after_status": user.QualityOwnerStatus,
|
||||
"before_deposit_cent": beforeDepositCent,
|
||||
"after_deposit_cent": user.QualityOwnerDepositCent,
|
||||
"before_deposit_source": beforeSource,
|
||||
"after_deposit_source": user.QualityOwnerDepositSource,
|
||||
},
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.Status(ctx, userID)
|
||||
}
|
||||
|
||||
func (r *Repository) AdminSet(ctx context.Context, adminID uint64, userID uint64, req AdminSetRequest, meta AuditMeta) (*StatusDTO, error) {
|
||||
if r == nil || r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
@@ -292,42 +356,7 @@ func (r *Repository) AdminSet(ctx context.Context, adminID uint64, userID uint64
|
||||
if beforeStatus != StatusActive {
|
||||
return ErrInactive
|
||||
}
|
||||
if beforeSource == DepositSourcePaid && beforeDepositCent > 0 {
|
||||
err := wallet.AppendEntries(tx,
|
||||
wallet.Entry{
|
||||
UserID: userID,
|
||||
Direction: "out",
|
||||
AmountCent: beforeDepositCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "quality_owner_deposit_refund",
|
||||
BizNo: fmt.Sprintf("QOR%d", userID),
|
||||
Remark: "取消优质号主,解冻保证金",
|
||||
},
|
||||
wallet.Entry{
|
||||
UserID: userID,
|
||||
Direction: "in",
|
||||
AmountCent: beforeDepositCent,
|
||||
BalanceType: "available",
|
||||
BizType: "quality_owner_deposit_refund",
|
||||
BizNo: fmt.Sprintf("QOR%d", userID),
|
||||
Remark: "优质号主保证金退回",
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
user.QualityOwnerStatus = StatusNone
|
||||
user.QualityOwnerDepositCent = 0
|
||||
user.QualityOwnerDepositSource = ""
|
||||
user.QualityOwnerActivatedAt = nil
|
||||
if err := tx.Model(&model.RentalListing{}).
|
||||
Where("owner_id = ? AND is_premium = ? AND status IN ?", userID, true, []string{"draft", "published"}).
|
||||
Updates(map[string]any{
|
||||
"status": "offline",
|
||||
"review_status": "none",
|
||||
"review_reason": "优质号主资格已取消",
|
||||
}).Error; err != nil {
|
||||
if err := cancelActiveQualityOwnerTx(tx, &user); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -362,11 +391,76 @@ func (r *Repository) AdminSet(ctx context.Context, adminID uint64, userID uint64
|
||||
return r.Status(ctx, userID)
|
||||
}
|
||||
|
||||
func toStatusDTO(user model.User, account model.WalletAccount, rules Rules) *StatusDTO {
|
||||
func activePremiumListingCount(db *gorm.DB, userID uint64) (int64, error) {
|
||||
var count int64
|
||||
err := db.Model(&model.RentalListing{}).
|
||||
Where(
|
||||
"owner_id = ? AND is_premium = ? AND (status IN ? OR in_transaction = ?)",
|
||||
userID,
|
||||
true,
|
||||
[]string{"published", "rented"},
|
||||
true,
|
||||
).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
func cancelActiveQualityOwnerTx(tx *gorm.DB, user *model.User) error {
|
||||
if tx == nil || user == nil || user.ID == 0 {
|
||||
return ErrInvalidUser
|
||||
}
|
||||
activeListings, err := activePremiumListingCount(tx, user.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if activeListings > 0 {
|
||||
return ActivePremiumListingsError{Count: activeListings}
|
||||
}
|
||||
if user.QualityOwnerDepositSource == DepositSourcePaid && user.QualityOwnerDepositCent > 0 {
|
||||
if err := wallet.AppendEntries(tx,
|
||||
wallet.Entry{
|
||||
UserID: user.ID,
|
||||
Direction: "out",
|
||||
AmountCent: user.QualityOwnerDepositCent,
|
||||
BalanceType: "frozen",
|
||||
BizType: "quality_owner_deposit_refund",
|
||||
BizNo: fmt.Sprintf("QOR%d", user.ID),
|
||||
Remark: "取消优质号主,解冻保证金",
|
||||
},
|
||||
wallet.Entry{
|
||||
UserID: user.ID,
|
||||
Direction: "in",
|
||||
AmountCent: user.QualityOwnerDepositCent,
|
||||
BalanceType: "available",
|
||||
BizType: "quality_owner_deposit_refund",
|
||||
BizNo: fmt.Sprintf("QOR%d", user.ID),
|
||||
Remark: "优质号主保证金退回",
|
||||
},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
user.QualityOwnerStatus = StatusNone
|
||||
user.QualityOwnerDepositCent = 0
|
||||
user.QualityOwnerDepositSource = ""
|
||||
user.QualityOwnerActivatedAt = nil
|
||||
return tx.Model(&model.RentalListing{}).
|
||||
Where("owner_id = ? AND is_premium = ? AND status IN ?", user.ID, true, []string{"draft", "published"}).
|
||||
Updates(map[string]any{
|
||||
"status": "offline",
|
||||
"review_status": "none",
|
||||
"review_reason": "优质号主资格已取消",
|
||||
}).Error
|
||||
}
|
||||
|
||||
func toStatusDTO(user model.User, account model.WalletAccount, rules Rules, activePremiumListings int64) *StatusDTO {
|
||||
active := user.QualityOwnerStatus == StatusActive
|
||||
return &StatusDTO{
|
||||
UserID: user.ID,
|
||||
Active: user.QualityOwnerStatus == StatusActive,
|
||||
Active: active,
|
||||
Status: user.QualityOwnerStatus,
|
||||
CanCancel: active && activePremiumListings == 0,
|
||||
ActivePremiumListings: activePremiumListings,
|
||||
DepositAmountCent: user.QualityOwnerDepositCent,
|
||||
DepositSource: user.QualityOwnerDepositSource,
|
||||
ActivatedAt: user.QualityOwnerActivatedAt,
|
||||
|
||||
Reference in New Issue
Block a user