CountCategory 统计书籍分类
()
| 197 | |
| 198 | // CountCategory 统计书籍分类 |
| 199 | func CountCategory() { |
| 200 | var ( |
| 201 | count []Count |
| 202 | tableCate = "md_category" |
| 203 | err error |
| 204 | ) |
| 205 | |
| 206 | o := orm.NewOrm() |
| 207 | o.Begin() |
| 208 | defer func() { |
| 209 | if err != nil { |
| 210 | beego.Error(err) |
| 211 | o.Rollback() |
| 212 | } else { |
| 213 | o.Commit() |
| 214 | } |
| 215 | }() |
| 216 | |
| 217 | 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" |
| 218 | o.Raw(sql).QueryRows(&count) |
| 219 | |
| 220 | // 重置为0 |
| 221 | if _, err = o.QueryTable(tableCate).Filter("id__gt", 0).Update(orm.Params{"cnt": 0}); err != nil { |
| 222 | beego.Error(err) |
| 223 | } |
| 224 | |
| 225 | for _, item := range count { |
| 226 | if _, err = o.QueryTable(tableCate).Filter("id", item.CategoryId).Update(orm.Params{"cnt": item.Cnt}); err != nil { |
| 227 | return |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func getTimeRange(t time.Time, prd period) (start, end string) { |
| 233 | switch prd { |
no test coverage detected