Skip to content

Commit 86ed023

Browse files
committed
书籍分类统计
1 parent a83de60 commit 86ed023

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

controllers/BookController.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (this *BookController) Setting() {
185185
this.TplName = "book/setting.html"
186186
}
187187

188-
//保存书籍信息
188+
// SaveBook 保存书籍信息
189189
func (this *BookController) SaveBook() {
190190

191191
bookResult, err := this.IsPermission()
@@ -260,7 +260,7 @@ func (this *BookController) SaveBook() {
260260
beego.Error(errSearch.Error())
261261
}
262262
}()
263-
263+
go models.CountCategory()
264264
this.JsonResult(0, "ok", bookResult)
265265
}
266266

@@ -687,7 +687,7 @@ func (this *BookController) Delete() {
687687
beego.Error(errDel.Error())
688688
}
689689
}()
690-
690+
go models.CountCategory()
691691
this.JsonResult(0, "ok")
692692
}
693693

controllers/ManagerController.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ func (this *ManagerController) DeleteBook() {
516516
beego.Error(errDel.Error())
517517
}
518518
}
519-
519+
go models.CountCategory()
520520
this.JsonResult(0, "书籍删除成功")
521521
}
522522

models/base.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -189,27 +189,17 @@ func SitemapUpdate(domain string) {
189189
Sitemap.CreateSitemapIndex(si, "sitemap.xml")
190190
}
191191

192-
// 统计书籍分类
193-
var counting = false
194-
195192
type Count struct {
196193
Cnt int
197194
CategoryId int
198195
}
199196

197+
// CountCategory 统计书籍分类
200198
func CountCategory() {
201-
if counting {
202-
return
203-
}
204-
counting = true
205-
defer func() {
206-
counting = false
207-
}()
208-
209199
var count []Count
210200

211201
o := orm.NewOrm()
212-
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"
202+
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 and bc.category_id>0 group by bc.category_id"
213203
o.Raw(sql).QueryRows(&count)
214204
if len(count) == 0 {
215205
return
@@ -227,21 +217,23 @@ func CountCategory() {
227217
o.Begin()
228218
defer func() {
229219
if err != nil {
220+
beego.Error(err)
230221
o.Rollback()
231222
} else {
232223
o.Commit()
233224
}
234225
}()
235226

236-
o.QueryTable(tableCate).Update(orm.Params{"cnt": 0})
237227
cateChild := make(map[int]int)
228+
if _, err = o.QueryTable(tableCate).Filter("id__gt", 0).Update(orm.Params{"cnt": 0}); err != nil {
229+
return
230+
}
231+
238232
for _, item := range count {
239-
if item.Cnt > 0 {
240-
cateChild[item.CategoryId] = item.Cnt
241-
_, err = o.QueryTable(tableCate).Filter("id", item.CategoryId).Update(orm.Params{"cnt": item.Cnt})
242-
if err != nil {
243-
return
244-
}
233+
cateChild[item.CategoryId] = item.Cnt
234+
_, err = o.QueryTable(tableCate).Filter("id", item.CategoryId).Update(orm.Params{"cnt": item.Cnt})
235+
if err != nil {
236+
return
245237
}
246238
}
247239
}

views/book/setting.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@
127127
<span class="input-group-btn">
128128
<button class="btn btn-default" type="button">广告标题</button>
129129
</span>
130-
<input type="text" placeholder="选填" value="{{.Model.AdTitle}}" name="ad_title" id="author" class="form-control">
130+
<input type="text" placeholder="选填" value="{{.Model.AdTitle}}" name="ad_title" class="form-control">
131131
<span class="input-group-btn">
132132
<button class="btn btn-default" style="border-left: 0px;border-right: 0px;border-radius: 0px;" type="button">广告链接</button>
133133
</span>
134-
<input type="text" placeholder="选填" value="{{.Model.AdLink}}" name="ad_link" id="author_url" class="form-control">
134+
<input type="text" placeholder="选填" value="{{.Model.AdLink}}" name="ad_link" class="form-control">
135135
</div>
136136
<p class="text help-block">文字广告,可以放原作者的打赏和赞助二维码链接,也可以放开源书籍的购买链接以支持作者</p>
137137
</div>

0 commit comments

Comments
 (0)