sqlx: tweet_help/tweet_manage logic is implemented complete. WIP %75

r/paopao-ce-plus
Michael Li 1 year ago
parent 8bf268af0a
commit 1e9f65626a
No known key found for this signature in database

@ -24,6 +24,13 @@ const (
ContentTypeChargeAttachment = dbr.ContentTypeChargeAttachment
)
const (
PostVisitPublic PostVisibleT = iota
PostVisitPrivate
PostVisitFriend
PostVisitInvalid
)
type (
PostStar = dbr.PostStar
PostCollection = dbr.PostCollection
@ -32,4 +39,5 @@ type (
Attachment = dbr.Attachment
AttachmentType = dbr.AttachmentType
PostContentT = dbr.PostContentT
PostVisibleT = dbr.PostVisibleT
)

@ -12,8 +12,8 @@ import (
// TweetService 推文检索服务
type TweetService interface {
GetPostByID(id int64) (*ms.Post, error)
GetPosts(conditions *ms.ConditionsT, offset, limit int) ([]*ms.Post, error)
GetPostCount(conditions *ms.ConditionsT) (int64, error)
GetPosts(conditions ms.ConditionsT, offset, limit int) ([]*ms.Post, error)
GetPostCount(conditions ms.ConditionsT) (int64, error)
GetUserPostStar(postID, userID int64) (*ms.PostStar, error)
GetUserPostStars(userID int64, offset, limit int) ([]*ms.PostStar, error)
GetUserPostStarCount(userID int64) (int64, error)

@ -120,7 +120,7 @@ func (p *Post) Get(db *gorm.DB) (*Post, error) {
return &post, nil
}
func (p *Post) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]*Post, error) {
func (p *Post) List(db *gorm.DB, conditions ConditionsT, offset, limit int) ([]*Post, error) {
var posts []*Post
var err error
if offset >= 0 && limit > 0 {
@ -129,7 +129,7 @@ func (p *Post) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]
if p.UserID > 0 {
db = db.Where("user_id = ?", p.UserID)
}
for k, v := range *conditions {
for k, v := range conditions {
if k == "ORDER" {
db = db.Order(v)
} else {
@ -178,12 +178,12 @@ func (p *Post) CountBy(db *gorm.DB, predicates Predicates) (count int64, err err
return
}
func (p *Post) Count(db *gorm.DB, conditions *ConditionsT) (int64, error) {
func (p *Post) Count(db *gorm.DB, conditions ConditionsT) (int64, error) {
var count int64
if p.UserID > 0 {
db = db.Where("user_id = ?", p.UserID)
}
for k, v := range *conditions {
for k, v := range conditions {
if k != "ORDER" {
db = db.Where(k, v)
}

@ -370,11 +370,11 @@ func (s *tweetSrv) GetPostByID(id int64) (*ms.Post, error) {
return post.Get(s.db)
}
func (s *tweetSrv) GetPosts(conditions *ms.ConditionsT, offset, limit int) ([]*ms.Post, error) {
func (s *tweetSrv) GetPosts(conditions ms.ConditionsT, offset, limit int) ([]*ms.Post, error) {
return (&dbr.Post{}).List(s.db, conditions, offset, limit)
}
func (s *tweetSrv) GetPostCount(conditions *ms.ConditionsT) (int64, error) {
func (s *tweetSrv) GetPostCount(conditions ms.ConditionsT) (int64, error) {
return (&dbr.Post{}).Count(s.db, conditions)
}

@ -13,6 +13,7 @@ import (
"github.com/alimy/yesql"
"github.com/jmoiron/sqlx"
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/internal/dao/sakila/yesql/cc"
"github.com/sirupsen/logrus"
)
@ -23,6 +24,7 @@ var (
type sqlxSrv struct {
db *sqlx.DB
y *cc.Yesql
}
func (s *sqlxSrv) with(handle func(tx *sqlx.Tx) error) error {
@ -111,6 +113,7 @@ func (s *sqlxSrv) inGetx(queryer sqlx.Queryer, dest any, query string, args ...a
func newSqlxSrv(db *sqlx.DB) *sqlxSrv {
return &sqlxSrv{
db: db,
y: mustBuild(db, cc.BuildYesql),
}
}

@ -5,6 +5,9 @@
package sakila
import (
"strings"
"time"
"github.com/jmoiron/sqlx"
"github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/internal/core/cs"
@ -123,20 +126,19 @@ func (s *tweetHelpSrv) RevampPosts(posts []*ms.PostFormated) ([]*ms.PostFormated
return posts, nil
}
func (s *tweetHelpSrv) getPostContentsByIDs(ids []int64) ([]*dbr.PostContentFormated, error) {
res := []*dbr.PostContentFormated{}
err := s.inSelect(&res, s.q.GetPostContentByIds, ids)
return res, err
func (s *tweetHelpSrv) getPostContentsByIDs(ids []int64) (res []*dbr.PostContentFormated, err error) {
err = s.inSelect(&res, s.q.GetPostContentByIds, ids)
return
}
func (s *tweetHelpSrv) getUsersByIDs(ids []int64) ([]*dbr.UserFormated, error) {
res := []*dbr.UserFormated{}
err := s.inSelect(&res, s.q.GetUsersByIds, ids)
return res, err
func (s *tweetHelpSrv) getUsersByIDs(ids []int64) (res []*dbr.UserFormated, err error) {
err = s.inSelect(&res, s.q.GetUsersByIds, ids)
return
}
func (s *tweetManageSrv) CreatePostCollection(postID, userID int64) (*ms.PostCollection, error) {
res, err := s.q.AddPostCollection.Exec(postID, userID)
now := time.Now().Unix()
res, err := s.q.AddPostCollection.Exec(postID, userID, now)
if err != nil {
return nil, err
}
@ -146,7 +148,8 @@ func (s *tweetManageSrv) CreatePostCollection(postID, userID int64) (*ms.PostCol
}
return &ms.PostCollection{
Model: &dbr.Model{
ID: id,
ID: id,
CreatedOn: now,
},
PostID: postID,
UserID: userID,
@ -154,16 +157,16 @@ func (s *tweetManageSrv) CreatePostCollection(postID, userID int64) (*ms.PostCol
}
func (s *tweetManageSrv) DeletePostCollection(r *ms.PostCollection) error {
_, err := s.q.DelPostCollection.Exec(r.ID)
_, err := s.q.DelPostCollection.Exec(time.Now().Unix(), r.ID)
return err
}
func (s *tweetManageSrv) CreatePostContent(r *ms.PostContent) (*ms.PostContent, error) {
res, err := s.q.AddPostContent.Exec(r.PostID, r.UserID, r.Content, r.Type, r.Sort)
r.Model = &ms.Model{CreatedOn: time.Now().Unix()}
res, err := s.q.AddPostContent.Exec(r)
if err != nil {
return nil, err
}
r.Model = &dbr.Model{}
r.ID, err = res.LastInsertId()
if err != nil {
return nil, err
@ -172,7 +175,8 @@ func (s *tweetManageSrv) CreatePostContent(r *ms.PostContent) (*ms.PostContent,
}
func (s *tweetManageSrv) CreateAttachment(r *ms.Attachment) (int64, error) {
res, err := s.q.AddAttachment.Exec(r.UserID, r.FileSize, r.ImgWidth, r.ImgHeight, r.Type, r.Content)
args := []any{r.UserID, r.FileSize, r.ImgWidth, r.ImgHeight, r.Type, r.Content, time.Now().Unix()}
res, err := s.q.AddAttachment.Exec(args...)
if err != nil {
return 0, err
}
@ -180,43 +184,96 @@ func (s *tweetManageSrv) CreateAttachment(r *ms.Attachment) (int64, error) {
}
func (s *tweetManageSrv) CreatePost(r *ms.Post) (*ms.Post, error) {
res, err := s.q.AddPost.Exec(r.UserID, r.Visibility, r.Tags, r.AttachmentPrice, r.IP, r.IPLoc)
now := time.Now().Unix()
r.Model = &dbr.Model{CreatedOn: now}
r.LatestRepliedOn = now
res, err := s.q.AddPost.Exec(r)
if err != nil {
return nil, err
}
if id, err := res.LastInsertId(); err == nil {
r.Model = &dbr.Model{
ID: id,
}
} else {
if r.ID, err = res.LastInsertId(); err != nil {
return nil, err
}
s.cis.SendAction(core.IdxActCreatePost, r)
return r, nil
}
func (s *tweetManageSrv) DeletePost(post *ms.Post) ([]string, error) {
func (s *tweetManageSrv) DeletePost(post *ms.Post) (mediaContents []string, err error) {
s.with(func(tx *sqlx.Tx) error {
// 获取多媒体内容
if err = tx.Stmtx(s.q.MediaContentByPostId).Get(&mediaContents, post.ID); err != nil {
return err
}
// 删推文
now := time.Now().Unix()
if _, err = tx.Stmtx(s.q.DelPostById).Exec(now, post.ID); err != nil {
return err
}
// 删评论
contents, err := s.deleteCommentByPostId(tx, post.ID)
if err != nil {
return err
}
mediaContents = append(mediaContents, contents...)
if tags := strings.Split(post.Tags, ","); len(tags) > 0 {
// 删tag宽松处理错误有错误不会回滚
s.deleteTags(tx, tags)
}
return nil
})
s.cis.SendAction(core.IdxActDeletePost, post)
return
}
func (s *tweetManageSrv) deleteCommentByPostId(tx *sqlx.Tx, postId int64) ([]string, error) {
// TODO
debug.NotImplemented()
return nil, nil
}
func (s *tweetManageSrv) LockPost(r *ms.Post) error {
_, err := s.q.LockPost.Exec(r.ID)
_, err := s.q.LockPost.Exec(time.Now().Unix(), r.ID)
return err
}
func (s *tweetManageSrv) StickPost(r *ms.Post) error {
_, err := s.q.StickPost.Exec(r.ID)
_, err := s.q.StickPost.Exec(time.Now().Unix(), r.ID)
return err
}
func (s *tweetManageSrv) VisiblePost(r *ms.Post, visibility core.PostVisibleT) error {
_, err := s.q.VisiblePost.Exec(r.ID, visibility)
return err
func (s *tweetManageSrv) VisiblePost(post *ms.Post, visibility core.PostVisibleT) (err error) {
oldVisibility := post.Visibility
post.Visibility = visibility
// TODO: 这个判断是否可以不要呢
if oldVisibility == visibility {
return nil
}
// 私密推文 特殊处理
if visibility == ms.PostVisitPrivate {
// 强制取消置顶
// TODO: 置顶推文用户是否有权设置成私密? 后续完善
post.IsTop = 0
}
s.with(func(tx *sqlx.Tx) error {
_, err = s.q.VisiblePost.Exec(visibility, post.IsTop, time.Now().Unix(), post.ID)
// tag处理
tags := strings.Split(post.Tags, ",")
// TODO: 暂时宽松不处理错误,这里或许可以有优化,后续完善
if oldVisibility == dbr.PostVisitPrivate {
// 从私密转为非私密才需要重新创建tag
s.createTags(tx, post.UserID, tags)
} else if visibility == dbr.PostVisitPrivate {
// 从非私密转为私密才需要删除tag
s.deleteTags(tx, tags)
}
return nil
})
s.cis.SendAction(core.IdxActVisiblePost, post)
return
}
func (s *tweetManageSrv) UpdatePost(r *ms.Post) error {
if _, err := s.q.UpdatePost.Exec(r.CommentCount, r.UpvoteCount, r.ID); err != nil {
r.ModifiedOn = time.Now().Unix()
if _, err := s.q.UpdatePost.Exec(r); err != nil {
return err
}
s.cis.SendAction(core.IdxActUpdatePost, r)
@ -224,7 +281,8 @@ func (s *tweetManageSrv) UpdatePost(r *ms.Post) error {
}
func (s *tweetManageSrv) CreatePostStar(postID, userID int64) (*ms.PostStar, error) {
res, err := s.q.AddPostStar.Exec(postID, userID)
now := time.Now().Unix()
res, err := s.q.AddPostStar.Exec(postID, userID, now)
if err != nil {
return nil, err
}
@ -234,7 +292,8 @@ func (s *tweetManageSrv) CreatePostStar(postID, userID int64) (*ms.PostStar, err
}
return &ms.PostStar{
Model: &dbr.Model{
ID: id,
ID: id,
CreatedOn: now,
},
PostID: postID,
UserID: userID,
@ -242,26 +301,35 @@ func (s *tweetManageSrv) CreatePostStar(postID, userID int64) (*ms.PostStar, err
}
func (s *tweetManageSrv) DeletePostStar(r *ms.PostStar) error {
_, err := s.q.DelPostStar.Exec(r.ID)
_, err := s.q.DelPostStar.Exec(time.Now().Unix(), r.ID)
return err
}
func (s *tweetSrv) GetPostByID(id int64) (*ms.Post, error) {
res := &ms.Post{}
err := s.q.GetPostById.Get(res, id)
return res, err
func (s *tweetSrv) GetPostByID(id int64) (res *ms.Post, err error) {
err = s.q.GetPostById.Get(res, id)
return
}
func (s *tweetSrv) GetPosts(conditions *ms.ConditionsT, offset, limit int) ([]*ms.Post, error) {
// TODO 需要精细化接口
debug.NotImplemented()
return nil, nil
func (s *tweetSrv) GetPosts(c ms.ConditionsT, offset, limit int) (res []*ms.Post, err error) {
userId, exist := c["user_id"]
visibility := c["visibility IN ?"]
if exist {
err = s.inSelect(&res, s.q.GetUserPosts, userId, visibility, limit, offset)
} else {
err = s.inSelect(&res, s.q.GetAnyPosts, visibility, limit, offset)
}
return
}
func (s *tweetSrv) GetPostCount(conditions *ms.ConditionsT) (int64, error) {
// TODO 需要精细化接口
debug.NotImplemented()
return 0, nil
func (s *tweetSrv) GetPostCount(c ms.ConditionsT) (res int64, err error) {
userId, exist := c["user_id"]
visibility := c["visibility IN ?"]
if exist {
err = s.inGet(&res, s.q.GetUserPostCount, userId, visibility)
} else {
err = s.inGet(&res, s.q.GetAnyPostCount, visibility)
}
return
}
func (s *tweetSrv) GetUserPostStar(postID, userID int64) (*ms.PostStar, error) {
@ -304,10 +372,9 @@ func (s *tweetSrv) GetPostAttatchmentBill(postID, userID int64) (*ms.PostAttachm
return res, err
}
func (s *tweetSrv) GetPostContentsByIDs(ids []int64) ([]*ms.PostContent, error) {
res := []*ms.PostContent{}
err := s.inSelect(&res, s.q.GetPostContetnsByIds, ids)
return res, err
func (s *tweetSrv) GetPostContentsByIDs(ids []int64) (res []*ms.PostContent, err error) {
err = s.inSelect(&res, s.q.GetPostContentsByIds, ids)
return
}
func (s *tweetSrv) GetPostContentByID(id int64) (*ms.PostContent, error) {

@ -0,0 +1,55 @@
// Copyright 2023 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package sakila
import (
"time"
"github.com/jmoiron/sqlx"
"github.com/rocboss/paopao-ce/internal/core/cs"
)
func (s *sqlxSrv) deleteTags(tx *sqlx.Tx, tags []string) (err error) {
var tagInfos []cs.TagInfo
if err = tx.Stmtx(s.y.TagsFromNames).Select(&tagInfos, tags); err != nil {
return
}
now := time.Now().Unix()
for _, tag := range tagInfos {
tag.QuoteNum--
if _, err = tx.Stmtx(s.y.UpdateTagQuote).Exec(tag.QuoteNum, now, tag.ID); err != nil {
return
}
}
return
}
func (s *sqlxSrv) createTags(tx *sqlx.Tx, userId int64, tags []string) (res cs.TagInfoList, err error) {
// for _, name := range tags {
// tag := &dbr.Tag{Tag: name}
// if tag, err = tag.Get(db); err == nil {
// // 更新
// tag.QuoteNum++
// if err = tag.Update(db); err != nil {
// return
// }
// } else {
// if tag, err = (&dbr.Tag{
// UserID: userId,
// QuoteNum: 1,
// Tag: name,
// }).Create(db); err != nil {
// return
// }
// }
// res = append(res, &cs.TagInfo{
// ID: tag.ID,
// UserID: tag.UserID,
// Tag: tag.Tag,
// QuoteNum: tag.QuoteNum,
// })
// }
return
}

@ -12,17 +12,19 @@ import (
)
const (
_TagsFromNames = `SELECT * FROM @tag WHERE tag IN (?) AND is_del=0`
_UpdateTagQuote = `UPDATE @tag SET quote_num=?, modified_on=? WHERE id=? AND is_del=0`
_AuthorizationManage_BeFriendIds = `SELECT user_id FROM @contact WHERE friend_id=? AND status=2 AND is_del=0`
_AuthorizationManage_IsFriend = `SELECT status FROM @contact WHERE user_id=? AND friend_id=? AND is_del=0`
_AuthorizationManage_MyFriendSet = `SELECT friend_id FROM @contact WHERE user_id=? AND status=2 AND is_del=0`
_Comment_GetCommentById = `SELECT * FROM @comment WHERE id=? AND is_del=0`
_Comment_GetCommentContentsByIds = `SELECT * FROM @comment_content WHERE comment_id IN ?`
_Comment_GetCommentContentsByIds = `SELECT * FROM @comment_content WHERE comment_id IN (?)`
_Comment_GetCommentCount = `SELECT count(*) FROM @comment WHERE post_id=:post_id AND is_del=0`
_Comment_GetCommentReplyById = `SELECT * FROM @comment_reply WHERE id=? AND is_del=0`
_Comment_GetCommentThumbs = `SELECT * FROM @tweet_comment_thumbs WHERE user_id=? AND tweet_id=?`
_Comment_GetComments = `SELECT * FROM @comment WHERE post_id=? AND is_del=0 ORDER BY %order% LIMIT ? OFFSET ?`
_Comment_GetCommmentRepliesByIds = `SELECT * FROM @comment_reply WHERE comment_id IN ? ORDER BY id ASC`
_Comment_GetUsersByIds = `SELECT id, nickname, username, status, avatar, is_admin FROM @user WHERE id IN ?`
_Comment_GetCommmentRepliesByIds = `SELECT * FROM @comment_reply WHERE comment_id IN (?) ORDER BY id ASC`
_Comment_GetUsersByIds = `SELECT id, nickname, username, status, avatar, is_admin FROM @user WHERE id IN (?)`
_CommentManage_CreateComment = `INSERT INTO @comment (post_id, user_id, ip, ip_loc, created_on) VALUES (?, ?, ?, ?, ?)`
_CommentManage_CreateCommentContent = `INSERT INTO @comment_content (comment_id, user_id, content, type, sort, created_on) VALUES (?, ?, ?, ?, ?, ?)`
_CommentManage_CreateCommentReply = `INSERT INTO @comment_reply (comment_id, user_id, content, at_user_id, ip, ip_loc, created_on) VALUES (?, ?, ?, ?, ?, ?, ?)`
@ -87,40 +89,43 @@ const (
_TweetA_UserTweetsByFriend = `SELECT * FROM @user WHERE username=?`
_TweetA_UserTweetsByGuest = `SELECT * FROM @user WHERE username=?`
_TweetA_UserTweetsBySelf = `SELECT * FROM @user WHERE username=?`
_Tweet_GetAnyPostCount = `SELECT count(*) FROM @post WHERE visibility IN (?)`
_Tweet_GetAnyPosts = `SELECT * FROM @post WHERE visibility IN (?) AND is_del=0 LIMIT ? OFFSET ?`
_Tweet_GetPostAttachmentBill = `SELECT * FROM @user WHERE username=?`
_Tweet_GetPostById = `SELECT * FROM @user WHERE username=?`
_Tweet_GetPostById = `SELECT * FROM @post WHERE id=? AND is_del=0`
_Tweet_GetPostContentById = `SELECT * FROM @user WHERE username=?`
_Tweet_GetPostContetnsByIds = `SELECT id, user_id, tag, quote_num FROM @tag WHERE tag IN (?)`
_Tweet_GetPostCount = `SELECT * FROM @user WHERE username=?`
_Tweet_GetPosts = `SELECT * FROM @user WHERE username=?`
_Tweet_GetPostContentsByIds = `SELECT * FROM @post_content WHERE post_id IN (?) AND is_del=0`
_Tweet_GetUserPostCollection = `SELECT * FROM @user WHERE username=?`
_Tweet_GetUserPostCollectionCount = `SELECT * FROM @user WHERE username=?`
_Tweet_GetUserPostCollections = `SELECT * FROM @user WHERE username=?`
_Tweet_GetUserPostCount = `SELECT count(*) FROM @post WHERE user_id=? AND visibility IN (?)`
_Tweet_GetUserPostStar = `SELECT * FROM @user WHERE username=?`
_Tweet_GetUserPostStarCount = `SELECT * FROM @user WHERE username=?`
_Tweet_GetUserPostStars = `SELECT * FROM @user WHERE username=?`
_Tweet_GetUserPosts = `SELECT * FROM @post WHERE user_id=? AND visibility IN (?) ORDER BY latest_replies_on DESC LIMIT ? OFFSET ?`
_TweetHelpA_UserInfo = `SELECT * FROM @user WHERE username=?`
_TweetHelp_GetPostContentByIds = `SELECT * FROM @user WHERE username=?`
_TweetHelp_GetUsersByIds = `SELECT * FROM @user WHERE username=?`
_TweetHelp_GetPostContentByIds = `SELECT id, post_id, content, type, sort FROM @post_content WHERE post_id IN (?) AND is_del=0`
_TweetHelp_GetUsersByIds = `SELECT id, username, nickname, status, avatar, is_admin FROM @user WHERE id IN (?) AND is_del=0`
_TweetManageA_UserInfo = `SELECT * FROM @user WHERE username=?`
_TweetManage_AddAttachment = `SELECT * FROM @user WHERE username=?`
_TweetManage_AddPost = `SELECT * FROM @user WHERE username=?`
_TweetManage_AddPostCollection = `SELECT * FROM @user WHERE username=?`
_TweetManage_AddPostContent = `SELECT * FROM @user WHERE username=?`
_TweetManage_AddPostStar = `SELECT * FROM @user WHERE username=?`
_TweetManage_DelPost = `SELECT * FROM @user WHERE username=?`
_TweetManage_DelPostCollection = `SELECT * FROM @user WHERE username=?`
_TweetManage_DelPostStar = `SELECT * FROM @user WHERE username=?`
_TweetManage_LockPost = `SELECT * FROM @user WHERE username=?`
_TweetManage_StickPost = `SELECT * FROM @user WHERE username=?`
_TweetManage_UpdatePost = `SELECT * FROM @user WHERE username=?`
_TweetManage_VisiblePost = `SELECT * FROM @user WHERE username=?`
_TweetManage_AddAttachment = `INSERT INTO @attachment (user_id, file_size, img_width, img_height, type, content, created_on) VALUES (?, ?, ?, ?, ?, ?, ?)`
_TweetManage_AddPost = `INSERT INTO @post (user_id, tags, ip, ip_loc, attachment_price, visibility, latest_replies_on, created_on) VALUES (:user_id, :tags, :ip, :ip_loc, :attachment_price, :visibility, :latest_replies_on, :created_on)`
_TweetManage_AddPostCollection = `INSERT INTO @post_collection (post_id, user_id, created_on) VALUES (?, ?, ?)`
_TweetManage_AddPostContent = `INSERT INTO @post_content (post_id, user_id, content, type, sort, created_on) VALUES (:post_id, :user_id, :content, :type, :sort, :created_on)`
_TweetManage_AddPostStar = `INSERT INTO @post_star (post_id, user_id, created_on) VALUES (?, ?, ?)`
_TweetManage_DelPostById = `UPDATE @post SET is_del=1, deleted_on=? WHERE id=? AND is_del=0`
_TweetManage_DelPostCollection = `UPDATE @post_collection SET is_del=1, deleted_on=? WHERE id=? AND is_del=0`
_TweetManage_DelPostStar = `UPDATE @post_star SET is_del=1, deleted_on=? WHERE id=? AND is_del=0`
_TweetManage_LockPost = `UPDATE @post SET is_lock=1-is_lock, modified_on=? WHERE id=? AND is_del=0`
_TweetManage_MediaContentByPostId = `SELECT content FROM post_content WHERE post_id=? AND is_del=0 AND type IN (3, 4, 5, 7, 8)`
_TweetManage_StickPost = `UPDATE @post SET is_top=1-is_top, modified_on=? WHERE id=? AND is_del=0`
_TweetManage_UpdatePost = `UPDATE @post SET comment_count=:comment_count, upvote_count=:upvote_count, collection_count=:collection_count, latest_replies_on=:latest_replies_on, modified_on=:modified_on WHERE id=:id AND is_del=0`
_TweetManage_VisiblePost = `UPDATE @post SET visibility=?, is_top=?, modified_on=? WHERE id=? AND is_del=0`
_UserManage_CreateUser = `INSERT INTO @user (username, nickname, password, salt, avatar, status, created_on) VALUES (:username, :nickname, :password, :salt, :avatar, :status, :created_on)`
_UserManage_GetAnyUsers = `SELECT * FROM @user WHERE is_del=0 ORDER BY id ASC limit 6`
_UserManage_GetUserById = `SELECT * FROM @user WHERE id=? AND is_del=0`
_UserManage_GetUserByPhone = `SELECT * FROM @user WHERE phone=? AND is_del=0`
_UserManage_GetUserByUsername = `SELECT * FROM @user WHERE username=? AND is_del=0`
_UserManage_GetUsersByIds = `SELECT * FROM @user WHERE id IN ? AND is_del=0`
_UserManage_GetUsersByIds = `SELECT * FROM @user WHERE id IN (?) AND is_del=0`
_UserManage_GetUsersByKeyword = `SELECT * FROM @user WHERE username LIKE ? AND is_del=0 limit 6`
_UserManage_UpdateUser = `UPDATE @user SET username=:username, nickname=:nickname, phone=:phone, password=:password, salt=:salt, status=:status, avatar=:avatar, balance=:balance, is_admin=:is_admin, modified_on=:modified_on WHERE id=? AND is_del=0`
_Wallet_AddUserBalance = `UPDATE @user SET balance=balance+?, modified_on=? WHERE id=? AND is_del=0`
@ -137,6 +142,11 @@ const (
_Wallet_NewPostBill = `INSERT INTO @wallet_statement (post_id, user_id, change_amount, balance_snapshot, reason, created_on) VALUES (?, ?, ?, ?, ?, ?)`
)
type Yesql struct {
TagsFromNames string `yesql:"tags_from_names"`
UpdateTagQuote *sqlx.Stmt `yesql:"update_tag_quote"`
}
type AuthorizationManage struct {
yesql.Namespace `yesql:"authorization_manage"`
BeFriendIds *sqlx.Stmt `yesql:"be_friend_ids"`
@ -262,12 +272,14 @@ type TopicA struct {
type Tweet struct {
yesql.Namespace `yesql:"tweet"`
GetPostContetnsByIds string `yesql:"get_post_contetns_by_ids"`
GetAnyPostCount string `yesql:"get_any_post_count"`
GetAnyPosts string `yesql:"get_any_posts"`
GetPostContentsByIds string `yesql:"get_post_contents_by_ids"`
GetUserPostCount string `yesql:"get_user_post_count"`
GetUserPosts string `yesql:"get_user_posts"`
GetPostAttachmentBill *sqlx.Stmt `yesql:"get_post_attachment_bill"`
GetPostById *sqlx.Stmt `yesql:"get_post_by_id"`
GetPostContentById *sqlx.Stmt `yesql:"get_post_content_by_id"`
GetPostCount *sqlx.Stmt `yesql:"get_post_count"`
GetPosts *sqlx.Stmt `yesql:"get_posts"`
GetUserPostCollection *sqlx.Stmt `yesql:"get_user_post_collection"`
GetUserPostCollectionCount *sqlx.Stmt `yesql:"get_user_post_collection_count"`
GetUserPostCollections *sqlx.Stmt `yesql:"get_user_post_collections"`
@ -304,19 +316,20 @@ type TweetHelpA struct {
}
type TweetManage struct {
yesql.Namespace `yesql:"tweet_manage"`
AddAttachment *sqlx.Stmt `yesql:"add_attachment"`
AddPost *sqlx.Stmt `yesql:"add_post"`
AddPostCollection *sqlx.Stmt `yesql:"add_post_collection"`
AddPostContent *sqlx.Stmt `yesql:"add_post_content"`
AddPostStar *sqlx.Stmt `yesql:"add_post_star"`
DelPost *sqlx.Stmt `yesql:"del_post"`
DelPostCollection *sqlx.Stmt `yesql:"del_post_collection"`
DelPostStar *sqlx.Stmt `yesql:"del_post_star"`
LockPost *sqlx.Stmt `yesql:"lock_post"`
StickPost *sqlx.Stmt `yesql:"stick_post"`
UpdatePost *sqlx.Stmt `yesql:"update_post"`
VisiblePost *sqlx.Stmt `yesql:"visible_post"`
yesql.Namespace `yesql:"tweet_manage"`
AddAttachment *sqlx.Stmt `yesql:"add_attachment"`
AddPostCollection *sqlx.Stmt `yesql:"add_post_collection"`
AddPostStar *sqlx.Stmt `yesql:"add_post_star"`
DelPostById *sqlx.Stmt `yesql:"del_post_by_id"`
DelPostCollection *sqlx.Stmt `yesql:"del_post_collection"`
DelPostStar *sqlx.Stmt `yesql:"del_post_star"`
LockPost *sqlx.Stmt `yesql:"lock_post"`
MediaContentByPostId *sqlx.Stmt `yesql:"media_content_by_post_id"`
StickPost *sqlx.Stmt `yesql:"stick_post"`
VisiblePost *sqlx.Stmt `yesql:"visible_post"`
AddPost *sqlx.NamedStmt `yesql:"add_post"`
AddPostContent *sqlx.NamedStmt `yesql:"add_post_content"`
UpdatePost *sqlx.NamedStmt `yesql:"update_post"`
}
type TweetManageA struct {
@ -352,6 +365,22 @@ type Wallet struct {
NewPostBill *sqlx.Stmt `yesql:"new_post_bill"`
}
func BuildYesql(p yesql.PreparexBuilder, ctx ...context.Context) (obj *Yesql, err error) {
var c context.Context
if len(ctx) > 0 && ctx[0] != nil {
c = ctx[0]
} else {
c = context.Background()
}
obj = &Yesql{
TagsFromNames: p.QueryHook(_TagsFromNames),
}
if obj.UpdateTagQuote, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_UpdateTagQuote))); err != nil {
return
}
return
}
func BuildAuthorizationManage(p yesql.PreparexBuilder, ctx ...context.Context) (obj *AuthorizationManage, err error) {
var c context.Context
if len(ctx) > 0 && ctx[0] != nil {
@ -698,7 +727,11 @@ func BuildTweet(p yesql.PreparexBuilder, ctx ...context.Context) (obj *Tweet, er
c = context.Background()
}
obj = &Tweet{
GetPostContetnsByIds: p.QueryHook(_Tweet_GetPostContetnsByIds),
GetAnyPostCount: p.QueryHook(_Tweet_GetAnyPostCount),
GetAnyPosts: p.QueryHook(_Tweet_GetAnyPosts),
GetPostContentsByIds: p.QueryHook(_Tweet_GetPostContentsByIds),
GetUserPostCount: p.QueryHook(_Tweet_GetUserPostCount),
GetUserPosts: p.QueryHook(_Tweet_GetUserPosts),
}
if obj.GetPostAttachmentBill, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Tweet_GetPostAttachmentBill))); err != nil {
return
@ -709,12 +742,6 @@ func BuildTweet(p yesql.PreparexBuilder, ctx ...context.Context) (obj *Tweet, er
if obj.GetPostContentById, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Tweet_GetPostContentById))); err != nil {
return
}
if obj.GetPostCount, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Tweet_GetPostCount))); err != nil {
return
}
if obj.GetPosts, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Tweet_GetPosts))); err != nil {
return
}
if obj.GetUserPostCollection, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Tweet_GetUserPostCollection))); err != nil {
return
}
@ -816,19 +843,13 @@ func BuildTweetManage(p yesql.PreparexBuilder, ctx ...context.Context) (obj *Twe
if obj.AddAttachment, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_AddAttachment))); err != nil {
return
}
if obj.AddPost, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_AddPost))); err != nil {
return
}
if obj.AddPostCollection, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_AddPostCollection))); err != nil {
return
}
if obj.AddPostContent, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_AddPostContent))); err != nil {
return
}
if obj.AddPostStar, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_AddPostStar))); err != nil {
return
}
if obj.DelPost, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_DelPost))); err != nil {
if obj.DelPostById, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_DelPostById))); err != nil {
return
}
if obj.DelPostCollection, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_DelPostCollection))); err != nil {
@ -840,15 +861,24 @@ func BuildTweetManage(p yesql.PreparexBuilder, ctx ...context.Context) (obj *Twe
if obj.LockPost, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_LockPost))); err != nil {
return
}
if obj.StickPost, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_StickPost))); err != nil {
if obj.MediaContentByPostId, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_MediaContentByPostId))); err != nil {
return
}
if obj.UpdatePost, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_UpdatePost))); err != nil {
if obj.StickPost, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_StickPost))); err != nil {
return
}
if obj.VisiblePost, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_TweetManage_VisiblePost))); err != nil {
return
}
if obj.AddPost, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_TweetManage_AddPost))); err != nil {
return
}
if obj.AddPostContent, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_TweetManage_AddPostContent))); err != nil {
return
}
if obj.UpdatePost, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_TweetManage_UpdatePost))); err != nil {
return
}
return
}

