fix: issues

This commit is contained in:
Rogee
2024-12-13 10:38:54 +08:00
parent 5a6b7bcdae
commit 19f445144d
13 changed files with 262 additions and 67 deletions

View File

@@ -2,9 +2,11 @@
-- +goose StatementBegin
-- add column bind_user_id to tenants
ALTER TABLE tenants ADD COLUMN bind_user_id INT8 NOT NULL DEFAULT 0;
ALTER TABLE tenants ADD COLUMN bind_user_contact varchar(128) NOT NULL DEFAULT '';
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP COLUMN bind_user_id FROM tenants;
ALTER TABLE tenants DROP COLUMN bind_user_id ;
ALTER TABLE tenants DROP COLUMN bind_user_contact ;
-- +goose StatementEnd

View File

@@ -12,12 +12,13 @@ import (
)
type Tenants struct {
ID int64 `sql:"primary_key" json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Description *string `json:"description"`
ExpireAt time.Time `json:"expire_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BindUserID int64 `json:"bind_user_id"`
ID int64 `sql:"primary_key" json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Description *string `json:"description"`
ExpireAt time.Time `json:"expire_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BindUserID int64 `json:"bind_user_id"`
BindUserContact string `json:"bind_user_contact"`
}

View File

@@ -17,14 +17,15 @@ type tenantsTable struct {
postgres.Table
// Columns
ID postgres.ColumnInteger
Name postgres.ColumnString
Slug postgres.ColumnString
Description postgres.ColumnString
ExpireAt postgres.ColumnTimestamp
CreatedAt postgres.ColumnTimestamp
UpdatedAt postgres.ColumnTimestamp
BindUserID postgres.ColumnInteger
ID postgres.ColumnInteger
Name postgres.ColumnString
Slug postgres.ColumnString
Description postgres.ColumnString
ExpireAt postgres.ColumnTimestamp
CreatedAt postgres.ColumnTimestamp
UpdatedAt postgres.ColumnTimestamp
BindUserID postgres.ColumnInteger
BindUserContact postgres.ColumnString
AllColumns postgres.ColumnList
MutableColumns postgres.ColumnList
@@ -65,30 +66,32 @@ func newTenantsTable(schemaName, tableName, alias string) *TenantsTable {
func newTenantsTableImpl(schemaName, tableName, alias string) tenantsTable {
var (
IDColumn = postgres.IntegerColumn("id")
NameColumn = postgres.StringColumn("name")
SlugColumn = postgres.StringColumn("slug")
DescriptionColumn = postgres.StringColumn("description")
ExpireAtColumn = postgres.TimestampColumn("expire_at")
CreatedAtColumn = postgres.TimestampColumn("created_at")
UpdatedAtColumn = postgres.TimestampColumn("updated_at")
BindUserIDColumn = postgres.IntegerColumn("bind_user_id")
allColumns = postgres.ColumnList{IDColumn, NameColumn, SlugColumn, DescriptionColumn, ExpireAtColumn, CreatedAtColumn, UpdatedAtColumn, BindUserIDColumn}
mutableColumns = postgres.ColumnList{NameColumn, SlugColumn, DescriptionColumn, ExpireAtColumn, CreatedAtColumn, UpdatedAtColumn, BindUserIDColumn}
IDColumn = postgres.IntegerColumn("id")
NameColumn = postgres.StringColumn("name")
SlugColumn = postgres.StringColumn("slug")
DescriptionColumn = postgres.StringColumn("description")
ExpireAtColumn = postgres.TimestampColumn("expire_at")
CreatedAtColumn = postgres.TimestampColumn("created_at")
UpdatedAtColumn = postgres.TimestampColumn("updated_at")
BindUserIDColumn = postgres.IntegerColumn("bind_user_id")
BindUserContactColumn = postgres.StringColumn("bind_user_contact")
allColumns = postgres.ColumnList{IDColumn, NameColumn, SlugColumn, DescriptionColumn, ExpireAtColumn, CreatedAtColumn, UpdatedAtColumn, BindUserIDColumn, BindUserContactColumn}
mutableColumns = postgres.ColumnList{NameColumn, SlugColumn, DescriptionColumn, ExpireAtColumn, CreatedAtColumn, UpdatedAtColumn, BindUserIDColumn, BindUserContactColumn}
)
return tenantsTable{
Table: postgres.NewTable(schemaName, tableName, alias, allColumns...),
//Columns
ID: IDColumn,
Name: NameColumn,
Slug: SlugColumn,
Description: DescriptionColumn,
ExpireAt: ExpireAtColumn,
CreatedAt: CreatedAtColumn,
UpdatedAt: UpdatedAtColumn,
BindUserID: BindUserIDColumn,
ID: IDColumn,
Name: NameColumn,
Slug: SlugColumn,
Description: DescriptionColumn,
ExpireAt: ExpireAtColumn,
CreatedAt: CreatedAtColumn,
UpdatedAt: UpdatedAtColumn,
BindUserID: BindUserIDColumn,
BindUserContact: BindUserContactColumn,
AllColumns: allColumns,
MutableColumns: mutableColumns,

View File

@@ -54,6 +54,7 @@ func (c *Controller) Info(ctx fiber.Ctx) error {
return errors.Wrapf(err, "get tenant: %d", claim.TenantID)
}
info.IsAdmin = tenant.BindUserID == claim.UserID
info.AdminContact = tenant.BindUserContact
return ctx.JSON(info)
}
@@ -92,3 +93,26 @@ func (c *Controller) GetChargeCodes(ctx fiber.Ctx) error {
return ctx.JSON(codes)
}
// BalanceHistory
func (c *Controller) BalanceHistory(ctx fiber.Ctx) error {
claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim)
log.Debug(claim)
histories, err := c.svc.GetBalanceHistory(ctx.Context(), claim.TenantID, claim.UserID)
if err != nil {
return errorx.Wrap(err)
}
items := []BalanceHistory{}
for _, h := range histories {
items = append(items, BalanceHistory{
Balance: h.Balance,
Type: h.Type,
Target: h.Target,
CreatedAt: h.CreatedAt,
})
}
return ctx.JSON(items)
}

View File

@@ -1,6 +1,20 @@
package users
import (
"time"
"backend/pkg/pg"
)
type UserInfo struct {
IsAdmin bool `json:"is_admin"`
Balance int64 `json:"balance"`
IsAdmin bool `json:"is_admin"`
AdminContact string `json:"admin_contact"`
Balance int64 `json:"balance"`
}
type BalanceHistory struct {
Type pg.BalanceType `json:"type"`
Target pg.BalanceTarget `json:"target"`
Balance int64 `json:"balance"`
CreatedAt time.Time `json:"created_at"`
}

View File

@@ -29,4 +29,5 @@ func (r *Router) Register(router fiber.Router) {
group.Get("info", r.controller.Info)
group.Patch("charge/:code", r.controller.Charge)
group.Get("codes", r.controller.GetChargeCodes)
group.Get("balance-histories", r.controller.BalanceHistory)
}

View File

@@ -413,3 +413,28 @@ func (svc *Service) SetTenantBindUserID(ctx context.Context, tenantID, adminUser
}
return nil
}
// GetBalanceHistory
func (svc *Service) GetBalanceHistory(ctx context.Context, tenantID, userID int64) ([]model.UserBalanceHistories, error) {
log := svc.log.WithField("method", "GetBalanceHistory")
tbl := table.UserBalanceHistories
stmt := tbl.
SELECT(
tbl.AllColumns,
).
WHERE(
tbl.TenantID.EQ(Int64(tenantID)).AND(tbl.UserID.EQ(Int64(userID))),
).
ORDER_BY(
tbl.CreatedAt.DESC(),
).
LIMIT(20)
log.Debug(stmt.DebugSql())
var items []model.UserBalanceHistories
if err := stmt.QueryContext(ctx, db.FromContext(ctx, svc.db), &items); err != nil {
return nil, errors.Wrap(err, "failed to query balance history")
}
return items, nil
}

View File

@@ -31,6 +31,7 @@ func Command() atom.Option {
return atom.Command(
atom.Name("tenants"),
atom.Short("租户相关操作"),
atom.Example("create Name --slug [slug] --contact [wechat_contact]"),
atom.Command(
atom.Name("create"),
atom.Providers(defaultProviders().With(
@@ -40,6 +41,7 @@ func Command() atom.Option {
)),
atom.Arguments(func(cmd *cobra.Command) {
cmd.Flags().String("slug", "", "slug")
cmd.Flags().String("contact", "", "contact")
}),
atom.RunE(func(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(t *tenant.Create) error {