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,34 +8,35 @@ 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 (
adminUserFieldNames = builder.RawFieldNames(&AdminUser{})
adminUserRows = strings.Join(adminUserFieldNames, ",")
adminUserRowsExpectAutoSet = strings.Join(stringx.Remove(adminUserFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
adminUserRowsExpectAutoSet = strings.Join(stringx.Remove(adminUserFieldNames, "`create_time`", "`update_time`"), ",")
adminUserRowsWithPlaceHolder = strings.Join(stringx.Remove(adminUserFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
cacheHmAdminUserIdPrefix = "cache:ycc:adminUser:id:"
cacheHmAdminUserRealNamePrefix = "cache:ycc:adminUser:realName:"
cacheHmAdminUserUsernamePrefix = "cache:ycc:adminUser:username:"
cacheYccAdminUserIdPrefix = "cache:ycc:adminUser:id:"
cacheYccAdminUserRealNamePrefix = "cache:ycc:adminUser:realName:"
cacheYccAdminUserUsernamePrefix = "cache:ycc:adminUser:username:"
)
type (
adminUserModel interface {
Insert(ctx context.Context, session sqlx.Session, data *AdminUser) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*AdminUser, error)
FindOne(ctx context.Context, id string) (*AdminUser, error)
FindOneByRealName(ctx context.Context, realName string) (*AdminUser, error)
FindOneByUsername(ctx context.Context, username string) (*AdminUser, error)
Update(ctx context.Context, session sqlx.Session, data *AdminUser) (sql.Result, error)
@@ -50,7 +51,7 @@ type (
FindPageListByPageWithTotal(ctx context.Context, rowBuilder squirrel.SelectBuilder, page, pageSize int64, orderBy string) ([]*AdminUser, int64, error)
FindPageListByIdDESC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMinId, pageSize int64) ([]*AdminUser, error)
FindPageListByIdASC(ctx context.Context, rowBuilder squirrel.SelectBuilder, preMaxId, pageSize int64) ([]*AdminUser, error)
Delete(ctx context.Context, session sqlx.Session, id int64) error
Delete(ctx context.Context, session sqlx.Session, id string) error
}
defaultAdminUserModel struct {
@@ -59,7 +60,7 @@ type (
}
AdminUser 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"` // 删除时间
@@ -81,22 +82,39 @@ func newAdminUserModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultAdminUserMo
func (m *defaultAdminUserModel) Insert(ctx context.Context, session sqlx.Session, data *AdminUser) (sql.Result, error) {
data.DelState = globalkey.DelStateNo
hmAdminUserIdKey := fmt.Sprintf("%s%v", cacheHmAdminUserIdPrefix, data.Id)
hmAdminUserRealNameKey := fmt.Sprintf("%s%v", cacheHmAdminUserRealNamePrefix, data.RealName)
hmAdminUserUsernameKey := fmt.Sprintf("%s%v", cacheHmAdminUserUsernamePrefix, data.Username)
m.insertUUID(data)
yccAdminUserIdKey := fmt.Sprintf("%s%v", cacheYccAdminUserIdPrefix, data.Id)
yccAdminUserRealNameKey := fmt.Sprintf("%s%v", cacheYccAdminUserRealNamePrefix, data.RealName)
yccAdminUserUsernameKey := fmt.Sprintf("%s%v", cacheYccAdminUserUsernamePrefix, data.Username)
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, adminUserRowsExpectAutoSet)
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, adminUserRowsExpectAutoSet)
if session != nil {
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Username, data.Password, data.RealName, data.Status)
return session.ExecCtx(ctx, query, data.Id, data.DeleteTime, data.DelState, data.Version, data.Username, data.Password, data.RealName, data.Status)
}
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Username, data.Password, data.RealName, data.Status)
}, hmAdminUserIdKey, hmAdminUserRealNameKey, hmAdminUserUsernameKey)
return conn.ExecCtx(ctx, query, data.Id, data.DeleteTime, data.DelState, data.Version, data.Username, data.Password, data.RealName, data.Status)
}, yccAdminUserIdKey, yccAdminUserRealNameKey, yccAdminUserUsernameKey)
}
func (m *defaultAdminUserModel) insertUUID(data *AdminUser) {
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 *defaultAdminUserModel) FindOne(ctx context.Context, id int64) (*AdminUser, error) {
hmAdminUserIdKey := fmt.Sprintf("%s%v", cacheHmAdminUserIdPrefix, id)
func (m *defaultAdminUserModel) FindOne(ctx context.Context, id string) (*AdminUser, error) {
yccAdminUserIdKey := fmt.Sprintf("%s%v", cacheYccAdminUserIdPrefix, id)
var resp AdminUser
err := m.QueryRowCtx(ctx, &resp, hmAdminUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
err := m.QueryRowCtx(ctx, &resp, yccAdminUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", adminUserRows, m.table)
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
})
@@ -111,9 +129,9 @@ func (m *defaultAdminUserModel) FindOne(ctx context.Context, id int64) (*AdminUs
}
func (m *defaultAdminUserModel) FindOneByRealName(ctx context.Context, realName string) (*AdminUser, error) {
hmAdminUserRealNameKey := fmt.Sprintf("%s%v", cacheHmAdminUserRealNamePrefix, realName)
yccAdminUserRealNameKey := fmt.Sprintf("%s%v", cacheYccAdminUserRealNamePrefix, realName)
var resp AdminUser
err := m.QueryRowIndexCtx(ctx, &resp, hmAdminUserRealNameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
err := m.QueryRowIndexCtx(ctx, &resp, yccAdminUserRealNameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
query := fmt.Sprintf("select %s from %s where `real_name` = ? and del_state = ? limit 1", adminUserRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, realName, globalkey.DelStateNo); err != nil {
return nil, err
@@ -131,9 +149,9 @@ func (m *defaultAdminUserModel) FindOneByRealName(ctx context.Context, realName
}
func (m *defaultAdminUserModel) FindOneByUsername(ctx context.Context, username string) (*AdminUser, error) {
hmAdminUserUsernameKey := fmt.Sprintf("%s%v", cacheHmAdminUserUsernamePrefix, username)
yccAdminUserUsernameKey := fmt.Sprintf("%s%v", cacheYccAdminUserUsernamePrefix, username)
var resp AdminUser
err := m.QueryRowIndexCtx(ctx, &resp, hmAdminUserUsernameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
err := m.QueryRowIndexCtx(ctx, &resp, yccAdminUserUsernameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
query := fmt.Sprintf("select %s from %s where `username` = ? and del_state = ? limit 1", adminUserRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, username, globalkey.DelStateNo); err != nil {
return nil, err
@@ -155,16 +173,16 @@ func (m *defaultAdminUserModel) Update(ctx context.Context, session sqlx.Session
if err != nil {
return nil, err
}
hmAdminUserIdKey := fmt.Sprintf("%s%v", cacheHmAdminUserIdPrefix, data.Id)
hmAdminUserRealNameKey := fmt.Sprintf("%s%v", cacheHmAdminUserRealNamePrefix, data.RealName)
hmAdminUserUsernameKey := fmt.Sprintf("%s%v", cacheHmAdminUserUsernamePrefix, data.Username)
yccAdminUserIdKey := fmt.Sprintf("%s%v", cacheYccAdminUserIdPrefix, data.Id)
yccAdminUserRealNameKey := fmt.Sprintf("%s%v", cacheYccAdminUserRealNamePrefix, data.RealName)
yccAdminUserUsernameKey := fmt.Sprintf("%s%v", cacheYccAdminUserUsernamePrefix, data.Username)
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, adminUserRowsWithPlaceHolder)
if session != nil {
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Username, newData.Password, newData.RealName, newData.Status, newData.Id)
}
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Username, newData.Password, newData.RealName, newData.Status, newData.Id)
}, hmAdminUserIdKey, hmAdminUserRealNameKey, hmAdminUserUsernameKey)
}, yccAdminUserIdKey, yccAdminUserRealNameKey, yccAdminUserUsernameKey)
}
func (m *defaultAdminUserModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *AdminUser) error {
@@ -179,16 +197,16 @@ func (m *defaultAdminUserModel) UpdateWithVersion(ctx context.Context, session s
if err != nil {
return err
}
hmAdminUserIdKey := fmt.Sprintf("%s%v", cacheHmAdminUserIdPrefix, data.Id)
hmAdminUserRealNameKey := fmt.Sprintf("%s%v", cacheHmAdminUserRealNamePrefix, data.RealName)
hmAdminUserUsernameKey := fmt.Sprintf("%s%v", cacheHmAdminUserUsernamePrefix, data.Username)
yccAdminUserIdKey := fmt.Sprintf("%s%v", cacheYccAdminUserIdPrefix, data.Id)
yccAdminUserRealNameKey := fmt.Sprintf("%s%v", cacheYccAdminUserRealNamePrefix, data.RealName)
yccAdminUserUsernameKey := fmt.Sprintf("%s%v", cacheYccAdminUserUsernamePrefix, data.Username)
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, adminUserRowsWithPlaceHolder)
if session != nil {
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Username, newData.Password, newData.RealName, newData.Status, newData.Id, oldVersion)
}
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Username, newData.Password, newData.RealName, newData.Status, newData.Id, oldVersion)
}, hmAdminUserIdKey, hmAdminUserRealNameKey, hmAdminUserUsernameKey)
}, yccAdminUserIdKey, yccAdminUserRealNameKey, yccAdminUserUsernameKey)
if err != nil {
return err
}
@@ -405,26 +423,26 @@ func (m *defaultAdminUserModel) Trans(ctx context.Context, fn func(ctx context.C
func (m *defaultAdminUserModel) SelectBuilder() squirrel.SelectBuilder {
return squirrel.Select().From(m.table)
}
func (m *defaultAdminUserModel) Delete(ctx context.Context, session sqlx.Session, id int64) error {
func (m *defaultAdminUserModel) Delete(ctx context.Context, session sqlx.Session, id string) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
hmAdminUserIdKey := fmt.Sprintf("%s%v", cacheHmAdminUserIdPrefix, id)
hmAdminUserRealNameKey := fmt.Sprintf("%s%v", cacheHmAdminUserRealNamePrefix, data.RealName)
hmAdminUserUsernameKey := fmt.Sprintf("%s%v", cacheHmAdminUserUsernamePrefix, data.Username)
yccAdminUserIdKey := fmt.Sprintf("%s%v", cacheYccAdminUserIdPrefix, id)
yccAdminUserRealNameKey := fmt.Sprintf("%s%v", cacheYccAdminUserRealNamePrefix, data.RealName)
yccAdminUserUsernameKey := fmt.Sprintf("%s%v", cacheYccAdminUserUsernamePrefix, data.Username)
_, 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)
}, hmAdminUserIdKey, hmAdminUserRealNameKey, hmAdminUserUsernameKey)
}, yccAdminUserIdKey, yccAdminUserRealNameKey, yccAdminUserUsernameKey)
return err
}
func (m *defaultAdminUserModel) formatPrimary(primary interface{}) string {
return fmt.Sprintf("%s%v", cacheHmAdminUserIdPrefix, primary)
return fmt.Sprintf("%s%v", cacheYccAdminUserIdPrefix, primary)
}
func (m *defaultAdminUserModel) 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", adminUserRows, m.table)