WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 29e314f

Browse files
committed
书籍分类统计优化
1 parent 3ebca48 commit 29e314f

File tree

6 files changed

+100
-41
lines changed

6 files changed

+100
-41
lines changed

change.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
# 开发日志
1010
## TODO
11-
12-
<a name="v2.0"></a>
13-
## V2.0 开发升级预告(微信小程序)
14-
- [ ] 微信小程序开发
11+
- [ ] a 标签中的英文括号统一转成中文括号
12+
- [ ] 增加文档发布的审核开关功能,避免一些用户发布垃圾文档(所有公开发布的文档都必须先审核)
13+
- [ ] 审核管理
14+
- [ ] 书籍点评审核管理
15+
- [ ] 书籍公开发布审核管理
1516
- [ ] 文档采集,replace过滤和替换采集到的网页标签和内容,图片懒加载的文档项目,也无处可逃。
1617
- [ ] 导出markdown
1718
- [ ] 在线HTML转markdown工具
@@ -31,31 +32,28 @@
3132
- [ ] 增加电子书下载管控,是否必须登录才能下载电子书
3233
- [ ] 发布队列
3334

34-
35+
36+
<a name="v2.0"></a>
37+
## V2.0 开发升级预告(微信小程序)
38+
- [ ] 微信小程序开发
3539

3640

3741
## v1.7 升级日志
38-
- [x] 书籍和文档搜索精度控制
39-
- [x] 增加`钉子`功能,把相关书籍钉在列表页首位
42+
- [x] 增加书籍和文档搜索精度控制(在管理后台可设置)
43+
- [x] 增加`钉子`功能,把想要置顶的书籍置顶在`发现`列表页首位
4044
- [x] 文档阅读,移动端展开菜单优化(右下角增加展开菜单按钮)
41-
- [x] 后台增加一个是否登录才能下载电子书的功能,用以扩大用户的注册数
4245
- [x] 跳转编辑指定文档
43-
- [x] 注释的方式,在编辑文档的时候显示采集的文档源链接,但是HTML不显示
4446
- [x] 流程图、时序图、数学公式的支持和优化
4547
- [x] 移除外部引入的js、css公共资源库,实现本地化和内网部署的优化
46-
- [x] 文章内容目录显示优化
48+
- [x] 文章`内容目录`显示优化
4749
- [x] 管理员删除书籍,需要输入管理员密码
4850
- [x] 增加书籍语种分类(中文、英文、其他)
4951
- [x] 增加首页最新推荐书籍内容
50-
- [x] 内容采集,书籍链接优化
51-
- [x] 增加关联书籍推荐功能
52+
- [x] 增加关联书籍推荐功能(需要启用elasticsearch)
5253
- [x] SEO 优化,如果文档内容图片缺少`alt`,自动使用文档标题填充
5354
- [x] 采集HTML的时候,block 里面的内容不转markdown
54-
- [ ] a 标签中的英文括号统一转成中文括号
55-
- [ ] 增加文档发布的审核开关功能,避免一些用户发布垃圾文档(所有公开发布的文档都必须先审核)
56-
- [ ] 审核管理
57-
- [ ] 书籍点评审核管理
58-
- [ ] 书籍公开发布审核管理
55+
- [x] 书籍分类统计不正确的问题
56+
- [x] 采集功能增强,自带截图功能。
5957

6058

6159

