(prd period, limit int, orderField string, withCache ...bool)
| 115 | } |
| 116 | |
| 117 | func (*BookCounter) _sort(prd period, limit int, orderField string, withCache ...bool) (books []SortedBook) { |
| 118 | field := "vcnt" // 浏览 |
| 119 | if orderField != "vcnt" { |
| 120 | field = "star" // 收藏 |
| 121 | } |
| 122 | |
| 123 | if prd == PeriodAll { |
| 124 | books2 := NewBook().Sorted(limit, field) |
| 125 | for _, book := range books2 { |
| 126 | cnt := book.Vcnt |
| 127 | if field != "vcnt" { |
| 128 | cnt = book.Star |
| 129 | } |
| 130 | books = append(books, SortedBook{ |
| 131 | BookId: book.BookId, |
| 132 | Identify: book.Identify, |
| 133 | Cover: strings.ReplaceAll(book.Cover, "\\", "/"), |
| 134 | BookName: book.BookName, |
| 135 | Cnt: cnt, |
| 136 | }) |
| 137 | } |
| 138 | return |
| 139 | } |
| 140 | |
| 141 | var b []byte |
| 142 | |
| 143 | cache := false |
| 144 | if len(withCache) > 0 { |
| 145 | cache = withCache[0] |
| 146 | } |
| 147 | |
| 148 | file := fmt.Sprintf(bookCounterCacheFmt, string(prd)+"-"+field, limit) |
| 149 | |
| 150 | if cache { |
| 151 | if info, err := os.Stat(file); err == nil && time.Now().Sub(info.ModTime()).Seconds() <= cacheTime { |
| 152 | // 文件存在,且在缓存时间内 |
| 153 | if b, err = ioutil.ReadFile(file); err == nil { |
| 154 | json.Unmarshal(b, &books) |
| 155 | if len(books) > 0 { |
| 156 | return |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | sqlSort := "SELECT sum(c.view_cnt) as cnt,b.book_id,b.identify,b.cover,b.book_name FROM `md_book_counter` c left JOIN md_books b on b.book_id=c.bid WHERE c.day>=? and c.day<=? and b.order_index>=0 and b.privately_owned=0 GROUP BY c.bid ORDER BY cnt desc limit ?" |
| 163 | if field == "star" { |
| 164 | sqlSort = "SELECT sum(c.star_cnt) as cnt,b.book_id,b.identify,b.cover,b.book_name FROM `md_book_counter` c left JOIN md_books b on b.book_id=c.bid WHERE c.day>=? and c.day<=? and b.order_index>=0 and b.privately_owned=0 GROUP BY c.bid ORDER BY cnt desc limit ?" |
| 165 | } |
| 166 | |
| 167 | start, end := getTimeRange(time.Now(), prd) |
| 168 | orm.NewOrm().Raw(sqlSort, start, end, limit).QueryRows(&books) |
| 169 | |
| 170 | if cache && len(books) > 0 { |
| 171 | b, _ = json.Marshal(books) |
| 172 | ioutil.WriteFile(file, b, os.ModePerm) |
| 173 | } |
| 174 |
no test coverage detected