Skip to content

Commit

Permalink
feat: 完善小鱼干功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Cbgogogog committed Nov 15, 2023
1 parent 79756fb commit 7d9bf69
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 55 deletions.
18 changes: 11 additions & 7 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,48 @@ type ContentServerImpl struct {
PlanService service.IPlanService
}

func (s *ContentServerImpl) CountDonateByPlan(ctx context.Context, req *content.CountDonateByPlanReq) (res *content.CountDonateByPlanResp, err error) {
return s.PlanService.CountDonateByPlan(ctx, req)
}

func (s *ContentServerImpl) CountDonateByUser(ctx context.Context, req *content.CountDonateByUserReq) (res *content.CountDonateByUserResp, err error) {
return s.PlanService.CountDonateByUser(ctx, req)
}

func (s *ContentServerImpl) DonateFish(ctx context.Context, req *content.DonateFishReq) (res *content.DonateFishResp, err error) {
return s.PlanService.DonateFish(ctx, req)

}

func (s *ContentServerImpl) AddUserFish(ctx context.Context, req *content.AddUserFishReq) (res *content.AddUserFishResp, err error) {
return s.PlanService.AddUserFish(ctx, req)

}

func (s *ContentServerImpl) ListFishByPlan(ctx context.Context, req *content.ListFishByPlanReq) (res *content.ListFishByPlanResp, err error) {
return s.PlanService.ListFishByPlan(ctx, req)
}

func (s *ContentServerImpl) ListDonateByUser(ctx context.Context, req *content.ListDonateByUserReq) (res *content.ListDonateByUserResp, err error) {
return s.PlanService.ListDonateByUser(ctx, req)
}

func (s *ContentServerImpl) RetrieveUserFish(ctx context.Context, req *content.RetrieveUserFishReq) (res *content.RetrieveUserFishResp, err error) {
return s.PlanService.RetrieveUserFish(ctx, req)

}

func (s *ContentServerImpl) ListPlan(ctx context.Context, req *content.ListPlanReq) (res *content.ListPlanResp, err error) {
return s.PlanService.ListPlan(ctx, req)

}

func (s *ContentServerImpl) CountPlan(ctx context.Context, req *content.CountPlanReq) (res *content.CountPlanResp, err error) {
return s.PlanService.CountPlan(ctx, req)

}

func (s *ContentServerImpl) RetrievePlan(ctx context.Context, req *content.RetrievePlanReq) (res *content.RetrievePlanResp, err error) {
return s.PlanService.RetrievePlan(ctx, req)

}

func (s *ContentServerImpl) CreatePlan(ctx context.Context, req *content.CreatePlanReq) (res *content.CreatePlanResp, err error) {
return s.PlanService.CreatePlan(ctx, req)

}