@ -1,3 +1,17 @@
--------------------------------------------------------------------------------
-- global sql dml
--------------------------------------------------------------------------------
-- name: tags_from_names
-- prepare: raw
-- clause: in
SELECT * FROM @tag WHERE tag IN (?) AND is_del=0;
-- name: update_tag_quote
-- prepare: stmt
UPDATE @tag SET quote_num=?, modified_on=?
WHERE id=? AND is_del=0;
--------------------------------------------------------------------------------
-- authorization_manage sql dml
--------------------------------------------------------------------------------
@ -37,17 +51,17 @@ SELECT * FROM @comment_reply WHERE id=? AND is_del=0;
-- name: get_comment_contents_by_ids@comment
-- prepare: raw
-- clause: in
SELECT * FROM @comment_content WHERE comment_id IN ?;
SELECT * FROM @comment_content WHERE comment_id IN (?);
-- name: get_commment_replies_by_ids@comment
-- prepare: raw
-- clause: in
SELECT * FROM @comment_reply WHERE comment_id IN ? ORDER BY id ASC;
SELECT * FROM @comment_reply WHERE comment_id IN (?) ORDER BY id ASC;
-- name: get_users_by_ids@comment
-- prepare: raw
-- clause: in
SELECT id, nickname, username, status, avatar, is_admin FROM @user WHERE id IN ?;
SELECT id, nickname, username, status, avatar, is_admin FROM @user WHERE id IN (?);
-- name: get_comment_thumbs@comment
-- prepare: stmt
@ -317,15 +331,30 @@ SELECT * FROM @user WHERE username=?
-- name: get_post_by_id@tweet
-- prepare: stmt
SELECT * FROM @user WHERE username=?
SELECT * FROM @post WHERE id=? AND is_del=0;
-- name: get_posts@tweet
-- prepare: stmt
SELECT * FROM @user WHERE username=?
-- name: get_user_posts@tweet
-- prepare: raw
-- clause: in
SELECT * FROM @post
WHERE user_id=? AND visibility IN (?)
ORDER BY latest_replies_on DESC
LIMIT ? OFFSET ?;
-- name: get_post_count@tweet
-- prepare: stmt
SELECT * FROM @user WHERE username=?
-- name: get_any_posts@tweet
-- prepare: raw
-- clause: in
SELECT * FROM @post WHERE visibility IN (?) AND is_del=0 LIMIT ? OFFSET ?;
-- name: get_user_post_count@tweet
-- prepare: raw
-- clause: in
SELECT count(*) FROM @post WHERE user_id=? AND visibility IN (?);
-- name: get_any_post_count@tweet
-- prepare: raw
-- clause: in
SELECT count(*) FROM @post WHERE visibility IN (?);
-- name: get_user_post_star@tweet
-- prepare: stmt
@ -355,10 +384,11 @@ SELECT * FROM @user WHERE username=?
-- prepare: stmt
SELECT * FROM @user WHERE username=?
-- name: get_post_contetns_by_ids@tweet
-- name: get_post_contents_by_ids@tweet
-- prepare: raw
-- clause: in
SELECT id, user_id, tag, quote_num FROM @tag WHERE tag IN (?);
SELECT * FROM @post_content
WHERE post_id IN (?) AND is_del=0;
-- name: get_post_content_by_id@tweet
-- prepare: stmt
@ -369,52 +399,66 @@ SELECT * FROM @user WHERE username=?
--------------------------------------------------------------------------------
-- name: add_post@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
-- prepare: named_stmt
INSERT INTO @post (user_id, tags, ip, ip_loc, attachment_price, visibility, latest_replies_on, created_on)
VALUES (:user_id, :tags, :ip, :ip_loc, :attachment_price, :visibility, :latest_replies_on, :created_on);
-- name: media_content_by_post_id@tweet_manage
-- prepare stmt
SELECT content FROM post_content WHERE post_id=? AND is_del=0 AND type IN (3, 4, 5, 7, 8);
-- name: del_post@tweet_manage
-- name: del_post_by_id@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
UPDATE @post SET is_del=1, deleted_on=? WHERE id=? AND is_del=0;
-- name: lock_post@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
UPDATE @post SET is_lock=1-is_lock, modified_on=? WHERE id=? AND is_del=0;
-- name: stick_post@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
UPDATE @post SET is_top=1-is_top, modified_on=? WHERE id=? AND is_del=0;
-- name: visible_post@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
UPDATE @post SET visibility=?, is_top=?, modified_on=? WHERE id=? AND is_del=0;
-- name: update_post@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
-- prepare: named_stmt
UPDATE @post SET comment_count=:comment_count,
upvote_count=:upvote_count,
collection_count=:collection_count,
latest_replies_on=:latest_replies_on,
modified_on=:modified_on
WHERE id=:id AND is_del=0;
-- name: add_post_star@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
INSERT INTO @post_star (post_id, user_id, created_on)
VALUES (?, ?, ?);
-- name: del_post_star@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
UPDATE @post_star SET is_del=1, deleted_on=? WHERE id=? AND is_del=0;
-- name: add_post_collection@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
INSERT INTO @post_collection (post_id, user_id, created_on)
VALUES (?, ?, ?);
-- name: del_post_collection@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
UPDATE @post_collection SET is_del=1, deleted_on=? WHERE id=? AND is_del=0;
-- name: add_post_content@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
-- prepare: named_stmt
INSERT INTO @post_content (post_id, user_id, content, type, sort, created_on)
VALUES (:post_id, :user_id, :content, :type, :sort, :created_on);
-- name: add_attachment@tweet_manage
-- prepare: stmt
SELECT * FROM @user WHERE username=?
INSERT INTO @attachment (user_id, file_size, img_width, img_height, type, content, created_on)
VALUES (?, ?, ?, ?, ?, ?, ?);
--------------------------------------------------------------------------------
-- tweet_help sql dml
@ -423,12 +467,16 @@ SELECT * FROM @user WHERE username=?
-- name: get_post_content_by_ids@tweet_help
-- prepare: raw
-- clause: in
SELECT * FROM @user WHERE username=?
SELECT id, post_id, content, type, sort
FROM @post_content
WHERE post_id IN (?) AND is_del=0;
-- name: get_users_by_ids@tweet_help
-- prepare: raw
-- clause: in
SELECT * FROM @user WHERE username=?
SELECT id, username, nickname, status, avatar, is_admin
FROM @user
WHERE id IN (?) AND is_del=0;
--------------------------------------------------------------------------------
-- tweet_a sql dml
@ -584,7 +632,7 @@ SELECT * FROM @user WHERE phone=? AND is_del=0;
-- name: get_users_by_ids@user_manage
-- prepare: raw
-- clause: in
SELECT * FROM @user WHERE id IN ? AND is_del=0;
SELECT * FROM @user WHERE id IN (?) AND is_del=0;
-- name: get_users_by_keyword@user_manage
-- prepare: stmt

