(limit int, prd period, withCache ...bool)
| 239 | } |
| 240 | |
| 241 | func (m *Sign) SortedByPeriod(limit int, prd period, withCache ...bool) (members []Member) { |
| 242 | |
| 243 | var b []byte |
| 244 | cache := false |
| 245 | if len(withCache) > 0 { |
| 246 | cache = withCache[0] |
| 247 | } |
| 248 | file := fmt.Sprintf(signCacheFmt, "month-"+prd, limit) |
| 249 | if cache { |
| 250 | if info, err := os.Stat(file); err == nil && time.Now().Sub(info.ModTime()).Seconds() <= cacheTime { |
| 251 | // 文件存在,且在缓存时间内 |
| 252 | if b, err = ioutil.ReadFile(file); err == nil { |
| 253 | json.Unmarshal(b, &members) |
| 254 | if len(members) > 0 { |
| 255 | return |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | sqlSort := "SELECT t.uid member_id,count(t.id) total_sign,m.account,m.avatar,m.nickname FROM `md_sign` t left JOIN md_members m on t.uid=m.member_id WHERE t.day>=? and t.day<=? GROUP BY t.uid ORDER BY total_sign desc limit ?" |
| 262 | start, end := getTimeRange(time.Now(), prd) |
| 263 | orm.NewOrm().Raw(sqlSort, start, end, limit).QueryRows(&members) |
| 264 | if cache && len(members) > 0 { |
| 265 | b, _ = json.Marshal(members) |
| 266 | ioutil.WriteFile(file, b, os.ModePerm) |
| 267 | } |
| 268 | return |
| 269 | } |
no test coverage detected