Authored by 陈峰

ranom y100

@@ -692,6 +692,38 @@ const article = { @@ -692,6 +692,38 @@ const article = {
692 }, 692 },
693 693
694 /** 694 /**
  695 + * Y100文章列表-随机
  696 + * @param req
  697 + * @param res
  698 + * @param next
  699 + */
  700 + async y100RandomList(req, res, next) {
  701 + const {actId, num} = req.query;
  702 +
  703 + if (!actId || num > 100) {
  704 + return res.json({
  705 + code: 400,
  706 + message: INVALID_PARAMS
  707 + });
  708 + }
  709 +
  710 + try {
  711 + const result = await req.ctx(ArticleModel).articleY100RandomList({
  712 + actId,
  713 + num
  714 + });
  715 +
  716 + return res.json({
  717 + code: 200,
  718 + data: result,
  719 + message: GET_SUCCESS
  720 + });
  721 + } catch (err) {
  722 + return next(err);
  723 + }
  724 + },
  725 +
  726 + /**
695 * Y100详情 727 * Y100详情
696 * @param req 728 * @param req
697 * @param res 729 * @param res
@@ -509,6 +509,57 @@ class ArticleModel extends global.yoho.BaseModel { @@ -509,6 +509,57 @@ class ArticleModel extends global.yoho.BaseModel {
509 } 509 }
510 510
511 /** 511 /**
  512 + * 获取Y100列表-随机
  513 + * @returns {*}
  514 + */
  515 + async articleY100RandomList({actId, num}) {
  516 + const params = {
  517 + actId,
  518 + };
  519 + let maxSql = `
  520 + SELECT
  521 + max(\`index\`) as maxIndex
  522 + FROM ${TABLE_ACT_ARTICLE_Y100} AAY
  523 + WHERE AAy.act_id = :actId`;
  524 +
  525 + const maxResult = await mysqlCli.query(maxSql, params);
  526 + let maxIndex = maxResult[0].maxIndex;
  527 +
  528 + if (num > maxIndex) {
  529 + throw '随机数量超过数据库大小';
  530 + }
  531 + const ids = [],
  532 + selectIds = [];
  533 +
  534 + for (let i = 0; i <= maxIndex; i++) {
  535 + ids.push(i);
  536 + }
  537 +
  538 + for (let i = 0; i < num; i++) {
  539 + const index = parseInt(Math.random() * ids.length, 10);
  540 +
  541 + selectIds.push(ids[index]);
  542 + ids.splice(index, 1);
  543 + }
  544 +
  545 + let sql = `
  546 + SELECT
  547 + AA.id,
  548 + AA.good_count,
  549 + AA.create_time,
  550 + AAY.name,
  551 + AAY.img_url
  552 + FROM ${TABLE_ACT_ARTICLE} AA
  553 + INNER JOIN ${TABLE_ACT_ARTICLE_Y100} AAY ON AA.id = AAY.article_id
  554 + WHERE AA.act_id = :actId`;
  555 +
  556 + sql += ` and AAY.index in (${selectIds.join(',')})`;
  557 + sql += ` order by field(AAY.index,${selectIds.join(',')});`;
  558 +
  559 + return mysqlCli.query(sql, params);
  560 + }
  561 +
  562 + /**
512 * 获取Y100详情 563 * 获取Y100详情
513 * @returns {*} 564 * @returns {*}
514 */ 565 */
@@ -525,8 +576,10 @@ class ArticleModel extends global.yoho.BaseModel { @@ -525,8 +576,10 @@ class ArticleModel extends global.yoho.BaseModel {
525 AAY.style, 576 AAY.style,
526 AAY.interest, 577 AAY.interest,
527 AAY.skns, 578 AAY.skns,
528 - AAY.create_time 579 + AAY.create_time,
  580 + AA.good_count
529 FROM ${TABLE_ACT_ARTICLE_Y100} AAY 581 FROM ${TABLE_ACT_ARTICLE_Y100} AAY
  582 + INNER JOIN ${TABLE_ACT_ARTICLE} AA ON AAY.article_id = AA.id
530 where AAY.id = :id`; 583 where AAY.id = :id`;
531 584
532 const query = await mysqlCli.query(sql, { 585 const query = await mysqlCli.query(sql, {
@@ -11,6 +11,7 @@ const article = require('./controllers/article'); @@ -11,6 +11,7 @@ const article = require('./controllers/article');
11 router.post('/like', article.like); 11 router.post('/like', article.like);
12 router.get('/list', article.list); 12 router.get('/list', article.list);
13 router.get('/y100list', article.y100List); 13 router.get('/y100list', article.y100List);
  14 +router.get('/y100randomlist', article.y100RandomList);
14 router.get('/y100detail', article.y100Detail); 15 router.get('/y100detail', article.y100Detail);
15 router.get('/listNoImg', article.listNoImg); 16 router.get('/listNoImg', article.listNoImg);
16 router.get('/listNoUser', article.listNoUser); 17 router.get('/listNoUser', article.listNoUser);
@@ -17,5 +17,6 @@ CREATE TABLE IF NOT EXISTS act_article_y100 ( @@ -17,5 +17,6 @@ CREATE TABLE IF NOT EXISTS act_article_y100 (
17 `style` varchar(200) DEFAULT '', 17 `style` varchar(200) DEFAULT '',
18 `interest` varchar(200) DEFAULT '', 18 `interest` varchar(200) DEFAULT '',
19 `skns` varchar(1000) DEFAULT '', 19 `skns` varchar(1000) DEFAULT '',
  20 + `index` int(8) NOT NULL DEFAULT 0,
20 `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP 21 `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
21 ) DEFAULT CHARSET=utf8; 22 ) DEFAULT CHARSET=utf8;