@ -207,13 +207,14 @@ func (s *DaoServant) PushPostsToSearch(c context.Context) {
defer s.Redis.DelPushToSearchJob(c)
splitNum := 1000
totalRows, _ := s.Ds.GetPostCount(&ms.ConditionsT{
conditions := ms.ConditionsT{
"visibility IN ?": []core.PostVisibleT{core.PostVisitPublic, core.PostVisitFriend},
})
}
totalRows, _ := s.Ds.GetPostCount(conditions)
pages := math.Ceil(float64(totalRows) / float64(splitNum))
nums := int(pages)
for i := 0; i < nums; i++ {
posts, postsFormated, err := s.GetTweetList(&ms.ConditionsT{}, i*splitNum, splitNum)
posts, postsFormated, err := s.GetTweetList(conditions, i*splitNum, splitNum)
if err != nil || len(posts) != len(postsFormated) {
continue
}
@ -264,7 +265,7 @@ func (s *DaoServant) DeleteSearchPost(post *ms.Post) error {
return s.Ts.DeleteDocuments([]string{fmt.Sprintf("%d", post.ID)})
}
func (s *DaoServant) GetTweetList(conditions *ms.ConditionsT, offset, limit int) ([]*ms.Post, []*ms.PostFormated, error) {
func (s *DaoServant) GetTweetList(conditions ms.ConditionsT, offset, limit int) ([]*ms.Post, []*ms.PostFormated, error) {
posts, err := s.Ds.GetPosts(conditions, offset, limit)
if err != nil {
return nil, nil, err

@ -152,7 +152,7 @@ func (s *looseSrv) getUserPostTweets(req *web.GetUserTweetsReq) (*web.GetUserTwe
visibilities = append(visibilities, core.PostVisitFriend)
}
}
conditions := &ms.ConditionsT{
conditions := ms.ConditionsT{
"user_id": other.ID,
"visibility IN ?": visibilities,
"ORDER": "latest_replied_on DESC",

Loading…
Cancel
Save