-
Notifications
You must be signed in to change notification settings - Fork 0
/
taxonomy.go
183 lines (158 loc) · 4.2 KB
/
taxonomy.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
"time"
"github.com/jinzhu/gorm"
"github.com/smtc/glog"
)
var (
// 未分类
UnCategory = Taxonomy{
Id: "0",
Name: "UnCategory",
Slug: "/uncategory",
Description: "uncategory",
Parent: "",
}
)
// create post提交的的json数据字段
type TaxInfo struct {
Taxonomy string `json:"taxonmy"`
TaxName string `json:"tax_name"`
TaxId string `json:"tax_id"`
}
type CategoryInfo struct {
Infoes []TaxInfo `json:"category_info"`
}
// 分类,类似于wordpress的分类,但是把wp_term_taxonomy和wp_terms合并为一个
type Taxonomy struct {
Id string `sql:"size:60" gorm:"primay_key" json:"id"`
SiteId int64 `json:"site_id"`
Name string `sql:"size:200" json:"name"`
Slug string `sql:"size:200" json:"slug"`
TermGroup int `json:"term_group"`
Taxonomy string `sql:"size:60" json:"taxonomy"`
Description string `sql:"size:100000" json:"description"`
Parent string `json:"parent"`
Count int64 `json:"count"`
}
type TermRelation struct {
ObjectId string `json:"object_id"` // post id, reply id, product id, etc...
SiteId int64 `json:"site_id"`
TermId string `json:"term_id"`
CreatedAt time.Time `json:"created_at"`
TermOrder int `json:"term_order"`
}
// 根据id查找taxonomy
func getTaxById(id string) *Taxonomy {
var term Taxonomy
err := db.Where("id=?", id).Find(&term).Error
if err != nil {
return nil
}
return &term
}
// 根据名称查找taxonomy
func getTaxByName(name, tax string) (*Taxonomy, error) {
var term Taxonomy
err := db.Where("name=? AND taxonomy=?", name, tax).Find(&term).Error
if err == gorm.RecordNotFound {
err = nil
}
return &term, nil
}
// 获取文章分类的大类
func getAllCategory() ([]*Taxonomy, error) {
var terms []*Taxonomy
err := db.Where("taxonomy=?", "category").Find(&terms).Error
if err == gorm.RecordNotFound {
err = nil
}
return terms, err
}
// 获取一级类别下的所有子类
func getAllTaxs(tax string) ([]*Taxonomy, error) {
var terms []*Taxonomy
err := db.Where("taxonomy=?", tax).Find(&terms).Error
if err == gorm.RecordNotFound {
err = nil
}
return terms, err
}
// 获取某个类别下的所有post id
func getObjectsByTerm(term *Taxonomy, start, count int) ([]*TermRelation, error) {
var rel []*TermRelation
err := db.Where("term_id=?", term.Id).Offset(start).Order("created_at desc").Limit(count).Find(&rel).Error
fmt.Println(term.Name, term.Id, rel)
return rel, err
}
// 设置post的分类
func setPostTaxonmoy(post *Post, taxes []*Taxonomy) error {
var (
tr TermRelation
now = time.Now()
err error
)
for _, tax := range taxes {
tr = TermRelation{
ObjectId: post.Id,
TermId: tax.Id,
CreatedAt: now,
}
err = db.Create(&tr).Error
if err != nil {
glog.Error("set post %s to taxonomy %s (%s %s) failed: %s\n",
post.Id, tax.Id, tax.Name)
}
}
return err
}
// 从[]TaxInfo中查找对应的Taxonomy, 并去重
func getTaxFromInfos(infos []TaxInfo) []*Taxonomy {
var (
err error
tax *Taxonomy
taxesMap = make(map[*Taxonomy]struct{})
taxes = []*Taxonomy{}
)
for _, taxInfo := range infos {
//首先根据tax_id来查找
if taxInfo.TaxId != "" {
if taxInfo.TaxId == "0" {
tax = &UnCategory
} else {
tax = getTaxById(taxInfo.TaxId)
}
taxesMap[tax] = struct{}{}
} else {
// 根据taxonomy和name来查找
tax, err = getTaxByName(taxInfo.TaxName, taxInfo.Taxonomy)
if err != nil {
glog.Error("Not found category by name %s taxonomy %s\n",
taxInfo.TaxName, taxInfo.Taxonomy)
} else {
taxesMap[tax] = struct{}{}
}
}
}
// 使用map来过滤可能出现的重复taxonomy问题
for key, _ := range taxesMap {
taxes = append(taxes, key)
}
// 如果没有找到任何分类,设置为未分类
if len(taxes) == 0 {
taxes = []*Taxonomy{&UnCategory}
}
return taxes
}
// 创建分类
func createTaxonomy(tax *Taxonomy, user *User) (err error) {
if user == nil {
return fmt.Errorf("create taxonomy NEED authencation")
}
user.parseUserCap()
if user.capability["create_taxonomy"] == false {
return fmt.Errorf("user %s no authorization to create taxonomy", user.Name)
}
return db.Create(tax).Error
}