作者 tangxvhui

小程序首页数据展示

@@ -314,14 +314,14 @@ type ( @@ -314,14 +314,14 @@ type (
314 TagGroup []ArticleTagGroup `json:"tagGroup"` 314 TagGroup []ArticleTagGroup `json:"tagGroup"`
315 } 315 }
316 ArticleTagGroup { 316 ArticleTagGroup {
317 - Group string `json:"group"` 317 + Category string `json:"category"`
318 Tags []ArticleTagItem `json:"tags"` 318 Tags []ArticleTagItem `json:"tags"`
319 } 319 }
320 ArticleTagItem { 320 ArticleTagItem {
321 - Id int64 `json:"id"`  
322 - Group string `json:"group"`  
323 - Name string `json:"name"`  
324 - Image string `json:"image"` 321 + Id int64 `json:"id"`
  322 + Category string `json:"category"`
  323 + Name string `json:"name"`
  324 + Image string `json:"image"`
325 } 325 }
326 ) 326 )
327 327
@@ -466,10 +466,11 @@ type ( @@ -466,10 +466,11 @@ type (
466 UserId int64 `path:",optional"` 466 UserId int64 `path:",optional"`
467 } 467 }
468 MiniHomePageRespose { 468 MiniHomePageRespose {
469 - Tags []ArticleTagCount `json:"tags"` 469 + TagCategory []string `json:"tagCategory"`
  470 + Tags []ArticleTagCount `json:"tags"`
470 } 471 }
471 ArticleTagCount { 472 ArticleTagCount {
472 - TagGroup string `json:"tagGroup"` // 标签分组 473 + TagCategory string `json:"tagCategory"` // 标签分组
473 TagId int64 `json:"tagId"` // 标签id 474 TagId int64 `json:"tagId"` // 标签id
474 TagImage string `json:"tagImage"` // 对应的图标 475 TagImage string `json:"tagImage"` // 对应的图标
475 TagName string `json:"tagName"` // 标签名称 476 TagName string `json:"tagName"` // 标签名称
@@ -3,26 +3,27 @@ package article @@ -3,26 +3,27 @@ package article
3 import ( 3 import (
4 "net/http" 4 "net/http"
5 5
6 - "github.com/zeromicro/go-zero/rest/httpx"  
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/article" 6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/article"
8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
10 ) 11 )
11 12
  13 +// 展示小程序端首页的数据
12 func MiniShowHomePageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 14 func MiniShowHomePageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
13 return func(w http.ResponseWriter, r *http.Request) { 15 return func(w http.ResponseWriter, r *http.Request) {
14 var req types.MiniHomePageRequest 16 var req types.MiniHomePageRequest
15 - if err := httpx.Parse(r, &req); err != nil {  
16 - httpx.ErrorCtx(r.Context(), w, err)  
17 - return  
18 - } 17 + // if err := httpx.Parse(r, &req); err != nil {
  18 + // httpx.ErrorCtx(r.Context(), w, err)
  19 + // return
  20 + // }
19 21
20 l := article.NewMiniShowHomePageLogic(r.Context(), svcCtx) 22 l := article.NewMiniShowHomePageLogic(r.Context(), svcCtx)
  23 + token := contextdata.GetUserTokenFromCtx(r.Context())
  24 + req.UserId = token.UserId
  25 + req.CompanyId = token.CompanyId
21 resp, err := l.MiniShowHomePage(&req) 26 resp, err := l.MiniShowHomePage(&req)
22 - if err != nil {  
23 - httpx.ErrorCtx(r.Context(), w, err)  
24 - } else {  
25 - httpx.OkJsonCtx(r.Context(), w, resp)  
26 - } 27 + result.HttpResult(r, w, resp, err)
27 } 28 }
28 } 29 }
@@ -38,20 +38,20 @@ func (l *MiniAllArticleTagLogic) MiniAllArticleTag(req *types.MiniAllArticleTagR @@ -38,20 +38,20 @@ func (l *MiniAllArticleTagLogic) MiniAllArticleTag(req *types.MiniAllArticleTagR
38 for _, val := range tagList { 38 for _, val := range tagList {
39 if m, ok := tagMap[val.Category]; ok { 39 if m, ok := tagMap[val.Category]; ok {
40 m = append(m, types.ArticleTagItem{ 40 m = append(m, types.ArticleTagItem{
41 - Id: val.Id,  
42 - Group: val.Category,  
43 - Name: val.Name,  
44 - Image: val.Image.Url, 41 + Id: val.Id,
  42 + Category: val.Category,
  43 + Name: val.Name,
  44 + Image: val.Image.Url,
45 }) 45 })
46 tagMap[val.Category] = m 46 tagMap[val.Category] = m
47 } else { 47 } else {
48 group = append(group, val.Category) 48 group = append(group, val.Category)
49 tagMap[val.Category] = []types.ArticleTagItem{ 49 tagMap[val.Category] = []types.ArticleTagItem{
50 { 50 {
51 - Id: val.Id,  
52 - Group: val.Category,  
53 - Name: val.Name,  
54 - Image: val.Image.Url, 51 + Id: val.Id,
  52 + Category: val.Category,
  53 + Name: val.Name,
  54 + Image: val.Image.Url,
55 }, 55 },
56 } 56 }
57 } 57 }
@@ -61,8 +61,8 @@ func (l *MiniAllArticleTagLogic) MiniAllArticleTag(req *types.MiniAllArticleTagR @@ -61,8 +61,8 @@ func (l *MiniAllArticleTagLogic) MiniAllArticleTag(req *types.MiniAllArticleTagR
61 } 61 }
62 for i := range group { 62 for i := range group {
63 resp.TagGroup = append(resp.TagGroup, types.ArticleTagGroup{ 63 resp.TagGroup = append(resp.TagGroup, types.ArticleTagGroup{
64 - Group: group[i],  
65 - Tags: tagMap[group[i]], 64 + Category: group[i],
  65 + Tags: tagMap[group[i]],
66 }) 66 })
67 } 67 }
68 return resp, nil 68 return resp, nil
@@ -5,7 +5,10 @@ import ( @@ -5,7 +5,10 @@ import (
5 5
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
8 10
  11 + "github.com/samber/lo"
9 "github.com/zeromicro/go-zero/core/logx" 12 "github.com/zeromicro/go-zero/core/logx"
10 ) 13 )
11 14
@@ -24,7 +27,46 @@ func NewMiniShowHomePageLogic(ctx context.Context, svcCtx *svc.ServiceContext) * @@ -24,7 +27,46 @@ func NewMiniShowHomePageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
24 } 27 }
25 28
26 func (l *MiniShowHomePageLogic) MiniShowHomePage(req *types.MiniHomePageRequest) (resp *types.MiniHomePageRespose, err error) { 29 func (l *MiniShowHomePageLogic) MiniShowHomePage(req *types.MiniHomePageRequest) (resp *types.MiniHomePageRespose, err error) {
27 - // todo: add your logic here and delete this line 30 + // 获取所有的标签
  31 + var conn = l.svcCtx.DefaultDBConn()
  32 + queryOption := domain.NewQueryOptions().WithFindOnly()
  33 + _, allTags, err := l.svcCtx.ArticleTagRepository.Find(l.ctx, conn, req.CompanyId, queryOption)
  34 + if err != nil {
  35 + return nil, xerr.NewErrMsgErr("获取标签列表失败", err)
  36 + }
  37 + // 获取统计数据
  38 + countData, err := l.svcCtx.ArticleAndTagRepository.CountArticleReadGroupByTag(l.ctx, conn, req.UserId, req.CompanyId)
  39 + if err != nil {
  40 + return nil, xerr.NewErrMsgErr("获取文章汇总数量失败", err)
  41 + }
  42 + countDataMap := map[int64]*domain.CountArticleTagRead{}
  43 + for _, val := range countData {
  44 + countDataMap[val.TagId] = val
  45 + }
28 46
29 - return 47 + tagCategory := []string{}
  48 + tagCount := []types.ArticleTagCount{}
  49 + for _, val := range allTags {
  50 + tagCategory = append(tagCategory, val.Category)
  51 + m := types.ArticleTagCount{
  52 + TagCategory: val.Category,
  53 + TagId: val.Id,
  54 + TagImage: val.Image.Url,
  55 + TagName: val.Name,
  56 + TagRemark: val.Remark,
  57 + TotalArticle: 0,
  58 + ReadArticle: 0,
  59 + }
  60 + if count, ok := countDataMap[val.Id]; ok {
  61 + m.TotalArticle = count.TotalArticle
  62 + m.ReadArticle = count.ReadArticle
  63 + }
  64 + tagCount = append(tagCount, m)
  65 + }
  66 + tagCategory = lo.Uniq(tagCategory)
  67 + resp = &types.MiniHomePageRespose{
  68 + TagCategory: tagCategory,
  69 + Tags: tagCount,
  70 + }
  71 + return resp, nil
30 } 72 }
@@ -2,6 +2,7 @@ package article @@ -2,6 +2,7 @@ package article
2 2
3 import ( 3 import (
4 "context" 4 "context"
  5 +
5 "github.com/samber/lo" 6 "github.com/samber/lo"
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
@@ -71,10 +72,10 @@ func (l *SystemGetArticleLogic) SystemGetArticle(req *types.SystemArticleGetRequ @@ -71,10 +72,10 @@ func (l *SystemGetArticleLogic) SystemGetArticle(req *types.SystemArticleGetRequ
71 if err == nil && len(tags) > 0 { 72 if err == nil && len(tags) > 0 {
72 lo.ForEach(tags, func(tag *domain.ArticleTag, index int) { 73 lo.ForEach(tags, func(tag *domain.ArticleTag, index int) {
73 resp.Tags = append(resp.Tags, types.ArticleTagItem{ 74 resp.Tags = append(resp.Tags, types.ArticleTagItem{
74 - Id: tag.Id,  
75 - Group: tag.Category,  
76 - Name: tag.Name,  
77 - Image: tag.Image.Url, 75 + Id: tag.Id,
  76 + Category: tag.Category,
  77 + Name: tag.Name,
  78 + Image: tag.Image.Url,
78 }) 79 })
79 }) 80 })
80 } 81 }
@@ -849,15 +849,15 @@ type MiniAllArticleTagResponse struct { @@ -849,15 +849,15 @@ type MiniAllArticleTagResponse struct {
849 } 849 }
850 850
851 type ArticleTagGroup struct { 851 type ArticleTagGroup struct {
852 - Group string `json:"group"`  
853 - Tags []ArticleTagItem `json:"tags"` 852 + Category string `json:"category"`
  853 + Tags []ArticleTagItem `json:"tags"`
854 } 854 }
855 855
856 type ArticleTagItem struct { 856 type ArticleTagItem struct {
857 - Id int64 `json:"id"`  
858 - Group string `json:"group"`  
859 - Name string `json:"name"`  
860 - Image string `json:"image"` 857 + Id int64 `json:"id"`
  858 + Category string `json:"category"`
  859 + Name string `json:"name"`
  860 + Image string `json:"image"`
861 } 861 }
862 862
863 type SystemArticleGetRequest struct { 863 type SystemArticleGetRequest struct {
@@ -999,11 +999,12 @@ type MiniHomePageRequest struct { @@ -999,11 +999,12 @@ type MiniHomePageRequest struct {
999 } 999 }
1000 1000
1001 type MiniHomePageRespose struct { 1001 type MiniHomePageRespose struct {
1002 - Tags []ArticleTagCount `json:"tags"` 1002 + TagCategory []string `json:"tagCategory"`
  1003 + Tags []ArticleTagCount `json:"tags"`
1003 } 1004 }
1004 1005
1005 type ArticleTagCount struct { 1006 type ArticleTagCount struct {
1006 - TagGroup string `json:"tagGroup"` // 标签分组 1007 + TagCategory string `json:"tagCategory"` // 标签分组
1007 TagId int64 `json:"tagId"` // 标签id 1008 TagId int64 `json:"tagId"` // 标签id
1008 TagImage string `json:"tagImage"` // 对应的图标 1009 TagImage string `json:"tagImage"` // 对应的图标
1009 TagName string `json:"tagName"` // 标签名称 1010 TagName string `json:"tagName"` // 标签名称
@@ -143,17 +143,17 @@ with @@ -143,17 +143,17 @@ with
143 -- 获取有标签的文章 143 -- 获取有标签的文章
144 -- 过滤出可展示的文章id 144 -- 过滤出可展示的文章id
145 t_article_and_tag_2 as ( 145 t_article_and_tag_2 as (
146 - select article_and_tag.article_id , article_and_tag.tag_id 146 + select article_and_tag.article_id,article_and_tag.tag_id
147 from article_and_tag 147 from article_and_tag
148 join article on article_and_tag.article_id = article.id 148 join article on article_and_tag.article_id = article.id
149 where article.deleted_at=0 149 where article.deleted_at=0
150 - and article.company_id =1598224576532189184 150 + and article.company_id =?
151 and article."show" =0 151 and article."show" =0
152 - and (article.target_user =0 or article.who_read @>'[1]') 152 + and (article.target_user =0 or article.who_read @> ?)
153 ), 153 ),
154 -- 查询人员已查看的文章 154 -- 查询人员已查看的文章
155 t_user_read as( 155 t_user_read as(
156 - select user_read_article.article_id from user_read_article where user_read_article.user_id =1 156 + select user_read_article.article_id from user_read_article where user_read_article.user_id =?
157 ) 157 )
158 -- 汇总统计 total_article 符合条件的文章总数,read_article 已浏览的数量 158 -- 汇总统计 total_article 符合条件的文章总数,read_article 已浏览的数量
159 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 159 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
@@ -175,7 +175,6 @@ group by t_article_and_tag_2.tag_id @@ -175,7 +175,6 @@ group by t_article_and_tag_2.tag_id
175 } 175 }
176 176
177 var dm []*domain.CountArticleTagRead 177 var dm []*domain.CountArticleTagRead
178 -  
179 for _, val := range m { 178 for _, val := range m {
180 dm = append(dm, &domain.CountArticleTagRead{ 179 dm = append(dm, &domain.CountArticleTagRead{
181 TagId: val.TagId, 180 TagId: val.TagId,
@@ -121,8 +121,7 @@ func (repository *ArticleTagRepository) Find(ctx context.Context, conn transacti @@ -121,8 +121,7 @@ func (repository *ArticleTagRepository) Find(ctx context.Context, conn transacti
121 ) 121 )
122 queryFunc := func() (interface{}, error) { 122 queryFunc := func() (interface{}, error) {
123 tx = tx.Model(&ms). 123 tx = tx.Model(&ms).
124 - Where("company_id=?", companyId).  
125 - Order("id asc") 124 + Where("company_id=?", companyId).Order("sort_by asc").Order("id asc")
126 125
127 if v, ok := queryOptions["name"]; ok { 126 if v, ok := queryOptions["name"]; ok {
128 tx = tx.Where("name like ?", v) 127 tx = tx.Where("name like ?", v)