feat: add charge

This commit is contained in:
Rogee
2024-12-12 17:59:30 +08:00
parent 1bc12248aa
commit 259b334711
11 changed files with 143 additions and 14 deletions

View File

@@ -174,13 +174,13 @@ func (svc *Service) TenantHasUser(ctx context.Context, userID, tenantID int64) (
log.Debug(stmt.DebugSql())
var result struct {
cnt int64
Cnt int64
}
if err := stmt.QueryContext(ctx, db.FromContext(ctx, svc.db), &result); err != nil {
return false, errors.Wrap(err, "failed to query user-tenant relation")
}
return result.cnt > 0, nil
return result.Cnt > 0, nil
}
// CreateTenantUser
@@ -268,7 +268,7 @@ func (svc *Service) GenerateChargeCode(ctx context.Context, tenantID, chargeAmou
if err != nil {
return "", errors.Wrap(err, "failed to encode charge code")
}
log.Infof("generate charge code: %s", code)
log.Infof("generate charge code: %s, info: %+v", code, []int64{tenantID, chargeAmount, timestamp})
return code, nil
}
@@ -379,3 +379,20 @@ func (svc *Service) Charge(ctx context.Context, claim *jwt.Claims, code string)
return nil
}
// GetTenantUserBalance
func (svc *Service) GetTenantUserBalance(ctx context.Context, tenantID, userID int64) (int64, error) {
log := svc.log.WithField("method", "GetTenantUserBalance")
tbl := table.UsersTenants
stmt := tbl.SELECT(tbl.Balance.AS("balance")).WHERE(tbl.TenantID.EQ(Int64(tenantID)).AND(tbl.UserID.EQ(Int64(userID))))
log.Debug(stmt.DebugSql())
var result struct {
Balance int64
}
if err := stmt.QueryContext(ctx, db.FromContext(ctx, svc.db), &result); err != nil {
return 0, errors.Wrap(err, "failed to query user balance")
}
return result.Balance, nil
}