This commit is contained in:
oneao committed 2026-01-25 22:18:27 +08:00
1 parent 500fab7a80
commit 8f3f6982db
54 files changed
+2076 -1877

No files matched your search

+1 -2
View File
@@ -18,7 +18,6 @@ FROM space s
ON s.id = m.space_id
WHERE m.user_id = $1;
-- 根据验证码获取空间
-- name: GetSpaceByInviteCode :one
-- name: FindSpaceByInviteCode :one
SELECT * FROM space WHERE invite_code = $1;
+32
View File
@@ -0,0 +1,32 @@
-- name: FindUserByOpenID :one
SELECT
u.id AS user_id,
u.username,
u.avatar,
u.status,
o.type AS login_type,
o.openid
FROM user_oauth o
JOIN "user" u ON u.id = o.user_id
WHERE o.openid = $1
AND o.type = $2
LIMIT 1;
-- name: InsertUser :exec
INSERT INTO "user" (
id,
username,
avatar,
status,
create_time,
update_time,
last_login_time
) VALUES (
$1, $2, $3,$4, NOW(), NOW(),now()
)
RETURNING id;
-- name: UpdateUserLastLoginTime :exec
UPDATE "user"
SET last_login_time = NOW()
WHERE id = $1;
@@ -1,8 +1,3 @@
-- 插入用户并返回刚创建的记录
-- name: InsertUser :execrows
INSERT INTO user_info (id, account, password,username)
VALUES ($1, $2, $3,$4);
-- 根据 ID 查询单条用户记录
-- name: GetUserById :one
SELECT *
@@ -0,0 +1,13 @@
-- name: InsertUserOAuth :exec
INSERT INTO user_oauth (
id,
user_id,
type,
openid,
create_time,
update_time
) VALUES (
$1, $2, $3, $4, NOW(), NOW()
);
+26 -18
View File
@@ -38,34 +38,32 @@ type MoneyUserCategory struct {
CreateTime pgtype.Timestamp
}
// 共享空间(一个空间包含多个成员)
type Space struct {
// 空间ID
ID int64
// 空间名称
Name pgtype.Text
// 空间邀请码
ID int64
Name pgtype.Text
InviteCode string
// 空间创建时间
CreateTime pgtype.Timestamp
// 空间更新时间
UpdateTime pgtype.Timestamp
}
// 共享空间成员表
type SpaceUser struct {
// 主键ID
ID int64
// 所属空间ID
SpaceID int64
// 用户ID(唯一,每用户只加入一个空间)
UserID int64
// 成员角色:0创建者 1管理员 2普通
Role int32
// 加入空间时间
ID int64
SpaceID int64
UserID int64
Role int32
JoinTime pgtype.Timestamp
}
type User struct {
ID int64
Username string
Avatar string
Status int32
LastLoginTime pgtype.Timestamptz
CreateTime pgtype.Timestamptz
UpdateTime pgtype.Timestamptz
}
type UserInfo struct {
ID int64
SeqNo pgtype.Int4
@@ -79,3 +77,13 @@ type UserInfo struct {
ActiveTime pgtype.Timestamp
LastLoginTime pgtype.Timestamp
}
type UserOauth struct {
ID int64
UserID int64
Type int32
Openid string
Unionid pgtype.Text
CreateTime pgtype.Timestamptz
UpdateTime pgtype.Timestamptz
}
+9 -10
View File
@@ -19,13 +19,12 @@ func (q *Queries) DeleteSpace(ctx context.Context, id int64) error {
return err
}
const getSpaceById = `-- name: GetSpaceById :one
SELECT id, name, invite_code, create_time, update_time FROM space WHERE id = $1
const findSpaceByInviteCode = `-- name: FindSpaceByInviteCode :one
SELECT id, name, invite_code, create_time, update_time FROM space WHERE invite_code = $1
`
// 根据id获取分享空间信息
func (q *Queries) GetSpaceById(ctx context.Context, id int64) (Space, error) {
row := q.db.QueryRow(ctx, getSpaceById, id)
func (q *Queries) FindSpaceByInviteCode(ctx context.Context, inviteCode string) (Space, error) {
row := q.db.QueryRow(ctx, findSpaceByInviteCode, inviteCode)
var i Space
err := row.Scan(
&i.ID,
@@ -37,13 +36,13 @@ func (q *Queries) GetSpaceById(ctx context.Context, id int64) (Space, error) {
return i, err
}
const getSpaceByInviteCode = `-- name: GetSpaceByInviteCode :one
SELECT id, name, invite_code, create_time, update_time FROM space WHERE invite_code = $1
const getSpaceById = `-- name: GetSpaceById :one
SELECT id, name, invite_code, create_time, update_time FROM space WHERE id = $1
`
// 根据验证码获取空间
func (q *Queries) GetSpaceByInviteCode(ctx context.Context, inviteCode string) (Space, error) {
row := q.db.QueryRow(ctx, getSpaceByInviteCode, inviteCode)
// 根据id获取分享空间信息
func (q *Queries) GetSpaceById(ctx context.Context, id int64) (Space, error) {
row := q.db.QueryRow(ctx, getSpaceById, id)
var i Space
err := row.Scan(
&i.ID,
+95
View File
@@ -0,0 +1,95 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: user.sql
package repo
import (
"context"
)
const findUserByOpenID = `-- name: FindUserByOpenID :one
SELECT
u.id AS user_id,
u.username,
u.avatar,
u.status,
o.type AS login_type,
o.openid
FROM user_oauth o
JOIN "user" u ON u.id = o.user_id
WHERE o.openid = $1
AND o.type = $2
LIMIT 1
`
type FindUserByOpenIDParams struct {
Openid string
Type int32
}
type FindUserByOpenIDRow struct {
UserID int64
Username string
Avatar string
Status int32
LoginType int32
Openid string
}
func (q *Queries) FindUserByOpenID(ctx context.Context, arg FindUserByOpenIDParams) (FindUserByOpenIDRow, error) {
row := q.db.QueryRow(ctx, findUserByOpenID, arg.Openid, arg.Type)
var i FindUserByOpenIDRow
err := row.Scan(
&i.UserID,
&i.Username,
&i.Avatar,
&i.Status,
&i.LoginType,
&i.Openid,
)
return i, err
}
const insertUser = `-- name: InsertUser :exec
INSERT INTO "user" (
id,
username,
avatar,
status,
create_time,
update_time
) VALUES (
$1, $2, $3,$4, NOW(), NOW()
)
RETURNING id
`
type InsertUserParams struct {
ID int64
Username string
Avatar string
Status int32
}
func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) error {
_, err := q.db.Exec(ctx, insertUser,
arg.ID,
arg.Username,
arg.Avatar,
arg.Status,
)
return err
}
const updateUserLastLoginTime = `-- name: UpdateUserLastLoginTime :exec
UPDATE "user"
SET last_login_time = NOW()
WHERE id = $1
`
func (q *Queries) UpdateUserLastLoginTime(ctx context.Context, id int64) error {
_, err := q.db.Exec(ctx, updateUserLastLoginTime, id)
return err
}
@@ -7,8 +7,6 @@ package repo
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const getUserByAccount = `-- name: GetUserByAccount :one
@@ -95,32 +93,6 @@ func (q *Queries) GetUserForLogin(ctx context.Context, arg GetUserForLoginParams
return i, err
}
const insertUser = `-- name: InsertUser :execrows
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,
arg.Username,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const updateUserActiveTime = `-- name: UpdateUserActiveTime :exec
UPDATE user_info
SET active_time = now()
@@ -0,0 +1,40 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: user_oauth.sql
package repo
import (
"context"
)
const insertUserOAuth = `-- name: InsertUserOAuth :exec
INSERT INTO user_oauth (
id,
user_id,
type,
openid,
create_time,
update_time
) VALUES (
$1, $2, $3, $4, NOW(), NOW()
)
`
type InsertUserOAuthParams struct {
ID int64
UserID int64
Type int32
Openid string
}
func (q *Queries) InsertUserOAuth(ctx context.Context, arg InsertUserOAuthParams) error {
_, err := q.db.Exec(ctx, insertUserOAuth,
arg.ID,
arg.UserID,
arg.Type,
arg.Openid,
)
return err
}
+19 -16
View File
@@ -12,6 +12,25 @@ CREATE TABLE user_info (
last_login_time TIMESTAMP
);
CREATE TABLE "user" (
"id" int8 NOT NULL,
"username" varchar(255) NOT NULL ,
"avatar" varchar(255) NOT NULL ,
"status" int4 NOT NULL DEFAULT 1,
"last_login_time" timestamptz(6),
"create_time" timestamptz(6),
"update_time" timestamptz(6)
);
CREATE TABLE "user_oauth" (
"id" int8 NOT NULL,
"user_id" int8 NOT NULL,
"type" int4 NOT NULL,
"openid" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
"create_time" timestamptz(6) NOT NULL,
"update_time" timestamptz(6) NOT NULL
);
CREATE TABLE money_record(
id BIGINT PRIMARY KEY,
category_id BIGINT NOT NULL,
@@ -51,18 +70,9 @@ CREATE TABLE space (
update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- 注释
COMMENT ON TABLE space IS '共享空间(一个空间包含多个成员)';
COMMENT ON COLUMN space.id IS '空间ID';
COMMENT ON COLUMN space.name IS '空间名称';
COMMENT ON COLUMN space.invite_code IS '空间邀请码';
COMMENT ON COLUMN space.create_time IS '空间创建时间';
COMMENT ON COLUMN space.update_time IS '空间更新时间';
ALTER TABLE space
ADD CONSTRAINT space_invite_code_key UNIQUE (invite_code);
-- 空间成员表(每个用户只能加入一个空间)
CREATE TABLE space_user (
id BIGSERIAL PRIMARY KEY,
@@ -72,10 +82,3 @@ CREATE TABLE space_user (
join_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- 注释
COMMENT ON TABLE space_user IS '共享空间成员表';
COMMENT ON COLUMN space_user.id IS '主键ID';
COMMENT ON COLUMN space_user.space_id IS '所属空间ID';
COMMENT ON COLUMN space_user.user_id IS '用户ID(唯一,每用户只加入一个空间)';
COMMENT ON COLUMN space_user.role IS '成员角色:0创建者 1管理员 2普通';
COMMENT ON COLUMN space_user.join_time IS '加入空间时间';