refactor(user):基本完成用户服务层的迁移。
This commit is contained in:
parent
f8025c5dea
commit
ce7b69923d
248
service/user.go
248
service/user.go
|
@ -1,6 +1,7 @@
|
||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"electricity_bill_calc/cache"
|
"electricity_bill_calc/cache"
|
||||||
"electricity_bill_calc/config"
|
"electricity_bill_calc/config"
|
||||||
"electricity_bill_calc/exceptions"
|
"electricity_bill_calc/exceptions"
|
||||||
|
@ -13,7 +14,7 @@ import (
|
||||||
|
|
||||||
"github.com/fufuok/utils"
|
"github.com/fufuok/utils"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"xorm.io/builder"
|
"github.com/uptrace/bun"
|
||||||
)
|
)
|
||||||
|
|
||||||
type _UserService struct{}
|
type _UserService struct{}
|
||||||
|
@ -106,17 +107,18 @@ func (u _UserService) InvalidUserPassword(uid string) (string, error) {
|
||||||
if user == nil && err != nil {
|
if user == nil && err != nil {
|
||||||
return "", exceptions.NewNotFoundError("指定的用户不存在。")
|
return "", exceptions.NewNotFoundError("指定的用户不存在。")
|
||||||
}
|
}
|
||||||
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
|
defer cancel()
|
||||||
verifyCode := tools.RandStr(10)
|
verifyCode := tools.RandStr(10)
|
||||||
user.Password = utils.Sha512Hex(verifyCode)
|
user.Password = utils.Sha512Hex(verifyCode)
|
||||||
user.ResetNeeded = true
|
user.ResetNeeded = true
|
||||||
affected, err := global.DBConn.ID(uid).Cols("password", "reset_needed").Update(user)
|
res, err := global.DB.NewUpdate().Model(user).WherePK().Column("password", "reset_needed").Exec(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if affected > 0 {
|
if affected, _ := res.RowsAffected(); affected > 0 {
|
||||||
// ! 同一个用户在缓存中有两个键。
|
// ! 清除与此用户所有相关的记录。
|
||||||
cache.AbolishRelation(fmt.Sprintf("user_%s", uid))
|
cache.AbolishRelation(fmt.Sprintf("user:%s", uid))
|
||||||
cache.AbolishRelation("user")
|
|
||||||
return verifyCode, nil
|
return verifyCode, nil
|
||||||
} else {
|
} else {
|
||||||
return "", exceptions.NewUnsuccessfulOperationError()
|
return "", exceptions.NewUnsuccessfulOperationError()
|
||||||
|
@ -141,15 +143,16 @@ func (u _UserService) ResetUserPassword(username, password string) (bool, error)
|
||||||
if user == nil || err != nil {
|
if user == nil || err != nil {
|
||||||
return false, exceptions.NewNotFoundError("指定的用户不存在。")
|
return false, exceptions.NewNotFoundError("指定的用户不存在。")
|
||||||
}
|
}
|
||||||
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
|
defer cancel()
|
||||||
user.Password = utils.Sha512Hex(password)
|
user.Password = utils.Sha512Hex(password)
|
||||||
user.ResetNeeded = false
|
user.ResetNeeded = false
|
||||||
affected, err := global.DBConn.ID(user.Id).Cols("password", "reset_needed").Update(user)
|
res, err := global.DB.NewUpdate().Model(user).WherePK().Column("password", "reset_needed").Exec(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
if affected > 0 {
|
if affected, _ := res.RowsAffected(); affected > 0 {
|
||||||
cache.AbolishRelation(fmt.Sprintf("user_%s", user.Id))
|
cache.AbolishRelation(fmt.Sprintf("user:%s", user.Id))
|
||||||
cache.AbolishRelation("user")
|
|
||||||
return true, nil
|
return true, nil
|
||||||
} else {
|
} else {
|
||||||
return false, nil
|
return false, nil
|
||||||
|
@ -160,9 +163,11 @@ func (_UserService) IsUserExists(uid string) (bool, error) {
|
||||||
if has, _ := cache.CheckExists("user", uid); has {
|
if has, _ := cache.CheckExists("user", uid); has {
|
||||||
return has, nil
|
return has, nil
|
||||||
}
|
}
|
||||||
has, err := global.DBConn.ID(uid).Exist(&model.User{})
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
|
defer cancel()
|
||||||
|
has, err := global.DB.NewSelect().Model((*model.User)(nil)).Where("id = ?", uid).Exists(ctx)
|
||||||
if has {
|
if has {
|
||||||
cache.CacheExists([]string{fmt.Sprintf("user_%s", uid)}, "user", uid)
|
cache.CacheExists([]string{"user", fmt.Sprintf("user_%s", uid)}, "user", uid)
|
||||||
}
|
}
|
||||||
return has, err
|
return has, err
|
||||||
}
|
}
|
||||||
|
@ -171,7 +176,9 @@ func (_UserService) IsUsernameExists(username string) (bool, error) {
|
||||||
if has, _ := cache.CheckExists("user", username); has {
|
if has, _ := cache.CheckExists("user", username); has {
|
||||||
return has, nil
|
return has, nil
|
||||||
}
|
}
|
||||||
has, err := global.DBConn.Where(builder.Eq{"username": username}).Exist(&model.User{})
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
|
defer cancel()
|
||||||
|
has, err := global.DB.NewSelect().Model((*model.User)(nil)).Where("username = ?", username).Exists(ctx)
|
||||||
if has {
|
if has {
|
||||||
cache.CacheExists([]string{"user"}, "user", username)
|
cache.CacheExists([]string{"user"}, "user", username)
|
||||||
}
|
}
|
||||||
|
@ -199,18 +206,19 @@ func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (st
|
||||||
finalAbbr := tools.PinyinAbbr(*detail.Name)
|
finalAbbr := tools.PinyinAbbr(*detail.Name)
|
||||||
detail.Abbr = &finalAbbr
|
detail.Abbr = &finalAbbr
|
||||||
}
|
}
|
||||||
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
tx := global.DBConn.NewSession()
|
tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{})
|
||||||
defer tx.Close()
|
if err != nil {
|
||||||
if err := tx.Begin(); err != nil {
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
_, err = tx.Insert(user)
|
_, err = tx.NewInsert().Model(user).Exec(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return "", fmt.Errorf("user create failed: %w", err)
|
return "", fmt.Errorf("user create failed: %w", err)
|
||||||
}
|
}
|
||||||
_, err = tx.Insert(detail)
|
_, err = tx.NewInsert().Model(detail).Exec(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return "", fmt.Errorf("user Detail create failed: %w", err)
|
return "", fmt.Errorf("user Detail create failed: %w", err)
|
||||||
|
@ -220,6 +228,7 @@ func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (st
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return "", fmt.Errorf("transaction commit unsuccessful: %w", err)
|
return "", fmt.Errorf("transaction commit unsuccessful: %w", err)
|
||||||
}
|
}
|
||||||
|
// ! 广谱关联关系的废除必须是在有新记录加入或者有记录被删除的情况下。
|
||||||
cache.AbolishRelation("user")
|
cache.AbolishRelation("user")
|
||||||
return verifyCode, nil
|
return verifyCode, nil
|
||||||
}
|
}
|
||||||
|
@ -234,10 +243,11 @@ func (u _UserService) SwitchUserState(uid string, enabled bool) error {
|
||||||
}
|
}
|
||||||
newStateUser := new(model.User)
|
newStateUser := new(model.User)
|
||||||
newStateUser.Enabled = enabled
|
newStateUser.Enabled = enabled
|
||||||
_, err = global.DBConn.ID(uid).Cols("enabled").Update(newStateUser)
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
if err != nil {
|
defer cancel()
|
||||||
cache.AbolishRelation("user")
|
res, err := global.DB.NewUpdate().Model(newStateUser).WherePK().Column("enabled").Exec(ctx)
|
||||||
cache.AbolishRelation(fmt.Sprintf("user_%s", uid))
|
if affected, _ := res.RowsAffected(); err == nil && affected > 0 {
|
||||||
|
cache.AbolishRelation(fmt.Sprintf("user:%s", uid))
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -246,60 +256,86 @@ func (_UserService) SearchLimitUsers(keyword string, limit int) ([]model.JoinedU
|
||||||
if cachedUsers, _ := cache.RetreiveSearch[[]model.JoinedUserDetail]("join_user_detail", keyword, strconv.Itoa(limit)); cachedUsers != nil {
|
if cachedUsers, _ := cache.RetreiveSearch[[]model.JoinedUserDetail]("join_user_detail", keyword, strconv.Itoa(limit)); cachedUsers != nil {
|
||||||
return *cachedUsers, nil
|
return *cachedUsers, nil
|
||||||
}
|
}
|
||||||
var users = make([]model.JoinedUserDetail, 0)
|
|
||||||
err := global.DBConn.
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
Table("user_detail").Alias("d").
|
defer cancel()
|
||||||
Join("INNER", []string{"user", "u"}, "d.id=u.id").
|
|
||||||
Where(
|
var users = make([]model.User, 0)
|
||||||
builder.NewCond().
|
keywordCond := "%" + keyword + "%"
|
||||||
Or(builder.Like{"u.username", keyword}).
|
err := global.DB.NewSelect().Model(&users).Relation("Detail").
|
||||||
Or(builder.Like{"d.name", keyword}).
|
Where("user.type = ?", model.USER_TYPE_ENT).
|
||||||
Or(builder.Like{"d.abbr", keyword})).
|
WhereGroup(" and ", func(q *bun.SelectQuery) *bun.SelectQuery {
|
||||||
And(builder.Eq{"u.type": 0}).
|
return q.Where("user.username like ?", keywordCond).
|
||||||
Asc("u.created_at").
|
WhereOr("user_detail.name like ?", keywordCond).
|
||||||
Limit(limit, 0).
|
WhereOr("user_detail like ?", keywordCond)
|
||||||
Find(&users)
|
}).
|
||||||
|
Order("user.created_at asc").
|
||||||
|
Limit(limit).
|
||||||
|
Offset(0).
|
||||||
|
Scan(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return make([]model.JoinedUserDetail, 0), err
|
return make([]model.JoinedUserDetail, 0), err
|
||||||
}
|
}
|
||||||
cache.CacheSearch(users, []string{"user"}, "join_user_detail", keyword, strconv.Itoa(limit))
|
var detailedUsers = make([]model.JoinedUserDetail, 0)
|
||||||
return users, nil
|
var relations = make([]string, 0)
|
||||||
|
// ! 这里的转换是为了兼容之前使用Xorm时构建的关联关系而存在的
|
||||||
|
for _, u := range users {
|
||||||
|
detailedUsers = append(detailedUsers, model.JoinedUserDetail{
|
||||||
|
UserDetail: *u.Detail,
|
||||||
|
Id: u.Id,
|
||||||
|
Username: u.Username,
|
||||||
|
Type: u.Type,
|
||||||
|
Enabled: u.Enabled,
|
||||||
|
})
|
||||||
|
relations = append(relations, fmt.Sprintf("user:%s", u.Id))
|
||||||
|
}
|
||||||
|
relations = append(relations, "user")
|
||||||
|
cache.CacheSearch(users, relations, "join_user_detail", keyword, strconv.Itoa(limit))
|
||||||
|
return detailedUsers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_UserService) findUserWithCredentialsByUsername(username string) (*model.UserWithCredentials, error) {
|
func (_UserService) findUserWithCredentialsByUsername(username string) (*model.UserWithCredentials, error) {
|
||||||
if cachedUser, _ := cache.RetreiveSearch[model.UserWithCredentials]("user_with_credentials", username); cachedUser != nil {
|
if cachedUser, _ := cache.RetreiveSearch[model.UserWithCredentials]("user_with_credentials", username); cachedUser != nil {
|
||||||
return cachedUser, nil
|
return cachedUser, nil
|
||||||
}
|
}
|
||||||
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
|
defer cancel()
|
||||||
user := new(model.UserWithCredentials)
|
user := new(model.UserWithCredentials)
|
||||||
has, err := global.DBConn.Where(builder.Eq{"username": username}).NoAutoCondition().Get(user)
|
err := global.DB.NewSelect().Model(&user).Where("username = ?", username).Scan(ctx)
|
||||||
if has {
|
if err == nil {
|
||||||
cache.CacheSearch(*user, []string{"user"}, "user_with_credentials", username)
|
cache.CacheSearch(*user, []string{fmt.Sprintf("user:%s", user.Id)}, "user_with_credentials", username)
|
||||||
}
|
}
|
||||||
return _postProcessSingle(user, has, err)
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_UserService) findUserByUsername(username string) (*model.User, error) {
|
func (_UserService) findUserByUsername(username string) (*model.User, error) {
|
||||||
if cachedUser, _ := cache.RetreiveSearch[model.User]("user", username); cachedUser != nil {
|
if cachedUser, _ := cache.RetreiveSearch[model.User]("user", username); cachedUser != nil {
|
||||||
return cachedUser, nil
|
return cachedUser, nil
|
||||||
}
|
}
|
||||||
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
|
defer cancel()
|
||||||
user := new(model.User)
|
user := new(model.User)
|
||||||
has, err := global.DBConn.Where(builder.Eq{"username": username}).NoAutoCondition().Get(user)
|
err := global.DB.NewSelect().Model(&user).Where("username = ?", username).Scan(ctx)
|
||||||
if has {
|
if err == nil {
|
||||||
cache.CacheSearch(*user, []string{"user"}, "user", username)
|
cache.CacheSearch(*user, []string{fmt.Sprintf("user:%s", user.Id)}, "user", username)
|
||||||
}
|
}
|
||||||
return _postProcessSingle(user, has, err)
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_UserService) retreiveUserDetail(uid string) (*model.UserDetail, error) {
|
func (_UserService) retreiveUserDetail(uid string) (*model.UserDetail, error) {
|
||||||
if cachedUser, _ := cache.RetreiveEntity[model.UserDetail]("user_detail", uid); cachedUser != nil {
|
if cachedUser, _ := cache.RetreiveEntity[model.UserDetail]("user_detail", uid); cachedUser != nil {
|
||||||
return cachedUser, nil
|
return cachedUser, nil
|
||||||
}
|
}
|
||||||
user := new(model.UserDetail)
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
has, err := global.DBConn.ID(uid).NoAutoCondition().Get(user)
|
defer cancel()
|
||||||
if has {
|
user := &model.UserDetail{
|
||||||
cache.CacheEntity(*user, []string{fmt.Sprintf("user_%s", uid)}, "user_detail", uid)
|
Id: uid,
|
||||||
}
|
}
|
||||||
return _postProcessSingle(user, has, err)
|
err := global.DB.NewSelect().Model(&user).WherePK().Scan(ctx)
|
||||||
|
if err == nil {
|
||||||
|
cache.CacheEntity(*user, []string{fmt.Sprintf("user:%s", uid)}, "user_detail", uid)
|
||||||
|
}
|
||||||
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_UserService) findUserByID(uid string) (*model.User, error) {
|
func (_UserService) findUserByID(uid string) (*model.User, error) {
|
||||||
|
@ -307,82 +343,96 @@ func (_UserService) findUserByID(uid string) (*model.User, error) {
|
||||||
if cachedUser != nil {
|
if cachedUser != nil {
|
||||||
return cachedUser, nil
|
return cachedUser, nil
|
||||||
}
|
}
|
||||||
user := new(model.User)
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
has, err := global.DBConn.ID(uid).NoAutoCondition().Get(user)
|
defer cancel()
|
||||||
if has {
|
user := &model.User{
|
||||||
cache.CacheEntity(*user, []string{fmt.Sprintf("user_%s", uid)}, "user", uid)
|
Id: uid,
|
||||||
}
|
}
|
||||||
return _postProcessSingle(user, has, err)
|
err := global.DB.NewSelect().Model(&user).WherePK().Scan(ctx)
|
||||||
|
if err == nil {
|
||||||
|
cache.CacheEntity(*user, []string{fmt.Sprintf("user:%s", uid)}, "user", uid)
|
||||||
|
}
|
||||||
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_UserService) ListUserDetail(keyword string, userType int, userState *bool, page int) ([]model.JoinedUserDetail, int64, error) {
|
func (_UserService) ListUserDetail(keyword string, userType int, userState *bool, page int) ([]model.JoinedUserDetail, int64, error) {
|
||||||
var (
|
var (
|
||||||
cond = builder.NewCond()
|
cond = global.DB.NewSelect()
|
||||||
cacheConditions = make([]string, 0)
|
cacheConditions = make([]string, 0)
|
||||||
|
users = make([]model.User, 0)
|
||||||
)
|
)
|
||||||
|
cond = cond.Model(&users).Relation("Detail")
|
||||||
cacheConditions = append(cacheConditions, strconv.Itoa(page))
|
cacheConditions = append(cacheConditions, strconv.Itoa(page))
|
||||||
cond = cond.And(builder.Neq{"d.id": "000"})
|
cond = cond.Where("d.id <> ?", "000")
|
||||||
if len(keyword) != 0 {
|
if len(keyword) != 0 {
|
||||||
keywordCond := builder.NewCond().
|
keywordCond := "%" + keyword + "%"
|
||||||
Or(builder.Like{"u.username", keyword}).
|
cond = cond.WhereGroup(" and ", func(q *bun.SelectQuery) *bun.SelectQuery {
|
||||||
Or(builder.Like{"d.name", keyword})
|
return q.Where("u.username like ?", keywordCond).
|
||||||
cond = cond.And(keywordCond)
|
WhereOr("d.name like ?", keywordCond)
|
||||||
|
})
|
||||||
cacheConditions = append(cacheConditions, keyword)
|
cacheConditions = append(cacheConditions, keyword)
|
||||||
}
|
}
|
||||||
if userType != -1 {
|
if userType != -1 {
|
||||||
cond = cond.And(builder.Eq{"u.type": userType})
|
cond = cond.Where("u.type = ?", userType)
|
||||||
cacheConditions = append(cacheConditions, strconv.Itoa(userType))
|
cacheConditions = append(cacheConditions, strconv.Itoa(userType))
|
||||||
}
|
}
|
||||||
if userState != nil {
|
if userState != nil {
|
||||||
cond = cond.And(builder.Eq{"u.enabled": *userState})
|
cond = cond.Where("u.enabled = ?", *userState)
|
||||||
cacheConditions = append(cacheConditions, strconv.FormatBool(*userState))
|
cacheConditions = append(cacheConditions, strconv.FormatBool(*userState))
|
||||||
}
|
}
|
||||||
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
|
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
|
||||||
var (
|
|
||||||
total int64
|
// * 这里利用已经构建完成的条件集合从缓存中获取数据,如果所有数据都可以从缓存中获取,那么就直接返回了。
|
||||||
err error
|
|
||||||
)
|
|
||||||
if cacheCounts, err := cache.RetreiveCount("join_user_detail", cacheConditions...); cacheCounts != -1 && err == nil {
|
if cacheCounts, err := cache.RetreiveCount("join_user_detail", cacheConditions...); cacheCounts != -1 && err == nil {
|
||||||
total = cacheCounts
|
|
||||||
} else {
|
|
||||||
total, err = global.DBConn.
|
|
||||||
Table("user_detail").Alias("d").
|
|
||||||
Join("INNER", []string{"user", "u"}, "d.id=u.id").
|
|
||||||
Where(cond).
|
|
||||||
Count(&model.User{})
|
|
||||||
if err != nil {
|
|
||||||
return nil, -1, err
|
|
||||||
}
|
|
||||||
cache.CacheCount([]string{"user"}, "join_user_detail", total, cacheConditions...)
|
|
||||||
}
|
|
||||||
users := make([]model.JoinedUserDetail, 0)
|
|
||||||
if cachedUsers, _ := cache.RetreiveSearch[[]model.JoinedUserDetail]("join_user_detail", cacheConditions...); cachedUsers != nil {
|
if cachedUsers, _ := cache.RetreiveSearch[[]model.JoinedUserDetail]("join_user_detail", cacheConditions...); cachedUsers != nil {
|
||||||
return *cachedUsers, total, nil
|
return *cachedUsers, cacheCounts, nil
|
||||||
}
|
}
|
||||||
err = global.DBConn.
|
}
|
||||||
Table("user_detail").Alias("d").
|
|
||||||
Join("INNER", []string{"user", "u"}, "d.id=u.id").
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
Where(cond).
|
defer cancel()
|
||||||
Limit(config.ServiceSettings.ItemsPageSize, startItem).
|
total, err := cond.
|
||||||
Find(&users)
|
Limit(config.ServiceSettings.ItemsPageSize).Offset(startItem).
|
||||||
cache.CacheSearch(users, []string{"user"}, "join_user_detail", cacheConditions...)
|
ScanAndCount(ctx)
|
||||||
return users, total, err
|
|
||||||
|
var (
|
||||||
|
joinedUsers = make([]model.JoinedUserDetail, 0)
|
||||||
|
relations = []string{"user"}
|
||||||
|
)
|
||||||
|
for _, u := range users {
|
||||||
|
joinedUsers = append(joinedUsers, model.JoinedUserDetail{
|
||||||
|
UserDetail: *u.Detail,
|
||||||
|
Id: u.Id,
|
||||||
|
Username: u.Username,
|
||||||
|
Type: u.Type,
|
||||||
|
Enabled: u.Enabled,
|
||||||
|
})
|
||||||
|
relations = append(relations, fmt.Sprintf("user:%s", u.Id))
|
||||||
|
}
|
||||||
|
|
||||||
|
cache.CacheCount(relations, "join_user_detail", int64(total), cacheConditions...)
|
||||||
|
cache.CacheSearch(joinedUsers, relations, "join_user_detail", cacheConditions...)
|
||||||
|
return joinedUsers, int64(total), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_UserService) FetchUserDetail(uid string) (*model.FullJoinedUserDetail, error) {
|
func (_UserService) FetchUserDetail(uid string) (*model.FullJoinedUserDetail, error) {
|
||||||
if cachedUser, _ := cache.RetreiveEntity[model.FullJoinedUserDetail]("full_join_user_detail", uid); cachedUser != nil {
|
if cachedUser, _ := cache.RetreiveEntity[model.FullJoinedUserDetail]("full_join_user_detail", uid); cachedUser != nil {
|
||||||
return cachedUser, nil
|
return cachedUser, nil
|
||||||
}
|
}
|
||||||
user := &model.FullJoinedUserDetail{}
|
|
||||||
has, err := global.DBConn.
|
ctx, cancel := global.TimeoutContext(30 * time.Second)
|
||||||
Table("user_detail").Alias("d").
|
defer cancel()
|
||||||
Join("INNER", []string{"user", "u"}, "d.id=u.id").
|
user := &model.User{}
|
||||||
Where(builder.Eq{"d.id": uid}).
|
err := global.DB.NewSelect().Model(&user).Relation("Detail").
|
||||||
NoAutoCondition().
|
Where("user.id = ?", uid).
|
||||||
Get(user)
|
Scan(ctx)
|
||||||
if has {
|
if err == nil {
|
||||||
cache.CacheEntity(*user, []string{fmt.Sprintf("user_%s", uid)}, "full_join_user_detail", uid)
|
fullJoinedUser := &model.FullJoinedUserDetail{
|
||||||
return user, nil
|
User: *user,
|
||||||
|
UserDetail: *user.Detail,
|
||||||
|
}
|
||||||
|
cache.CacheEntity(*fullJoinedUser, []string{fmt.Sprintf("user:%s", uid)}, "full_join_user_detail", uid)
|
||||||
|
return fullJoinedUser, nil
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user