add uid generate
This commit is contained in:
parent
a64aa7e176
commit
994597cd94
4
Makefile
4
Makefile
|
@ -48,8 +48,8 @@ build-docker:
|
|||
.PHONY: gen-my-sql-model
|
||||
gen-my-sql-model: # 建立 rpc 資料庫
|
||||
goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020011_account_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i ''
|
||||
goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020011_account_uid_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i ''
|
||||
goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020011_user_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i ''
|
||||
goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020012_account_uid_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i ''
|
||||
goctl model mysql ddl -c no -s ./generate/database/mysql/20230529020013_user_table.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i ''
|
||||
goctl model mysql ddl -c no -s ./generate/database/mysql/20230719061241_machine_node.up.sql --style $(GO_ZERO_STYLE) -d ./internal/model -i ''
|
||||
@echo "Generate mysql model files successfully"
|
||||
|
||||
|
|
|
@ -12,5 +12,6 @@ CREATE TABLE `user_table` (
|
|||
`update_time` BIGINT NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (`id`),
|
||||
INDEX `idx_create_time` (`create_time` ASC),
|
||||
INDEX `idx_nick_name` (`nick_name` ASC),
|
||||
UNIQUE INDEX `uk_uid` (`uid` ASC)
|
||||
)ENGINE=InnoDB COMMENT='使用者資訊';
|
||||
|
|
|
@ -85,9 +85,10 @@ message UpdateUserInfoReq {
|
|||
optional string language = 2;
|
||||
optional string currency = 3;
|
||||
optional string nick_name = 4;
|
||||
optional VerifyType verify_type = 5;
|
||||
optional AlarmType alarm_type = 6;
|
||||
optional MemberStatus status = 7;
|
||||
optional string avatar = 5;
|
||||
optional VerifyType verify_type = 6;
|
||||
optional AlarmType alarm_type = 7;
|
||||
optional MemberStatus status = 8;
|
||||
}
|
||||
|
||||
message GetUIDByAccountReq {
|
||||
|
@ -102,6 +103,7 @@ message GetUidByAccountResp {
|
|||
message UpdateTokenReq {
|
||||
string account = 1;
|
||||
string token = 2;
|
||||
int64 platform=3;
|
||||
}
|
||||
|
||||
message GenerateRefreshCodeReq {
|
||||
|
|
2
go.mod
2
go.mod
|
@ -13,6 +13,7 @@ require (
|
|||
go.mongodb.org/mongo-driver v1.16.0
|
||||
go.uber.org/goleak v1.3.0
|
||||
go.uber.org/mock v0.4.0
|
||||
golang.org/x/crypto v0.25.0
|
||||
google.golang.org/grpc v1.65.0
|
||||
google.golang.org/protobuf v1.34.2
|
||||
)
|
||||
|
@ -90,7 +91,6 @@ require (
|
|||
go.uber.org/automaxprocs v1.5.3 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
go.uber.org/zap v1.24.0 // indirect
|
||||
golang.org/x/crypto v0.25.0 // indirect
|
||||
golang.org/x/net v0.27.0 // indirect
|
||||
golang.org/x/oauth2 v0.20.0 // indirect
|
||||
golang.org/x/sync v0.7.0 // indirect
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
@ -12,6 +15,7 @@ type (
|
|||
// and implement the added methods in customAccountModel.
|
||||
AccountModel interface {
|
||||
accountModel
|
||||
UpdateTokenByLoginID(ctx context.Context, account string, token string) error
|
||||
}
|
||||
|
||||
customAccountModel struct {
|
||||
|
@ -25,3 +29,18 @@ func NewAccountModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option)
|
|||
defaultAccountModel: newAccountModel(conn, c, opts...),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultAccountModel) UpdateTokenByLoginID(ctx context.Context, account string, token string) error {
|
||||
data, err := m.FindOneByAccount(ctx, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
accountAccountKey := fmt.Sprintf("%s%v", cacheAccountAccountPrefix, data.Account)
|
||||
accountIdKey := fmt.Sprintf("%s%v", cacheAccountIdPrefix, data.Id)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set `token` = ? where `id` = ?", m.table)
|
||||
return conn.ExecCtx(ctx, query, token, data.Id)
|
||||
}, accountAccountKey, accountIdKey)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,8 +1,15 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"app-cloudep-member-server/gen_result/pb/member"
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var _ UserTableModel = (*customUserTableModel)(nil)
|
||||
|
@ -12,11 +19,27 @@ type (
|
|||
// and implement the added methods in customUserTableModel.
|
||||
UserTableModel interface {
|
||||
userTableModel
|
||||
FindOneByNickName(ctx context.Context, uid string) (*UserTable, error)
|
||||
ListMembers(ctx context.Context, params *UserQueryParams) ([]*UserTable, error)
|
||||
Count(ctx context.Context) (int64, error)
|
||||
UpdateStatus(ctx context.Context, uid string, status int32) error
|
||||
UpdateSome(ctx context.Context, newData *member.UpdateUserInfoReq) error
|
||||
}
|
||||
|
||||
customUserTableModel struct {
|
||||
*defaultUserTableModel
|
||||
}
|
||||
|
||||
UserQueryParams struct {
|
||||
RoleId *string
|
||||
VerifyType *int32
|
||||
AlarmType *int32
|
||||
Status *int32
|
||||
CreateStartTime *int64
|
||||
CreateEndTime *int64
|
||||
PageSize int64
|
||||
PageIndex int64
|
||||
}
|
||||
)
|
||||
|
||||
// NewUserTableModel returns a model for the database table.
|
||||
|
@ -25,3 +48,166 @@ func NewUserTableModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Optio
|
|||
defaultUserTableModel: newUserTableModel(conn, c, opts...),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUserTableModel) FindOneByNickName(ctx context.Context, nickName string) (*UserTable, error) {
|
||||
userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, nickName)
|
||||
var resp UserTable
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, userTableUidKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `nick_name` = ? limit 1", userTableRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, nickName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUserTableModel) ListMembers(ctx context.Context, params *UserQueryParams) ([]*UserTable, error) {
|
||||
query := fmt.Sprintf("select %s from %s", userTableRows, m.table)
|
||||
var args = make([]any, 0, 10)
|
||||
var conditions []string
|
||||
|
||||
if params.RoleId != nil {
|
||||
conditions = append(conditions, "role_id = ?")
|
||||
args = append(args, *params.RoleId)
|
||||
}
|
||||
|
||||
if params.VerifyType != nil {
|
||||
conditions = append(conditions, "verify_type = ?")
|
||||
args = append(args, *params.VerifyType)
|
||||
}
|
||||
|
||||
if params.AlarmType != nil {
|
||||
conditions = append(conditions, "alarm_type = ?")
|
||||
args = append(args, *params.AlarmType)
|
||||
}
|
||||
|
||||
if params.Status != nil {
|
||||
conditions = append(conditions, "status = ?")
|
||||
args = append(args, *params.Status)
|
||||
}
|
||||
|
||||
if params.CreateStartTime != nil {
|
||||
conditions = append(conditions, "create_time >= ?")
|
||||
args = append(args, *params.CreateStartTime)
|
||||
}
|
||||
|
||||
if params.CreateEndTime != nil {
|
||||
conditions = append(conditions, "create_time <= ?")
|
||||
args = append(args, *params.CreateEndTime)
|
||||
}
|
||||
|
||||
// 加入條件到查詢語句中
|
||||
if len(conditions) > 0 {
|
||||
query += " WHERE " + strings.Join(conditions, " AND ")
|
||||
}
|
||||
|
||||
// 分頁處理
|
||||
offset := (params.PageIndex - 1) * params.PageSize
|
||||
query += " LIMIT ? OFFSET ?"
|
||||
args = append(args, params.PageSize, offset)
|
||||
|
||||
var users []*UserTable
|
||||
fmt.Println("query:", query)
|
||||
fmt.Println("args:", args)
|
||||
err := m.QueryRowsNoCacheCtx(ctx, &users, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (m *defaultUserTableModel) Count(ctx context.Context) (int64, error) {
|
||||
var count int64
|
||||
query := fmt.Sprintf("select count(*) from %s", m.table)
|
||||
err := m.QueryRowNoCacheCtx(ctx, &count, query)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (m *defaultUserTableModel) UpdateStatus(ctx context.Context, uid string, status int32) error {
|
||||
data, err := m.FindOneByUid(ctx, uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
userTableIdKey := fmt.Sprintf("%s%v", cacheUserTableIdPrefix, data.Id)
|
||||
userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, data.Uid)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set `status` = ? where `id` = ?", m.table)
|
||||
return conn.ExecCtx(ctx, query, status, data.Id)
|
||||
}, userTableIdKey, userTableUidKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultUserTableModel) UpdateSome(ctx context.Context, newData *member.UpdateUserInfoReq) error {
|
||||
data, err := m.FindOneByUid(ctx, newData.Uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 初始化缓存键
|
||||
userTableIdKey := fmt.Sprintf("%s%v", cacheUserTableIdPrefix, data.Id)
|
||||
userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, data.Uid)
|
||||
|
||||
query := fmt.Sprintf("update %s set ", m.table)
|
||||
var args []interface{}
|
||||
var updates []string
|
||||
|
||||
if newData.VerifyType != nil {
|
||||
updates = append(updates, "verify_type = ?")
|
||||
args = append(args, *newData.VerifyType)
|
||||
}
|
||||
if newData.AlarmType != nil {
|
||||
updates = append(updates, "alarm_type = ?")
|
||||
args = append(args, *newData.AlarmType)
|
||||
}
|
||||
if newData.Status != nil {
|
||||
updates = append(updates, "status = ?")
|
||||
args = append(args, *newData.Status)
|
||||
}
|
||||
|
||||
if newData.Language != nil {
|
||||
updates = append(updates, "language = ?")
|
||||
args = append(args, *newData.Language)
|
||||
}
|
||||
if newData.Currency != nil {
|
||||
updates = append(updates, "currency = ?")
|
||||
args = append(args, *newData.Currency)
|
||||
}
|
||||
if newData.NickName != nil {
|
||||
updates = append(updates, "nick_name = ?")
|
||||
args = append(args, *newData.NickName)
|
||||
}
|
||||
if newData.Avatar != nil {
|
||||
updates = append(updates, "avatar = ?")
|
||||
args = append(args, *newData.Avatar)
|
||||
}
|
||||
|
||||
if len(updates) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
update := time.Now().UTC().Unix()
|
||||
updates = append(updates, "update_time = ?")
|
||||
args = append(args, &update)
|
||||
|
||||
query += strings.Join(updates, ", ") + " where `id` = ?"
|
||||
args = append(args, data.Id)
|
||||
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
return conn.ExecCtx(ctx, query, args...)
|
||||
}, userTableIdKey, userTableUidKey)
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue