处理书籍分类
(bookId int, cids []string)
| 30 | |
| 31 | //处理书籍分类 |
| 32 | func (this *BookCategory) SetBookCates(bookId int, cids []string) { |
| 33 | |
| 34 | if len(cids) == 0 { |
| 35 | return |
| 36 | } |
| 37 | |
| 38 | var ( |
| 39 | cates []Category |
| 40 | tableCategory = "md_category" |
| 41 | tableBookCategory = "md_book_category" |
| 42 | ) |
| 43 | |
| 44 | o := orm.NewOrm() |
| 45 | o.QueryTable(tableCategory).Filter("id__in", cids).All(&cates, "id", "pid") |
| 46 | |
| 47 | cidMap := make(map[string]bool) |
| 48 | for _, cate := range cates { |
| 49 | cidMap[strconv.Itoa(cate.Pid)] = true |
| 50 | cidMap[strconv.Itoa(cate.Id)] = true |
| 51 | } |
| 52 | cids = []string{} |
| 53 | for cid, _ := range cidMap { |
| 54 | cids = append(cids, cid) |
| 55 | } |
| 56 | |
| 57 | o.QueryTable(tableBookCategory).Filter("book_id", bookId).Delete() |
| 58 | var bcs []BookCategory |
| 59 | for _, cid := range cids { |
| 60 | cidNum, _ := strconv.Atoi(cid) |
| 61 | bookCate := BookCategory{ |
| 62 | CategoryId: cidNum, |
| 63 | BookId: bookId, |
| 64 | } |
| 65 | bcs = append(bcs, bookCate) |
| 66 | } |
| 67 | if l := len(bcs); l > 0 { |
| 68 | o.InsertMulti(l, &bcs) |
| 69 | } |
| 70 | go CountCategory() |
| 71 | } |
no test coverage detected