作者 tangxvhui

日常保存

... ... @@ -488,15 +488,15 @@ type (
//小程序首页搜索文章
type (
MiniSearchArticleRequest {
Page int `json:"page"`
Size int `json:"size"`
CompanyId int64 `json:",optional"`
UserId int64 `json:",optional"`
TagGroup string `json:"tagGroup"`
TagId int64 `json:"tagId"`
BeginTime int64 `json:"beginTime"`
EndTime int `json:"endTime"`
SearchWord string `json:"searchWord"`
Page int `json:"page"`
Size int `json:"size"`
CompanyId int64 `json:",optional"`
UserId int64 `json:",optional"`
TagCategory string `json:"tagCategory"`
TagId int64 `json:"tagId"`
BeginTime int64 `json:"beginTime"`
EndTime int64 `json:"endTime"`
SearchWord string `json:"searchWord"`
}
//
MiniSearchArticleResponse {
... ...
... ... @@ -5,6 +5,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"github.com/zeromicro/go-zero/core/logx"
)
... ... @@ -23,8 +24,24 @@ func NewMiniSearchArticlePageLogic(ctx context.Context, svcCtx *svc.ServiceConte
}
}
// 小程序端搜索展示文章列表
func (l *MiniSearchArticlePageLogic) MiniSearchArticlePage(req *types.MiniSearchArticleRequest) (resp *types.MiniSearchArticleResponse, err error) {
// todo: add your logic here and delete this line
var conn = l.svcCtx.DefaultDBConn()
cnt, articleList, err := l.svcCtx.ArticleRepository.CustomSearchBy(l.ctx, conn, req.UserId, req.CompanyId,
req.TagCategory, req.TagId, [2]int64{req.BeginTime, req.EndTime}, req.SearchWord, req.Page, req.Size)
if err != nil {
return nil, xerr.NewErrMsgErr("获取文章列表", err)
}
articleIds := []int64{}
for _, val := range articleList {
articleIds = append(articleIds, val.Id)
}
resp = &types.MiniSearchArticleResponse{
Total: int(cnt),
}
return
}
... ...
... ... @@ -1027,15 +1027,15 @@ type ArticleTagCount struct {
}
type MiniSearchArticleRequest struct {
Page int `json:"page"`
Size int `json:"size"`
CompanyId int64 `json:",optional"`
UserId int64 `json:",optional"`
TagGroup string `json:"tagGroup"`
TagId int64 `json:"tagId"`
BeginTime int64 `json:"beginTime"`
EndTime int `json:"endTime"`
SearchWord string `json:"searchWord"`
Page int `json:"page"`
Size int `json:"size"`
CompanyId int64 `json:",optional"`
UserId int64 `json:",optional"`
TagCategory string `json:"tagCategory"`
TagId int64 `json:"tagId"`
BeginTime int64 `json:"beginTime"`
EndTime int64 `json:"endTime"`
SearchWord string `json:"searchWord"`
}
type MiniSearchArticleResponse struct {
... ...
... ... @@ -2,6 +2,7 @@ package repository
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
... ... @@ -279,9 +280,67 @@ func NewArticleRepository(cache *cache.CachedRepository) domain.ArticleRepositor
}
// 小程序端搜索查询文章
func (repository *ArticleRepository) SearchBy(ctx context.Context, conn transaction.Conn, userId int64, companyId int64,
tagCategory string, tagId int64, createdAt [2]int) {
// userId 人员id,谁查看文章
// companyId 公司id
// tagCategory 标签分类
// tagId 标签id
// createdAt 文章的发布时间,按范围查询 [开始时间,结束时间]
// titleLike 搜索标题
func (repository *ArticleRepository) CustomSearchBy(ctx context.Context, conn transaction.Conn, userId int64, companyId int64,
tagCategory string, tagId int64, createdAt [2]int64, titleLike string, page int, size int) (int64, []*domain.Article, error) {
var (
tx = conn.DB()
ms []*models.Article
dms = make([]*domain.Article, 0)
total int64
)
tx = tx.Model(&ms).
Where(`article."show" =?`, domain.ArticleShowEnable).
Where(`article."deleted_at" = 0`).
Where(`article."company_id"=?`, companyId).
Where(
fmt.Sprintf(`(article.target_user = 0 or article.who_read @> '[%d]' )`, userId),
)
if createdAt[0] > 0 {
tx = tx.Where("article.created_at >=?", createdAt[0])
}
if createdAt[1] > 0 {
tx = tx.Where("article.created_at <=?", createdAt[1])
}
if tagId > 0 {
tx = tx.Joins(`join article_and_tag on article.id = article_and_tag.article_id`)
tx = tx.Where("article_and_tag.tag_id=?", tagId)
} else if len(tagCategory) > 0 {
tx = tx.Joins(`join article_and_tag on article.id = article_and_tag.article_id`)
tx = tx.Where(`article_and_tag.tag_id =any(select article_tag.id from article_tag where category =%s )`, tagCategory)
}
if len(titleLike) > 0 {
tx = tx.Where("article.title like ?", "%"+titleLike+"%")
}
result := tx.Count(&total)
if result.Error != nil {
return 0, nil, result.Error
}
if size <= 0 {
size = 20
}
if page <= 0 {
page = 1
}
result = tx.Limit(size).Offset((page - 1) * size).Order("id desc").Find(&ms)
if result.Error != nil {
return 0, nil, result.Error
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
// select *
... ...
... ... @@ -127,6 +127,9 @@ func (repository *UserReadArticleRepository) Find(ctx context.Context, conn tran
if v, ok := queryOptions["articleId"]; ok {
tx = tx.Where("article_id=?", v)
}
if v, ok := queryOptions["articleIds"]; ok {
tx = tx.Where("article_id in (?)", v)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
... ...
... ... @@ -41,6 +41,16 @@ type ArticleRepository interface {
IncreaseCountLove(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //点赞数量变动
IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //评论数量变动
IncreaseCountRead(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //浏览数量变动
// 小程序端搜索查询文章
// userId 人员id,谁查看文章
// companyId 公司id
// tagCategory 标签分类
// tagId 标签id
// createdAt 文章的发布时间,按范围查询 [开始时间,结束时间]
// titleLike 搜索标题
CustomSearchBy(ctx context.Context, conn transaction.Conn, userId int64, companyId int64,
tagCategory string, tagId int64, createdAt [2]int64, titleLike string, page int, size int) (int64, []*Article, error)
}
type ArticleTarget int
... ...