-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
453 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
/.idea | ||
/data | ||
/output | ||
etc | ||
<<<<<<< HEAD | ||
etc | ||
======= | ||
>>>>>>> 1614d94c8a1344a30c74ad9847dc271f8381a8a4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/config" | ||
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/consts" | ||
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/convertor" | ||
usermapper "github.com/CloudStriver/cloudmind-content/biz/infrastructure/mapper/user" | ||
"github.com/CloudStriver/go-pkg/utils/pagination/esp" | ||
"github.com/CloudStriver/go-pkg/utils/pconvertor" | ||
"github.com/CloudStriver/go-pkg/utils/util/log" | ||
gencontent "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/content" | ||
"github.com/google/wire" | ||
"github.com/zeromicro/go-zero/core/stores/redis" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
type UserService interface { | ||
GetUser(ctx context.Context, req *gencontent.GetUserReq) (resp *gencontent.GetUserResp, err error) | ||
CreateUser(ctx context.Context, req *gencontent.CreateUserReq) (resp *gencontent.CreateUserResp, err error) | ||
UpdateUser(ctx context.Context, req *gencontent.UpdateUserReq) (resp *gencontent.UpdateUserResp, err error) | ||
GetUserDetail(ctx context.Context, req *gencontent.GetUserDetailReq) (resp *gencontent.GetUserDetailResp, err error) | ||
SearchUser(ctx context.Context, req *gencontent.SearchUserReq) (resp *gencontent.SearchUserResp, err error) | ||
DeleteUser(ctx context.Context, req *gencontent.DeleteUserReq) (resp *gencontent.DeleteUserResp, err error) | ||
} | ||
|
||
type UserServiceImpl struct { | ||
Config *config.Config | ||
UserMongoMapper usermapper.UserMongoMapper | ||
UserEsMapper usermapper.UserEsMapper | ||
Redis *redis.Redis | ||
} | ||
|
||
var UserSet = wire.NewSet( | ||
wire.Struct(new(UserServiceImpl), "*"), | ||
wire.Bind(new(UserService), new(*UserServiceImpl)), | ||
) | ||
|
||
func (s *UserServiceImpl) DeleteUser(ctx context.Context, req *gencontent.DeleteUserReq) (resp *gencontent.DeleteUserResp, err error) { | ||
resp = new(gencontent.DeleteUserResp) | ||
_, err = s.UserMongoMapper.Delete(ctx, req.UserId) | ||
if err != nil { | ||
log.CtxError(ctx, "删除用户信息异常[%v]\n", err) | ||
return resp, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
func (s *UserServiceImpl) SearchUser(ctx context.Context, req *gencontent.SearchUserReq) (resp *gencontent.SearchUserResp, err error) { | ||
resp = new(gencontent.SearchUserResp) | ||
p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions) | ||
user, total, err := s.UserEsMapper.Search(ctx, req.Keyword, p, esp.ScoreCursorType) | ||
if err != nil { | ||
log.CtxError(ctx, "搜索用户信息异常[%v]\n", err) | ||
return resp, err | ||
} | ||
|
||
if p.LastToken != nil { | ||
resp.LastToken = *p.LastToken | ||
} | ||
resp.Total = total | ||
resp.Users = make([]*gencontent.User, 0, len(user)) | ||
for _, u := range user { | ||
resp.Users = append(resp.Users, convertor.UserMapperToUser(u)) | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (s *UserServiceImpl) GetUserDetail(ctx context.Context, req *gencontent.GetUserDetailReq) (resp *gencontent.GetUserDetailResp, err error) { | ||
resp = new(gencontent.GetUserDetailResp) | ||
var user *usermapper.User | ||
user, err = s.UserMongoMapper.FindOne(ctx, req.UserId) | ||
if err != nil { | ||
log.CtxError(ctx, "查询用户信息异常[%v]\n", err) | ||
return resp, err | ||
} | ||
|
||
resp.UserDetail = convertor.UserMapperToUserDetail(user) | ||
return resp, nil | ||
} | ||
|
||
func (s *UserServiceImpl) GetUser(ctx context.Context, req *gencontent.GetUserReq) (resp *gencontent.GetUserResp, err error) { | ||
resp = new(gencontent.GetUserResp) | ||
var user *usermapper.User | ||
user, err = s.UserMongoMapper.FindOne(ctx, req.UserId) | ||
if err != nil { | ||
log.CtxError(ctx, "查询用户信息异常[%v]\n", err) | ||
return resp, err | ||
} | ||
|
||
resp.User = convertor.UserMapperToUser(user) | ||
return resp, nil | ||
} | ||
|
||
func (s *UserServiceImpl) CreateUser(ctx context.Context, req *gencontent.CreateUserReq) (resp *gencontent.CreateUserResp, err error) { | ||
resp = new(gencontent.CreateUserResp) | ||
ID, err := primitive.ObjectIDFromHex(req.UserInfo.UserId) | ||
if err != nil { | ||
return resp, consts.ErrInvalidObjectId | ||
} | ||
if _, err = s.UserMongoMapper.Insert(ctx, &usermapper.User{ | ||
ID: ID, | ||
Name: req.UserInfo.Name, | ||
Sex: int32(req.UserInfo.Sex), | ||
Description: consts.DefaultDescription, | ||
Url: consts.DefaultAvatarUrl, | ||
}); err != nil { | ||
log.CtxError(ctx, "插入用户信息异常[%v]\n", err) | ||
return resp, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
func (s *UserServiceImpl) UpdateUser(ctx context.Context, req *gencontent.UpdateUserReq) (resp *gencontent.UpdateUserResp, err error) { | ||
resp = new(gencontent.UpdateUserResp) | ||
if _, err = s.UserMongoMapper.Update(ctx, convertor.UserDetailToUserMapper(req.UserDetailInfo)); err != nil { | ||
log.CtxError(ctx, "修改用户信息异常[%v]\n", err) | ||
return resp, err | ||
} | ||
return resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
package consts | ||
|
||
const ( | ||
ID = "_id" | ||
UserId = "userId" | ||
Name = "name" | ||
Type = "type" | ||
Path = "path" | ||
FatherId = "fatherId" | ||
Size = "size" | ||
FileMd5 = "fileMd5" | ||
IsDel = "isDel" | ||
Tag = "tag" | ||
Description = "description" | ||
CreateAt = "createAt" | ||
UpdateAt = "updateAt" | ||
DeletedAt = "deletedAt" | ||
ID = "_id" | ||
UserId = "userId" | ||
Name = "name" | ||
Type = "type" | ||
Path = "path" | ||
FatherId = "fatherId" | ||
Size = "size" | ||
FileMd5 = "fileMd5" | ||
IsDel = "isDel" | ||
Tag = "tag" | ||
Description = "description" | ||
CreateAt = "createAt" | ||
UpdateAt = "updateAt" | ||
DeletedAt = "deletedAt" | ||
TargetId = "targetId" | ||
TargetType = "targetType" | ||
RelationType = "relationType" | ||
DefaultAvatarUrl = "d2042520dce2223751906a11e547d43e.png" | ||
DefaultDescription = "点击添加描述,让大家更好的了解你..." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package convertor | ||
|
||
import ( | ||
usermapper "github.com/CloudStriver/cloudmind-content/biz/infrastructure/mapper/user" | ||
gencontent "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/content" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
func UserMapperToUserDetail(in *usermapper.User) *gencontent.UserDetail { | ||
return &gencontent.UserDetail{ | ||
Name: in.Name, | ||
Sex: in.Sex, | ||
FullName: in.FullName, | ||
IdCard: in.IdCard, | ||
CreatedAt: in.CreateAt.UnixMilli(), | ||
UpdatedAt: in.UpdateAt.UnixMilli(), | ||
Description: in.Description, | ||
Url: in.Url, | ||
UserId: in.ID.Hex(), | ||
} | ||
} | ||
|
||
func UserDetailToUserMapper(in *gencontent.UserDetailInfo) *usermapper.User { | ||
ID, _ := primitive.ObjectIDFromHex(in.UserId) | ||
return &usermapper.User{ | ||
ID: ID, | ||
Name: in.Name, | ||
Sex: int32(in.GetSex()), | ||
FullName: in.FullName, | ||
IdCard: in.IdCard, | ||
Description: in.Description, | ||
Url: in.Url, | ||
} | ||
} | ||
|
||
func UserMapperToUser(in *usermapper.User) *gencontent.User { | ||
return &gencontent.User{ | ||
UserId: in.ID.Hex(), | ||
Name: in.Name, | ||
Url: in.Url, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package user | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/config" | ||
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/consts" | ||
"github.com/CloudStriver/go-pkg/utils/pagination" | ||
"github.com/CloudStriver/go-pkg/utils/pagination/esp" | ||
"github.com/bytedance/sonic" | ||
"github.com/elastic/go-elasticsearch/v8" | ||
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search" | ||
"github.com/elastic/go-elasticsearch/v8/typedapi/types" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/samber/lo" | ||
"github.com/zeromicro/go-zero/core/logx" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"time" | ||
) | ||
|
||
type ( | ||
UserEsMapper interface { | ||
Search(ctx context.Context, keyword string, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*User, int32, error) | ||
} | ||
|
||
EsMapper struct { | ||
es *elasticsearch.TypedClient | ||
IndexName string | ||
} | ||
) | ||
|
||
func (e *EsMapper) Search(ctx context.Context, keyword string, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*User, int32, error) { | ||
p := esp.NewEsPaginator(pagination.NewRawStore(sorter), popts) | ||
s, sa, err := p.MakeSortOptions(ctx) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
res, err := e.es.Search().Index(e.IndexName).Request(&search.Request{ | ||
Query: &types.Query{ | ||
Bool: &types.BoolQuery{ | ||
Must: []types.Query{ | ||
{ | ||
MultiMatch: &types.MultiMatchQuery{ | ||
Fields: []string{consts.Name, consts.ID}, | ||
Query: keyword, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Sort: s, | ||
SearchAfter: sa, | ||
Size: lo.ToPtr(int(*popts.Limit)), | ||
}).Do(ctx) | ||
if err != nil { | ||
logx.Errorf("es查询异常[%v]\n", err) | ||
return nil, 0, err | ||
} | ||
|
||
total := res.Hits.Total.Value | ||
users := make([]*User, 0, len(res.Hits.Hits)) | ||
for _, hit := range res.Hits.Hits { | ||
user := &User{} | ||
source := make(map[string]any) | ||
err = sonic.Unmarshal(hit.Source_, &source) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
if source[consts.CreateAt], err = time.Parse("2006-01-02T15:04:05Z07:00", source[consts.CreateAt].(string)); err != nil { | ||
return nil, 0, err | ||
} | ||
if source[consts.UpdateAt], err = time.Parse("2006-01-02T15:04:05Z07:00", source[consts.UpdateAt].(string)); err != nil { | ||
return nil, 0, err | ||
} | ||
err = mapstructure.Decode(source, user) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
oid := hit.Id_ | ||
user.ID, err = primitive.ObjectIDFromHex(oid) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
user.Score_ = float64(hit.Score_) | ||
users = append(users, user) | ||
} | ||
|
||
if *popts.Backward { | ||
users = lo.Reverse(users) | ||
} | ||
|
||
// 更新游标 | ||
if len(users) > 0 { | ||
err = p.StoreCursor(ctx, users[0], users[len(users)-1]) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
} | ||
return users, int32(total), nil | ||
} | ||
|
||
func NewEsMapper(config *config.Config) UserEsMapper { | ||
es, err := elasticsearch.NewTypedClient(elasticsearch.Config{Addresses: config.Elasticsearch.Addresses, Username: config.Elasticsearch.Username, Password: config.Elasticsearch.Password}) | ||
if err != nil { | ||
logx.Errorf("elasticsearch连接异常[%v]\n", err) | ||
} | ||
return &EsMapper{ | ||
es: es, | ||
IndexName: fmt.Sprintf("%s.%s", config.Mongo.DB, CollectionName), | ||
} | ||
} |
Oops, something went wrong.