|
|
'use strict';
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
const model = require('../../../lib/model');
|
|
|
const MysqlPromise = require('../../../lib/mysql-promise');
|
|
|
const Promise = require('bluebird');
|
|
|
|
|
|
const keywordsRedis = {
|
|
|
'keywordsAllIds': `golobal:yoho:seo:keywords:allIds`,// 关键词表符合条件所有id
|
|
|
'keywordsSortId': `golobal:yoho:seo:keywords:sortId`,// 分类下的所有关键词
|
|
|
'keywordsId': `golobal:yoho:seo:keywords:id`,// 每条关键词关联其它12条关键词
|
|
|
};
|
|
|
|
|
|
class expandModel extends model {
|
|
|
|
|
|
constructor(ctx) {
|
|
|
super(ctx);
|
|
|
|
|
|
this.mysql = new MysqlPromise();
|
|
|
this.redis = ctx.redis;
|
|
|
}
|
|
|
|
|
|
getRanData() {
|
|
|
this.dataIds = [];
|
|
|
|
|
|
return this.mysql.query(`SELECT sort_id FROM seo_keywords WHERE status = 1 GROUP BY sort_id;`).then(rdata => {
|
|
|
return Promise.each(rdata, (item) => this._setSortToRedis(item.sort_id));
|
|
|
}).then(d => {
|
|
|
return this.redis.setAsync(`${keywordsRedis.keywordsAllIds}`, JSON.stringify(this.dataIds));
|
|
|
}).then(d => {
|
|
|
this.dataIds = [];
|
|
|
return d;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
_setSortToRedis(sortId) {
|
|
|
return this.mysql.query(`SELECT id, keyword, root_id, brand_id, sort_id FROM seo_keywords WHERE status = 1 AND yoho_goods_num > 3 AND sort_id=${sortId};`).then(dsort => {
|
|
|
let len = dsort.length;
|
|
|
let key;
|
|
|
let tdata;
|
|
|
|
|
|
if (len <= 0) {
|
|
|
return Promise.resolve(true);
|
|
|
}
|
|
|
|
|
|
return this.redis.setAsync(`${keywordsRedis.keywordsSortId}:${sortId}`, JSON.stringify(dsort)).then(() => {
|
|
|
return Promise.each(dsort, (el, index) => {
|
|
|
key = `${keywordsRedis.keywordsId}:${el.id}`;
|
|
|
|
|
|
this.dataIds.push(el.id);
|
|
|
|
|
|
tdata = {
|
|
|
name: el.keyword,
|
|
|
data: _.compact(_.map(this._getRandom(dsort, index), (mval) => {
|
|
|
return dsort[mval];
|
|
|
}))
|
|
|
};
|
|
|
|
|
|
console.log(`setSortToRedis, index: ${index}, len: ${len}, id: ${el.id}, keywords: ${el.keyword}, key: ${key}`);
|
|
|
return this.redis.setAsync(key, JSON.stringify(tdata));
|
|
|
});
|
|
|
}).then((d) => {
|
|
|
dsort = [];
|
|
|
return true;
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
|
|
|
_getRandom(dsort, index) {
|
|
|
return _.shuffle(_.difference(_.keys(dsort), [`${index}`])).slice(0, 12);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
module.exports = expandModel;
|
|
|
|
...
|
...
|
|