|
@@ -6,10 +6,10 @@ const MysqlPromise = require('../../../lib/mysql-promise'); |
|
@@ -6,10 +6,10 @@ const MysqlPromise = require('../../../lib/mysql-promise'); |
6
|
const Promise = require('bluebird');
|
6
|
const Promise = require('bluebird');
|
7
|
|
7
|
|
8
|
const keywordsRedis = {
|
8
|
const keywordsRedis = {
|
9
|
- 'keywordsAllIds': `golobal:yoho:seo:keywords:allIds`,// 关键词表符合条件所有id
|
|
|
10
|
- 'keywordsSortId': `golobal:yoho:seo:keywords:sortId`,// 分类下的所有关键词
|
|
|
11
|
- 'keywordsId': `golobal:yoho:seo:keywords:id`,// 每条关键词关联其它12条关键词
|
|
|
12
|
- 'keywordsIsRun': `golobal:yoho:seo:keywords:isRun`,// 判断是否正在执行中
|
9
|
+ 'keywordsAllIds': `global:yoho:seo:keywords:allIds`,// 关键词表符合条件所有id
|
|
|
10
|
+ 'keywordsSortId': `global:yoho:seo:keywords:sortId`,// 分类下的所有关键词
|
|
|
11
|
+ 'keywordsId': `global:yoho:seo:keywords:id`,// 每条关键词关联其它12条关键词
|
|
|
12
|
+ 'keywordsIsRun': `global:yoho:seo:keywords:isRun`,// 判断是否正在执行中
|
13
|
};
|
13
|
};
|
14
|
|
14
|
|
15
|
class expandModel extends model {
|
15
|
class expandModel extends model {
|
|
@@ -22,59 +22,131 @@ class expandModel extends model { |
|
@@ -22,59 +22,131 @@ class expandModel extends model { |
22
|
}
|
22
|
}
|
23
|
|
23
|
|
24
|
getRanData() {
|
24
|
getRanData() {
|
25
|
- this.dataIds = [];
|
|
|
26
|
return this.redis.setAsync(`${keywordsRedis.keywordsIsRun}`, 1).then(() => {// 设置正在运行的key
|
25
|
return this.redis.setAsync(`${keywordsRedis.keywordsIsRun}`, 1).then(() => {// 设置正在运行的key
|
27
|
- return this.redis.expireAsync(`${keywordsRedis.keywordsIsRun}`, 5 * 60);// 设置正在运行的key有效期
|
26
|
+ return this.redis.expireAsync(`${keywordsRedis.keywordsIsRun}`, 10800);// 设置正在运行的key有效期
|
28
|
}).then(() => {
|
27
|
}).then(() => {
|
29
|
- return this.mysql.query(`SELECT sort_id FROM seo_keywords WHERE status = 1 GROUP BY sort_id;`);
|
28
|
+ return this.sortIdInit();
|
30
|
}).then(rdata => {
|
29
|
}).then(rdata => {
|
31
|
- return Promise.each(rdata, (item) => this._setSortToRedis(item.sort_id));
|
|
|
32
|
- }).then(d => {
|
|
|
33
|
- return this.redis.setAsync(`${keywordsRedis.keywordsAllIds}`, JSON.stringify(this.dataIds));
|
|
|
34
|
- }).then(d => {
|
30
|
+ return this.msortInit();
|
|
|
31
|
+ }).then(() => {
|
|
|
32
|
+ return this.allIdsExec(1);
|
|
|
33
|
+ }).then(() => {
|
|
|
34
|
+ return this.redis.delAsync(`${keywordsRedis.keywordsIsRun}`);
|
|
|
35
|
+ }).then(() => {
|
|
|
36
|
+ return true;
|
|
|
37
|
+ });
|
|
|
38
|
+ }
|
|
|
39
|
+
|
|
|
40
|
+ //处理小分类function
|
|
|
41
|
+ sortIdInit() {
|
|
|
42
|
+ return this.mysql.query(`SELECT sort_id FROM seo_keywords WHERE status = 1 AND sort_id > 0 GROUP BY sort_id;`).then(rdata => {
|
|
|
43
|
+ return Promise.each(rdata, (item) => this.sortIdExec(item.sort_id, 1));
|
|
|
44
|
+ });
|
|
|
45
|
+ }
|
|
|
46
|
+ sortIdExec(sortId, page) {
|
|
|
47
|
+ let pageSize = 1000;
|
|
|
48
|
+ let pageStart = (page - 1) * pageSize;
|
|
|
49
|
+
|
|
|
50
|
+ return this.sleep(5000).then(() => {
|
|
|
51
|
+ 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} LIMIT ${pageStart}, ${pageSize};`);
|
|
|
52
|
+ }).then(dsort => {
|
|
|
53
|
+ if (dsort.length <= 0) {
|
|
|
54
|
+ return false;
|
|
|
55
|
+ }
|
35
|
|
56
|
|
36
|
- this.redis.delAsync(`${keywordsRedis.keywordsIsRun}`);
|
57
|
+ return this.execRandData(dsort).then(() => {
|
|
|
58
|
+ return this.redis.setAsync(`${keywordsRedis.keywordsSortId}:${sortId}:page:${page}`, JSON.stringify(dsort));
|
|
|
59
|
+ }).then(() => {
|
|
|
60
|
+ dsort = [];
|
|
|
61
|
+ return this.sortIdExec(sortId, ++page);
|
|
|
62
|
+ });
|
|
|
63
|
+ });
|
|
|
64
|
+ }
|
37
|
|
65
|
|
38
|
- this.dataIds = [];
|
|
|
39
|
- return d;
|
66
|
+ //处理小分类为0且大分类>=0的function
|
|
|
67
|
+ msortInit() {
|
|
|
68
|
+ return this.mysql.query(`SELECT msort FROM seo_keywords WHERE status = 1 AND sort_id = 0 GROUP BY msort;`).then(rdata => {
|
|
|
69
|
+ return Promise.each(rdata, (item) => this.msortExec(item.msort, 1));
|
40
|
});
|
70
|
});
|
41
|
}
|
71
|
}
|
|
|
72
|
+ msortExec(sortId, page) {
|
|
|
73
|
+ let pageSize = 1000;
|
|
|
74
|
+ let pageStart = (page - 1) * pageSize;
|
|
|
75
|
+ return this.sleep(5000).then(() => {
|
|
|
76
|
+ return this.mysql.query(`SELECT id, keyword, root_id, brand_id, sort_id FROM seo_keywords WHERE status = 1 AND sort_id = 0 AND yoho_goods_num > 3 AND msort=${sortId} LIMIT ${pageStart}, ${pageSize};`);
|
|
|
77
|
+ }).then(dsort => {
|
|
|
78
|
+ if (dsort.length <= 0) {
|
|
|
79
|
+ return false;
|
|
|
80
|
+ }
|
42
|
|
81
|
|
43
|
- _setSortToRedis(sortId) {
|
|
|
44
|
- 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 => {
|
82
|
+ /**
|
|
|
83
|
+ * 1、小分类为0且大分类大于0,要随机关联12词
|
|
|
84
|
+ * 2、小分类为0且大分类小于0,不要随机关联12个词。即大分类和小分类都为0的情况,data: [];
|
|
|
85
|
+ */
|
|
|
86
|
+ return this.execRandData(dsort).then(() => {
|
|
|
87
|
+ dsort = [];
|
|
|
88
|
+ return this.msortExec(sortId, ++page);
|
|
|
89
|
+ });
|
|
|
90
|
+ });
|
|
|
91
|
+ }
|
|
|
92
|
+
|
|
|
93
|
+ //随机关联12个关键词
|
|
|
94
|
+ execRandData(dsort) {
|
45
|
let len = dsort.length;
|
95
|
let len = dsort.length;
|
46
|
let key;
|
96
|
let key;
|
47
|
let tdata;
|
97
|
let tdata;
|
48
|
|
98
|
|
49
|
if (len <= 0) {
|
99
|
if (len <= 0) {
|
50
|
- return Promise.resolve(true);
|
100
|
+ return Promise.resolve(false);
|
51
|
}
|
101
|
}
|
52
|
|
102
|
|
53
|
- return this.redis.setAsync(`${keywordsRedis.keywordsSortId}:${sortId}`, JSON.stringify(dsort)).then(() => {
|
|
|
54
|
return Promise.each(dsort, (el, index) => {
|
103
|
return Promise.each(dsort, (el, index) => {
|
55
|
key = `${keywordsRedis.keywordsId}:${el.id}`;
|
104
|
key = `${keywordsRedis.keywordsId}:${el.id}`;
|
56
|
|
105
|
|
57
|
- this.dataIds.push(el.id);
|
|
|
58
|
-
|
|
|
59
|
tdata = {
|
106
|
tdata = {
|
60
|
name: el.keyword,
|
107
|
name: el.keyword,
|
61
|
- data: _.compact(_.map(this._getRandom(dsort, index), (mval) => {
|
108
|
+ data: _.compact(_.map(this.getRandom(dsort, index), (mval) => {
|
62
|
return dsort[mval];
|
109
|
return dsort[mval];
|
63
|
- }))
|
110
|
+ })) || []
|
64
|
};
|
111
|
};
|
65
|
|
112
|
|
66
|
- console.log(`setSortToRedis, index: ${index}, len: ${len}, id: ${el.id}, keywords: ${el.keyword}, key: ${key}`);
|
113
|
+ console.log(`execRandData, index: ${index}, len: ${len}, id: ${el.id}, keywords: ${el.keyword}, key: ${key}`);
|
67
|
return this.redis.setAsync(key, JSON.stringify(tdata));
|
114
|
return this.redis.setAsync(key, JSON.stringify(tdata));
|
68
|
});
|
115
|
});
|
69
|
- }).then((d) => {
|
116
|
+ }
|
|
|
117
|
+
|
|
|
118
|
+ getRandom(dsort, index) {
|
|
|
119
|
+ return _.shuffle(_.difference(_.keys(dsort), [`${index}`])).slice(0, 12);
|
|
|
120
|
+ }
|
|
|
121
|
+
|
|
|
122
|
+ //符合条件和关键词设置redis中
|
|
|
123
|
+ allIdsExec(page) {
|
|
|
124
|
+ let pageSize = 40000;
|
|
|
125
|
+ let pageStart = (page - 1) * pageSize;
|
|
|
126
|
+
|
|
|
127
|
+ return this.mysql.query(`SELECT id FROM seo_keywords WHERE status = 1 AND yoho_goods_num > 3 LIMIT ${pageStart}, ${pageSize};`).then(dsort => {
|
|
|
128
|
+ if (dsort.length <= 0) {
|
|
|
129
|
+ return false;
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ let ids = _.map(dsort, (sort) => {
|
|
|
133
|
+ return sort.id;
|
|
|
134
|
+ });
|
|
|
135
|
+
|
|
|
136
|
+ return this.redis.setAsync(`${keywordsRedis.keywordsAllIds}:page:${page}`, JSON.stringify(ids)).then(() => {
|
70
|
dsort = [];
|
137
|
dsort = [];
|
71
|
- return true;
|
138
|
+ ids = [];
|
|
|
139
|
+ return this.allIdsExec(++page);
|
72
|
});
|
140
|
});
|
73
|
});
|
141
|
});
|
74
|
}
|
142
|
}
|
75
|
|
143
|
|
76
|
- _getRandom(dsort, index) {
|
|
|
77
|
- return _.shuffle(_.difference(_.keys(dsort), [`${index}`])).slice(0, 12);
|
144
|
+ sleep(time) {
|
|
|
145
|
+ return new Promise((resolve, reject) => {
|
|
|
146
|
+ setTimeout(() => {
|
|
|
147
|
+ resolve();
|
|
|
148
|
+ }, time);
|
|
|
149
|
+ });
|
78
|
}
|
150
|
}
|
79
|
}
|
151
|
}
|
80
|
|
152
|
|