forked from free-lancers/electricity_bill_calc_service
feat(user):通过完成用户检索功能,继续确定项目的基本代码结构。
This commit is contained in:
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/cache"
|
||||
"electricity_bill_calc/config"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
@@ -28,7 +29,7 @@ var UserRepository = _UserRepository{
|
||||
// 使用用户名查询指定用户的基本信息
|
||||
func (ur _UserRepository) FindUserByUsername(username string) (*model.User, error) {
|
||||
ur.log.Info("根据用户名查询指定用户的基本信息。", zap.String("username", username))
|
||||
if cachedUser, _ := cache.RetreiveEntity[model.User]("user", username); cachedUser != nil {
|
||||
if cachedUser, _ := cache.RetrieveEntity[model.User]("user", username); cachedUser != nil {
|
||||
ur.log.Info("已经从缓存获取到了符合指定用户名条件的用户基本信息。", zap.String("username", username))
|
||||
return cachedUser, nil
|
||||
}
|
||||
@@ -48,7 +49,7 @@ func (ur _UserRepository) FindUserByUsername(username string) (*model.User, erro
|
||||
// 使用用户唯一编号查询指定用户的基本信息
|
||||
func (ur _UserRepository) FindUserById(uid string) (*model.User, error) {
|
||||
ur.log.Info("根据用户唯一编号查询指定用户的基本信息。", zap.String("user id", uid))
|
||||
if cachedUser, _ := cache.RetreiveEntity[model.User]("user", uid); cachedUser != nil {
|
||||
if cachedUser, _ := cache.RetrieveEntity[model.User]("user", uid); cachedUser != nil {
|
||||
ur.log.Info("已经从缓存获取到了符合指定用户唯一编号的用户基本信息。")
|
||||
return cachedUser, nil
|
||||
}
|
||||
@@ -68,7 +69,7 @@ func (ur _UserRepository) FindUserById(uid string) (*model.User, error) {
|
||||
// 使用用户的唯一编号获取用户的详细信息
|
||||
func (ur _UserRepository) FindUserDetailById(uid string) (*model.UserDetail, error) {
|
||||
ur.log.Info("根据用户唯一编号查询指定用户的详细信息。", zap.String("user id", uid))
|
||||
if cachedUser, _ := cache.RetreiveEntity[model.UserDetail]("user_detail", uid); cachedUser != nil {
|
||||
if cachedUser, _ := cache.RetrieveEntity[model.UserDetail]("user_detail", uid); cachedUser != nil {
|
||||
ur.log.Info("已经从缓存获取到了符合指定用户唯一编号的用户详细信息。")
|
||||
return cachedUser, nil
|
||||
}
|
||||
@@ -88,7 +89,7 @@ func (ur _UserRepository) FindUserDetailById(uid string) (*model.UserDetail, err
|
||||
// 使用用户唯一编号获取用户的综合详细信息
|
||||
func (ur _UserRepository) FindUserInformation(uid string) (*model.UserWithDetail, error) {
|
||||
ur.log.Info("根据用户唯一编号查询用户的综合详细信息", zap.String("user id", uid))
|
||||
if cachedUser, _ := cache.RetreiveEntity[model.UserWithDetail]("user_information", uid); cachedUser != nil {
|
||||
if cachedUser, _ := cache.RetrieveEntity[model.UserWithDetail]("user_information", uid); cachedUser != nil {
|
||||
ur.log.Info("已经从缓存获取到了符合指定用户唯一编号的用户综合详细信息。")
|
||||
return cachedUser, nil
|
||||
}
|
||||
@@ -141,8 +142,31 @@ func (ur _UserRepository) IsUserExists(uid string) (bool, error) {
|
||||
return userCount > 0, nil
|
||||
}
|
||||
|
||||
// 检查指定用户名在数据库中是否已经存在
|
||||
func (ur _UserRepository) IsUsernameExists(username string) (bool, error) {
|
||||
ur.log.Info("检查指定用户名在数据库中是否已经存在。", zap.String("username", username))
|
||||
if exists, _ := cache.CheckExists("user", username); exists {
|
||||
ur.log.Info("已经从缓存获取到了符合指定用户名的用户基本信息。")
|
||||
return exists, nil
|
||||
}
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
var userCount int
|
||||
sql, params, _ := ur.ds.From("user").Select(goqu.COUNT("*")).Where(goqu.Ex{"username": username}).Prepared(true).ToSQL()
|
||||
if err := pgxscan.Get(ctx, global.DB, &userCount, sql, params...); err != nil {
|
||||
ur.log.Error("从数据库查询指定用户名的用户基本信息失败。", zap.String("username", username), zap.Error(err))
|
||||
return false, err
|
||||
}
|
||||
if userCount > 0 {
|
||||
cache.CacheExists([]string{"user", fmt.Sprintf("user:%s", username)}, "user", username)
|
||||
}
|
||||
return userCount > 0, nil
|
||||
}
|
||||
|
||||
// 创建一个新用户
|
||||
func (ur _UserRepository) CreateUser(user model.User, detail model.UserDetail, operator *string) (bool, error) {
|
||||
ur.log.Info("创建一个新用户。", zap.String("username", user.Username))
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
tx, err := global.DB.Begin(ctx)
|
||||
@@ -197,3 +221,106 @@ func (ur _UserRepository) CreateUser(user model.User, detail model.UserDetail, o
|
||||
}
|
||||
return userResult.RowsAffected() > 0 && detailResult.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
// 根据给定的条件检索用户
|
||||
func (ur _UserRepository) FindUser(keyword *string, userType int16, state *bool, page uint) (*[]model.UserWithDetail, int64, error) {
|
||||
ur.log.Info("根据给定的条件检索用户。", zap.Uint("page", page), zap.Stringp("keyword", keyword), zap.Int16("user type", userType), zap.Boolp("state", state))
|
||||
if users, total, err := cache.RetrievePagedSearch[[]model.UserWithDetail]("user_with_detail", []string{
|
||||
fmt.Sprintf("%d", page),
|
||||
tools.CondFn(
|
||||
func(v int16) bool {
|
||||
return v != -1
|
||||
},
|
||||
userType,
|
||||
fmt.Sprintf("%d", userType),
|
||||
"UNDEF",
|
||||
),
|
||||
tools.DefaultStrTo("%s", state, "UNDEF"),
|
||||
tools.DefaultTo(keyword, ""),
|
||||
}...); err == nil && users != nil && total != -1 {
|
||||
return users, total, nil
|
||||
}
|
||||
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
var (
|
||||
userWithDetails []model.UserWithDetail
|
||||
userCount int64
|
||||
)
|
||||
userQuery := ur.ds.
|
||||
From(goqu.T("user").As("u")).
|
||||
Join(goqu.T("user_detail").As("ud"), goqu.On(goqu.Ex{"ud.id": goqu.I("u.id")})).
|
||||
Select(
|
||||
"u.id", "u.username", "u.reset_needed", "u.type", "u.enabled",
|
||||
"ud.name", "ud.abbr", "ud.region", "ud.address", "ud.contact", "ud.phone",
|
||||
"ud.unit_service_fee", "ud.service_expiration",
|
||||
"ud.created_at", "ud.created_by", "ud.last_modified_at", "ud.last_modified_by",
|
||||
)
|
||||
countQuery := ur.ds.
|
||||
From(goqu.T("user").As("u")).
|
||||
Join(goqu.T("user_detail").As("ud"), goqu.On(goqu.Ex{"ud.id": goqu.I("u.id")})).
|
||||
Select(goqu.COUNT("*"))
|
||||
|
||||
if keyword != nil && len(*keyword) > 0 {
|
||||
pattern := fmt.Sprintf("%%%s%%", *keyword)
|
||||
userQuery = userQuery.Where(
|
||||
goqu.Or(
|
||||
goqu.Ex{"u.username": goqu.Op{"like": pattern}},
|
||||
goqu.Ex{"ud.name": goqu.Op{"like": pattern}},
|
||||
goqu.Ex{"ud.abbr": goqu.Op{"like": pattern}},
|
||||
),
|
||||
)
|
||||
countQuery = countQuery.Where(
|
||||
goqu.Or(
|
||||
goqu.Ex{"u.username": goqu.Op{"like": pattern}},
|
||||
goqu.Ex{"ud.name": goqu.Op{"like": pattern}},
|
||||
goqu.Ex{"ud.abbr": goqu.Op{"like": pattern}},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
if userType != -1 {
|
||||
userQuery = userQuery.Where(goqu.Ex{"u.type": userType})
|
||||
countQuery = countQuery.Where(goqu.Ex{"u.type": userType})
|
||||
}
|
||||
|
||||
if state != nil {
|
||||
userQuery = userQuery.Where(goqu.Ex{"u.enabled": state})
|
||||
countQuery = countQuery.Where(goqu.Ex{"u.enabled": state})
|
||||
}
|
||||
|
||||
currentPosition := (page - 1) * config.ServiceSettings.ItemsPageSize
|
||||
userQuery = userQuery.Offset(currentPosition).Limit(config.ServiceSettings.ItemsPageSize)
|
||||
|
||||
userSql, userParams, _ := userQuery.Prepared(true).ToSQL()
|
||||
countSql, countParams, _ := countQuery.Prepared(true).ToSQL()
|
||||
if err := pgxscan.Select(ctx, global.DB, &userWithDetails, userSql, userParams...); err != nil {
|
||||
ur.log.Error("从数据库查询用户列表失败。", zap.Error(err))
|
||||
return nil, 0, err
|
||||
}
|
||||
if err := pgxscan.Get(ctx, global.DB, &userCount, countSql, countParams...); err != nil {
|
||||
ur.log.Error("从数据库查询用户列表总数失败。", zap.Error(err))
|
||||
return nil, 0, err
|
||||
}
|
||||
cache.CachePagedSearch(
|
||||
userWithDetails,
|
||||
userCount,
|
||||
[]string{"user"},
|
||||
"user_with_detail",
|
||||
[]string{
|
||||
fmt.Sprintf("%d", page),
|
||||
tools.CondFn(
|
||||
func(v int16) bool {
|
||||
return v != -1
|
||||
},
|
||||
userType,
|
||||
fmt.Sprintf("%d", userType),
|
||||
"UNDEF",
|
||||
),
|
||||
tools.DefaultStrTo("%s", state, "UNDEF"),
|
||||
tools.DefaultTo(keyword, ""),
|
||||
}...,
|
||||
)
|
||||
return &userWithDetails, userCount, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user