feat: update admin

This commit is contained in:
yanghao05
2025-04-28 19:12:31 +08:00
parent 82c112b1eb
commit 685c87207f
12 changed files with 251 additions and 238 deletions

View File

@@ -257,3 +257,21 @@ func (m *mediasModel) GetRelations(ctx context.Context, hash string) ([]*model.M
return &media
}), nil
}
// Count
func (m *mediasModel) Count(ctx context.Context) (int64, error) {
tbl := table.Medias
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count"))
m.log.Infof("sql: %s", stmt.DebugSql())
var count struct {
Count int64
}
if err := stmt.QueryContext(ctx, db, &count); err != nil {
m.log.Errorf("error counting media items: %v", err)
return 0, err
}
return count.Count, nil
}

View File

@@ -203,3 +203,43 @@ func (m *ordersModel) SetStatus(ctx context.Context, orderNo string, status fiel
}
return nil
}
// Count
func (m *ordersModel) Count(ctx context.Context, cond BoolExpression) (int64, error) {
tbl := table.Orders
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(cond)
m.log.Infof("sql: %s", stmt.DebugSql())
var cnt struct {
Cnt int64
}
err := stmt.QueryContext(ctx, db, &cnt)
if err != nil {
m.log.Errorf("error counting orders: %v", err)
return 0, err
}
return cnt.Cnt, nil
}
// SumAmount
func (m *ordersModel) SumAmount(ctx context.Context) (int64, error) {
tbl := table.Orders
stmt := SELECT(SUM(tbl.Price).AS("cnt")).FROM(tbl).WHERE(
tbl.Status.EQ(Int(int64(fields.OrderStatusPaid))),
)
m.log.Infof("sql: %s", stmt.DebugSql())
var cnt struct {
Cnt int64
}
err := stmt.QueryContext(ctx, db, &cnt)
if err != nil {
m.log.Errorf("error summing order amount: %v", err)
return 0, err
}
return cnt.Cnt, nil
}

View File

@@ -395,3 +395,21 @@ func (m *postsModel) GetMediaByIds(ctx context.Context, ids []int64) ([]model.Me
return medias, nil
}
// Count
func (m *postsModel) Count(ctx context.Context, cond BoolExpression) (int64, error) {
tbl := table.Posts
stmt := tbl.
SELECT(COUNT(tbl.ID).AS("count")).
WHERE(cond)
var count struct {
Count int64
}
if err := stmt.QueryContext(ctx, db, &count); err != nil {
m.log.Errorf("error counting posts: %v", err)
return 0, err
}
return count.Count, nil
}

View File

@@ -310,3 +310,24 @@ func (m *usersModel) HasBought(ctx context.Context, userID, postID int64) (bool,
return userPost.ID > 0, nil
}
// Count
func (m *usersModel) Count(ctx context.Context, cond BoolExpression) (int64, error) {
tbl := table.Users
stmt := tbl.
SELECT(COUNT(tbl.ID).AS("cnt")).
WHERE(cond)
m.log.Infof("sql: %s", stmt.DebugSql())
var cnt struct {
Cnt int64
}
if err := stmt.QueryContext(ctx, db, &cnt); err != nil {
m.log.Errorf("error counting users: %v", err)
return 0, err
}
return cnt.Cnt, nil
}