...
|
...
|
@@ -2,6 +2,7 @@ package repository |
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
"github.com/pkg/errors"
|
...
|
...
|
@@ -132,6 +133,59 @@ func (repository *ArticleAndTagRepository) DomainModelToModel(from *domain.Artic |
|
|
return to, err
|
|
|
}
|
|
|
|
|
|
// 以TagId作为分组,统计所有已有标签的文章和人员已读的文章
|
|
|
func (repository *ArticleAndTagRepository) CountArticleReadGroupByTag(ctx context.Context, conn transaction.Conn, userId int64, companyId int64) ([]*domain.CountArticleTagRead, error) {
|
|
|
|
|
|
sqlStr := `
|
|
|
-- 首页统计数据
|
|
|
with
|
|
|
-- 按查看权限查询文章
|
|
|
-- 获取有标签的文章
|
|
|
-- 过滤出可展示的文章id
|
|
|
t_article_and_tag_2 as (
|
|
|
select article_and_tag.article_id , article_and_tag.tag_id
|
|
|
from article_and_tag
|
|
|
join article on article_and_tag.article_id = article.id
|
|
|
where article.deleted_at=0
|
|
|
and article.company_id =1598224576532189184
|
|
|
and article."show" =0
|
|
|
and (article.target_user =0 or article.who_read @>'[1]')
|
|
|
),
|
|
|
-- 查询人员已查看的文章
|
|
|
t_user_read as(
|
|
|
select user_read_article.article_id from user_read_article where user_read_article.user_id =1
|
|
|
)
|
|
|
-- 汇总统计 total_article 符合条件的文章总数,read_article 已浏览的数量
|
|
|
select count(t_article_and_tag_2.article_id) as total_article ,count(t_user_read.article_id) as read_article, t_article_and_tag_2.tag_id
|
|
|
from t_article_and_tag_2
|
|
|
left join t_user_read on t_article_and_tag_2.article_id=t_user_read.article_id
|
|
|
group by t_article_and_tag_2.tag_id
|
|
|
`
|
|
|
condition := []interface{}{
|
|
|
companyId,
|
|
|
fmt.Sprintf("[%d]", userId),
|
|
|
userId,
|
|
|
}
|
|
|
|
|
|
m := []*models.CountArticleTagRead{}
|
|
|
db := conn.DB()
|
|
|
result := db.Raw(sqlStr, condition...).Scan(&m)
|
|
|
if result.Error != nil {
|
|
|
return nil, result.Error
|
|
|
}
|
|
|
|
|
|
var dm []*domain.CountArticleTagRead
|
|
|
|
|
|
for _, val := range m {
|
|
|
dm = append(dm, &domain.CountArticleTagRead{
|
|
|
TagId: val.TagId,
|
|
|
TotalArticle: val.TotalArticle,
|
|
|
ReadArticle: val.ReadArticle,
|
|
|
})
|
|
|
}
|
|
|
return dm, nil
|
|
|
}
|
|
|
|
|
|
func NewArticleAndTagRepository(cache *cache.CachedRepository) domain.ArticleAndTagRepository {
|
|
|
return &ArticleAndTagRepository{CachedRepository: cache}
|
|
|
} |
...
|
...
|
|