fix(user):根据行政区划部分的测试结论,修正用户部分的模型扫描。

This commit is contained in:
徐涛 2023-06-02 06:26:22 +08:00
parent ee55507e05
commit 04dc9c51aa

View File

@ -226,7 +226,7 @@ func (ur _UserRepository) CreateUser(user model.User, detail model.UserDetail, o
} }
// 根据给定的条件检索用户 // 根据给定的条件检索用户
func (ur _UserRepository) FindUser(keyword *string, userType int16, state *bool, page uint) ([]model.UserWithDetail, int64, error) { 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)) ur.log.Info("根据给定的条件检索用户。", zap.Uint("page", page), zap.Stringp("keyword", keyword), zap.Int16("user type", userType), zap.Boolp("state", state))
cacheConditions := []string{ cacheConditions := []string{
fmt.Sprintf("%d", page), fmt.Sprintf("%d", page),
@ -241,7 +241,7 @@ func (ur _UserRepository) FindUser(keyword *string, userType int16, state *bool,
tools.DefaultStrTo("%s", state, "UNDEF"), tools.DefaultStrTo("%s", state, "UNDEF"),
tools.DefaultTo(keyword, ""), tools.DefaultTo(keyword, ""),
} }
if users, total, err := cache.RetrievePagedSearch[[]model.UserWithDetail]("user_with_detail", cacheConditions...); err == nil && users != nil && total != -1 { if users, total, err := cache.RetrievePagedSearch[[]*model.UserWithDetail]("user_with_detail", cacheConditions...); err == nil && users != nil && total != -1 {
return *users, total, nil return *users, total, nil
} }
@ -249,7 +249,7 @@ func (ur _UserRepository) FindUser(keyword *string, userType int16, state *bool,
defer cancel() defer cancel()
var ( var (
userWithDetails []model.UserWithDetail userWithDetails []*model.UserWithDetail
userCount int64 userCount int64
) )
userQuery := ur.ds. userQuery := ur.ds.
@ -303,11 +303,11 @@ func (ur _UserRepository) FindUser(keyword *string, userType int16, state *bool,
countSql, countParams, _ := countQuery.Prepared(true).ToSQL() countSql, countParams, _ := countQuery.Prepared(true).ToSQL()
if err := pgxscan.Select(ctx, global.DB, &userWithDetails, userSql, userParams...); err != nil { if err := pgxscan.Select(ctx, global.DB, &userWithDetails, userSql, userParams...); err != nil {
ur.log.Error("从数据库查询用户列表失败。", zap.Error(err)) ur.log.Error("从数据库查询用户列表失败。", zap.Error(err))
return make([]model.UserWithDetail, 0), 0, err return make([]*model.UserWithDetail, 0), 0, err
} }
if err := pgxscan.Get(ctx, global.DB, &userCount, countSql, countParams...); err != nil { if err := pgxscan.Get(ctx, global.DB, &userCount, countSql, countParams...); err != nil {
ur.log.Error("从数据库查询用户列表总数失败。", zap.Error(err)) ur.log.Error("从数据库查询用户列表总数失败。", zap.Error(err))
return make([]model.UserWithDetail, 0), 0, err return make([]*model.UserWithDetail, 0), 0, err
} }
cache.CachePagedSearch( cache.CachePagedSearch(
userWithDetails, userWithDetails,
@ -397,7 +397,7 @@ func (ur _UserRepository) ChangeState(uid string, state bool) (bool, error) {
} }
// 检索条目数量有限的用户详细信息 // 检索条目数量有限的用户详细信息
func (ur _UserRepository) SearchUsersWithLimit(userType *int16, keyword *string, limit uint) (*[]model.UserDetail, error) { func (ur _UserRepository) SearchUsersWithLimit(userType *int16, keyword *string, limit uint) ([]*model.UserDetail, error) {
ur.log.Info("检索条目数量有限的用户详细信息。", zap.Int16p("user type", userType), zap.Uint("limit", limit), zap.Stringp("keyword", keyword)) ur.log.Info("检索条目数量有限的用户详细信息。", zap.Int16p("user type", userType), zap.Uint("limit", limit), zap.Stringp("keyword", keyword))
actualUserType := tools.DefaultTo(userType, model.USER_TYPE_ENT) actualUserType := tools.DefaultTo(userType, model.USER_TYPE_ENT)
cacheConditions := []string{ cacheConditions := []string{
@ -405,13 +405,13 @@ func (ur _UserRepository) SearchUsersWithLimit(userType *int16, keyword *string,
tools.DefaultTo(keyword, ""), tools.DefaultTo(keyword, ""),
fmt.Sprintf("%d", limit), fmt.Sprintf("%d", limit),
} }
if users, err := cache.RetrieveSearch[[]model.UserDetail]("user_with_detail_limited", cacheConditions...); err == nil && users != nil { if users, err := cache.RetrieveSearch[[]*model.UserDetail]("user_with_detail_limited", cacheConditions...); err == nil && users != nil {
return users, nil return *users, nil
} }
ctx, cancel := global.TimeoutContext() ctx, cancel := global.TimeoutContext()
defer cancel() defer cancel()
var users []model.UserDetail var users []*model.UserDetail
userQuery := ur.ds. userQuery := ur.ds.
From(goqu.T("user").As("u")). From(goqu.T("user").As("u")).
Join(goqu.T("user_detail").As("ud"), goqu.On(goqu.Ex{"ud.id": goqu.I("u.id")})). Join(goqu.T("user_detail").As("ud"), goqu.On(goqu.Ex{"ud.id": goqu.I("u.id")})).
@ -438,12 +438,12 @@ func (ur _UserRepository) SearchUsersWithLimit(userType *int16, keyword *string,
userQuery.Order(goqu.I("u.created_at").Desc()).Limit(limit) userQuery.Order(goqu.I("u.created_at").Desc()).Limit(limit)
userSql, userParams, _ := userQuery.Prepared(true).ToSQL() userSql, userParams, _ := userQuery.Prepared(true).ToSQL()
if err := pgxscan.Select(ctx, global.DB, &users, userSql, userParams...); err != nil { if err := pgxscan.Select(ctx, global.DB, users, userSql, userParams...); err != nil {
ur.log.Error("从数据库查询用户列表失败。", zap.Error(err)) ur.log.Error("从数据库查询用户列表失败。", zap.Error(err))
return nil, err return nil, err
} }
cache.CacheSearch(users, []string{"user"}, "user_with_detail_limited", cacheConditions...) cache.CacheSearch(users, []string{"user"}, "user_with_detail_limited", cacheConditions...)
return &users, nil return users, nil
} }
// 更新指定用户的服务有效期限 // 更新指定用户的服务有效期限
@ -470,29 +470,29 @@ func (ur _UserRepository) UpdateServiceExpiration(uid string, expiration stdTime
} }
// 检索指定用户列表的详细信息 // 检索指定用户列表的详细信息
func (ur _UserRepository) RetrieveUsersDetail(uids []string) ([]model.UserDetail, error) { func (ur _UserRepository) RetrieveUsersDetail(uids []string) ([]*model.UserDetail, error) {
ur.log.Info("检索指定用户列表的详细信息。", zap.Strings("user ids", uids)) ur.log.Info("检索指定用户列表的详细信息。", zap.Strings("user ids", uids))
if len(uids) == 0 { if len(uids) == 0 {
return make([]model.UserDetail, 0), nil return make([]*model.UserDetail, 0), nil
} }
cacheConditions := []string{ cacheConditions := []string{
strings.Join(uids, ","), strings.Join(uids, ","),
} }
if users, err := cache.RetrieveSearch[[]model.UserDetail]("user_detail", cacheConditions...); err == nil && users != nil { if users, err := cache.RetrieveSearch[[]*model.UserDetail]("user_detail", cacheConditions...); err == nil && users != nil {
return *users, nil return *users, nil
} }
ctx, cancel := global.TimeoutContext() ctx, cancel := global.TimeoutContext()
defer cancel() defer cancel()
var users []model.UserDetail var users []*model.UserDetail
userQuery := ur.ds. userQuery := ur.ds.
From("user_detail"). From("user_detail").
Where(goqu.Ex{"id": goqu.Any(uids)}) Where(goqu.Ex{"id": goqu.Any(uids)})
userSql, userParams, _ := userQuery.Prepared(true).ToSQL() userSql, userParams, _ := userQuery.Prepared(true).ToSQL()
if err := pgxscan.Select(ctx, global.DB, &users, userSql, userParams...); err != nil { if err := pgxscan.Select(ctx, global.DB, users, userSql, userParams...); err != nil {
ur.log.Error("从数据库查询用户列表失败。", zap.Error(err)) ur.log.Error("从数据库查询用户列表失败。", zap.Error(err))
return make([]model.UserDetail, 0), err return make([]*model.UserDetail, 0), err
} }
cache.CacheSearch(users, []string{"user", "user_detail"}, "user", cacheConditions...) cache.CacheSearch(users, []string{"user", "user_detail"}, "user", cacheConditions...)
return users, nil return users, nil