expandModel.js
5.28 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const _ = require('lodash');
const model = require('../../../lib/model');
const MysqlPromise = require('../../../lib/mysql-promise');
const Promise = require('bluebird');
const keywordsRedis = {
'keywordsAllIds': `global:yoho:seo:keywords:allIds`,// 关键词表符合条件所有id
'keywordsSortId': `global:yoho:seo:keywords:sortId`,// 分类下的所有关键词
'keywordsId': `global:yoho:seo:keywords:id`,// 每条关键词关联其它12条关键词
'keywordsIsRun': `global:yoho:seo:keywords:isRun`,// 判断是否正在执行中
};
class expandModel extends model {
constructor(ctx) {
super(ctx);
this.mysql = new MysqlPromise();
this.redis = ctx.redis;
}
getRanData() {
return this.redis.setAsync(`${keywordsRedis.keywordsIsRun}`, 1).then(() => {// 设置正在运行的key
return this.redis.expireAsync(`${keywordsRedis.keywordsIsRun}`, 10800);// 设置正在运行的key有效期
}).then(() => {
return this.sortIdInit();
}).then(rdata => {
return this.msortInit();
}).then(() => {
return this.allIdsExec(1);
}).then(() => {
return this.redis.delAsync(`${keywordsRedis.keywordsIsRun}`);
}).then(() => {
return true;
});
}
//处理小分类function
sortIdInit() {
return this.mysql.query(`SELECT sort_id FROM seo_keywords WHERE status = 1 AND sort_id > 0 GROUP BY sort_id;`).then(rdata => {
return Promise.each(rdata, (item) => this.sortIdExec(item.sort_id, 1));
});
}
sortIdExec(sortId, page) {
let pageSize = 1000;
let pageStart = (page - 1) * pageSize;
return this.sleep(5000).then(() => {
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};`);
}).then(dsort => {
if (dsort.length <= 0) {
return false;
}
return this.execRandData(dsort).then(() => {
return this.redis.setAsync(`${keywordsRedis.keywordsSortId}:${sortId}:page:${page}`, JSON.stringify(dsort));
}).then(() => {
dsort = [];
return this.sortIdExec(sortId, ++page);
});
});
}
//处理小分类为0且大分类>=0的function
msortInit() {
return this.mysql.query(`SELECT msort FROM seo_keywords WHERE status = 1 AND sort_id = 0 GROUP BY msort;`).then(rdata => {
return Promise.each(rdata, (item) => this.msortExec(item.msort, 1));
});
}
msortExec(sortId, page) {
let pageSize = 1000;
let pageStart = (page - 1) * pageSize;
return this.sleep(5000).then(() => {
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};`);
}).then(dsort => {
if (dsort.length <= 0) {
return false;
}
/**
* 1、小分类为0且大分类大于0,要随机关联12词
* 2、小分类为0且大分类小于0,不要随机关联12个词。即大分类和小分类都为0的情况,data: [];
*/
return this.execRandData(dsort).then(() => {
dsort = [];
return this.msortExec(sortId, ++page);
});
});
}
//随机关联12个关键词
execRandData(dsort) {
let len = dsort.length;
let key;
let tdata;
if (len <= 0) {
return Promise.resolve(false);
}
return Promise.each(dsort, (el, index) => {
key = `${keywordsRedis.keywordsId}:${el.id}`;
tdata = {
name: el.keyword,
data: _.compact(_.map(this.getRandom(dsort, index), (mval) => {
return dsort[mval];
})) || []
};
console.log(`execRandData, index: ${index}, len: ${len}, id: ${el.id}, keywords: ${el.keyword}, key: ${key}`);
return this.redis.setAsync(key, JSON.stringify(tdata));
});
}
getRandom(dsort, index) {
return _.shuffle(_.difference(_.keys(dsort), [`${index}`])).slice(0, 12);
}
//符合条件和关键词设置redis中
allIdsExec(page) {
let pageSize = 40000;
let pageStart = (page - 1) * pageSize;
return this.mysql.query(`SELECT id FROM seo_keywords WHERE status = 1 AND yoho_goods_num > 3 LIMIT ${pageStart}, ${pageSize};`).then(dsort => {
if (dsort.length <= 0) {
return false;
}
let ids = _.map(dsort, (sort) => {
return sort.id;
});
return this.redis.setAsync(`${keywordsRedis.keywordsAllIds}:page:${page}`, JSON.stringify(ids)).then(() => {
dsort = [];
ids = [];
return this.allIdsExec(++page);
});
});
}
sleep(time) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, time);
});
}
}
module.exports = expandModel;