Files
quyun/backend/app/model/users.go
2025-05-23 23:50:26 +08:00

396 lines
9.3 KiB
Go

package model
import (
"context"
"time"
"quyun/app/requests"
"quyun/database/fields"
"quyun/database/table"
. "github.com/go-jet/jet/v2/postgres"
"github.com/go-jet/jet/v2/qrm"
"github.com/pkg/errors"
"github.com/samber/lo"
)
var usersUpdateExcludeColumns = []Column{
table.Users.OpenID,
table.Users.Balance,
table.Users.CreatedAt,
table.Users.DeletedAt,
}
func (m *Users) Update(ctx context.Context) error {
m.UpdatedAt = time.Now()
stmt := table.Users.UPDATE(table.Users.MutableColumns.Except(table.Users.OpenID, table.Users.Balance, table.Users.CreatedAt, table.Users.DeletedAt)).SET(m).WHERE(table.Users.ID.EQ(Int(m.ID))).RETURNING(table.Users.AllColumns)
m.log().WithField("func", "Update").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {
m.log().WithField("func", "Update").Errorf("error updating Users item: %v", err)
return err
}
m.log().WithField("func", "Update").Infof("Users item updated successfully")
return nil
}
// BuildConditionWithKey builds the WHERE clause for user queries
func (m *Users) BuildConditionWithKey(key *string) BoolExpression {
tbl := table.Users
cond := tbl.DeletedAt.IS_NULL()
if key == nil || *key == "" {
return cond
}
cond = cond.AND(
tbl.Username.LIKE(String("%" + *key + "%")),
)
return cond
}
// countByCondition counts users matching the given condition
func (m *Users) countByCondition(ctx context.Context, expr BoolExpression) (int64, error) {
var cnt struct {
Cnt int64
}
tbl := table.Users
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(expr)
m.log().Infof("sql: %s", stmt.DebugSql())
err := stmt.QueryContext(ctx, db, &cnt)
if err != nil {
m.log().Errorf("error counting users: %v", err)
return 0, err
}
return cnt.Cnt, nil
}
// List returns a paginated list of users
func (m *Users) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression) (*requests.Pager, error) {
pagination.Format()
tbl := table.Users
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(cond).
ORDER_BY(tbl.ID.DESC()).
LIMIT(pagination.Limit).
OFFSET(pagination.Offset)
m.log().Infof("sql: %s", stmt.DebugSql())
var users []Users = make([]Users, 0)
err := stmt.QueryContext(ctx, db, &users)
if err != nil {
m.log().Errorf("error querying users: %v", err)
return nil, err
}
count, err := m.countByCondition(ctx, cond)
if err != nil {
m.log().Errorf("error getting user count: %v", err)
return nil, err
}
return &requests.Pager{
Items: users,
Total: count,
Pagination: *pagination,
}, nil
}
// PostList returns a paginated list of posts for a user
func (m *Users) PostList(ctx context.Context, userId int64, pagination *requests.Pagination, conds ...Cond) (*requests.Pager, error) {
pagination.Format()
tblUserPosts := table.UserPosts
cond := CondJoin(ExprCond(tblUserPosts.UserID.EQ(Int64(userId))), conds...)
tbl := table.Posts
stmt := SELECT(tbl.AllColumns).
FROM(tbl.
RIGHT_JOIN(
tblUserPosts,
tblUserPosts.PostID.EQ(tbl.ID),
),
).
WHERE(CondTrue(cond...)).
ORDER_BY(tblUserPosts.ID.DESC()).
LIMIT(pagination.Limit).
OFFSET(pagination.Offset)
m.log().Infof("sql: %s", stmt.DebugSql())
var posts []Posts
err := stmt.QueryContext(ctx, db, &posts)
if err != nil {
if errors.Is(err, qrm.ErrNoRows) {
return &requests.Pager{
Items: nil,
Total: 0,
Pagination: *pagination,
}, nil
}
m.log().Errorf("error querying posts: %v", err)
return nil, err
}
// total count
var cnt struct {
Cnt int64
}
stmtCnt := tblUserPosts.SELECT(COUNT(tblUserPosts.ID).AS("cnt")).WHERE(tblUserPosts.UserID.EQ(Int64(userId)))
m.log().Infof("sql: %s", stmtCnt.DebugSql())
if err := stmtCnt.QueryContext(ctx, db, &cnt); err != nil {
m.log().Errorf("error counting users: %v", err)
return nil, err
}
return &requests.Pager{
Items: posts,
Total: cnt.Cnt,
Pagination: *pagination,
}, nil
}
// GetUserIDByOpenID
func (m *Users) GetUserByOpenID(ctx context.Context, openID string) (*Users, error) {
tbl := table.Users
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(
tbl.OpenID.EQ(String(openID)),
)
m.log().Infof("sql: %s", stmt.DebugSql())
var user Users
if err := stmt.QueryContext(ctx, db, &user); err != nil {
m.log().Errorf("error querying user by OpenID: %v", err)
return nil, err
}
return &user, nil
}
// GetUserByOpenIDOrCreate
func (m *Users) GetUserByOpenIDOrCreate(ctx context.Context, openID string, userModel *Users) (*Users, error) {
user, err := m.GetUserByOpenID(ctx, openID)
if err != nil {
if errors.Is(err, qrm.ErrNoRows) {
if err = userModel.Create(ctx); err != nil {
return nil, errors.Wrap(err, "failed to create user")
}
} else {
return nil, errors.Wrap(err, "failed to get user")
}
} else {
if user.Username == "" || user.Username == "-" || user.Username == "暂未设置昵称" {
user.Username = userModel.Username
user.Avatar = userModel.Avatar
}
user.Metas = userModel.Metas
user.AuthToken = userModel.AuthToken
if err := user.Update(ctx); err != nil {
return nil, errors.Wrap(err, "failed to update user")
}
}
return user, nil
}
// GetUsersMapByIDs
func (m *Users) GetUsersMapByIDs(ctx context.Context, ids []int64) (map[int64]Users, error) {
if len(ids) == 0 {
return nil, nil
}
tbl := table.Users
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(
tbl.ID.IN(lo.Map(ids, func(id int64, _ int) Expression { return Int64(id) })...),
)
m.log().Infof("sql: %s", stmt.DebugSql())
var users []Users = make([]Users, 0)
err := stmt.QueryContext(ctx, db, &users)
if err != nil {
m.log().Errorf("error querying users: %v", err)
return nil, err
}
return lo.SliceToMap(users, func(item Users) (int64, Users) {
return item.ID, item
}), nil
}
func (m *Users) BatchCheckHasBought(ctx context.Context, postIDs []int64) (map[int64]bool, error) {
tbl := table.UserPosts
stmt := tbl.SELECT(tbl.PostID.AS("post_id")).WHERE(
tbl.UserID.EQ(Int64(m.ID)).AND(
tbl.PostID.IN(lo.Map(postIDs, func(id int64, _ int) Expression { return Int64(id) })...),
),
)
var userPosts []struct {
PostID int64
}
if err := stmt.QueryContext(ctx, db, &userPosts); err != nil {
m.log().Errorf("error querying user posts: %v", err)
return nil, err
}
result := make(map[int64]bool)
for _, post := range userPosts {
result[post.PostID] = true
}
return result, nil
}
// HasBought
func (m *Users) HasBought(ctx context.Context, postID int64) (bool, error) {
tbl := table.UserPosts
stmt := tbl.
SELECT(tbl.ID).
WHERE(
tbl.UserID.EQ(Int64(m.ID)).AND(
tbl.PostID.EQ(Int64(postID)),
),
)
m.log().Infof("sql: %s", stmt.DebugSql())
var userPost UserPosts
if err := stmt.QueryContext(ctx, db, &userPost); err != nil {
if errors.Is(err, qrm.ErrNoRows) {
return false, nil
}
m.log().Errorf("error querying user post: %v", err)
return false, err
}
return userPost.ID > 0, nil
}
// SetUsername
func (m *Users) SetUsername(ctx context.Context, username string) error {
tbl := table.Users
stmt := tbl.
UPDATE(tbl.Username).
SET(String(username)).
WHERE(
tbl.ID.EQ(Int64(m.ID)),
)
m.log().Infof("sql: %s", stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log().Errorf("error updating username: %v", err)
return err
}
return nil
}
// UpdateUserToken
func (m *Users) UpdateUserToken(ctx context.Context, token fields.UserAuthToken) error {
tbl := table.Users
stmt := tbl.
UPDATE(tbl.AuthToken).
SET(fields.ToJson(token)).
WHERE(
tbl.ID.EQ(Int64(m.ID)),
)
m.log().Infof("sql: %s", stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log().Errorf("error updating user token: %v", err)
return err
}
return nil
}
// BuyPosts
func (m *Users) BuyPosts(ctx context.Context, postID, price int64) error {
tbl := table.UserPosts
stmt := tbl.
INSERT(tbl.MutableColumns).
MODEL(&UserPosts{
UserID: m.ID,
PostID: postID,
Price: price,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
m.log().Infof("sql: %s", stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log().Errorf("error inserting user post: %v", err)
return err
}
return nil
}
func (m *Users) RevokePosts(ctx context.Context, postID int64) error {
tbl := table.UserPosts
stmt := tbl.
DELETE().
WHERE(
tbl.UserID.EQ(Int64(m.ID)).AND(
tbl.PostID.EQ(Int64(postID)),
),
)
m.log().Infof("sql: %s", stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log().Errorf("error revoking user post: %v", err)
return err
}
return nil
}
// SetBalance
func (m *Users) SetBalance(ctx context.Context, balance int64) error {
tbl := table.Users
stmt := tbl.
UPDATE(tbl.Balance).
SET(Int64(balance)).
WHERE(
tbl.ID.EQ(Int64(m.ID)),
)
m.log().Infof("sql: %s", stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log().Errorf("error updating user balance: %v", err)
return err
}
return nil
}
// AddBalance adds the given amount to the user's balance
func (m *Users) AddBalance(ctx context.Context, amount int64) error {
tbl := table.Users
stmt := tbl.
UPDATE(tbl.Balance).
SET(tbl.Balance.ADD(Int64(amount))).
WHERE(
tbl.ID.EQ(Int64(m.ID)),
)
m.log().Infof("sql: %s", stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log().Errorf("error updating user balance: %v", err)
return err
}
return nil
}