Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -173,6 +173,9 @@ func (l *RefundOrderLogic) RefundOrder(req *types.RefundOrderRequest) error {
|
||||
orderUser := &modeluser.User{Id: orderInfo.UserId}
|
||||
userCacheTargets = append(userCacheTargets, orderUser)
|
||||
}
|
||||
if userSub.UserId > 0 && userSub.UserId != orderInfo.UserId {
|
||||
userCacheTargets = append(userCacheTargets, &modeluser.User{Id: userSub.UserId})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -199,16 +202,16 @@ func (l *RefundOrderLogic) RefundOrder(req *types.RefundOrderRequest) error {
|
||||
}
|
||||
|
||||
func (l *RefundOrderLogic) lockRefundTargetSubscription(tx *gorm.DB, orderInfo *modelorder.Order) (*modeluser.Subscribe, error) {
|
||||
var userSub modeluser.Subscribe
|
||||
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Model(&modeluser.Subscribe{}).
|
||||
Where("order_id = ?", orderInfo.Id).
|
||||
First(&userSub).Error
|
||||
if err == nil {
|
||||
return &userSub, nil
|
||||
if userSub, err := l.lockSubscriptionByOrderID(tx, orderInfo.Id); err != nil {
|
||||
return nil, err
|
||||
} else if userSub != nil {
|
||||
return userSub, nil
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query refund subscription failed: %v", err)
|
||||
|
||||
if userSub, err := l.lockSubscriptionByEntitlement(tx, orderInfo); err != nil {
|
||||
return nil, err
|
||||
} else if userSub != nil {
|
||||
return userSub, nil
|
||||
}
|
||||
|
||||
if orderInfo.Type != 2 {
|
||||
@@ -219,17 +222,122 @@ func (l *RefundOrderLogic) lockRefundTargetSubscription(tx *gorm.DB, orderInfo *
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.OrderRefundNoSubscription), "renewal order %d has no parent order", orderInfo.Id)
|
||||
}
|
||||
|
||||
err = tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
if userSub, err := l.lockSubscriptionByOrderID(tx, orderInfo.ParentId); err != nil {
|
||||
return nil, err
|
||||
} else if userSub != nil {
|
||||
return userSub, nil
|
||||
}
|
||||
|
||||
if userSub, err := l.lockRenewalParentEntitlementSubscription(tx, orderInfo); err != nil {
|
||||
return nil, err
|
||||
} else if userSub != nil {
|
||||
return userSub, nil
|
||||
}
|
||||
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.OrderRefundNoSubscription), "renewal order %d parent subscription not found", orderInfo.Id)
|
||||
}
|
||||
|
||||
func (l *RefundOrderLogic) lockSubscriptionByOrderID(tx *gorm.DB, orderID int64) (*modeluser.Subscribe, error) {
|
||||
if orderID <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var userSub modeluser.Subscribe
|
||||
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Model(&modeluser.Subscribe{}).
|
||||
Where("order_id = ?", orderInfo.ParentId).
|
||||
Where("order_id = ?", orderID).
|
||||
First(&userSub).Error
|
||||
if err == nil {
|
||||
return &userSub, nil
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query refund subscription failed: %v", err)
|
||||
}
|
||||
|
||||
func (l *RefundOrderLogic) lockSubscriptionByEntitlement(tx *gorm.DB, orderInfo *modelorder.Order) (*modeluser.Subscribe, error) {
|
||||
entitlementUserID := orderInfo.SubscriptionUserId
|
||||
if entitlementUserID == 0 {
|
||||
entitlementUserID = orderInfo.UserId
|
||||
}
|
||||
if entitlementUserID <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if userSub, err := l.lockExactEntitlementSubscription(tx, orderInfo, entitlementUserID); err != nil {
|
||||
return nil, err
|
||||
} else if userSub != nil {
|
||||
return userSub, nil
|
||||
}
|
||||
|
||||
var userSub modeluser.Subscribe
|
||||
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Model(&modeluser.Subscribe{}).
|
||||
Where("user_id = ? AND subscribe_id = ?", entitlementUserID, orderInfo.SubscribeId).
|
||||
Where("status IN ?", []int64{0, 1, 2, 3, 5}).
|
||||
Order("expire_time DESC").
|
||||
Order("updated_at DESC").
|
||||
Order("id DESC").
|
||||
First(&userSub).Error
|
||||
if err == nil {
|
||||
return &userSub, nil
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query entitlement subscription failed: %v", err)
|
||||
}
|
||||
|
||||
func (l *RefundOrderLogic) lockExactEntitlementSubscription(tx *gorm.DB, orderInfo *modelorder.Order, entitlementUserID int64) (*modeluser.Subscribe, error) {
|
||||
if orderInfo.Id <= 0 && orderInfo.SubscribeToken == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
query := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Model(&modeluser.Subscribe{}).
|
||||
Where("user_id = ? AND subscribe_id = ?", entitlementUserID, orderInfo.SubscribeId)
|
||||
if orderInfo.Id > 0 && orderInfo.SubscribeToken != "" {
|
||||
query = query.Where("(order_id = ? OR token = ?)", orderInfo.Id, orderInfo.SubscribeToken)
|
||||
} else if orderInfo.Id > 0 {
|
||||
query = query.Where("order_id = ?", orderInfo.Id)
|
||||
} else {
|
||||
query = query.Where("token = ?", orderInfo.SubscribeToken)
|
||||
}
|
||||
|
||||
var userSub modeluser.Subscribe
|
||||
err := query.Order("id DESC").First(&userSub).Error
|
||||
if err == nil {
|
||||
return &userSub, nil
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query exact entitlement subscription failed: %v", err)
|
||||
}
|
||||
|
||||
func (l *RefundOrderLogic) lockRenewalParentEntitlementSubscription(tx *gorm.DB, orderInfo *modelorder.Order) (*modeluser.Subscribe, error) {
|
||||
var parentOrder modelorder.Order
|
||||
err := tx.Model(&modelorder.Order{}).
|
||||
Where("id = ?", orderInfo.ParentId).
|
||||
First(&parentOrder).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.OrderRefundNoSubscription), "renewal order %d parent subscription not found", orderInfo.Id)
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query renewal subscription failed: %v", err)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query renewal parent order failed: %v", err)
|
||||
}
|
||||
return &userSub, nil
|
||||
|
||||
if parentOrder.SubscriptionUserId == 0 && orderInfo.SubscriptionUserId > 0 {
|
||||
parentOrder.SubscriptionUserId = orderInfo.SubscriptionUserId
|
||||
}
|
||||
if parentOrder.UserId == 0 {
|
||||
parentOrder.UserId = orderInfo.UserId
|
||||
}
|
||||
if parentOrder.SubscribeId == 0 {
|
||||
parentOrder.SubscribeId = orderInfo.SubscribeId
|
||||
}
|
||||
return l.lockSubscriptionByEntitlement(tx, &parentOrder)
|
||||
}
|
||||
|
||||
func (l *RefundOrderLogic) lockCommissionSource(tx *gorm.DB, orderNo string, orderCommission int64) (*modeluser.User, int64, error) {
|
||||
|
||||
Reference in New Issue
Block a user