var express = require('express');
var router = express.Router();
var request = require('superagent');
var dbUtils = require('../utils/dbUtils');

/* GET ranking listing. */
router.get('/', function(req, res, next) {
    var isAjax = !!req.headers['x-requested-with'];
    var pageNo = req.query.p;
    pageNo = !!pageNo && Number(pageNo) > 0 ? Number(pageNo) : 1;
    var pageSize = 12; // pageSize
    dbUtils.commonQuery(`select count(0) as sum from bing where !ISNULL(qiniu_url) || qiniu_url<>""`, function(rows) {
        var count = rows[0]['sum'] || 0;
        if (count > 0) {
            var page = {
                size: pageSize,
                count: count,
                pageCount: Math.ceil(count / pageSize),
                next: pageNo + 1 > Math.ceil(count / pageSize) ? Math.ceil(count / pageSize) : pageNo + 1,
                prev: pageNo - 1 > 0 ? pageNo - 1 : 1,
                curr: pageNo > Math.ceil(count / pageSize) ? Math.ceil(count / pageSize) : pageNo,
                currText: pageNo === 1 ? '首页' : '第' + pageNo + '页',
                currPage: 'ranking'
            }
            if (pageNo > page.curr && !isAjax) {
                res.redirect(`/?p=${page.curr}`);
            }
            var sql = `select id,title,attribute,description,copyright,qiniu_url as photo,city,country,continent,DATE_FORMAT(enddate, '%Y-%m-%d') as dt,likes,views,downloads from bing 
                        where !ISNULL(qiniu_url) || qiniu_url<>""
                        order by likes desc
                        limit ${(page.curr-1)*page.size},${page.size}`;
            dbUtils.commonQuery(sql, function(rs) {
                if (rs.length > 0) {
                    var data = [];
                    for (var i in rs) {
                        var temp = rs[i];
                        data.push({
                            id: temp['id'],
                            title: temp['title'],
                            attribute: temp['attribute'],
                            description: temp['description'],
                            copyright: temp['copyright'],
                            photo: temp['photo'],
                            city: temp['city'],
                            country: temp['country'],
                            continent: temp['continent'],
                            thumbnail: `https://static.ioliu.cn/bing/${temp['photo']}_800x600.jpg`,
                            smallpic: `https://static.ioliu.cn/bing/${temp['photo']}_800x600.jpg?imageView2/1/w/4/h/3/q/100`,
                            dt: temp['dt'],
                            likes: temp['likes'],
                            views: temp['views'],
                            downloads: temp['downloads']
                        });
                    }
                    if (isAjax) {
                        res.json({
                            doc: data,
                            page: page
                        });
                    } else {
                        res.render('index', {
                            doc: data,
                            page: page
                        });
                    }
                }
            });
        }
    });
});
/**
 * 如果没有参数，则跳转到首页
 */
router.get('/', function(req, res, next) {
    res.redirect('/');
});

module.exports = router;
@@ -1385,16 +1385,43 @@
 ownloads
+,thumbnail_pic,original_pic
  from bi
@@ -1424,16 +1424,16 @@
 m bing %0A
-
         
@@ -1812,16 +1812,553 @@
  rs%5Bi%5D;%0A
+                        /**%0A                         * 1024x576%0A                         * 120x67%0A                         */%0A                        var thumbnail = temp%5B'original_pic'%5D ? temp%5B'original_pic'%5D.replace('http', 'https') : %60https://static.ioliu.cn/bing/$%7Btemp%5B'photo'%5D%7D_1920x1080.jpg?imageView2/1/w/1024/h/576/q/100%60;%0A                        var smallpic = temp%5B'thumbnail_pic'%5D ? temp%5B'thumbnail_pic'%5D.replace('http', 'https') : %60https://static.ioliu.cn/bing/$%7Btemp%5B'photo'%5D%7D_1920x1080.jpg?imageView2/1/w/120/h/67/q/100%60;%0A
         
@@ -2910,193 +2910,65 @@
 il: 
-%60https://static.ioliu.cn/bing/$%7Btemp%5B'photo'%5D%7D_800x600.jpg%60,%0A                            smallpic: %60https://static.ioliu.cn/bing/$%7Btemp%5B'photo'%5D%7D_800x600.jpg?imageView2/1/w/4/h/3/q/100%60
+thumbnail,%0A                            smallpic: smallpic
 ,%0A  

change some cdn