expandModel.js
2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'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条关键词
'keywordsIsRun': `golobal:yoho:seo:keywords:isRun`,// 判断是否正在执行中
};
class expandModel extends model {
constructor(ctx) {
super(ctx);
this.mysql = new MysqlPromise();
this.redis = ctx.redis;
}
getRanData() {
this.dataIds = [];
return this.redis.setAsync(`${keywordsRedis.keywordsIsRun}`, 1).then(() => {// 设置正在运行的key
return this.redis.expireAsync(`${keywordsRedis.keywordsIsRun}`, 5 * 60);// 设置正在运行的key有效期
}).then(() => {
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.redis.delAsync(`${keywordsRedis.keywordsIsRun}`);
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;