Authored by 陈峰

ranom y100

... ... @@ -692,6 +692,38 @@ const article = {
},
/**
* Y100文章列表-随机
* @param req
* @param res
* @param next
*/
async y100RandomList(req, res, next) {
const {actId, num} = req.query;
if (!actId || num > 100) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
try {
const result = await req.ctx(ArticleModel).articleY100RandomList({
actId,
num
});
return res.json({
code: 200,
data: result,
message: GET_SUCCESS
});
} catch (err) {
return next(err);
}
},
/**
* Y100详情
* @param req
* @param res
... ...
... ... @@ -509,6 +509,57 @@ class ArticleModel extends global.yoho.BaseModel {
}
/**
* 获取Y100列表-随机
* @returns {*}
*/
async articleY100RandomList({actId, num}) {
const params = {
actId,
};
let maxSql = `
SELECT
max(\`index\`) as maxIndex
FROM ${TABLE_ACT_ARTICLE_Y100} AAY
WHERE AAy.act_id = :actId`;
const maxResult = await mysqlCli.query(maxSql, params);
let maxIndex = maxResult[0].maxIndex;
if (num > maxIndex) {
throw '随机数量超过数据库大小';
}
const ids = [],
selectIds = [];
for (let i = 0; i <= maxIndex; i++) {
ids.push(i);
}
for (let i = 0; i < num; i++) {
const index = parseInt(Math.random() * ids.length, 10);
selectIds.push(ids[index]);
ids.splice(index, 1);
}
let sql = `
SELECT
AA.id,
AA.good_count,
AA.create_time,
AAY.name,
AAY.img_url
FROM ${TABLE_ACT_ARTICLE} AA
INNER JOIN ${TABLE_ACT_ARTICLE_Y100} AAY ON AA.id = AAY.article_id
WHERE AA.act_id = :actId`;
sql += ` and AAY.index in (${selectIds.join(',')})`;
sql += ` order by field(AAY.index,${selectIds.join(',')});`;
return mysqlCli.query(sql, params);
}
/**
* 获取Y100详情
* @returns {*}
*/
... ... @@ -525,8 +576,10 @@ class ArticleModel extends global.yoho.BaseModel {
AAY.style,
AAY.interest,
AAY.skns,
AAY.create_time
AAY.create_time,
AA.good_count
FROM ${TABLE_ACT_ARTICLE_Y100} AAY
INNER JOIN ${TABLE_ACT_ARTICLE} AA ON AAY.article_id = AA.id
where AAY.id = :id`;
const query = await mysqlCli.query(sql, {
... ...
... ... @@ -11,6 +11,7 @@ const article = require('./controllers/article');
router.post('/like', article.like);
router.get('/list', article.list);
router.get('/y100list', article.y100List);
router.get('/y100randomlist', article.y100RandomList);
router.get('/y100detail', article.y100Detail);
router.get('/listNoImg', article.listNoImg);
router.get('/listNoUser', article.listNoUser);
... ...
... ... @@ -17,5 +17,6 @@ CREATE TABLE IF NOT EXISTS act_article_y100 (
`style` varchar(200) DEFAULT '',
`interest` varchar(200) DEFAULT '',
`skns` varchar(1000) DEFAULT '',
`index` int(8) NOT NULL DEFAULT 0,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) DEFAULT CHARSET=utf8;
... ...