...
|
...
|
@@ -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 *
|
...
|
...
|
|