Merge pull request #54 from alimy/pr-dao-abstract

optimize: abstract backend process logic to service interface
pull/57/head
ROC 3 years ago committed by GitHub
commit 804bb50bae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,6 @@
package core
// CacheService cache service interface that implement base Redis or other
type CacheService interface {
// TODO
}

@ -0,0 +1,72 @@
package core
import (
"github.com/rocboss/paopao-ce/internal/model"
)
// DataService data service interface that process data related logic on database
type DataService interface {
WalletService
SearchService
StorageService
GetComments(conditions *model.ConditionsT, offset, limit int) ([]*model.Comment, error)
GetCommentByID(id int64) (*model.Comment, error)
DeleteComment(comment *model.Comment) error
GetCommentCount(conditions *model.ConditionsT) (int64, error)
CreateComment(comment *model.Comment) (*model.Comment, error)
CreateCommentReply(reply *model.CommentReply) (*model.CommentReply, error)
GetCommentReplyByID(id int64) (*model.CommentReply, error)
DeleteCommentReply(reply *model.CommentReply) error
GetCommentContentsByIDs(ids []int64) ([]*model.CommentContent, error)
GetCommentRepliesByID(ids []int64) ([]*model.CommentReplyFormated, error)
CreateCommentContent(content *model.CommentContent) (*model.CommentContent, error)
CreateMessage(msg *model.Message) (*model.Message, error)
GetUnreadCount(userID int64) (int64, error)
GetMessageByID(id int64) (*model.Message, error)
ReadMessage(message *model.Message) error
GetMessages(conditions *model.ConditionsT, offset, limit int) ([]*model.MessageFormated, error)
GetMessageCount(conditions *model.ConditionsT) (int64, error)
CreatePost(post *model.Post) (*model.Post, error)
DeletePost(post *model.Post) error
LockPost(post *model.Post) error
StickPost(post *model.Post) error
GetPostByID(id int64) (*model.Post, error)
GetPosts(conditions *model.ConditionsT, offset, limit int) ([]*model.Post, error)
GetPostCount(conditions *model.ConditionsT) (int64, error)
UpdatePost(post *model.Post) error
GetUserPostStar(postID, userID int64) (*model.PostStar, error)
GetUserPostStars(userID int64, offset, limit int) ([]*model.PostStar, error)
GetUserPostStarCount(userID int64) (int64, error)
CreatePostStar(postID, userID int64) (*model.PostStar, error)
DeletePostStar(p *model.PostStar) error
GetUserPostCollection(postID, userID int64) (*model.PostCollection, error)
GetUserPostCollections(userID int64, offset, limit int) ([]*model.PostCollection, error)
GetUserPostCollectionCount(userID int64) (int64, error)
GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStatement, error)
GetUserWalletBillCount(userID int64) (int64, error)
CreatePostCollection(postID, userID int64) (*model.PostCollection, error)
DeletePostCollection(p *model.PostCollection) error
GetPostAttatchmentBill(postID, userID int64) (*model.PostAttachmentBill, error)
CreatePostContent(content *model.PostContent) (*model.PostContent, error)
GetPostContentsByIDs(ids []int64) ([]*model.PostContent, error)
GetPostContentByID(id int64) (*model.PostContent, error)
CreateTag(tag *model.Tag) (*model.Tag, error)
DeleteTag(tag *model.Tag) error
GetTags(conditions *model.ConditionsT, offset, limit int) ([]*model.Tag, error)
GetUserByID(id int64) (*model.User, error)
GetUserByUsername(username string) (*model.User, error)
GetUserByPhone(phone string) (*model.User, error)
GetUsersByIDs(ids []int64) ([]*model.User, error)
GetUsersByKeyword(keyword string) ([]*model.User, error)
GetTagsByKeyword(keyword string) ([]*model.Tag, error)
CreateUser(user *model.User) (*model.User, error)
UpdateUser(user *model.User) error
GetLatestPhoneCaptcha(phone string) (*model.Captcha, error)
UsePhoneCaptcha(captcha *model.Captcha) error
SendPhoneCaptcha(phone string) error
}

@ -0,0 +1,27 @@
package core
import (
"github.com/rocboss/paopao-ce/pkg/zinc"
)
const (
SearchTypeDefault SearchType = "search"
SearchTypeTag SearchType = "tag"
)
type SearchType string
type QueryT struct {
Query string
Type SearchType
}
// SearchService search service interface that implement base zinc
type SearchService interface {
CreateSearchIndex(indexName string)
BulkPushDoc(data []map[string]interface{}) (bool, error)
DelDoc(indexName, id string) error
QueryAll(q *QueryT, indexName string, offset, limit int) (*zinc.QueryResultT, error)
QuerySearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error)
QueryTagSearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error)
}

@ -0,0 +1,10 @@
package core
import (
"github.com/rocboss/paopao-ce/internal/model"
)
// StorageService storage service interface that implement base AliOSS、MINIO or other
type StorageService interface {
CreateAttachment(attachment *model.Attachment) (*model.Attachment, error)
}

@ -0,0 +1,13 @@
package core
import (
"github.com/rocboss/paopao-ce/internal/model"
)
// WalletService wallet service interface
type WalletService interface {
GetRechargeByID(id int64) (*model.WalletRecharge, error)
CreateRecharge(userId, amount int64) (*model.WalletRecharge, error)
HandleRechargeSuccess(recharge *model.WalletRecharge, tradeNo string) error
HandlePostAttachmentBought(post *model.Post, user *model.User) error
}

@ -2,6 +2,6 @@ package dao
import "github.com/rocboss/paopao-ce/internal/model" import "github.com/rocboss/paopao-ce/internal/model"
func (d *Dao) CreateAttachment(attachment *model.Attachment) (*model.Attachment, error) { func (d *dataServant) CreateAttachment(attachment *model.Attachment) (*model.Attachment, error) {
return attachment.Create(d.engine) return attachment.Create(d.engine)
} }

