This commit is contained in:
2025-12-09 18:55:28 +08:00
parent 8d00d67540
commit c23ab8338b
209 changed files with 5445 additions and 3963 deletions

View File

@@ -8,33 +8,34 @@ import (
"fmt"
"strings"
"reflect"
"time"
"ycc-server/common/globalkey"
"github.com/Masterminds/squirrel"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
"ycc-server/common/globalkey"
)
var (
userFieldNames = builder.RawFieldNames(&User{})
userRows = strings.Join(userFieldNames, ",")
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "`create_time`", "`update_time`"), ",")
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
cacheHmUserIdPrefix = "cache:ycc:user:id:"
cacheHmUserMobilePrefix = "cache:ycc:user:mobile:"
cacheYccUserIdPrefix = "cache:ycc:user:id:"
cacheYccUserMobilePrefix = "cache:ycc:user:mobile:"
)
type (
userModel interface {
Insert(ctx context.Context, session sqlx.Session, data *User) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*User, error)
FindOne(ctx context.Context, id string) (*User, error)
FindOneByMobile(ctx context.Context, mobile sql.NullString) (*User, error)
Update(ctx context.Context, session sqlx.Session, data *User) (sql.Result, error)
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *User) error
@@ -48,7 +49,7 @@ type (
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*User, int64, error)
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*User, error)
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*User, error)
Delete(ctx context.Context, session sqlx.Session, id int64) error
Delete(ctx context.Context, session sqlx.Session, id string) error
}
defaultUserModel struct {
@@ -57,7 +58,7 @@ type (
}
User struct {
Id int64 `db:"id"`
Id string `db:"id"`
CreateTime time.Time `db:"create_time"`
UpdateTime time.Time `db:"update_time"`
DeleteTime sql.NullTime `db:"delete_time"` // 删除时间
@@ -80,21 +81,38 @@ func newUserModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultUserModel {
func (m *defaultUserModel) Insert(ctx context.Context, session sqlx.Session, data *User) (sql.Result, error) {
data.DelState = globalkey.DelStateNo
hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, data.Id)
hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile)
m.insertUUID(data)
yccUserIdKey := fmt.Sprintf("%s%v", cacheYccUserIdPrefix, data.Id)
yccUserMobileKey := fmt.Sprintf("%s%v", cacheYccUserMobilePrefix, data.Mobile)
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, userRowsExpectAutoSet)
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, userRowsExpectAutoSet)
if session != nil {
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside)
return session.ExecCtx(ctx, query, data.Id, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside)
}
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside)
}, hmUserIdKey, hmUserMobileKey)
return conn.ExecCtx(ctx, query, data.Id, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside)
}, yccUserIdKey, yccUserMobileKey)
}
func (m *defaultUserModel) insertUUID(data *User) {
t := reflect.TypeOf(data).Elem()
v := reflect.ValueOf(data).Elem()
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
if sf.Tag.Get("db") == "id" {
f := v.Field(i)
if f.IsValid() && f.CanSet() && f.Kind() == reflect.String {
if f.String() == "" {
f.SetString(uuid.NewString())
}
}
break
}
}
}
func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error) {
hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, id)
func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error) {
yccUserIdKey := fmt.Sprintf("%s%v", cacheYccUserIdPrefix, id)
var resp User
err := m.QueryRowCtx(ctx, &resp, hmUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
err := m.QueryRowCtx(ctx, &resp, yccUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", userRows, m.table)
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
})
@@ -109,9 +127,9 @@ func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error)
}
func (m *defaultUserModel) FindOneByMobile(ctx context.Context, mobile sql.NullString) (*User, error) {
hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, mobile)
yccUserMobileKey := fmt.Sprintf("%s%v", cacheYccUserMobilePrefix, mobile)
var resp User
err := m.QueryRowIndexCtx(ctx, &resp, hmUserMobileKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
err := m.QueryRowIndexCtx(ctx, &resp, yccUserMobileKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
query := fmt.Sprintf("select %s from %s where `mobile` = ? and del_state = ? limit 1", userRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, mobile, globalkey.DelStateNo); err != nil {
return nil, err
@@ -133,15 +151,15 @@ func (m *defaultUserModel) Update(ctx context.Context, session sqlx.Session, new
if err != nil {
return nil, err
}
hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, data.Id)
hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile)
yccUserIdKey := fmt.Sprintf("%s%v", cacheYccUserIdPrefix, data.Id)
yccUserMobileKey := fmt.Sprintf("%s%v", cacheYccUserMobilePrefix, data.Mobile)
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, userRowsWithPlaceHolder)
if session != nil {
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id)
}
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id)
}, hmUserIdKey, hmUserMobileKey)
}, yccUserIdKey, yccUserMobileKey)
}
func (m *defaultUserModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *User) error {
@@ -156,15 +174,15 @@ func (m *defaultUserModel) UpdateWithVersion(ctx context.Context, session sqlx.S
if err != nil {
return err
}
hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, data.Id)
hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile)
yccUserIdKey := fmt.Sprintf("%s%v", cacheYccUserIdPrefix, data.Id)
yccUserMobileKey := fmt.Sprintf("%s%v", cacheYccUserMobilePrefix, data.Mobile)
sqlResult, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, userRowsWithPlaceHolder)
if session != nil {
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id, oldVersion)
}
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id, oldVersion)
}, hmUserIdKey, hmUserMobileKey)
}, yccUserIdKey, yccUserMobileKey)
if err != nil {
return err
}
@@ -381,25 +399,25 @@ func (m *defaultUserModel) Trans(ctx context.Context, fn func(ctx context.Contex
func (m *defaultUserModel) SelectBuilder() squirrel.SelectBuilder {
return squirrel.Select().From(m.table)
}
func (m *defaultUserModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
func (m *defaultUserModel) Delete(ctx context.Context, session sqlx.Session, id string) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, id)
hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile)
yccUserIdKey := fmt.Sprintf("%s%v", cacheYccUserIdPrefix, id)
yccUserMobileKey := fmt.Sprintf("%s%v", cacheYccUserMobilePrefix, data.Mobile)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
if session != nil {
return session.ExecCtx(ctx, query, id)
}
return conn.ExecCtx(ctx, query, id)
}, hmUserIdKey, hmUserMobileKey)
}, yccUserIdKey, yccUserMobileKey)
return err
}
func (m *defaultUserModel) formatPrimary(primary interface{}) string {
return fmt.Sprintf("%s%v", cacheHmUserIdPrefix, primary)
return fmt.Sprintf("%s%v", cacheYccUserIdPrefix, primary)
}
func (m *defaultUserModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", userRows, m.table)