fix(user):修正检索用户时语句中的引用问题。

This commit is contained in:
徐涛 2022-09-18 10:50:49 +08:00
parent 2aa6939186
commit e40ba55825

View File

@ -6,6 +6,7 @@ import (
"electricity_bill_calc/config" "electricity_bill_calc/config"
"electricity_bill_calc/exceptions" "electricity_bill_calc/exceptions"
"electricity_bill_calc/global" "electricity_bill_calc/global"
"electricity_bill_calc/logger"
"electricity_bill_calc/model" "electricity_bill_calc/model"
"electricity_bill_calc/tools" "electricity_bill_calc/tools"
"fmt" "fmt"
@ -15,11 +16,16 @@ import (
"github.com/fufuok/utils" "github.com/fufuok/utils"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"go.uber.org/zap"
) )
type _UserService struct{} type _UserService struct {
l *zap.Logger
}
var UserService _UserService var UserService = _UserService{
l: logger.Named("Service", "User"),
}
func (u _UserService) ProcessEnterpriseUserLogin(username, password string) (*model.Session, error) { func (u _UserService) ProcessEnterpriseUserLogin(username, password string) (*model.Session, error) {
user, err := u.findUserWithCredentialsByUsername(username) user, err := u.findUserWithCredentialsByUsername(username)
@ -242,6 +248,7 @@ func (u _UserService) SwitchUserState(uid string, enabled bool) error {
return err return err
} }
newStateUser := new(model.User) newStateUser := new(model.User)
newStateUser.Id = uid
newStateUser.Enabled = enabled newStateUser.Enabled = enabled
ctx, cancel := global.TimeoutContext() ctx, cancel := global.TimeoutContext()
defer cancel() defer cancel()
@ -308,14 +315,14 @@ func (_UserService) findUserWithCredentialsByUsername(username string) (*model.U
return user, err return user, err
} }
func (_UserService) findUserByUsername(username string) (*model.User, error) { func (u _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() ctx, cancel := global.TimeoutContext()
defer cancel() defer cancel()
user := new(model.User) user := new(model.User)
err := global.DB.NewSelect().Model(&user).Where("username = ?", username).Scan(ctx) err := global.DB.NewSelect().Model(user).Where("username = ?", username).Scan(ctx)
if err == nil { if err == nil {
cache.CacheSearch(*user, []string{fmt.Sprintf("user:%s", user.Id)}, "user", username) cache.CacheSearch(*user, []string{fmt.Sprintf("user:%s", user.Id)}, "user", username)
} }
@ -363,12 +370,12 @@ func (_UserService) ListUserDetail(keyword string, userType int, userState *bool
) )
cond = cond.Model(&users).Relation("Detail") cond = cond.Model(&users).Relation("Detail")
cacheConditions = append(cacheConditions, strconv.Itoa(page)) cacheConditions = append(cacheConditions, strconv.Itoa(page))
cond = cond.Where("d.id <> ?", "000") cond = cond.Where("detail.id <> ?", "000")
if len(keyword) != 0 { if len(keyword) != 0 {
keywordCond := "%" + keyword + "%" keywordCond := "%" + keyword + "%"
cond = cond.WhereGroup(" and ", func(q *bun.SelectQuery) *bun.SelectQuery { cond = cond.WhereGroup(" and ", func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("u.username like ?", keywordCond). return q.Where("u.username like ?", keywordCond).
WhereOr("d.name like ?", keywordCond) WhereOr("detail.name like ?", keywordCond)
}) })
cacheConditions = append(cacheConditions, keyword) cacheConditions = append(cacheConditions, keyword)
} }