-
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
11 changed files
with
682 additions
and
25 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
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,157 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"github.com/google/wire" | ||
"github.com/xh-polaris/gopkg/pagination/esp" | ||
"github.com/xh-polaris/gopkg/pagination/mongop" | ||
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts" | ||
"github.com/xh-polaris/meowchat-content/biz/infrastructure/mapper/plan" | ||
"github.com/xh-polaris/meowchat-content/biz/infrastructure/util/convertor" | ||
"github.com/xh-polaris/service-idl-gen-go/kitex_gen/meowchat/content" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"time" | ||
) | ||
|
||
type IPlanService interface { | ||
ListPlan(ctx context.Context, req *content.ListPlanReq) (*content.ListPlanResp, error) | ||
CountPlan(ctx context.Context, req *content.CountPlanReq) (*content.CountPlanResp, error) | ||
RetrievePlan(ctx context.Context, req *content.RetrievePlanReq) (*content.RetrievePlanResp, error) | ||
CreatePlan(ctx context.Context, req *content.CreatePlanReq) (*content.CreatePlanResp, error) | ||
UpdatePlan(ctx context.Context, req *content.UpdatePlanReq) (*content.UpdatePlanResp, error) | ||
DeletePlan(ctx context.Context, req *content.DeletePlanReq) (*content.DeletePlanResp, error) | ||
} | ||
|
||
type PlanService struct { | ||
PlanMongoMapper plan.IMongoMapper | ||
PlanEsMapper plan.IEsMapper | ||
} | ||
|
||
var PlanSet = wire.NewSet( | ||
wire.Struct(new(PlanService), "*"), | ||
wire.Bind(new(IPlanService), new(*PlanService)), | ||
) | ||
|
||
func (s *PlanService) ListPlan(ctx context.Context, req *content.ListPlanReq) (*content.ListPlanResp, error) { | ||
resp := new(content.ListPlanResp) | ||
var plans []*plan.Plan | ||
var total int64 | ||
var err error | ||
|
||
filter := convertor.ParsePlanFilter(req.FilterOptions) | ||
p := convertor.ParsePagination(req.PaginationOptions) | ||
if req.SearchOptions == nil { | ||
plans, total, err = s.PlanMongoMapper.FindManyAndCount(ctx, filter, p, mongop.IdCursorType) | ||
print(plans) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
switch o := req.SearchOptions.Type.(type) { | ||
case *content.SearchOptions_AllFieldsKey: | ||
plans, total, err = s.PlanEsMapper.Search(ctx, convertor.ConvertPlanAllFieldsSearchQuery(o), filter, p, esp.ScoreCursorType) | ||
case *content.SearchOptions_MultiFieldsKey: | ||
plans, total, err = s.PlanEsMapper.Search(ctx, convertor.ConvertPlanMultiFieldsSearchQuery(o), filter, p, esp.ScoreCursorType) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
resp.Total = total | ||
if p.LastToken != nil { | ||
resp.Token = *p.LastToken | ||
} | ||
resp.Plans = make([]*content.Plan, 0, len(plans)) | ||
for _, Plan_ := range plans { | ||
resp.Plans = append(resp.Plans, convertor.ConvertPlan(Plan_)) | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (s *PlanService) CountPlan(ctx context.Context, req *content.CountPlanReq) (*content.CountPlanResp, error) { | ||
resp := new(content.CountPlanResp) | ||
var err error | ||
filter := convertor.ParsePlanFilter(req.FilterOptions) | ||
if req.SearchOptions == nil { | ||
resp.Total, err = s.PlanMongoMapper.Count(ctx, filter) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
switch o := req.SearchOptions.Type.(type) { | ||
case *content.SearchOptions_AllFieldsKey: | ||
resp.Total, err = s.PlanEsMapper.CountWithQuery(ctx, convertor.ConvertPlanAllFieldsSearchQuery(o), filter) | ||
case *content.SearchOptions_MultiFieldsKey: | ||
resp.Total, err = s.PlanEsMapper.CountWithQuery(ctx, convertor.ConvertPlanMultiFieldsSearchQuery(o), filter) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (s *PlanService) RetrievePlan(ctx context.Context, req *content.RetrievePlanReq) (*content.RetrievePlanResp, error) { | ||
data, err := s.PlanMongoMapper.FindOne(ctx, req.PlanId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m := convertor.ConvertPlan(data) | ||
return &content.RetrievePlanResp{Plan: m}, nil | ||
} | ||
|
||
func (s *PlanService) CreatePlan(ctx context.Context, req *content.CreatePlanReq) (*content.CreatePlanResp, error) { | ||
m := req.Plan | ||
data := &plan.Plan{ | ||
CatId: m.CatId, | ||
PlanType: m.PlanType, | ||
StartTime: time.Unix(m.StartTime, 0), | ||
EndTime: time.Unix(m.EndTime, 0), | ||
Description: m.Description, | ||
ImageUrls: m.ImageUrls, | ||
Name: m.Name, | ||
InitiatorIds: m.InitiatorIds, | ||
} | ||
|
||
err := s.PlanMongoMapper.Insert(ctx, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &content.CreatePlanResp{PlanId: data.ID.Hex()}, nil | ||
} | ||
|
||
func (s *PlanService) UpdatePlan(ctx context.Context, req *content.UpdatePlanReq) (*content.UpdatePlanResp, error) { | ||
m := req.Plan | ||
PlanId, err := primitive.ObjectIDFromHex(m.Id) | ||
if err != nil { | ||
return nil, consts.ErrInvalidObjectId | ||
} | ||
|
||
err = s.PlanMongoMapper.Update(ctx, &plan.Plan{ | ||
ID: PlanId, | ||
CatId: m.CatId, | ||
PlanType: m.PlanType, | ||
StartTime: time.Unix(m.StartTime, 0), | ||
EndTime: time.Unix(m.EndTime, 0), | ||
Description: m.Description, | ||
ImageUrls: m.ImageUrls, | ||
InitiatorIds: m.InitiatorIds, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &content.UpdatePlanResp{}, nil | ||
} | ||
|
||
func (s *PlanService) DeletePlan(ctx context.Context, req *content.DeletePlanReq) (*content.DeletePlanResp, error) { | ||
err := s.PlanMongoMapper.Delete(ctx, req.PlanId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &content.DeletePlanResp{}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
package consts | ||
|
||
const ( | ||
ID = "_id" | ||
CatId = "catId" | ||
CommunityId = "communityId" | ||
Title = "title" | ||
Text = "text" | ||
Tags = "tags" | ||
UserId = "userId" | ||
Flags = "flags" | ||
Area = "area" | ||
Color = "color" | ||
Details = "details" | ||
Name = "name" | ||
Score = "_score" | ||
UpdateAt = "updateAt" | ||
CreateAt = "createAt" | ||
ID = "_id" | ||
CatId = "catId" | ||
CommunityId = "communityId" | ||
Title = "title" | ||
Text = "text" | ||
Description = "description" | ||
Tags = "tags" | ||
UserId = "userId" | ||
Flags = "flags" | ||
Area = "area" | ||
Color = "color" | ||
Details = "details" | ||
Name = "name" | ||
Score = "_score" | ||
UpdateAt = "updateAt" | ||
CreateAt = "createAt" | ||
StartTime = "startTime" | ||
EndTime = "endTime" | ||
InitiatorIds = "initiatorIds" | ||
) |
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,140 @@ | ||
package plan | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/elastic/go-elasticsearch/v8/typedapi/core/count" | ||
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search" | ||
"github.com/elastic/go-elasticsearch/v8/typedapi/types" | ||
"github.com/xh-polaris/gopkg/pagination" | ||
"github.com/xh-polaris/gopkg/pagination/esp" | ||
"github.com/xh-polaris/meowchat-content/biz/infrastructure/config" | ||
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/elastic/go-elasticsearch/v8" | ||
"github.com/mitchellh/mapstructure" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
type ( | ||
IEsMapper interface { | ||
Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*Plan, int64, error) | ||
CountWithQuery(ctx context.Context, query []types.Query, fopts *FilterOptions) (int64, error) | ||
} | ||
|
||
EsMapper struct { | ||
es *elasticsearch.TypedClient | ||
indexName string | ||
} | ||
) | ||
|
||
func NewEsMapper(config *config.Config) IEsMapper { | ||
esClient, err := elasticsearch.NewTypedClient(elasticsearch.Config{ | ||
Username: config.Elasticsearch.Username, | ||
Password: config.Elasticsearch.Password, | ||
Addresses: config.Elasticsearch.Addresses, | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return &EsMapper{ | ||
es: esClient, | ||
indexName: fmt.Sprintf("%s.%s-alias", config.Mongo.DB, CollectionName), | ||
} | ||
} | ||
|
||
func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*Plan, int64, error) { | ||
p := esp.NewEsPaginator(pagination.NewRawStore(sorter), popts) | ||
s, sa, err := p.MakeSortOptions(ctx) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
f := makeEsFilter(fopts) | ||
res, err := m.es.Search().From(int(*popts.Offset)).Size(int(*popts.Limit)).Index(m.indexName).Request(&search.Request{ | ||
Query: &types.Query{ | ||
Bool: &types.BoolQuery{ | ||
Must: query, | ||
Filter: f, | ||
}, | ||
}, | ||
Sort: s, | ||
SearchAfter: sa, | ||
}).Do(ctx) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
hits := res.Hits.Hits | ||
total := res.Hits.Total.Value | ||
datas := make([]*Plan, 0, len(hits)) | ||
for i := range hits { | ||
hit := hits[i] | ||
var source map[string]any | ||
err = json.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 | ||
} | ||
if source[consts.StartTime], err = time.Parse("2006-01-02T15:04:05Z07:00", source[consts.StartTime].(string)); err != nil { | ||
return nil, 0, err | ||
} | ||
if source[consts.EndTime], err = time.Parse("2006-01-02T15:04:05Z07:00", source[consts.EndTime].(string)); err != nil { | ||
return nil, 0, err | ||
} | ||
data := &Plan{} | ||
err = mapstructure.Decode(source, data) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
oid := hit.Id_ | ||
data.ID, err = primitive.ObjectIDFromHex(oid) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
data.Score_ = float64(hit.Score_) | ||
datas = append(datas, data) | ||
} | ||
// 如果是反向查询,反转数据 | ||
if *popts.Backward { | ||
for i := 0; i < len(datas)/2; i++ { | ||
datas[i], datas[len(datas)-i-1] = datas[len(datas)-i-1], datas[i] | ||
} | ||
} | ||
if len(datas) > 0 { | ||
err = p.StoreCursor(ctx, datas[0], datas[len(datas)-1]) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
} | ||
return datas, total, nil | ||
} | ||
|
||
func (m *EsMapper) CountWithQuery(ctx context.Context, query []types.Query, fopts *FilterOptions) (int64, error) { | ||
f := makeEsFilter(fopts) | ||
res, err := m.es.Count().Index(m.indexName).Request(&count.Request{ | ||
Query: &types.Query{ | ||
Bool: &types.BoolQuery{ | ||
Must: query, | ||
Filter: f, | ||
}, | ||
}, | ||
}).Do(ctx) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return res.Count, nil | ||
} |
Oops, something went wrong.