/
tag.go
68 lines (62 loc) · 1.8 KB
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package service
import (
"context"
"go-common/library/log"
"strings"
"go-common/app/interface/main/tag/model"
"go-common/app/interface/openplatform/article/dao"
artmdl "go-common/app/interface/openplatform/article/model"
)
// bindTags add tags for article by upper.
func (s *Service) bindTags(c context.Context, mid, aid int64, tags []string, ip string) (err error) {
arg := &model.ArgBind{Type: model.PicResType, Mid: mid, Oid: aid, Names: tags}
if err = s.tagRPC.UpBind(c, arg); err != nil {
dao.PromError("rpc:bind tag")
log.Error("s.tagRPC.UpBind(%v) error(%+v)", arg, err)
}
return
}
// Tags gets article tags.
func (s *Service) Tags(c context.Context, aid int64, skipAct bool) (res []*artmdl.Tag, err error) {
var (
tags map[int64][]*model.Tag
arg = &model.ArgResTags{Type: model.PicResType, Oids: []int64{aid}}
)
if tags, err = s.tagRPC.ResTags(c, arg); err != nil {
log.Error("s.Tags(%d) error(%+v)", aid, err)
dao.PromError("rpc:获取Tag")
return
} else if tags == nil || len(tags[aid]) == 0 {
return
}
for _, t := range tags[aid] {
if skipAct && t.Type == 4 {
continue
}
tag := &artmdl.Tag{Tid: t.ID, Name: t.Name}
res = append(res, tag)
}
return
}
// BindTags bind tags with activity
func (s *Service) BindTags(c context.Context, mid, aid int64, tags []string, ip string, activityID int64) (err error) {
activity := s.activities[activityID]
if (activityID) > 0 && (activity != nil) && (activity.Tags != "") {
actTags := strings.Split(activity.Tags, ",")
tags = mergeActivityTags(tags, actTags)
}
return s.bindTags(c, mid, aid, tags, ip)
}
func mergeActivityTags(tags, actTags []string) (res []string) {
m := map[string]bool{}
for _, t := range tags {
m[t] = true
res = append(res, t)
}
for _, t := range actTags {
if !m[t] {
res = append(res, t)
}
}
return
}