func (s *ContentServerImpl) UpdatePlan(ctx context.Context, req *content.UpdatePlanReq) (res *content.UpdatePlanResp, err error) {
Expand Down
110 changes: 88 additions & 22 deletions biz/application/service/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package service

import (
"context"
"sort"
"time"

"github.com/apache/rocketmq-client-go/v2"
"github.com/google/wire"
"github.com/xh-polaris/gopkg/pagination/esp"
"github.com/xh-polaris/gopkg/pagination/mongop"
"github.com/xh-polaris/service-idl-gen-go/kitex_gen/meowchat/content"
"github.com/zeromicro/go-zero/core/stores/monc"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"

Expand All @@ -30,7 +30,10 @@ type IPlanService interface {
DonateFish(ctx context.Context, req *content.DonateFishReq) (*content.DonateFishResp, error)
AddUserFish(ctx context.Context, req *content.AddUserFishReq) (*content.AddUserFishResp, error)
ListFishByPlan(ctx context.Context, req *content.ListFishByPlanReq) (*content.ListFishByPlanResp, error)
ListDonateByUser(ctx context.Context, req *content.ListDonateByUserReq) (*content.ListDonateByUserResp, error)
RetrieveUserFish(ctx context.Context, req *content.RetrieveUserFishReq) (*content.RetrieveUserFishResp, error)
CountDonateByPlan(ctx context.Context, req *content.CountDonateByPlanReq) (*content.CountDonateByPlanResp, error)
CountDonateByUser(ctx context.Context, req *content.CountDonateByUserReq) (*content.CountDonateByUserResp, error)
}

type PlanService struct {
Expand Down Expand Up @@ -235,13 +238,32 @@ func (s *PlanService) DonateFish(ctx context.Context, req *content.DonateFishReq
if err != nil {
return err
}

err = s.DonateMongoMapper.Insert(sessionContext, &donate.Donate{
UserId: req.UserId,
PlanId: req.PlanId,
FishNum: req.Fish,
})
if err != nil {
data, err := s.DonateMongoMapper.FindOneById(sessionContext, req.PlanId, req.UserId)
switch err {
case nil:
data.FishNum += req.Fish
err := s.DonateMongoMapper.Update(sessionContext, data)
if err != nil {
err = sessionContext.AbortTransaction(sessionContext)
if err != nil {
return err
}
return err
}
case monc.ErrNotFound:
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
}
default:
err = sessionContext.AbortTransaction(sessionContext)
if err != nil {
return err
Expand Down Expand Up @@ -301,28 +323,52 @@ func (s *PlanService) AddUserFish(ctx context.Context, req *content.AddUserFishR
}

func (s *PlanService) ListFishByPlan(ctx context.Context, req *content.ListFishByPlanReq) (*content.ListFishByPlanResp, error) {
data, err := s.DonateMongoMapper.ListDonateByPlan(ctx, req.PlanId)
resp := new(content.ListFishByPlanResp)

p := convertor.ParsePagination(req.PaginationOptions)
data, total, err := s.DonateMongoMapper.FindManyAndCountByPlanId(ctx, req.PlanId, p)
if err != nil {
return nil, err
}
fishMap := make(map[string]int64, len(data))
userIds := make([]string, 0, len(data))
for _, value := range data {
i, ok := fishMap[value.UserId]
if ok == true {
fishMap[value.UserId] = value.FishNum + i
} else {
fishMap[value.UserId] = value.FishNum
userIds = append(userIds, value.UserId)
fishMap[value.UserId] = value.FishNum
userIds = append(userIds, value.UserId)
}
resp.Total = total
resp.FishMap = fishMap
resp.UserIds = userIds
return resp, nil
}

func (s *PlanService) ListDonateByUser(ctx context.Context, req *content.ListDonateByUserReq) (*content.ListDonateByUserResp, error) {
resp := new(content.ListDonateByUserResp)

p := convertor.ParsePagination(req.PaginationOptions)
data, total, err := s.DonateMongoMapper.FindManyAndCountByUserId(ctx, req.UserId, p, mongop.IdCursorType)
if err != nil {
return nil, err
}

resp.Total = total
if p.LastToken != nil {
resp.Token = *p.LastToken
}
resp.PlanPreviews = make([]*content.PlanPreview, 0)
for _, v := range data {
temp := &content.PlanPreview{}
temp.Id = v.ID.Hex()
temp.DonateNum = v.FishNum
temp.DonateTime = v.CreateAt.Unix()
plan_, err := s.PlanMongoMapper.FindOne(ctx, v.PlanId)
if err == nil {
temp.CatId = plan_.CatId
temp.Name = plan_.Name
}
resp.PlanPreviews = append(resp.PlanPreviews, temp)
}
sort.Slice(userIds, func(i, j int) bool {
return fishMap[userIds[i]] > fishMap[userIds[j]]
})
return &content.ListFishByPlanResp{
UserIds: userIds,
FishMap: fishMap,
}, nil
return resp, nil
}

func (s *PlanService) RetrieveUserFish(ctx context.Context, req *content.RetrieveUserFishReq) (*content.RetrieveUserFishResp, error) {
Expand All @@ -336,3 +382,23 @@ func (s *PlanService) RetrieveUserFish(ctx context.Context, req *content.Retriev
return nil, err
}
}

func (s *PlanService) CountDonateByPlan(ctx context.Context, req *content.CountDonateByPlanReq) (*content.CountDonateByPlanResp, error) {
total, err := s.DonateMongoMapper.CountByPlanId(ctx, req.PlanId)
if err != nil {
return nil, err
}
return &content.CountDonateByPlanResp{
Total: total,
}, nil
}

func (s *PlanService) CountDonateByUser(ctx context.Context, req *content.CountDonateByUserReq) (*content.CountDonateByUserResp, error) {
total, err := s.DonateMongoMapper.CountByUserId(ctx, req.UserId)
if err != nil {
return nil, err
}
return &content.CountDonateByUserResp{
Total: total,
}, nil
}
Loading

0 comments on commit 7d9bf69

Please sign in to comment.