Skip to content

Commit

Permalink
feat: 优化plan,并增加判断每日首次发布内容
Browse files Browse the repository at this point in the history
  • Loading branch information
Cbgogogog committed Aug 20, 2023
1 parent e160adb commit 9024f7f
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 27 deletions.
68 changes: 67 additions & 1 deletion biz/application/service/moment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import (
"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/config"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/mapper/moment"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/util/convertor"
"github.com/xh-polaris/service-idl-gen-go/kitex_gen/meowchat/content"
"github.com/zeromicro/go-zero/core/stores/redis"
"go.mongodb.org/mongo-driver/bson/primitive"
"strconv"
"time"
)

type IMomentService interface {
Expand All @@ -22,8 +26,10 @@ type IMomentService interface {
}

type MomentService struct {
Config *config.Config
MomentMongoMapper moment.IMongoMapper
MomentEsMapper moment.IEsMapper
Redis *redis.Redis
}

var MomentSet = wire.NewSet(
Expand Down Expand Up @@ -102,6 +108,7 @@ func (s *MomentService) RetrieveMoment(ctx context.Context, req *content.Retriev
}

func (s *MomentService) CreateMoment(ctx context.Context, req *content.CreateMomentReq) (*content.CreateMomentResp, error) {
resp := new(content.CreateMomentResp)
m := req.Moment
data := &moment.Moment{
Photos: m.Photos,
Expand All @@ -117,7 +124,66 @@ func (s *MomentService) CreateMoment(ctx context.Context, req *content.CreateMom
return nil, err
}

return &content.CreateMomentResp{MomentId: data.ID.Hex()}, nil
resp.MomentId = data.ID.Hex()
t, err := s.Redis.GetCtx(ctx, "contentTimes"+m.UserId)
if err != nil {
return resp, nil
}
r, err := s.Redis.GetCtx(ctx, "contentDate"+m.UserId)
if err != nil {
return resp, nil
} else if r == "" {
resp.GetFish = true
resp.GetFishTimes = 1
err = s.Redis.SetexCtx(ctx, "contentTimes"+m.UserId, "1", 86400)
if err != nil {
resp.GetFish = false
return resp, nil
}
err = s.Redis.SetexCtx(ctx, "contentDate"+m.UserId, strconv.FormatInt(time.Now().Unix(), 10), 86400)
if err != nil {
resp.GetFish = false
return resp, nil
}
} else {
times, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return resp, nil
}
resp.GetFishTimes = times + 1
date, err := strconv.ParseInt(r, 10, 64)
if err != nil {
return resp, nil
}
lastTime := time.Unix(date, 0)
err = s.Redis.SetexCtx(ctx, "contentTimes"+m.UserId, strconv.FormatInt(times+1, 10), 86400)
if err != nil {
return resp, nil
}
err = s.Redis.SetexCtx(ctx, "contentDate"+m.UserId, strconv.FormatInt(time.Now().Unix(), 10), 86400)
if err != nil {
return resp, nil
}
if lastTime.Day() == time.Now().Day() && lastTime.Month() == time.Now().Month() && lastTime.Year() == time.Now().Year() {
err = s.Redis.SetexCtx(ctx, "contentTimes"+m.UserId, strconv.FormatInt(times+1, 10), 86400)
if err != nil {
return resp, nil
}
if times >= s.Config.GetFishTimes {
resp.GetFish = false
} else {
resp.GetFish = true
}
} else {
err = s.Redis.SetexCtx(ctx, "contentTimes"+m.UserId, "1", 86400)
if err != nil {
return resp, nil
}
resp.GetFish = true
resp.GetFishTimes = 1
}
}
return resp, nil
}

func (s *MomentService) UpdateMoment(ctx context.Context, req *content.UpdateMomentReq) (*content.UpdateMomentResp, error) {
Expand Down
63 changes: 41 additions & 22 deletions biz/application/service/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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"
"go.mongodb.org/mongo-driver/mongo"
"sort"
"time"
)
Expand Down Expand Up @@ -165,33 +166,51 @@ func (s *PlanService) DeletePlan(ctx context.Context, req *content.DeletePlanReq
}

func (s *PlanService) DonateFish(ctx context.Context, req *content.DonateFishReq) (*content.DonateFishResp, error) {
data, err := s.FishMongoMapper.FindOne(ctx, req.UserId)
if err != nil {
return nil, err
}
data.FishNum = data.FishNum - req.Fish
err = s.FishMongoMapper.Update(ctx, data)
dbClient, err := s.FishMongoMapper.StartClient(ctx)
if err != nil {
return nil, err
}

err = s.DonateMongoMapper.Insert(ctx, &donate.Donate{
UserId: req.UserId,
PlanId: req.PlanId,
FishNum: req.Fish,
err = dbClient.UseSession(ctx, func(sessionContext mongo.SessionContext) error {
err = sessionContext.StartTransaction()
if err != nil {
return err
}
err = s.FishMongoMapper.Add(sessionContext, req.UserId, -req.Fish)
if err != nil {
return err
}

err = s.DonateMongoMapper.Insert(sessionContext, &donate.Donate{
UserId: req.UserId,
PlanId: req.PlanId,
FishNum: req.Fish,
})
if err != nil {
err = sessionContext.AbortTransaction(sessionContext)
if err != nil {
return err
}
return err
}
err = s.PlanMongoMapper.Add(sessionContext, req.PlanId, req.Fish)
if err != nil {
err = sessionContext.AbortTransaction(sessionContext)
if err != nil {
return err
}
return err
}
err = sessionContext.CommitTransaction(sessionContext)
if err != nil {
err = sessionContext.AbortTransaction(sessionContext)
if err != nil {
return err
}
return err
}
return nil
})
if err != nil {
return nil, err
}
res, err := s.PlanMongoMapper.FindOne(ctx, req.PlanId)
if err != nil {
return nil, err
}
res.NowFish = res.NowFish + req.Fish
err = s.PlanMongoMapper.Update(ctx, res)
if err != nil {
return nil, err
}

return &content.DonateFishResp{}, nil
}
Expand Down
68 changes: 67 additions & 1 deletion biz/application/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import (
"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/config"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/mapper/post"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/util/convertor"
"github.com/xh-polaris/service-idl-gen-go/kitex_gen/meowchat/content"
"github.com/zeromicro/go-zero/core/stores/redis"
"go.mongodb.org/mongo-driver/bson/primitive"
"strconv"
"time"
)

type IPostService interface {
Expand All @@ -23,8 +27,10 @@ type IPostService interface {
}

type PostService struct {
Config *config.Config
PostMongoMapper post.IMongoMapper
PostEsMapper post.IEsMapper
Redis *redis.Redis
}

var PostSet = wire.NewSet(
Expand All @@ -33,6 +39,7 @@ var PostSet = wire.NewSet(
)

func (s *PostService) CreatePost(ctx context.Context, req *content.CreatePostReq) (*content.CreatePostResp, error) {
resp := new(content.CreatePostResp)
p := &post.Post{
Title: req.Title,
Text: req.Text,
Expand All @@ -44,7 +51,66 @@ func (s *PostService) CreatePost(ctx context.Context, req *content.CreatePostReq
if err != nil {
return nil, err
}
return &content.CreatePostResp{PostId: p.ID.Hex()}, nil
resp.PostId = p.ID.Hex()
data, err := s.Redis.GetCtx(ctx, "contentTimes"+req.UserId)
if err != nil {
return resp, nil
}
r, err := s.Redis.GetCtx(ctx, "contentDate"+req.UserId)
if err != nil {
return resp, nil
} else if r == "" {
resp.GetFish = true
resp.GetFishTimes = 1
err = s.Redis.SetexCtx(ctx, "contentTimes"+req.UserId, "1", 86400)
if err != nil {
resp.GetFish = false
return resp, nil
}
err = s.Redis.SetexCtx(ctx, "contentDate"+req.UserId, strconv.FormatInt(time.Now().Unix(), 10), 86400)
if err != nil {
resp.GetFish = false
return resp, nil
}
} else {
times, err := strconv.ParseInt(data, 10, 64)
if err != nil {
return resp, nil
}
resp.GetFishTimes = times + 1
m, err := strconv.ParseInt(r, 10, 64)
if err != nil {
return resp, nil
}
lastTime := time.Unix(m, 0)
err = s.Redis.SetexCtx(ctx, "contentTimes"+req.UserId, strconv.FormatInt(times+1, 10), 86400)
if err != nil {
return resp, nil
}
err = s.Redis.SetexCtx(ctx, "contentDate"+req.UserId, strconv.FormatInt(time.Now().Unix(), 10), 86400)
if err != nil {
return resp, nil
}
if lastTime.Day() == time.Now().Day() && lastTime.Month() == time.Now().Month() && lastTime.Year() == time.Now().Year() {
err = s.Redis.SetexCtx(ctx, "contentTimes"+req.UserId, strconv.FormatInt(times+1, 10), 86400)
if err != nil {
return resp, nil
}
if times >= s.Config.GetFishTimes {
resp.GetFish = false
} else {
resp.GetFish = true
}
} else {
err = s.Redis.SetexCtx(ctx, "contentTimes"+req.UserId, "1", 86400)
if err != nil {
return resp, nil
}
resp.GetFish = true
resp.GetFishTimes = 1
}
}
return resp, nil
}

func (s *PostService) RetrievePost(ctx context.Context, req *content.RetrievePostReq) (*content.RetrievePostResp, error) {
Expand Down
3 changes: 3 additions & 0 deletions biz/infrastructure/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/redis"
"os"
)

Expand All @@ -22,6 +23,8 @@ type Config struct {
}
Cache cache.CacheConf
Elasticsearch ElasticsearchConf
Redis *redis.RedisConf
GetFishTimes int64
}

func NewConfig() (*Config, error) {
Expand Down
16 changes: 16 additions & 0 deletions biz/infrastructure/mapper/fish/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fish
import (
"context"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts"
"go.mongodb.org/mongo-driver/mongo"
"time"

"github.com/xh-polaris/meowchat-content/biz/infrastructure/config"
Expand All @@ -22,6 +23,8 @@ type (
FindOne(ctx context.Context, id string) (*Fish, error)
Update(ctx context.Context, data *Fish) error
Delete(ctx context.Context, id string) error
Add(ctx context.Context, id string, add int64) error
StartClient(ctx context.Context) (*mongo.Client, error)
}

MongoMapper struct {
Expand Down Expand Up @@ -86,3 +89,16 @@ func (m *MongoMapper) Delete(ctx context.Context, id string) error {
_, err = m.conn.DeleteOne(ctx, key, bson.M{consts.ID: oid})
return err
}

func (m *MongoMapper) Add(ctx context.Context, id string, add int64) error {
key := prefixFishCacheKey + id
filter := bson.M{consts.ID: id}
update := bson.M{"$inc": bson.M{consts.FishNum: add}, "$set": bson.M{consts.UpdateAt: time.Now()}}
_, err := m.conn.UpdateOne(ctx, key, filter, update)
return err
}

func (m *MongoMapper) StartClient(ctx context.Context) (*mongo.Client, error) {
client := m.conn.Database().Client()
return client, nil
}
9 changes: 9 additions & 0 deletions biz/infrastructure/mapper/plan/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
FindOne(ctx context.Context, id string) (*Plan, error)
Update(ctx context.Context, data *Plan) error
Delete(ctx context.Context, id string) error
Add(ctx context.Context, id string, add int64) error
FindMany(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Plan, error)
Count(ctx context.Context, filter *FilterOptions) (int64, error)
FindManyAndCount(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Plan, int64, error)
Expand Down Expand Up @@ -181,3 +182,11 @@ func (m *MongoMapper) Delete(ctx context.Context, id string) error {
_, err = m.conn.DeleteOne(ctx, key, bson.M{consts.ID: oid})
return err
}

func (m *MongoMapper) Add(ctx context.Context, id string, add int64) error {
key := prefixPlanCacheKey + id
filter := bson.M{consts.ID: id}
update := bson.M{"$inc": bson.M{consts.FishNum: add}, "$set": bson.M{consts.UpdateAt: time.Now()}}
_, err := m.conn.UpdateOne(ctx, key, filter, update)
return err
}
10 changes: 10 additions & 0 deletions biz/infrastructure/stores/redis/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package redis

import (
"github.com/xh-polaris/meowchat-content/biz/infrastructure/config"
"github.com/zeromicro/go-zero/core/stores/redis"
)

func NewRedis(config *config.Config) *redis.Redis {
return redis.MustNewRedis(*config.Redis)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/kitex-contrib/obs-opentelemetry v0.2.3
github.com/mitchellh/mapstructure v1.1.2
github.com/xh-polaris/gopkg v0.0.0-20230805125627-ce41556a654d
github.com/xh-polaris/service-idl-gen-go v0.0.0-20230813101042-26c6385e25a8
github.com/xh-polaris/service-idl-gen-go v0.0.0-20230819155341-4a3ac3fe3b89
github.com/zeromicro/go-zero v1.5.4
go.mongodb.org/mongo-driver v1.12.0
google.golang.org/grpc v1.56.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/xh-polaris/gopkg v0.0.0-20230805125627-ce41556a654d h1:xAbXPYbhcqB9/NOnmwsGcjhTVQzRd0xP7jJpGTXmOHM=
github.com/xh-polaris/gopkg v0.0.0-20230805125627-ce41556a654d/go.mod h1:eFkuwj6uq1k4hbDa66TijCWGL4PrNeWaNmAnxfWDeU0=
github.com/xh-polaris/service-idl-gen-go v0.0.0-20230813101042-26c6385e25a8 h1:us6cv8ashLDusy5qj4/9rS9E/pP3lFfpcFtdE22G+9Y=
github.com/xh-polaris/service-idl-gen-go v0.0.0-20230813101042-26c6385e25a8/go.mod h1:KjBt4ZOfugCsdAbFlrniKMDrpJAJobm8KEezTvKVnJM=
github.com/xh-polaris/service-idl-gen-go v0.0.0-20230819155341-4a3ac3fe3b89 h1:LxD9sZVUqcdi+6npBUvxm5NaNLLJlPLpWNmXNFDqWfE=
github.com/xh-polaris/service-idl-gen-go v0.0.0-20230819155341-4a3ac3fe3b89/go.mod h1:KjBt4ZOfugCsdAbFlrniKMDrpJAJobm8KEezTvKVnJM=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
Loading

0 comments on commit 9024f7f

Please sign in to comment.