feat(tenant): 按租户隔离用户身份与后台权限
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/wallet"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -30,9 +31,14 @@ func (r *Repository) resolveSiteID(ctx context.Context, siteCode string) (uint64
|
||||
// 用户创建提现申请
|
||||
func (r *Repository) Create(ctx context.Context, userID uint64, req CreateWithdrawalRequest, siteCode ...string) (*WithdrawalDTO, error) {
|
||||
db := r.db.WithContext(ctx)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
// 验证收款账号
|
||||
var paymentAccount model.UserPaymentAccount
|
||||
if err := db.Where("id = ? AND user_id = ? AND status = ?", req.PaymentAccountID, userID, "active").
|
||||
accountQuery := db.Where("id = ? AND user_id = ? AND status = ?", req.PaymentAccountID, userID, "active")
|
||||
if tenantID > 0 {
|
||||
accountQuery = accountQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := accountQuery.
|
||||
First(&paymentAccount).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("payment account not found")
|
||||
@@ -67,6 +73,7 @@ func (r *Repository) Create(ctx context.Context, userID uint64, req CreateWithdr
|
||||
|
||||
withdrawal := model.WithdrawalRequest{
|
||||
WithdrawNo: withdrawNo,
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
DistributionSiteID: siteID,
|
||||
AmountCent: req.AmountCent,
|
||||
@@ -90,6 +97,7 @@ func (r *Repository) Create(ctx context.Context, userID uint64, req CreateWithdr
|
||||
|
||||
// 冻结用户余额
|
||||
if err := wallet.AppendEntries(tx, wallet.Entry{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
DistributionSiteID: siteID,
|
||||
Direction: "out",
|
||||
@@ -99,6 +107,7 @@ func (r *Repository) Create(ctx context.Context, userID uint64, req CreateWithdr
|
||||
BizNo: withdrawNo,
|
||||
Remark: "提现冻结",
|
||||
}, wallet.Entry{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
DistributionSiteID: siteID,
|
||||
Direction: "in",
|
||||
@@ -124,6 +133,7 @@ func (r *Repository) Create(ctx context.Context, userID uint64, req CreateWithdr
|
||||
// 用户查询提现列表
|
||||
func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int, siteCode ...string) (*PaginatedResult, error) {
|
||||
db := r.db.WithContext(ctx)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var code string
|
||||
if len(siteCode) > 0 {
|
||||
code = siteCode[0]
|
||||
@@ -135,6 +145,10 @@ func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int
|
||||
|
||||
countDB := db.Model(&model.WithdrawalRequest{}).Where("user_id = ?", userID)
|
||||
dataDB := db.Where("user_id = ?", userID)
|
||||
if tenantID > 0 {
|
||||
countDB = countDB.Where("tenant_id = ?", tenantID)
|
||||
dataDB = dataDB.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if siteID > 0 {
|
||||
countDB = countDB.Where("distribution_site_id = ?", siteID)
|
||||
dataDB = dataDB.Where("distribution_site_id = ?", siteID)
|
||||
@@ -174,7 +188,11 @@ func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int
|
||||
// 用户查询提现详情
|
||||
func (r *Repository) FindByID(ctx context.Context, userID, id uint64) (*WithdrawalDTO, error) {
|
||||
var withdrawal model.WithdrawalRequest
|
||||
if err := r.db.WithContext(ctx).Where("id = ? AND user_id = ?", id, userID).
|
||||
query := r.db.WithContext(ctx).Where("id = ? AND user_id = ?", id, userID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.
|
||||
First(&withdrawal).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrWithdrawalNotFound
|
||||
@@ -191,8 +209,12 @@ func (r *Repository) FindByID(ctx context.Context, userID, id uint64) (*Withdraw
|
||||
func (r *Repository) Cancel(ctx context.Context, userID, id uint64) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var withdrawal model.WithdrawalRequest
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("id = ? AND user_id = ?", id, userID).
|
||||
query := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("id = ? AND user_id = ?", id, userID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.
|
||||
First(&withdrawal).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrWithdrawalNotFound
|
||||
@@ -212,6 +234,7 @@ func (r *Repository) Cancel(ctx context.Context, userID, id uint64) error {
|
||||
|
||||
// 解冻余额
|
||||
if err := wallet.AppendEntries(tx, wallet.Entry{
|
||||
TenantID: withdrawal.TenantID,
|
||||
UserID: userID,
|
||||
DistributionSiteID: withdrawal.DistributionSiteID,
|
||||
Direction: "out",
|
||||
@@ -221,6 +244,7 @@ func (r *Repository) Cancel(ctx context.Context, userID, id uint64) error {
|
||||
BizNo: withdrawal.WithdrawNo,
|
||||
Remark: "用户取消提现",
|
||||
}, wallet.Entry{
|
||||
TenantID: withdrawal.TenantID,
|
||||
UserID: userID,
|
||||
DistributionSiteID: withdrawal.DistributionSiteID,
|
||||
Direction: "in",
|
||||
|
||||
Reference in New Issue
Block a user