mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
221 lines
5.3 KiB
221 lines
5.3 KiB
2 years ago
|
// 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 (
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
1 year ago
|
"github.com/alimy/cfg"
|
||
1 year ago
|
"github.com/bitbus/sqlx"
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/internal/core"
|
||
|
"github.com/rocboss/paopao-ce/internal/core/cs"
|
||
1 year ago
|
"github.com/rocboss/paopao-ce/internal/dao/sakila/auto/ac"
|
||
1 year ago
|
"github.com/rocboss/paopao-ce/internal/dao/sakila/auto/pga"
|
||
2 years ago
|
)
|
||
|
|
||
|
var (
|
||
2 years ago
|
_ core.TopicService = (*topicSrvA)(nil)
|
||
|
_ core.TopicServantA = (*topicSrvA)(nil)
|
||
2 years ago
|
)
|
||
|
|
||
1 year ago
|
type topicInfo struct {
|
||
|
TopicId int64
|
||
|
IsTop int8
|
||
|
}
|
||
|
|
||
2 years ago
|
type topicSrvA struct {
|
||
2 years ago
|
*sqlxSrv
|
||
1 year ago
|
q *ac.TopicA
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (s *topicSrvA) UpsertTags(userId int64, tags []string) (res cs.TagInfoList, xerr error) {
|
||
2 years ago
|
if len(tags) == 0 {
|
||
|
return nil, nil
|
||
|
}
|
||
1 year ago
|
xerr = s.db.Withx(func(tx *sqlx.Tx) error {
|
||
2 years ago
|
var upTags cs.TagInfoList
|
||
1 year ago
|
if err := tx.InSelect(&upTags, s.q.TagsForIncr, tags); err != nil {
|
||
2 years ago
|
return err
|
||
|
}
|
||
|
now := time.Now().Unix()
|
||
|
if len(upTags) > 0 {
|
||
|
var ids []int64
|
||
|
for _, t := range upTags {
|
||
|
ids = append(ids, t.ID)
|
||
|
t.QuoteNum++
|
||
|
// prepare remain tags just delete updated tag
|
||
|
// notice ensure tags slice is distinct elements
|
||
|
for i, name := range tags {
|
||
|
if name == t.Tag {
|
||
|
lastIdx := len(tags) - 1
|
||
|
tags[i] = tags[lastIdx]
|
||
|
tags = tags[:lastIdx]
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
1 year ago
|
if _, err := tx.InExec(s.q.IncrTagsById, now, ids); err != nil {
|
||
2 years ago
|
return err
|
||
|
}
|
||
|
res = append(res, upTags...)
|
||
|
}
|
||
|
// process remain tags if tags is not empty
|
||
|
if len(tags) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
var ids []int64
|
||
|
for _, tag := range tags {
|
||
2 years ago
|
res, err := s.q.InsertTag.Exec(userId, tag, now, now)
|
||
2 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
id, err := res.LastInsertId()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
ids = append(ids, id)
|
||
|
}
|
||
|
var newTags cs.TagInfoList
|
||
1 year ago
|
if err := tx.InSelect(&newTags, s.q.TagsByIdB, ids); err != nil {
|
||
2 years ago
|
return err
|
||
|
}
|
||
|
res = append(res, newTags...)
|
||
|
return nil
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *topicSrvA) DecrTagsById(ids []int64) error {
|
||
1 year ago
|
return s.db.Withx(func(tx *sqlx.Tx) error {
|
||
2 years ago
|
var ids []int64
|
||
1 year ago
|
err := tx.InSelect(&ids, s.q.TagsByIdA, ids)
|
||
2 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
1 year ago
|
_, err = tx.InExec(s.q.DecrTagsById, time.Now().Unix(), ids)
|
||
2 years ago
|
return err
|
||
|
})
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *topicSrvA) ListTags(typ cs.TagType, limit int, offset int) (res cs.TagList, err error) {
|
||
2 years ago
|
switch typ {
|
||
|
case cs.TagTypeHot:
|
||
2 years ago
|
err = s.q.HotTags.Select(&res, limit, offset)
|
||
2 years ago
|
case cs.TagTypeNew:
|
||
2 years ago
|
err = s.q.NewestTags.Select(&res, limit, offset)
|
||
2 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *topicSrvA) TagsByKeyword(keyword string) (res cs.TagInfoList, err error) {
|
||
2 years ago
|
keyword = "%" + strings.Trim(keyword, " ") + "%"
|
||
|
if keyword == "%%" {
|
||
2 years ago
|
err = s.q.TagsByKeywordA.Select(&res)
|
||
2 years ago
|
} else {
|
||
2 years ago
|
err = s.q.TagsByKeywordB.Select(&res)
|
||
2 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
1 year ago
|
func (s *topicSrvA) GetHotTags(userId int64, limit int, offset int) (res cs.TagList, err error) {
|
||
|
if err = s.q.HotTags.Select(&res, limit, offset); err != nil {
|
||
|
return
|
||
|
}
|
||
|
return s.tagsFormatA(userId, res)
|
||
2 years ago
|
}
|
||
|
|
||
1 year ago
|
func (s *topicSrvA) GetNewestTags(userId int64, limit int, offset int) (res cs.TagList, err error) {
|
||
|
err = s.q.NewestTags.Select(&res, limit, offset)
|
||
|
return
|
||
2 years ago
|
}
|
||
|
|
||
1 year ago
|
func (s *topicSrvA) GetFollowTags(userId int64, limit int, offset int) (res cs.TagList, err error) {
|
||
|
if err = s.q.FollowTags.Select(&res, userId, limit, offset); err != nil {
|
||
|
return
|
||
|
}
|
||
|
return s.tagsFormatA(userId, res)
|
||
2 years ago
|
}
|
||
|
|
||
1 year ago
|
func (s *topicSrvA) FollowTopic(userId int64, topicId int64) (err error) {
|
||
|
exist := false
|
||
1 year ago
|
if err = s.q.ExistTopicUser.Get(&exist, userId, topicId); err != nil {
|
||
1 year ago
|
_, err = s.q.FollowTopic.Exec(userId, topicId, time.Now().Unix())
|
||
|
}
|
||
|
return
|
||
2 years ago
|
}
|
||
|
|
||
|
func (s *topicSrvA) UnfollowTopic(userId int64, topicId int64) error {
|
||
1 year ago
|
_, err := s.q.UnfollowTopic.Exec(userId, topicId)
|
||
1 year ago
|
return err
|
||
2 years ago
|
}
|
||
|
|
||
1 year ago
|
func (s *topicSrvA) StickTopic(userId int64, topicId int64) (res int8, err error) {
|
||
|
s.db.Withx(func(tx *sqlx.Tx) error {
|
||
|
_, err = tx.Stmtx(s.q.StickTopic).Exec(time.Now().Unix(), userId, topicId)
|
||
|
if err == nil {
|
||
|
err = tx.Stmtx(s.q.TopicIsTop).Get(&res, userId, topicId)
|
||
|
}
|
||
|
return err
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (s *topicSrvA) tagsFormatA(userId int64, tags cs.TagList) (cs.TagList, error) {
|
||
|
// 获取创建者User IDs
|
||
|
tagIds := []int64{}
|
||
|
for _, tag := range tags {
|
||
|
tagIds = append(tagIds, tag.ID)
|
||
|
}
|
||
|
// 填充话题follow信息
|
||
|
if userId > -1 {
|
||
|
userTopics := []*topicInfo{}
|
||
|
err := s.db.InSelect(&userTopics, s.q.TopicInfos, userId, tagIds)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
userTopicsMap := make(map[int64]*topicInfo, len(userTopics))
|
||
|
for _, info := range userTopics {
|
||
|
userTopicsMap[info.TopicId] = info
|
||
|
}
|
||
|
for _, tag := range tags {
|
||
|
if info, exist := userTopicsMap[tag.ID]; exist {
|
||
|
tag.IsFollowing, tag.IsTop = 1, info.IsTop
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return tags, nil
|
||
2 years ago
|
}
|
||
|
|
||
1 year ago
|
func newTopicService(db *sqlx.DB) (s core.TopicService) {
|
||
|
ts := &topicSrvA{
|
||
2 years ago
|
sqlxSrv: newSqlxSrv(db),
|
||
1 year ago
|
q: acBuild(db, ac.BuildTopicA),
|
||
2 years ago
|
}
|
||
1 year ago
|
s = ts
|
||
|
if cfg.Any("PostgreSQL", "PgSQL", "Postgres") {
|
||
|
s = &pgaTopicSrvA{
|
||
|
topicSrvA: ts,
|
||
|
p: pgaBuild(db, pga.BuildTopicA),
|
||
|
}
|
||
|
}
|
||
|
return
|
||
2 years ago
|
}
|
||
|
|
||
1 year ago
|
func newTopicServantA(db *sqlx.DB) (s core.TopicServantA) {
|
||
|
ts := &topicSrvA{
|
||
2 years ago
|
sqlxSrv: newSqlxSrv(db),
|
||
1 year ago
|
q: acBuild(db, ac.BuildTopicA),
|
||
2 years ago
|
}
|
||
1 year ago
|
s = ts
|
||
|
if cfg.Any("PostgreSQL", "PgSQL", "Postgres") {
|
||
|
s = &pgaTopicSrvA{
|
||
|
topicSrvA: ts,
|
||
|
p: pgaBuild(db, pga.BuildTopicA),
|
||
|
}
|
||
|
}
|
||
|
return
|
||
2 years ago
|
}
|