This commit is contained in:
oneao committed 2025-12-04 17:31:33 +08:00
1 parent 4dcac7b5a0
commit 59d3be2299
25 files changed
+380 -125

No files matched your search

@@ -18,19 +18,21 @@ SET role = $1
WHERE user_id = $2
AND space_id = $3;
-- 获取空间下的所有用户信息
-- 获取空间下的所有用户信息(按 role, active_time, username 排序)
-- name: ListSpaceUsers :many
SELECT
u.id as user_id,
u.id AS user_id,
u.username,
u.active_time,
su.role,
su.join_time
FROM space_user su
JOIN space s ON su.space_id = s.id
JOIN user_info u ON su.user_id = u.id
WHERE s.id = $1
ORDER BY su.join_time ASC;
ORDER BY su.role ASC,
u.active_time DESC,
u.username ASC;
-- name: GetSpaceUserByUserId :one
SELECT * FROM space_user WHERE user_id = $1;
@@ -1,7 +1,7 @@
-- 插入用户并返回刚创建的记录
-- name: InsertUser :execrows
INSERT INTO user_info (id, account, password)
VALUES ($1, $2, $3);
INSERT INTO user_info (id, account, password,username)
VALUES ($1, $2, $3,$4);
-- 根据 ID 查询单条用户记录
-- name: GetUserById :one
@@ -23,8 +23,13 @@ WHERE account = $1
AND password = $2;
-- 更新用户最后登录时间,并返回更新后的记录
-- name: UpdateUserLoginLastTime :execrows
-- name: UpdateUserLoginLastTime :exec
UPDATE user_info
SET last_login_time = now()
WHERE id = $1;
-- 更新用户活跃时间
-- name: UpdateUserActiveTime :exec
UPDATE user_info
SET active_time = now()
WHERE id = $1;
@@ -76,5 +76,6 @@ type UserInfo struct {
Gender pgtype.Int4
CreateTime pgtype.Timestamp
UpdateTime pgtype.Timestamp
ActiveTime pgtype.Timestamp
LastLoginTime pgtype.Timestamp
}
@@ -109,25 +109,29 @@ func (q *Queries) InsertSpaceUser(ctx context.Context, arg InsertSpaceUserParams
const listSpaceUsers = `-- name: ListSpaceUsers :many
SELECT
u.id as user_id,
u.id AS user_id,
u.username,
u.active_time,
su.role,
su.join_time
FROM space_user su
JOIN space s ON su.space_id = s.id
JOIN user_info u ON su.user_id = u.id
WHERE s.id = $1
ORDER BY su.join_time ASC
ORDER BY su.role ASC,
u.active_time DESC,
u.username ASC
`
type ListSpaceUsersRow struct {
UserID int64
Username pgtype.Text
Role int32
JoinTime pgtype.Timestamp
UserID int64
Username pgtype.Text
ActiveTime pgtype.Timestamp
Role int32
JoinTime pgtype.Timestamp
}
// 获取空间下的所有用户信息
// 获取空间下的所有用户信息(按 role, active_time, username 排序)
func (q *Queries) ListSpaceUsers(ctx context.Context, id int64) ([]ListSpaceUsersRow, error) {
rows, err := q.db.Query(ctx, listSpaceUsers, id)
if err != nil {
@@ -140,6 +144,7 @@ func (q *Queries) ListSpaceUsers(ctx context.Context, id int64) ([]ListSpaceUser
if err := rows.Scan(
&i.UserID,
&i.Username,
&i.ActiveTime,
&i.Role,
&i.JoinTime,
); err != nil {
@@ -7,10 +7,12 @@ package repo
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const getUserByAccount = `-- name: GetUserByAccount :one
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, last_login_time
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, active_time, last_login_time
FROM user_info
WHERE account = $1
`
@@ -29,13 +31,14 @@ func (q *Queries) GetUserByAccount(ctx context.Context, account string) (UserInf
&i.Gender,
&i.CreateTime,
&i.UpdateTime,
&i.ActiveTime,
&i.LastLoginTime,
)
return i, err
}
const getUserById = `-- name: GetUserById :one
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, last_login_time
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, active_time, last_login_time
FROM user_info
WHERE id = $1
`
@@ -54,13 +57,14 @@ func (q *Queries) GetUserById(ctx context.Context, id int64) (UserInfo, error) {
&i.Gender,
&i.CreateTime,
&i.UpdateTime,
&i.ActiveTime,
&i.LastLoginTime,
)
return i, err
}
const getUserForLogin = `-- name: GetUserForLogin :one
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, last_login_time
SELECT id, seq_no, account, password, email, username, gender, create_time, update_time, active_time, last_login_time
FROM user_info
WHERE account = $1
AND password = $2
@@ -85,42 +89,58 @@ func (q *Queries) GetUserForLogin(ctx context.Context, arg GetUserForLoginParams
&i.Gender,
&i.CreateTime,
&i.UpdateTime,
&i.ActiveTime,
&i.LastLoginTime,
)
return i, err
}
const insertUser = `-- name: InsertUser :execrows
INSERT INTO user_info (id, account, password)
VALUES ($1, $2, $3)
INSERT INTO user_info (id, account, password,username)
VALUES ($1, $2, $3,$4)
`
type InsertUserParams struct {
ID int64
Account string
Password string
Username pgtype.Text
}
// 插入用户并返回刚创建的记录
func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) (int64, error) {
result, err := q.db.Exec(ctx, insertUser, arg.ID, arg.Account, arg.Password)
result, err := q.db.Exec(ctx, insertUser,
arg.ID,
arg.Account,
arg.Password,
arg.Username,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const updateUserLoginLastTime = `-- name: UpdateUserLoginLastTime :execrows
const updateUserActiveTime = `-- name: UpdateUserActiveTime :exec
UPDATE user_info
SET active_time = now()
WHERE id = $1
`
// 更新用户活跃时间
func (q *Queries) UpdateUserActiveTime(ctx context.Context, id int64) error {
_, err := q.db.Exec(ctx, updateUserActiveTime, id)
return err
}
const updateUserLoginLastTime = `-- name: UpdateUserLoginLastTime :exec
UPDATE user_info
SET last_login_time = now()
WHERE id = $1
`
// 更新用户最后登录时间,并返回更新后的记录
func (q *Queries) UpdateUserLoginLastTime(ctx context.Context, id int64) (int64, error) {
result, err := q.db.Exec(ctx, updateUserLoginLastTime, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
func (q *Queries) UpdateUserLoginLastTime(ctx context.Context, id int64) error {
_, err := q.db.Exec(ctx, updateUserLoginLastTime, id)
return err
}
@@ -8,6 +8,7 @@ CREATE TABLE user_info (
gender INT DEFAULT 0,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
active_time TIMESTAMP,
last_login_time TIMESTAMP
);