controllers/BookController.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ func (this *BookController) PrivatelyOwned() {
294294
this.JsonResult(6004, "保存失败")
295295
}
296296
go func() {
297+
models.CountCategory()
298+
297299
public := true
298300
if state == 1 {
299301
public = false

controllers/ManagerController.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ func (this *ManagerController) PrivatelyOwned() {
602602
}
603603

604604
go func() {
605+
models.CountCategory()
605606
public := true
606607
if state == 1 {
607608
public = false

models/base.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package models
22

33
import (
44
"fmt"
5-
65
"os"
76

87
"strconv"
@@ -153,3 +152,60 @@ func SitemapUpdate(domain string) {
153152
}
154153
Sitemap.CreateSitemapIndex(si, "sitemap.xml")
155154
}
155+
156+
// 统计书籍分类
157+
var counting = false
158+
159+
type Count struct {
160+
Cnt int
161+
CategoryId int
162+
}
163+
164+
func CountCategory() {
165+
if counting {
166+
return
167+
}
168+
counting = true
169+
defer func() {
170+
counting = false
171+
}()
172+
173+
var count []Count
174+
175+
o := orm.NewOrm()
176+
sql := "select count(bc.id) cnt, bc.category_id from md_book_category bc left join md_books b on b.book_id=bc.book_id where b.privately_owned=0 group by bc.category_id"
177+
o.Raw(sql).QueryRows(&count)
178+
if len(count) == 0 {
179+
return
180+
}
181+
182+
var cates []Category
183+
tableCate := "md_category"
184+
o.QueryTable(tableCate).All(&cates, "id", "pid", "cnt")
185+
if len(cates) == 0 {
186+
return
187+
}
188+
189+
var err error
190+
191+
o.Begin()
192+
defer func() {
193+
if err != nil {
194+
o.Rollback()
195+
} else {
196+
o.Commit()
197+
}
198+
}()
199+
200+
o.QueryTable(tableCate).Update(orm.Params{"cnt": 0})
201+
cateChild := make(map[int]int)
202+
for _, item := range count {
203+
if item.Cnt > 0 {
204+
cateChild[item.CategoryId] = item.Cnt
205+
_, err = o.QueryTable(tableCate).Filter("id", item.CategoryId).Update(orm.Params{"cnt": item.Cnt})
206+
if err != nil {
207+
return
208+
}
209+
}
210+
}
211+
}

models/book_category.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ package models
33
import (
44
"strconv"
55

6-
"fmt"
7-
"strings"
8-
96
"github.com/astaxie/beego/orm"
107
)
118

@@ -33,37 +30,42 @@ func (this *BookCategory) GetByBookId(book_id int) (cates []Category, rows int64
3330

3431
//处理书籍分类
3532
func (this *BookCategory) SetBookCates(bookId int, cids []string) {
36-
var (
37-
cates []Category
38-
bc []BookCategory
3933

40-
oldCids []string
34+
if len(cids) == 0 {
35+
return
36+
}
37+
38+
var (
39+
cates []Category
4140
tableCategory = "md_category"
4241
tableBookCategory = "md_book_category"
4342
)
43+
4444
o := orm.NewOrm()
45-
//1、查找当前分类的父级分类
4645
o.QueryTable(tableCategory).Filter("id__in", cids).All(&cates, "id", "pid")
46+
47+
cidMap := make(map[string]bool)
4748
for _, cate := range cates {
48-
cids = append(cids, strconv.Itoa(cate.Pid))
49+
cidMap[strconv.Itoa(cate.Pid)] = true
50+
cidMap[strconv.Itoa(cate.Id)] = true
4951
}
50-
//2、删除原有的分类关系,并减少计数
51-
qs := o.QueryTable(tableBookCategory).Filter("book_id", bookId)
52-
qs.All(&bc) //查询
53-
qs.Delete() //删除
54-
//减少计数
55-
for _, c := range bc {
56-
oldCids = append(oldCids, strconv.Itoa(c.CategoryId))
52+
cids = []string{}
53+
for cid, _ := range cidMap {
54+
cids = append(cids, cid)
5755
}
58-
SetIncreAndDecre(tableCategory, "cnt", fmt.Sprintf("id in(%v)", strings.Join(oldCids, ",")), false)
59-
//3、新增现在的分类关系,并增加计数
60-
SetIncreAndDecre(tableCategory, "cnt", fmt.Sprintf("id in(%v)", strings.Join(cids, ",")), true) //计算增加
61-
for _, cid := range cids { //这里逐条添加记录,不是批量添加,因为设置了唯一键,批量添加可能会导致全部都添加失败
56+
57+
o.QueryTable(tableBookCategory).Filter("book_id", bookId).Delete()
58+
var bcs []BookCategory
59+
for _, cid := range cids {
6260
cidNum, _ := strconv.Atoi(cid)
63-
var bookCate = BookCategory{
61+
bookCate := BookCategory{
6462
CategoryId: cidNum,
6563
BookId: bookId,
6664
}
67-
o.Insert(&bookCate)
65+
bcs = append(bcs, bookCate)
66+
}
67+
if l := len(bcs); l > 0 {
68+
o.InsertMulti(l, &bcs)
6869
}
70+
go CountCategory()
6971
}

utils/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import (
4646

4747
//更多存储类型有待扩展
4848
const (
49-
Version = "1.7 beta6"
49+
Version = "1.7"
5050
StoreLocal string = "local"
5151
StoreOss string = "oss"
5252
)

0 commit comments

Comments
 (0)