@ -2,11 +2,11 @@ package dao
import "github.com/rocboss/paopao-ce/internal/model" import "github.com/rocboss/paopao-ce/internal/model"
func (d *Dao) GetComments(conditions *model.ConditionsT, offset, limit int) ([]*model.Comment, error) { func (d *dataServant) GetComments(conditions *model.ConditionsT, offset, limit int) ([]*model.Comment, error) {
return (&model.Comment{}).List(d.engine, conditions, offset, limit) return (&model.Comment{}).List(d.engine, conditions, offset, limit)
} }
func (d *Dao) GetCommentByID(id int64) (*model.Comment, error) { func (d *dataServant) GetCommentByID(id int64) (*model.Comment, error) {
comment := &model.Comment{ comment := &model.Comment{
Model: &model.Model{ Model: &model.Model{
ID: id, ID: id,
@ -15,23 +15,23 @@ func (d *Dao) GetCommentByID(id int64) (*model.Comment, error) {
return comment.Get(d.engine) return comment.Get(d.engine)
} }
func (d *Dao) DeleteComment(comment *model.Comment) error { func (d *dataServant) DeleteComment(comment *model.Comment) error {
return comment.Delete(d.engine) return comment.Delete(d.engine)
} }
func (d *Dao) GetCommentCount(conditions *model.ConditionsT) (int64, error) { func (d *dataServant) GetCommentCount(conditions *model.ConditionsT) (int64, error) {
return (&model.Comment{}).Count(d.engine, conditions) return (&model.Comment{}).Count(d.engine, conditions)
} }
func (d *Dao) CreateComment(comment *model.Comment) (*model.Comment, error) { func (d *dataServant) CreateComment(comment *model.Comment) (*model.Comment, error) {
return comment.Create(d.engine) return comment.Create(d.engine)
} }
func (d *Dao) CreateCommentReply(reply *model.CommentReply) (*model.CommentReply, error) { func (d *dataServant) CreateCommentReply(reply *model.CommentReply) (*model.CommentReply, error) {
return reply.Create(d.engine) return reply.Create(d.engine)
} }
func (d *Dao) GetCommentReplyByID(id int64) (*model.CommentReply, error) { func (d *dataServant) GetCommentReplyByID(id int64) (*model.CommentReply, error) {
reply := &model.CommentReply{ reply := &model.CommentReply{
Model: &model.Model{ Model: &model.Model{
ID: id, ID: id,
@ -40,6 +40,6 @@ func (d *Dao) GetCommentReplyByID(id int64) (*model.CommentReply, error) {
return reply.Get(d.engine) return reply.Get(d.engine)
} }
func (d *Dao) DeleteCommentReply(reply *model.CommentReply) error { func (d *dataServant) DeleteCommentReply(reply *model.CommentReply) error {
return reply.Delete(d.engine) return reply.Delete(d.engine)
} }

@ -2,13 +2,13 @@ package dao
import "github.com/rocboss/paopao-ce/internal/model" import "github.com/rocboss/paopao-ce/internal/model"
func (d *Dao) GetCommentContentsByIDs(ids []int64) ([]*model.CommentContent, error) { func (d *dataServant) GetCommentContentsByIDs(ids []int64) ([]*model.CommentContent, error) {
commentContent := &model.CommentContent{} commentContent := &model.CommentContent{}
return commentContent.List(d.engine, &model.ConditionsT{ return commentContent.List(d.engine, &model.ConditionsT{
"comment_id IN ?": ids, "comment_id IN ?": ids,
}, 0, 0) }, 0, 0)
} }
func (d *Dao) GetCommentRepliesByID(ids []int64) ([]*model.CommentReplyFormated, error) { func (d *dataServant) GetCommentRepliesByID(ids []int64) ([]*model.CommentReplyFormated, error) {
CommentReply := &model.CommentReply{} CommentReply := &model.CommentReply{}
replies, err := CommentReply.List(d.engine, &model.ConditionsT{ replies, err := CommentReply.List(d.engine, &model.ConditionsT{
"comment_id IN ?": ids, "comment_id IN ?": ids,
@ -45,6 +45,6 @@ func (d *Dao) GetCommentRepliesByID(ids []int64) ([]*model.CommentReplyFormated,
return repliesFormated, nil return repliesFormated, nil
} }
func (d *Dao) CreateCommentContent(content *model.CommentContent) (*model.CommentContent, error) { func (d *dataServant) CreateCommentContent(content *model.CommentContent) (*model.CommentContent, error) {
return content.Create(d.engine) return content.Create(d.engine)
} }

@ -1,17 +1,22 @@
package dao package dao
import ( import (
"github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/pkg/zinc" "github.com/rocboss/paopao-ce/pkg/zinc"
"gorm.io/gorm" "gorm.io/gorm"
) )
type Dao struct { var (
_ core.DataService = (*dataServant)(nil)
)
type dataServant struct {
engine *gorm.DB engine *gorm.DB
zinc *zinc.ZincClient zinc *zinc.ZincClient
} }
func New(engine *gorm.DB, zinc *zinc.ZincClient) *Dao { func NewDataService(engine *gorm.DB, zinc *zinc.ZincClient) core.DataService {
return &Dao{ return &dataServant{
engine: engine, engine: engine,
zinc: zinc, zinc: zinc,
} }

@ -2,18 +2,18 @@ package dao
import "github.com/rocboss/paopao-ce/internal/model" import "github.com/rocboss/paopao-ce/internal/model"
func (d *Dao) CreateMessage(msg *model.Message) (*model.Message, error) { func (d *dataServant) CreateMessage(msg *model.Message) (*model.Message, error) {
return msg.Create(d.engine) return msg.Create(d.engine)
} }
func (d *Dao) GetUnreadCount(userID int64) (int64, error) { func (d *dataServant) GetUnreadCount(userID int64) (int64, error) {
return (&model.Message{}).Count(d.engine, &model.ConditionsT{ return (&model.Message{}).Count(d.engine, &model.ConditionsT{
"receiver_user_id": userID, "receiver_user_id": userID,
"is_read": model.MSG_UNREAD, "is_read": model.MSG_UNREAD,
}) })
} }
func (d *Dao) GetMessageByID(id int64) (*model.Message, error) { func (d *dataServant) GetMessageByID(id int64) (*model.Message, error) {
return (&model.Message{ return (&model.Message{
Model: &model.Model{ Model: &model.Model{
ID: id, ID: id,
@ -21,12 +21,12 @@ func (d *Dao) GetMessageByID(id int64) (*model.Message, error) {
}).Get(d.engine) }).Get(d.engine)
} }
func (d *Dao) ReadMessage(message *model.Message) error { func (d *dataServant) ReadMessage(message *model.Message) error {
message.IsRead = 1 message.IsRead = 1
return message.Update(d.engine) return message.Update(d.engine)
} }
func (d *Dao) GetMessages(conditions *model.ConditionsT, offset, limit int) ([]*model.MessageFormated, error) { func (d *dataServant) GetMessages(conditions *model.ConditionsT, offset, limit int) ([]*model.MessageFormated, error) {
messages, err := (&model.Message{}).List(d.engine, conditions, offset, limit) messages, err := (&model.Message{}).List(d.engine, conditions, offset, limit)
if err != nil { if err != nil {
return nil, err return nil, err
@ -41,6 +41,6 @@ func (d *Dao) GetMessages(conditions *model.ConditionsT, offset, limit int) ([]*
return mfs, nil return mfs, nil
} }
func (d *Dao) GetMessageCount(conditions *model.ConditionsT) (int64, error) { func (d *dataServant) GetMessageCount(conditions *model.ConditionsT) (int64, error) {
return (&model.Message{}).Count(d.engine, conditions) return (&model.Message{}).Count(d.engine, conditions)
} }

@ -6,26 +6,26 @@ import (
"github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/internal/model"
) )
func (d *Dao) CreatePost(post *model.Post) (*model.Post, error) { func (d *dataServant) CreatePost(post *model.Post) (*model.Post, error) {
post.LatestRepliedOn = time.Now().Unix() post.LatestRepliedOn = time.Now().Unix()
return post.Create(d.engine) return post.Create(d.engine)
} }
func (d *Dao) DeletePost(post *model.Post) error { func (d *dataServant) DeletePost(post *model.Post) error {
return post.Delete(d.engine) return post.Delete(d.engine)
} }
func (d *Dao) LockPost(post *model.Post) error { func (d *dataServant) LockPost(post *model.Post) error {
post.IsLock = 1 - post.IsLock post.IsLock = 1 - post.IsLock
return post.Update(d.engine) return post.Update(d.engine)
} }
func (d *Dao) StickPost(post *model.Post) error { func (d *dataServant) StickPost(post *model.Post) error {
post.IsTop = 1 - post.IsTop post.IsTop = 1 - post.IsTop
return post.Update(d.engine) return post.Update(d.engine)
} }
func (d *Dao) GetPostByID(id int64) (*model.Post, error) { func (d *dataServant) GetPostByID(id int64) (*model.Post, error) {
post := &model.Post{ post := &model.Post{
Model: &model.Model{ Model: &model.Model{
ID: id, ID: id,
@ -34,19 +34,19 @@ func (d *Dao) GetPostByID(id int64) (*model.Post, error) {
return post.Get(d.engine) return post.Get(d.engine)
} }
func (d *Dao) GetPosts(conditions *model.ConditionsT, offset, limit int) ([]*model.Post, error) { func (d *dataServant) GetPosts(conditions *model.ConditionsT, offset, limit int) ([]*model.Post, error) {
return (&model.Post{}).List(d.engine, conditions, offset, limit) return (&model.Post{}).List(d.engine, conditions, offset, limit)
} }
func (d *Dao) GetPostCount(conditions *model.ConditionsT) (int64, error) { func (d *dataServant) GetPostCount(conditions *model.ConditionsT) (int64, error) {
return (&model.Post{}).Count(d.engine, conditions) return (&model.Post{}).Count(d.engine, conditions)
} }
func (d *Dao) UpdatePost(post *model.Post) error { func (d *dataServant) UpdatePost(post *model.Post) error {
return post.Update(d.engine) return post.Update(d.engine)
} }
func (d *Dao) GetUserPostStar(postID, userID int64) (*model.PostStar, error) { func (d *dataServant) GetUserPostStar(postID, userID int64) (*model.PostStar, error) {
star := &model.PostStar{ star := &model.PostStar{
PostID: postID, PostID: postID,
UserID: userID, UserID: userID,
@ -55,7 +55,7 @@ func (d *Dao) GetUserPostStar(postID, userID int64) (*model.PostStar, error) {
return star.Get(d.engine) return star.Get(d.engine)
} }
func (d *Dao) GetUserPostStars(userID int64, offset, limit int) ([]*model.PostStar, error) { func (d *dataServant) GetUserPostStars(userID int64, offset, limit int) ([]*model.PostStar, error) {
star := &model.PostStar{ star := &model.PostStar{
UserID: userID, UserID: userID,
} }
@ -65,14 +65,14 @@ func (d *Dao) GetUserPostStars(userID int64, offset, limit int) ([]*model.PostSt
}, offset, limit) }, offset, limit)
} }
func (d *Dao) GetUserPostStarCount(userID int64) (int64, error) { func (d *dataServant) GetUserPostStarCount(userID int64) (int64, error) {
star := &model.PostStar{ star := &model.PostStar{
UserID: userID, UserID: userID,
} }
return star.Count(d.engine, &model.ConditionsT{}) return star.Count(d.engine, &model.ConditionsT{})
} }
func (d *Dao) CreatePostStar(postID, userID int64) (*model.PostStar, error) { func (d *dataServant) CreatePostStar(postID, userID int64) (*model.PostStar, error) {
star := &model.PostStar{ star := &model.PostStar{
PostID: postID, PostID: postID,
UserID: userID, UserID: userID,
@ -81,11 +81,11 @@ func (d *Dao) CreatePostStar(postID, userID int64) (*model.PostStar, error) {
return star.Create(d.engine) return star.Create(d.engine)
} }
func (d *Dao) DeletePostStar(p *model.PostStar) error { func (d *dataServant) DeletePostStar(p *model.PostStar) error {
return p.Delete(d.engine) return p.Delete(d.engine)
} }
func (d *Dao) GetUserPostCollection(postID, userID int64) (*model.PostCollection, error) { func (d *dataServant) GetUserPostCollection(postID, userID int64) (*model.PostCollection, error) {
star := &model.PostCollection{ star := &model.PostCollection{
PostID: postID, PostID: postID,
UserID: userID, UserID: userID,
@ -94,7 +94,7 @@ func (d *Dao) GetUserPostCollection(postID, userID int64) (*model.PostCollection
return star.Get(d.engine) return star.Get(d.engine)
} }
func (d *Dao) GetUserPostCollections(userID int64, offset, limit int) ([]*model.PostCollection, error) { func (d *dataServant) GetUserPostCollections(userID int64, offset, limit int) ([]*model.PostCollection, error) {
collection := &model.PostCollection{ collection := &model.PostCollection{
UserID: userID, UserID: userID,
} }
@ -104,13 +104,13 @@ func (d *Dao) GetUserPostCollections(userID int64, offset, limit int) ([]*model.
}, offset, limit) }, offset, limit)
} }
func (d *Dao) GetUserPostCollectionCount(userID int64) (int64, error) { func (d *dataServant) GetUserPostCollectionCount(userID int64) (int64, error) {
collection := &model.PostCollection{ collection := &model.PostCollection{
UserID: userID, UserID: userID,
} }
return collection.Count(d.engine, &model.ConditionsT{}) return collection.Count(d.engine, &model.ConditionsT{})
} }
func (d *Dao) GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStatement, error) { func (d *dataServant) GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStatement, error) {
statement := &model.WalletStatement{ statement := &model.WalletStatement{
UserID: userID, UserID: userID,
} }
@ -120,14 +120,14 @@ func (d *Dao) GetUserWalletBills(userID int64, offset, limit int) ([]*model.Wall
}, offset, limit) }, offset, limit)
} }
func (d *Dao) GetUserWalletBillCount(userID int64) (int64, error) { func (d *dataServant) GetUserWalletBillCount(userID int64) (int64, error) {
statement := &model.WalletStatement{ statement := &model.WalletStatement{
UserID: userID, UserID: userID,
} }
return statement.Count(d.engine, &model.ConditionsT{}) return statement.Count(d.engine, &model.ConditionsT{})
} }
func (d *Dao) CreatePostCollection(postID, userID int64) (*model.PostCollection, error) { func (d *dataServant) CreatePostCollection(postID, userID int64) (*model.PostCollection, error) {
collection := &model.PostCollection{ collection := &model.PostCollection{
PostID: postID, PostID: postID,
UserID: userID, UserID: userID,
@ -136,11 +136,11 @@ func (d *Dao) CreatePostCollection(postID, userID int64) (*model.PostCollection,
return collection.Create(d.engine) return collection.Create(d.engine)
} }
func (d *Dao) DeletePostCollection(p *model.PostCollection) error { func (d *dataServant) DeletePostCollection(p *model.PostCollection) error {
return p.Delete(d.engine) return p.Delete(d.engine)
} }
func (d *Dao) GetPostAttatchmentBill(postID, userID int64) (*model.PostAttachmentBill, error) { func (d *dataServant) GetPostAttatchmentBill(postID, userID int64) (*model.PostAttachmentBill, error) {
bill := &model.PostAttachmentBill{ bill := &model.PostAttachmentBill{
PostID: postID, PostID: postID,
UserID: userID, UserID: userID,

@ -2,18 +2,18 @@ package dao
import "github.com/rocboss/paopao-ce/internal/model" import "github.com/rocboss/paopao-ce/internal/model"
func (d *Dao) CreatePostContent(content *model.PostContent) (*model.PostContent, error) { func (d *dataServant) CreatePostContent(content *model.PostContent) (*model.PostContent, error) {
return content.Create(d.engine) return content.Create(d.engine)
} }
func (d *Dao) GetPostContentsByIDs(ids []int64) ([]*model.PostContent, error) { func (d *dataServant) GetPostContentsByIDs(ids []int64) ([]*model.PostContent, error) {
return (&model.PostContent{}).List(d.engine, &model.ConditionsT{ return (&model.PostContent{}).List(d.engine, &model.ConditionsT{
"post_id IN ?": ids, "post_id IN ?": ids,
"ORDER": "sort ASC", "ORDER": "sort ASC",
}, 0, 0) }, 0, 0)
} }
func (d *Dao) GetPostContentByID(id int64) (*model.PostContent, error) { func (d *dataServant) GetPostContentByID(id int64) (*model.PostContent, error) {
return (&model.PostContent{ return (&model.PostContent{
Model: &model.Model{ Model: &model.Model{
ID: id, ID: id,

@ -1,20 +1,11 @@
package dao package dao
import ( import (
"github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/pkg/zinc" "github.com/rocboss/paopao-ce/pkg/zinc"
) )
type SearchType string func (d *dataServant) CreateSearchIndex(indexName string) {
const SearchTypeDefault SearchType = "search"
const SearchTypeTag SearchType = "tag"
type QueryT struct {
Query string
Type SearchType
}
func (d *Dao) CreateSearchIndex(indexName string) {
// 不存在则创建索引 // 不存在则创建索引
d.zinc.CreateIndex(indexName, &zinc.ZincIndexProperty{ d.zinc.CreateIndex(indexName, &zinc.ZincIndexProperty{
"id": &zinc.ZincIndexPropertyT{ "id": &zinc.ZincIndexPropertyT{
@ -104,21 +95,21 @@ func (d *Dao) CreateSearchIndex(indexName string) {
} }
func (d *Dao) BulkPushDoc(data []map[string]interface{}) (bool, error) { func (d *dataServant) BulkPushDoc(data []map[string]interface{}) (bool, error) {
return d.zinc.BulkPushDoc(data) return d.zinc.BulkPushDoc(data)
} }
func (d *Dao) DelDoc(indexName, id string) error { func (d *dataServant) DelDoc(indexName, id string) error {
return d.zinc.DelDoc(indexName, id) return d.zinc.DelDoc(indexName, id)
} }
func (d *Dao) QueryAll(q *QueryT, indexName string, offset, limit int) (*zinc.QueryResultT, error) { func (d *dataServant) QueryAll(q *core.QueryT, indexName string, offset, limit int) (*zinc.QueryResultT, error) {
// 普通搜索 // 普通搜索
if q.Type == SearchTypeDefault && q.Query != "" { if q.Type == core.SearchTypeDefault && q.Query != "" {
return d.QuerySearch(indexName, q.Query, offset, limit) return d.QuerySearch(indexName, q.Query, offset, limit)
} }
// Tag分类 // Tag分类
if q.Type == SearchTypeTag && q.Query != "" { if q.Type == core.SearchTypeTag && q.Query != "" {
return d.QueryTagSearch(indexName, q.Query, offset, limit) return d.QueryTagSearch(indexName, q.Query, offset, limit)
} }
@ -138,7 +129,7 @@ func (d *Dao) QueryAll(q *QueryT, indexName string, offset, limit int) (*zinc.Qu
return rsp, err return rsp, err
} }
func (d *Dao) QuerySearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error) { func (d *dataServant) QuerySearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error) {
rsp, err := d.zinc.EsQuery(indexName, map[string]interface{}{ rsp, err := d.zinc.EsQuery(indexName, map[string]interface{}{
"query": map[string]interface{}{ "query": map[string]interface{}{
"match_phrase": map[string]interface{}{ "match_phrase": map[string]interface{}{
@ -156,7 +147,7 @@ func (d *Dao) QuerySearch(indexName, query string, offset, limit int) (*zinc.Que
return rsp, err return rsp, err
} }
func (d *Dao) QueryTagSearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error) { func (d *dataServant) QueryTagSearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error) {
rsp, err := d.zinc.ApiQuery(indexName, map[string]interface{}{ rsp, err := d.zinc.ApiQuery(indexName, map[string]interface{}{
"search_type": "querystring", "search_type": "querystring",
"query": map[string]interface{}{ "query": map[string]interface{}{

@ -2,7 +2,7 @@ package dao
import "github.com/rocboss/paopao-ce/internal/model" import "github.com/rocboss/paopao-ce/internal/model"
func (d *Dao) CreateTag(tag *model.Tag) (*model.Tag, error) { func (d *dataServant) CreateTag(tag *model.Tag) (*model.Tag, error) {
t, err := tag.Get(d.engine) t, err := tag.Get(d.engine)
if err != nil { if err != nil {
tag.QuoteNum = 1 tag.QuoteNum = 1
@ -20,7 +20,7 @@ func (d *Dao) CreateTag(tag *model.Tag) (*model.Tag, error) {
return t, nil return t, nil
} }
func (d *Dao) DeleteTag(tag *model.Tag) error { func (d *dataServant) DeleteTag(tag *model.Tag) error {
tag, err := tag.Get(d.engine) tag, err := tag.Get(d.engine)
if err != nil { if err != nil {
return err return err
@ -30,6 +30,6 @@ func (d *Dao) DeleteTag(tag *model.Tag) error {
return tag.Update(d.engine) return tag.Update(d.engine)
} }
func (d *Dao) GetTags(conditions *model.ConditionsT, offset, limit int) ([]*model.Tag, error) { func (d *dataServant) GetTags(conditions *model.ConditionsT, offset, limit int) ([]*model.Tag, error) {
return (&model.Tag{}).List(d.engine, conditions, offset, limit) return (&model.Tag{}).List(d.engine, conditions, offset, limit)
} }

@ -21,7 +21,7 @@ type JuhePhoneCaptchaRsp struct {
} }
// 根据用户ID获取用户 // 根据用户ID获取用户
func (d *Dao) GetUserByID(id int64) (*model.User, error) { func (d *dataServant) GetUserByID(id int64) (*model.User, error) {
user := &model.User{ user := &model.User{
Model: &model.Model{ Model: &model.Model{
ID: id, ID: id,
@ -32,7 +32,7 @@ func (d *Dao) GetUserByID(id int64) (*model.User, error) {
} }
// 根据用户名获取用户 // 根据用户名获取用户
func (d *Dao) GetUserByUsername(username string) (*model.User, error) { func (d *dataServant) GetUserByUsername(username string) (*model.User, error) {
user := &model.User{ user := &model.User{
Username: username, Username: username,
} }
@ -41,7 +41,7 @@ func (d *Dao) GetUserByUsername(username string) (*model.User, error) {
} }
// 根据手机号获取用户 // 根据手机号获取用户
func (d *Dao) GetUserByPhone(phone string) (*model.User, error) { func (d *dataServant) GetUserByPhone(phone string) (*model.User, error) {
user := &model.User{ user := &model.User{
Phone: phone, Phone: phone,
} }
@ -50,7 +50,7 @@ func (d *Dao) GetUserByPhone(phone string) (*model.User, error) {
} }
// 根据IDs获取用户列表 // 根据IDs获取用户列表
func (d *Dao) GetUsersByIDs(ids []int64) ([]*model.User, error) { func (d *dataServant) GetUsersByIDs(ids []int64) ([]*model.User, error) {
user := &model.User{} user := &model.User{}
return user.List(d.engine, &model.ConditionsT{ return user.List(d.engine, &model.ConditionsT{
@ -59,7 +59,7 @@ func (d *Dao) GetUsersByIDs(ids []int64) ([]*model.User, error) {
} }
// 根据关键词模糊获取用户列表 // 根据关键词模糊获取用户列表
func (d *Dao) GetUsersByKeyword(keyword string) ([]*model.User, error) { func (d *dataServant) GetUsersByKeyword(keyword string) ([]*model.User, error) {
user := &model.User{} user := &model.User{}
if strings.Trim(keyword, "") == "" { if strings.Trim(keyword, "") == "" {
@ -75,7 +75,7 @@ func (d *Dao) GetUsersByKeyword(keyword string) ([]*model.User, error) {
} }
// 根据关键词模糊获取用户列表 // 根据关键词模糊获取用户列表
func (d *Dao) GetTagsByKeyword(keyword string) ([]*model.Tag, error) { func (d *dataServant) GetTagsByKeyword(keyword string) ([]*model.Tag, error) {
tag := &model.Tag{} tag := &model.Tag{}
if strings.Trim(keyword, "") == "" { if strings.Trim(keyword, "") == "" {
@ -92,30 +92,30 @@ func (d *Dao) GetTagsByKeyword(keyword string) ([]*model.Tag, error) {
} }
// 创建用户 // 创建用户
func (d *Dao) CreateUser(user *model.User) (*model.User, error) { func (d *dataServant) CreateUser(user *model.User) (*model.User, error) {
return user.Create(d.engine) return user.Create(d.engine)
} }
// 更新用户 // 更新用户
func (d *Dao) UpdateUser(user *model.User) error { func (d *dataServant) UpdateUser(user *model.User) error {
return user.Update(d.engine) return user.Update(d.engine)
} }
// 获取最新短信验证码 // 获取最新短信验证码
func (d *Dao) GetLatestPhoneCaptcha(phone string) (*model.Captcha, error) { func (d *dataServant) GetLatestPhoneCaptcha(phone string) (*model.Captcha, error) {
return (&model.Captcha{ return (&model.Captcha{
Phone: phone, Phone: phone,
}).Get(d.engine) }).Get(d.engine)
} }
// 更新短信验证码 // 更新短信验证码
func (d *Dao) UsePhoneCaptcha(captcha *model.Captcha) error { func (d *dataServant) UsePhoneCaptcha(captcha *model.Captcha) error {
captcha.UseTimes++ captcha.UseTimes++
return captcha.Update(d.engine) return captcha.Update(d.engine)
} }
// 发送短信验证码 // 发送短信验证码
func (d *Dao) SendPhoneCaptcha(phone string) error { func (d *dataServant) SendPhoneCaptcha(phone string) error {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
captcha := rand.Intn(900000) + 100000 captcha := rand.Intn(900000) + 100000
m := 5 m := 5

@ -6,7 +6,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func (d *Dao) GetRechargeByID(id int64) (*model.WalletRecharge, error) { func (d *dataServant) GetRechargeByID(id int64) (*model.WalletRecharge, error) {
recharge := &model.WalletRecharge{ recharge := &model.WalletRecharge{
Model: &model.Model{ Model: &model.Model{
ID: id, ID: id,
@ -15,7 +15,7 @@ func (d *Dao) GetRechargeByID(id int64) (*model.WalletRecharge, error) {
return recharge.Get(d.engine) return recharge.Get(d.engine)
} }
func (d *Dao) CreateRecharge(userId, amount int64) (*model.WalletRecharge, error) { func (d *dataServant) CreateRecharge(userId, amount int64) (*model.WalletRecharge, error) {
recharge := &model.WalletRecharge{ recharge := &model.WalletRecharge{
UserID: userId, UserID: userId,
Amount: amount, Amount: amount,
@ -24,7 +24,7 @@ func (d *Dao) CreateRecharge(userId, amount int64) (*model.WalletRecharge, error
return recharge.Create(d.engine) return recharge.Create(d.engine)
} }
func (d *Dao) HandleRechargeSuccess(recharge *model.WalletRecharge, tradeNo string) error { func (d *dataServant) HandleRechargeSuccess(recharge *model.WalletRecharge, tradeNo string) error {
user, _ := (&model.User{ user, _ := (&model.User{
Model: &model.Model{ Model: &model.Model{
ID: recharge.UserID, ID: recharge.UserID,
@ -61,7 +61,7 @@ func (d *Dao) HandleRechargeSuccess(recharge *model.WalletRecharge, tradeNo stri
}) })
} }
func (d *Dao) HandlePostAttachmentBought(post *model.Post, user *model.User) error { func (d *dataServant) HandlePostAttachmentBought(post *model.Post, user *model.User) error {
return d.engine.Transaction(func(tx *gorm.DB) error { return d.engine.Transaction(func(tx *gorm.DB) error {
// 扣除金额 // 扣除金额
if err := tx.Model(user).Update("balance", gorm.Expr("balance - ?", post.AttachmentPrice)).Error; err != nil { if err := tx.Model(user).Update("balance", gorm.Expr("balance - ?", post.AttachmentPrice)).Error; err != nil {

@ -3,7 +3,7 @@ package api
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/global" "github.com/rocboss/paopao-ce/global"
"github.com/rocboss/paopao-ce/internal/dao" "github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/internal/model"
"github.com/rocboss/paopao-ce/internal/service" "github.com/rocboss/paopao-ce/internal/service"
"github.com/rocboss/paopao-ce/pkg/app" "github.com/rocboss/paopao-ce/pkg/app"
@ -14,7 +14,7 @@ import (
func GetPostList(c *gin.Context) { func GetPostList(c *gin.Context) {
response := app.NewResponse(c) response := app.NewResponse(c)
q := &dao.QueryT{ q := &core.QueryT{
Query: c.Query("query"), Query: c.Query("query"),
Type: "search", Type: "search",
} }

@ -3,5 +3,5 @@ package service
import "github.com/rocboss/paopao-ce/internal/model" import "github.com/rocboss/paopao-ce/internal/model"
func CreateAttachment(attachment *model.Attachment) (*model.Attachment, error) { func CreateAttachment(attachment *model.Attachment) (*model.Attachment, error) {
return myDao.CreateAttachment(attachment) return ds.CreateAttachment(attachment)
} }

@ -34,7 +34,7 @@ func GetPostComments(postID int64, sort string, offset, limit int) ([]*model.Com
"post_id": postID, "post_id": postID,
"ORDER": sort, "ORDER": sort,
} }
comments, err := myDao.GetComments(conditions, offset, limit) comments, err := ds.GetComments(conditions, offset, limit)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
@ -47,17 +47,17 @@ func GetPostComments(postID int64, sort string, offset, limit int) ([]*model.Com
commentIDs = append(commentIDs, comment.ID) commentIDs = append(commentIDs, comment.ID)
} }
users, err := myDao.GetUsersByIDs(userIDs) users, err := ds.GetUsersByIDs(userIDs)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
contents, err := myDao.GetCommentContentsByIDs(commentIDs) contents, err := ds.GetCommentContentsByIDs(commentIDs)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
replies, err := myDao.GetCommentRepliesByID(commentIDs) replies, err := ds.GetCommentRepliesByID(commentIDs)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
@ -85,14 +85,14 @@ func GetPostComments(postID int64, sort string, offset, limit int) ([]*model.Com
} }
// 获取总量 // 获取总量
totalRows, _ := myDao.GetCommentCount(conditions) totalRows, _ := ds.GetCommentCount(conditions)
return commentsFormated, totalRows, nil return commentsFormated, totalRows, nil
} }
func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq) (*model.Comment, error) { func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq) (*model.Comment, error) {
// 加载Post // 加载Post
post, err := myDao.GetPostByID(param.PostID) post, err := ds.GetPostByID(param.PostID)
if err != nil { if err != nil {
return nil, err return nil, err
@ -108,7 +108,7 @@ func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq)
IP: ip, IP: ip,
IPLoc: util.GetIPLoc(ip), IPLoc: util.GetIPLoc(ip),
} }
comment, err = myDao.CreateComment(comment) comment, err = ds.CreateComment(comment)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -128,21 +128,21 @@ func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq)
Type: item.Type, Type: item.Type,
Sort: item.Sort, Sort: item.Sort,
} }
myDao.CreateCommentContent(postContent) ds.CreateCommentContent(postContent)
} }
// 更新Post回复数 // 更新Post回复数
post.CommentCount++ post.CommentCount++
post.LatestRepliedOn = time.Now().Unix() post.LatestRepliedOn = time.Now().Unix()
myDao.UpdatePost(post) ds.UpdatePost(post)
// 更新索引 // 更新索引
go PushPostToSearch(post) go PushPostToSearch(post)
// 创建用户消息提醒 // 创建用户消息提醒
postMaster, err := myDao.GetUserByID(post.UserID) postMaster, err := ds.GetUserByID(post.UserID)
if err == nil && postMaster.ID != userID { if err == nil && postMaster.ID != userID {
go myDao.CreateMessage(&model.Message{ go ds.CreateMessage(&model.Message{
SenderUserID: userID, SenderUserID: userID,
ReceiverUserID: postMaster.ID, ReceiverUserID: postMaster.ID,
Type: model.MESSAGE_COMMENT, Type: model.MESSAGE_COMMENT,
@ -152,13 +152,13 @@ func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq)
}) })
} }
for _, u := range param.Users { for _, u := range param.Users {
user, err := myDao.GetUserByUsername(u) user, err := ds.GetUserByUsername(u)
if err != nil || user.ID == userID || user.ID == postMaster.ID { if err != nil || user.ID == userID || user.ID == postMaster.ID {
continue continue
} }
// 创建消息提醒 // 创建消息提醒
go myDao.CreateMessage(&model.Message{ go ds.CreateMessage(&model.Message{
SenderUserID: userID, SenderUserID: userID,
ReceiverUserID: user.ID, ReceiverUserID: user.ID,
Type: model.MESSAGE_COMMENT, Type: model.MESSAGE_COMMENT,
@ -172,31 +172,31 @@ func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq)
} }
func GetPostComment(id int64) (*model.Comment, error) { func GetPostComment(id int64) (*model.Comment, error) {
return myDao.GetCommentByID(id) return ds.GetCommentByID(id)
} }
func DeletePostComment(comment *model.Comment) error { func DeletePostComment(comment *model.Comment) error {
// 加载post // 加载post
post, err := myDao.GetPostByID(comment.PostID) post, err := ds.GetPostByID(comment.PostID)
if err == nil { if err == nil {
// 更新post回复数 // 更新post回复数
post.CommentCount-- post.CommentCount--
myDao.UpdatePost(post) ds.UpdatePost(post)
} }
return myDao.DeleteComment(comment) return ds.DeleteComment(comment)
} }
func createPostPreHandler(commentID int64, userID, atUserID int64) (*model.Post, *model.Comment, int64, func createPostPreHandler(commentID int64, userID, atUserID int64) (*model.Post, *model.Comment, int64,
error) { error) {
// 加载Comment // 加载Comment
comment, err := myDao.GetCommentByID(commentID) comment, err := ds.GetCommentByID(commentID)
if err != nil { if err != nil {
return nil, nil, atUserID, err return nil, nil, atUserID, err
} }
// 加载comment的post // 加载comment的post
post, err := myDao.GetPostByID(comment.PostID) post, err := ds.GetPostByID(comment.PostID)
if err != nil { if err != nil {
return nil, nil, atUserID, err return nil, nil, atUserID, err
} }
@ -211,7 +211,7 @@ func createPostPreHandler(commentID int64, userID, atUserID int64) (*model.Post,
if atUserID > 0 { if atUserID > 0 {
// 检测目前用户是否存在 // 检测目前用户是否存在
users, _ := myDao.GetUsersByIDs([]int64{atUserID}) users, _ := ds.GetUsersByIDs([]int64{atUserID})
if len(users) == 0 { if len(users) == 0 {
atUserID = 0 atUserID = 0
} }
@ -241,7 +241,7 @@ func CreatePostCommentReply(ctx *gin.Context, commentID int64, content string, u
IPLoc: util.GetIPLoc(ip), IPLoc: util.GetIPLoc(ip),
} }
reply, err = myDao.CreateCommentReply(reply) reply, err = ds.CreateCommentReply(reply)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -249,15 +249,15 @@ func CreatePostCommentReply(ctx *gin.Context, commentID int64, content string, u
// 更新Post回复数 // 更新Post回复数
post.CommentCount++ post.CommentCount++
post.LatestRepliedOn = time.Now().Unix() post.LatestRepliedOn = time.Now().Unix()
myDao.UpdatePost(post) ds.UpdatePost(post)
// 更新索引 // 更新索引
go PushPostToSearch(post) go PushPostToSearch(post)
// 创建用户消息提醒 // 创建用户消息提醒
commentMaster, err := myDao.GetUserByID(comment.UserID) commentMaster, err := ds.GetUserByID(comment.UserID)
if err == nil && commentMaster.ID != userID { if err == nil && commentMaster.ID != userID {
go myDao.CreateMessage(&model.Message{ go ds.CreateMessage(&model.Message{
SenderUserID: userID, SenderUserID: userID,
ReceiverUserID: commentMaster.ID, ReceiverUserID: commentMaster.ID,
Type: model.MESSAGE_REPLY, Type: model.MESSAGE_REPLY,
@ -267,9 +267,9 @@ func CreatePostCommentReply(ctx *gin.Context, commentID int64, content string, u
ReplyID: reply.ID, ReplyID: reply.ID,
}) })
} }
postMaster, err := myDao.GetUserByID(post.UserID) postMaster, err := ds.GetUserByID(post.UserID)
if err == nil && postMaster.ID != userID && commentMaster.ID != postMaster.ID { if err == nil && postMaster.ID != userID && commentMaster.ID != postMaster.ID {
go myDao.CreateMessage(&model.Message{ go ds.CreateMessage(&model.Message{
SenderUserID: userID, SenderUserID: userID,
ReceiverUserID: postMaster.ID, ReceiverUserID: postMaster.ID,
Type: model.MESSAGE_REPLY, Type: model.MESSAGE_REPLY,
@ -280,10 +280,10 @@ func CreatePostCommentReply(ctx *gin.Context, commentID int64, content string, u
}) })
} }
if atUserID > 0 { if atUserID > 0 {
user, err := myDao.GetUserByID(atUserID) user, err := ds.GetUserByID(atUserID)
if err == nil && user.ID != userID && commentMaster.ID != user.ID && postMaster.ID != user.ID { if err == nil && user.ID != userID && commentMaster.ID != user.ID && postMaster.ID != user.ID {
// 创建消息提醒 // 创建消息提醒
go myDao.CreateMessage(&model.Message{ go ds.CreateMessage(&model.Message{
SenderUserID: userID, SenderUserID: userID,
ReceiverUserID: user.ID, ReceiverUserID: user.ID,
Type: model.MESSAGE_REPLY, Type: model.MESSAGE_REPLY,
@ -299,23 +299,23 @@ func CreatePostCommentReply(ctx *gin.Context, commentID int64, content string, u
} }
func GetPostCommentReply(id int64) (*model.CommentReply, error) { func GetPostCommentReply(id int64) (*model.CommentReply, error) {
return myDao.GetCommentReplyByID(id) return ds.GetCommentReplyByID(id)
} }
func DeletePostCommentReply(reply *model.CommentReply) error { func DeletePostCommentReply(reply *model.CommentReply) error {
err := myDao.DeleteCommentReply(reply) err := ds.DeleteCommentReply(reply)
if err != nil { if err != nil {
return err return err
} }
// 加载Comment // 加载Comment
comment, err := myDao.GetCommentByID(reply.CommentID) comment, err := ds.GetCommentByID(reply.CommentID)
if err != nil { if err != nil {
return err return err
} }
// 加载comment的post // 加载comment的post
post, err := myDao.GetPostByID(comment.PostID) post, err := ds.GetPostByID(comment.PostID)
if err != nil { if err != nil {
return err return err
} }
@ -323,7 +323,7 @@ func DeletePostCommentReply(reply *model.CommentReply) error {
// 更新Post回复数 // 更新Post回复数
post.CommentCount-- post.CommentCount--
post.LatestRepliedOn = time.Now().Unix() post.LatestRepliedOn = time.Now().Unix()
myDao.UpdatePost(post) ds.UpdatePost(post)
// 更新索引 // 更新索引
go PushPostToSearch(post) go PushPostToSearch(post)

@ -32,7 +32,7 @@ func CreateWhisper(c *gin.Context, msg *model.Message) (*model.Message, error) {
} }
// 创建私信 // 创建私信
msg, err := myDao.CreateMessage(msg) msg, err := ds.CreateMessage(msg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -48,11 +48,11 @@ func CreateWhisper(c *gin.Context, msg *model.Message) (*model.Message, error) {
} }
func GetUnreadCount(userID int64) (int64, error) { func GetUnreadCount(userID int64) (int64, error) {
return myDao.GetUnreadCount(userID) return ds.GetUnreadCount(userID)
} }
func ReadMessage(id, userID int64) error { func ReadMessage(id, userID int64) error {
// 获取message // 获取message
message, err := myDao.GetMessageByID(id) message, err := ds.GetMessageByID(id)
if err != nil { if err != nil {
return err return err
} }
@ -62,7 +62,7 @@ func ReadMessage(id, userID int64) error {
} }
// 已读消息 // 已读消息
return myDao.ReadMessage(message) return ds.ReadMessage(message)
} }
func GetMessages(userID int64, offset, limit int) ([]*model.MessageFormated, int64, error) { func GetMessages(userID int64, offset, limit int) ([]*model.MessageFormated, int64, error) {
@ -70,12 +70,12 @@ func GetMessages(userID int64, offset, limit int) ([]*model.MessageFormated, int
"receiver_user_id": userID, "receiver_user_id": userID,
"ORDER": "id DESC", "ORDER": "id DESC",
} }
messages, err := myDao.GetMessages(conditions, offset, limit) messages, err := ds.GetMessages(conditions, offset, limit)
for _, mf := range messages { for _, mf := range messages {
if mf.SenderUserID > 0 { if mf.SenderUserID > 0 {
user, err := myDao.GetUserByID(mf.SenderUserID) user, err := ds.GetUserByID(mf.SenderUserID)
if err == nil { if err == nil {
mf.SenderUser = user.Format() mf.SenderUser = user.Format()
} }
@ -109,7 +109,7 @@ func GetMessages(userID int64, offset, limit int) ([]*model.MessageFormated, int
} }
// 获取总量 // 获取总量
totalRows, _ := myDao.GetMessageCount(conditions) totalRows, _ := ds.GetMessageCount(conditions)
return messages, totalRows, nil return messages, totalRows, nil
} }

@ -9,7 +9,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/global" "github.com/rocboss/paopao-ce/global"
"github.com/rocboss/paopao-ce/internal/dao" "github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/internal/model"
"github.com/rocboss/paopao-ce/pkg/util" "github.com/rocboss/paopao-ce/pkg/util"
"github.com/rocboss/paopao-ce/pkg/zinc" "github.com/rocboss/paopao-ce/pkg/zinc"
@ -92,7 +92,7 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos
IPLoc: util.GetIPLoc(ip), IPLoc: util.GetIPLoc(ip),
AttachmentPrice: param.AttachmentPrice, AttachmentPrice: param.AttachmentPrice,
} }
post, err := myDao.CreatePost(post) post, err := ds.CreatePost(post)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -103,7 +103,7 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos
UserID: userID, UserID: userID,
Tag: t, Tag: t,
} }
myDao.CreateTag(tag) ds.CreateTag(tag)
} }
for _, item := range param.Contents { for _, item := range param.Contents {
@ -123,7 +123,7 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos
Type: item.Type, Type: item.Type,
Sort: item.Sort, Sort: item.Sort,
} }
myDao.CreatePostContent(postContent) ds.CreatePostContent(postContent)
} }
// 推送Search // 推送Search
@ -131,13 +131,13 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos
// 创建用户消息提醒 // 创建用户消息提醒
for _, u := range param.Users { for _, u := range param.Users {
user, err := myDao.GetUserByUsername(u) user, err := ds.GetUserByUsername(u)
if err != nil || user.ID == userID { if err != nil || user.ID == userID {
continue continue
} }
// 创建消息提醒 // 创建消息提醒
go myDao.CreateMessage(&model.Message{ go ds.CreateMessage(&model.Message{
SenderUserID: userID, SenderUserID: userID,
ReceiverUserID: user.ID, ReceiverUserID: user.ID,
Type: model.MESSAGE_POST, Type: model.MESSAGE_POST,
@ -150,7 +150,7 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos
} }
func DeletePost(id int64) error { func DeletePost(id int64) error {
post, _ := myDao.GetPostByID(id) post, _ := ds.GetPostByID(id)
// tag删除 // tag删除
tags := strings.Split(post.Tags, ",") tags := strings.Split(post.Tags, ",")
@ -159,10 +159,10 @@ func DeletePost(id int64) error {
tag := &model.Tag{ tag := &model.Tag{
Tag: t, Tag: t,
} }
myDao.DeleteTag(tag) ds.DeleteTag(tag)
} }
err := myDao.DeletePost(post) err := ds.DeletePost(post)
if err != nil { if err != nil {
return err return err
@ -175,9 +175,9 @@ func DeletePost(id int64) error {
} }
func LockPost(id int64) error { func LockPost(id int64) error {
post, _ := myDao.GetPostByID(id) post, _ := ds.GetPostByID(id)
err := myDao.LockPost(post) err := ds.LockPost(post)
if err != nil { if err != nil {
return err return err
@ -187,9 +187,9 @@ func LockPost(id int64) error {
} }
func StickPost(id int64) error { func StickPost(id int64) error {
post, _ := myDao.GetPostByID(id) post, _ := ds.GetPostByID(id)
err := myDao.StickPost(post) err := ds.StickPost(post)
if err != nil { if err != nil {
return err return err
@ -199,23 +199,23 @@ func StickPost(id int64) error {
} }
func GetPostStar(postID, userID int64) (*model.PostStar, error) { func GetPostStar(postID, userID int64) (*model.PostStar, error) {
return myDao.GetUserPostStar(postID, userID) return ds.GetUserPostStar(postID, userID)
} }
func CreatePostStar(postID, userID int64) (*model.PostStar, error) { func CreatePostStar(postID, userID int64) (*model.PostStar, error) {
// 加载Post // 加载Post
post, err := myDao.GetPostByID(postID) post, err := ds.GetPostByID(postID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
star, err := myDao.CreatePostStar(postID, userID) star, err := ds.CreatePostStar(postID, userID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// 更新Post点赞数 // 更新Post点赞数
post.UpvoteCount++ post.UpvoteCount++
myDao.UpdatePost(post) ds.UpdatePost(post)
// 更新索引 // 更新索引
go PushPostToSearch(post) go PushPostToSearch(post)
@ -224,18 +224,18 @@ func CreatePostStar(postID, userID int64) (*model.PostStar, error) {
} }
func DeletePostStar(star *model.PostStar) error { func DeletePostStar(star *model.PostStar) error {
err := myDao.DeletePostStar(star) err := ds.DeletePostStar(star)
if err != nil { if err != nil {
return err return err
} }
// 加载Post // 加载Post
post, err := myDao.GetPostByID(star.PostID) post, err := ds.GetPostByID(star.PostID)
if err != nil { if err != nil {
return err return err
} }
// 更新Post点赞数 // 更新Post点赞数
post.UpvoteCount-- post.UpvoteCount--
myDao.UpdatePost(post) ds.UpdatePost(post)
// 更新索引 // 更新索引
go PushPostToSearch(post) go PushPostToSearch(post)
@ -244,23 +244,23 @@ func DeletePostStar(star *model.PostStar) error {
} }
func GetPostCollection(postID, userID int64) (*model.PostCollection, error) { func GetPostCollection(postID, userID int64) (*model.PostCollection, error) {
return myDao.GetUserPostCollection(postID, userID) return ds.GetUserPostCollection(postID, userID)
} }
func CreatePostCollection(postID, userID int64) (*model.PostCollection, error) { func CreatePostCollection(postID, userID int64) (*model.PostCollection, error) {
// 加载Post // 加载Post
post, err := myDao.GetPostByID(postID) post, err := ds.GetPostByID(postID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
collection, err := myDao.CreatePostCollection(postID, userID) collection, err := ds.CreatePostCollection(postID, userID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// 更新Post点赞数 // 更新Post点赞数
post.CollectionCount++ post.CollectionCount++
myDao.UpdatePost(post) ds.UpdatePost(post)
// 更新索引 // 更新索引
go PushPostToSearch(post) go PushPostToSearch(post)
@ -269,18 +269,18 @@ func CreatePostCollection(postID, userID int64) (*model.PostCollection, error) {
} }
func DeletePostCollection(collection *model.PostCollection) error { func DeletePostCollection(collection *model.PostCollection) error {
err := myDao.DeletePostCollection(collection) err := ds.DeletePostCollection(collection)
if err != nil { if err != nil {
return err return err
} }
// 加载Post // 加载Post
post, err := myDao.GetPostByID(collection.PostID) post, err := ds.GetPostByID(collection.PostID)
if err != nil { if err != nil {
return err return err
} }
// 更新Post点赞数 // 更新Post点赞数
post.CollectionCount-- post.CollectionCount--
myDao.UpdatePost(post) ds.UpdatePost(post)
// 更新索引 // 更新索引
go PushPostToSearch(post) go PushPostToSearch(post)
@ -289,18 +289,18 @@ func DeletePostCollection(collection *model.PostCollection) error {
} }
func GetPost(id int64) (*model.PostFormated, error) { func GetPost(id int64) (*model.PostFormated, error) {
post, err := myDao.GetPostByID(id) post, err := ds.GetPostByID(id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
postContents, err := myDao.GetPostContentsByIDs([]int64{post.ID}) postContents, err := ds.GetPostContentsByIDs([]int64{post.ID})
if err != nil { if err != nil {
return nil, err return nil, err
} }
users, err := myDao.GetUsersByIDs([]int64{post.UserID}) users, err := ds.GetUsersByIDs([]int64{post.UserID})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -319,11 +319,11 @@ func GetPost(id int64) (*model.PostFormated, error) {
} }
func GetPostContentByID(id int64) (*model.PostContent, error) { func GetPostContentByID(id int64) (*model.PostContent, error) {
return myDao.GetPostContentByID(id) return ds.GetPostContentByID(id)
} }
func GetPostList(req *PostListReq) ([]*model.PostFormated, error) { func GetPostList(req *PostListReq) ([]*model.PostFormated, error) {
posts, err := myDao.GetPosts(req.Conditions, req.Offset, req.Limit) posts, err := ds.GetPosts(req.Conditions, req.Offset, req.Limit)
if err != nil { if err != nil {
return nil, err return nil, err
@ -340,12 +340,12 @@ func FormatPosts(posts []*model.Post) ([]*model.PostFormated, error) {
userIds = append(userIds, post.UserID) userIds = append(userIds, post.UserID)
} }
postContents, err := myDao.GetPostContentsByIDs(postIds) postContents, err := ds.GetPostContentsByIDs(postIds)
if err != nil { if err != nil {
return nil, err return nil, err
} }
users, err := myDao.GetUsersByIDs(userIds) users, err := ds.GetUsersByIDs(userIds)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -373,11 +373,11 @@ func FormatPosts(posts []*model.Post) ([]*model.PostFormated, error) {
} }
func GetPostCount(conditions *model.ConditionsT) (int64, error) { func GetPostCount(conditions *model.ConditionsT) (int64, error) {
return myDao.GetPostCount(conditions) return ds.GetPostCount(conditions)
} }
func GetPostListFromSearch(q *dao.QueryT, offset, limit int) ([]*model.PostFormated, int64, error) { func GetPostListFromSearch(q *core.QueryT, offset, limit int) ([]*model.PostFormated, int64, error) {
queryResult, err := myDao.QueryAll(q, global.SearchSetting.ZincIndex, offset, limit) queryResult, err := ds.QueryAll(q, global.SearchSetting.ZincIndex, offset, limit)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
@ -391,7 +391,7 @@ func GetPostListFromSearch(q *dao.QueryT, offset, limit int) ([]*model.PostForma
} }
func GetPostListFromSearchByQuery(query string, offset, limit int) ([]*model.PostFormated, int64, error) { func GetPostListFromSearchByQuery(query string, offset, limit int) ([]*model.PostFormated, int64, error) {
queryResult, err := myDao.QuerySearch(global.SearchSetting.ZincIndex, query, offset, limit) queryResult, err := ds.QuerySearch(global.SearchSetting.ZincIndex, query, offset, limit)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
@ -411,7 +411,7 @@ func PushPostToSearch(post *model.Post) {
postFormated.User = &model.UserFormated{ postFormated.User = &model.UserFormated{
ID: post.UserID, ID: post.UserID,
} }
contents, _ := myDao.GetPostContentsByIDs([]int64{post.ID}) contents, _ := ds.GetPostContentsByIDs([]int64{post.ID})
for _, content := range contents { for _, content := range contents {
postFormated.Contents = append(postFormated.Contents, content.Format()) postFormated.Contents = append(postFormated.Contents, content.Format())
} }
@ -452,13 +452,13 @@ func PushPostToSearch(post *model.Post) {
"modified_on": post.ModifiedOn, "modified_on": post.ModifiedOn,
}) })
myDao.BulkPushDoc(data) ds.BulkPushDoc(data)
} }
func DeleteSearchPost(post *model.Post) error { func DeleteSearchPost(post *model.Post) error {
indexName := global.SearchSetting.ZincIndex indexName := global.SearchSetting.ZincIndex
return myDao.DelDoc(indexName, fmt.Sprintf("%d", post.ID)) return ds.DelDoc(indexName, fmt.Sprintf("%d", post.ID))
} }
func PushPostsToSearch(c *gin.Context) { func PushPostsToSearch(c *gin.Context) {
@ -471,7 +471,7 @@ func PushPostsToSearch(c *gin.Context) {
indexName := global.SearchSetting.ZincIndex indexName := global.SearchSetting.ZincIndex
// 创建索引 // 创建索引
myDao.CreateSearchIndex(indexName) ds.CreateSearchIndex(indexName)
for i := 0; i < nums; i++ { for i := 0; i < nums; i++ {
data := []map[string]interface{}{} data := []map[string]interface{}{}
@ -515,7 +515,7 @@ func PushPostsToSearch(c *gin.Context) {
} }
if len(data) > 0 { if len(data) > 0 {
myDao.BulkPushDoc(data) ds.BulkPushDoc(data)
} }
} }
@ -541,12 +541,12 @@ func FormatZincPost(queryResult *zinc.QueryResultT) ([]*model.PostFormated, erro
postIds = append(postIds, post.ID) postIds = append(postIds, post.ID)
userIds = append(userIds, post.UserID) userIds = append(userIds, post.UserID)
} }
postContents, err := myDao.GetPostContentsByIDs(postIds) postContents, err := ds.GetPostContentsByIDs(postIds)
if err != nil { if err != nil {
return nil, err return nil, err
} }
users, err := myDao.GetUsersByIDs(userIds) users, err := ds.GetUsersByIDs(userIds)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -591,7 +591,7 @@ func GetPostTags(param *PostTagsReq) ([]*model.TagFormated, error) {
} }
} }
tags, err := myDao.GetTags(conditions, 0, num) tags, err := ds.GetTags(conditions, 0, num)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -602,7 +602,7 @@ func GetPostTags(param *PostTagsReq) ([]*model.TagFormated, error) {
userIds = append(userIds, tag.UserID) userIds = append(userIds, tag.UserID)
} }
users, _ := myDao.GetUsersByIDs(userIds) users, _ := ds.GetUsersByIDs(userIds)
tagsFormated := []*model.TagFormated{} tagsFormated := []*model.TagFormated{}
for _, tag := range tags { for _, tag := range tags {
@ -619,7 +619,7 @@ func GetPostTags(param *PostTagsReq) ([]*model.TagFormated, error) {
} }
func CheckPostAttachmentIsPaid(postID, userID int64) bool { func CheckPostAttachmentIsPaid(postID, userID int64) bool {
bill, err := myDao.GetPostAttatchmentBill(postID, userID) bill, err := ds.GetPostAttatchmentBill(postID, userID)
return err == nil && bill.Model != nil && bill.ID > 0 return err == nil && bill.Model != nil && bill.ID > 0
} }

@ -1,15 +1,16 @@
package service package service
import ( import (
"github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/internal/dao" "github.com/rocboss/paopao-ce/internal/dao"
"github.com/rocboss/paopao-ce/pkg/zinc" "github.com/rocboss/paopao-ce/pkg/zinc"
"gorm.io/gorm" "gorm.io/gorm"
) )
var ( var (
myDao *dao.Dao ds core.DataService
) )
func Initialize(engine *gorm.DB, client *zinc.ZincClient) { func Initialize(engine *gorm.DB, client *zinc.ZincClient) {
myDao = dao.New(engine, client) ds = dao.NewDataService(engine, client)
} }

@ -57,7 +57,7 @@ const MAX_LOGIN_ERR_TIMES = 10
// DoLogin 用户认证 // DoLogin 用户认证
func DoLogin(ctx *gin.Context, param *AuthRequest) (*model.User, error) { func DoLogin(ctx *gin.Context, param *AuthRequest) (*model.User, error) {
user, err := myDao.GetUserByUsername(param.Username) user, err := ds.GetUserByUsername(param.Username)
if err != nil { if err != nil {
return nil, errcode.UnauthorizedAuthNotExist return nil, errcode.UnauthorizedAuthNotExist
} }
@ -115,7 +115,7 @@ func ValidUsername(username string) error {
} }
// 重复检查 // 重复检查
user, _ := myDao.GetUserByUsername(username) user, _ := ds.GetUserByUsername(username)
if user.Model != nil && user.ID > 0 { if user.Model != nil && user.ID > 0 {
return errcode.UsernameHasExisted return errcode.UsernameHasExisted
@ -136,7 +136,7 @@ func CheckPassword(password string) error {
// CheckPhoneCaptcha 验证手机验证码 // CheckPhoneCaptcha 验证手机验证码
func CheckPhoneCaptcha(phone, captcha string) *errcode.Error { func CheckPhoneCaptcha(phone, captcha string) *errcode.Error {
c, err := myDao.GetLatestPhoneCaptcha(phone) c, err := ds.GetLatestPhoneCaptcha(phone)
if err != nil { if err != nil {
return errcode.ErrorPhoneCaptcha return errcode.ErrorPhoneCaptcha
} }
@ -154,14 +154,14 @@ func CheckPhoneCaptcha(phone, captcha string) *errcode.Error {
} }
// 更新检测次数 // 更新检测次数
myDao.UsePhoneCaptcha(c) ds.UsePhoneCaptcha(c)
return nil return nil
} }
// CheckPhoneExist 检测手机号是否存在 // CheckPhoneExist 检测手机号是否存在
func CheckPhoneExist(uid int64, phone string) bool { func CheckPhoneExist(uid int64, phone string) bool {
u, err := myDao.GetUserByPhone(phone) u, err := ds.GetUserByPhone(phone)
if err != nil { if err != nil {
return false return false
} }
@ -198,7 +198,7 @@ func Register(username, password string) (*model.User, error) {
Status: model.UserStatusNormal, Status: model.UserStatusNormal,
} }
user, err := myDao.CreateUser(user) user, err := ds.CreateUser(user)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -208,7 +208,7 @@ func Register(username, password string) (*model.User, error) {
// GetUserInfo 获取用户信息 // GetUserInfo 获取用户信息
func GetUserInfo(param *AuthRequest) (*model.User, error) { func GetUserInfo(param *AuthRequest) (*model.User, error) {
user, err := myDao.GetUserByUsername(param.Username) user, err := ds.GetUserByUsername(param.Username)
if err != nil { if err != nil {
return nil, err return nil, err
@ -222,7 +222,7 @@ func GetUserInfo(param *AuthRequest) (*model.User, error) {
} }
func GetUserByUsername(username string) (*model.User, error) { func GetUserByUsername(username string) (*model.User, error) {
user, err := myDao.GetUserByUsername(username) user, err := ds.GetUserByUsername(username)
if err != nil { if err != nil {
return nil, err return nil, err
@ -237,16 +237,16 @@ func GetUserByUsername(username string) (*model.User, error) {
// UpdateUserInfo 更新用户信息 // UpdateUserInfo 更新用户信息
func UpdateUserInfo(user *model.User) error { func UpdateUserInfo(user *model.User) error {
return myDao.UpdateUser(user) return ds.UpdateUser(user)
} }
// GetUserCollections 获取用户收藏列表 // GetUserCollections 获取用户收藏列表
func GetUserCollections(userID int64, offset, limit int) ([]*model.PostFormated, int64, error) { func GetUserCollections(userID int64, offset, limit int) ([]*model.PostFormated, int64, error) {
collections, err := myDao.GetUserPostCollections(userID, offset, limit) collections, err := ds.GetUserPostCollections(userID, offset, limit)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
totalRows, err := myDao.GetUserPostCollectionCount(userID) totalRows, err := ds.GetUserPostCollectionCount(userID)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
@ -256,7 +256,7 @@ func GetUserCollections(userID int64, offset, limit int) ([]*model.PostFormated,
} }
// 获取Posts // 获取Posts
posts, err := myDao.GetPosts(&model.ConditionsT{ posts, err := ds.GetPosts(&model.ConditionsT{
"id IN ?": postIDs, "id IN ?": postIDs,
"ORDER": "id DESC", "ORDER": "id DESC",
}, 0, 0) }, 0, 0)
@ -274,11 +274,11 @@ func GetUserCollections(userID int64, offset, limit int) ([]*model.PostFormated,
// GetUserStars 获取用户点赞列表 // GetUserStars 获取用户点赞列表
func GetUserStars(userID int64, offset, limit int) ([]*model.PostFormated, int64, error) { func GetUserStars(userID int64, offset, limit int) ([]*model.PostFormated, int64, error) {
stars, err := myDao.GetUserPostStars(userID, offset, limit) stars, err := ds.GetUserPostStars(userID, offset, limit)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
totalRows, err := myDao.GetUserPostStarCount(userID) totalRows, err := ds.GetUserPostStarCount(userID)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
@ -288,7 +288,7 @@ func GetUserStars(userID int64, offset, limit int) ([]*model.PostFormated, int64
} }
// 获取Posts // 获取Posts
posts, err := myDao.GetPosts(&model.ConditionsT{ posts, err := ds.GetPosts(&model.ConditionsT{
"id IN ?": postIDs, "id IN ?": postIDs,
"ORDER": "id DESC", "ORDER": "id DESC",
}, 0, 0) }, 0, 0)
@ -306,11 +306,11 @@ func GetUserStars(userID int64, offset, limit int) ([]*model.PostFormated, int64
// GetUserWalletBills 获取用户账单列表 // GetUserWalletBills 获取用户账单列表
func GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStatement, int64, error) { func GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStatement, int64, error) {
bills, err := myDao.GetUserWalletBills(userID, offset, limit) bills, err := ds.GetUserWalletBills(userID, offset, limit)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
totalRows, err := myDao.GetUserWalletBillCount(userID) totalRows, err := ds.GetUserWalletBillCount(userID)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
@ -321,7 +321,7 @@ func GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStateme
// SendPhoneCaptcha 发送短信验证码 // SendPhoneCaptcha 发送短信验证码
func SendPhoneCaptcha(ctx *gin.Context, phone string) error { func SendPhoneCaptcha(ctx *gin.Context, phone string) error {
err := myDao.SendPhoneCaptcha(phone) err := ds.SendPhoneCaptcha(phone)
if err != nil { if err != nil {
return err return err
} }
@ -339,7 +339,7 @@ func SendPhoneCaptcha(ctx *gin.Context, phone string) error {
// GetSuggestUsers 根据关键词获取用户推荐 // GetSuggestUsers 根据关键词获取用户推荐
func GetSuggestUsers(keyword string) ([]string, error) { func GetSuggestUsers(keyword string) ([]string, error) {
users, err := myDao.GetUsersByKeyword(keyword) users, err := ds.GetUsersByKeyword(keyword)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -354,7 +354,7 @@ func GetSuggestUsers(keyword string) ([]string, error) {
// GetSuggestTags 根据关键词获取标签推荐 // GetSuggestTags 根据关键词获取标签推荐
func GetSuggestTags(keyword string) ([]string, error) { func GetSuggestTags(keyword string) ([]string, error) {
tags, err := myDao.GetTagsByKeyword(keyword) tags, err := ds.GetTagsByKeyword(keyword)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -14,16 +14,16 @@ type RechargeReq struct {
} }
func GetRechargeByID(id int64) (*model.WalletRecharge, error) { func GetRechargeByID(id int64) (*model.WalletRecharge, error) {
return myDao.GetRechargeByID(id) return ds.GetRechargeByID(id)
} }
func CreateRecharge(userID, amount int64) (*model.WalletRecharge, error) { func CreateRecharge(userID, amount int64) (*model.WalletRecharge, error) {
return myDao.CreateRecharge(userID, amount) return ds.CreateRecharge(userID, amount)
} }
func FinishRecharge(ctx *gin.Context, id int64, tradeNo string) error { func FinishRecharge(ctx *gin.Context, id int64, tradeNo string) error {
if ok, _ := global.Redis.SetNX(ctx, "PaoPaoRecharge:"+tradeNo, 1, time.Second*5).Result(); ok { if ok, _ := global.Redis.SetNX(ctx, "PaoPaoRecharge:"+tradeNo, 1, time.Second*5).Result(); ok {
recharge, err := myDao.GetRechargeByID(id) recharge, err := ds.GetRechargeByID(id)
if err != nil { if err != nil {
return err return err
} }
@ -31,7 +31,7 @@ func FinishRecharge(ctx *gin.Context, id int64, tradeNo string) error {
if recharge.TradeStatus != "TRADE_SUCCESS" { if recharge.TradeStatus != "TRADE_SUCCESS" {
// 标记为已付款 // 标记为已付款
err := myDao.HandleRechargeSuccess(recharge, tradeNo) err := ds.HandleRechargeSuccess(recharge, tradeNo)
defer global.Redis.Del(ctx, "PaoPaoRecharge:"+tradeNo) defer global.Redis.Del(ctx, "PaoPaoRecharge:"+tradeNo)
if err != nil { if err != nil {
@ -50,5 +50,5 @@ func BuyPostAttachment(post *model.Post, user *model.User) error {
} }
// 执行购买 // 执行购买
return myDao.HandlePostAttachmentBought(post, user) return ds.HandlePostAttachmentBought(post, user)
} }

Loading…
Cancel
Save