feat(tenant): 按租户隔离支付退款与商户配置
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -42,10 +43,14 @@ func (r *Repository) prepareQualityOwnerPayment(ctx context.Context, userID uint
|
||||
err = r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Select("id").
|
||||
Select("id, tenant_id").
|
||||
First(&user, userID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
requestTenantID := tenancy.IDFromContext(ctx)
|
||||
if requestTenantID > 0 && user.TenantID > 0 && requestTenantID != user.TenantID {
|
||||
return ErrPaymentCannotStart
|
||||
}
|
||||
var existing model.PaymentOrder
|
||||
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("user_id = ? AND biz_type = ? AND biz_id = ?", userID, qualityOwnerPayBizType, userID).
|
||||
@@ -56,6 +61,7 @@ func (r *Repository) prepareQualityOwnerPayment(ctx context.Context, userID uint
|
||||
existing.PayWay = firstNonEmpty(req.PayWay, existing.PayWay, runtimeConfig.PayWay, "ZFBZF")
|
||||
existing.JSPayFlag = firstNonEmpty(req.JSPayFlag, existing.JSPayFlag, runtimeConfig.JSPayFlag, "2")
|
||||
existing.AmountCent = amountCent
|
||||
existing.TenantID = firstNonZero(existing.TenantID, user.TenantID, requestTenantID)
|
||||
existing.PaymentConfigID = firstNonZero(existing.PaymentConfigID, runtimeConfig.ID)
|
||||
existing.Provider = firstNonEmpty(existing.Provider, runtimeConfig.Provider)
|
||||
existing.MerchantID = firstNonEmpty(existing.MerchantID, runtimeConfig.MerchantID)
|
||||
@@ -71,7 +77,7 @@ func (r *Repository) prepareQualityOwnerPayment(ctx context.Context, userID uint
|
||||
} else if err != gorm.ErrRecordNotFound {
|
||||
return err
|
||||
}
|
||||
payment, err := newQualityOwnerPayment(userID, amountCent, req, *runtimeConfig)
|
||||
payment, err := newQualityOwnerPayment(userID, amountCent, req, *runtimeConfig, firstNonZero(user.TenantID, requestTenantID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -96,13 +102,14 @@ func canReuseQualityOwnerPayment(payment model.PaymentOrder, runtimeConfig runti
|
||||
return canReuseOrderPayment(payment, runtimeConfig)
|
||||
}
|
||||
|
||||
func newQualityOwnerPayment(userID uint64, amountCent int64, req StartPaymentRequest, runtimeConfig runtimePaymentConfig) (model.PaymentOrder, error) {
|
||||
func newQualityOwnerPayment(userID uint64, amountCent int64, req StartPaymentRequest, runtimeConfig runtimePaymentConfig, tenantID uint64) (model.PaymentOrder, error) {
|
||||
paymentNo, err := newPaymentNo()
|
||||
if err != nil {
|
||||
return model.PaymentOrder{}, err
|
||||
}
|
||||
orderNo := fmt.Sprintf("QO%d", userID)
|
||||
payment := model.PaymentOrder{
|
||||
TenantID: tenantID,
|
||||
PaymentNo: paymentNo,
|
||||
OrderID: 0,
|
||||
OrderNo: orderNo,
|
||||
@@ -127,8 +134,12 @@ func newQualityOwnerPayment(userID uint64, amountCent int64, req StartPaymentReq
|
||||
|
||||
func (r *Repository) QueryQualityOwner(ctx context.Context, userID uint64) (*PaymentDTO, error) {
|
||||
var payment model.PaymentOrder
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("user_id = ? AND biz_type = ? AND biz_id = ?", userID, qualityOwnerPayBizType, userID).
|
||||
query := r.db.WithContext(ctx).
|
||||
Where("user_id = ? AND biz_type = ? AND biz_id = ?", userID, qualityOwnerPayBizType, userID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.
|
||||
Order("id DESC").
|
||||
First(&payment).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
|
||||
Reference in New Issue
Block a user