incre-relation-task.js
1.38 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
/**
* 每天定时增量更新,UFO商品到爬虫关系表中
*/
const {logger} = require('../../libs/logger');
const {sendMessage} = require('../../libs/influx-report');
const {mysqlPool} = require('../../libs/mysql');
const {ProductRelationModel, DuDataModel, ShenjianModel} = require('../../models');
module.exports = async() => {
const [duProducts, sjProduct] = await Promise.all([
DuDataModel.findAll(),
ShenjianModel.findAll(),
]);
const ufoProducts = await mysqlPool.query(`
select
p.id,p.product_name,p.product_code
from
product p
where
shelve_status = 1
`);
const relations = ufoProducts.map(product => {
const duFind = duProducts.find(dp => dp.articleNumber === product.product_code);
const sjFinds = sjProduct.filter(dp => dp.model === product.product_code);
return {
productId: product.id,
productName: product.product_name,
productCode: product.product_code,
third: {
ufo: {
productId: product.id
},
du: duFind ? {
productId: duFind.productId
} : void 0,
sj: sjFinds.map(f => f.id)
}
};
});
await ProductRelationModel.remove({});
ProductRelationModel.insert(...relations);
sendMessage(relations.length, new Date().getTime() * 1000000, 'incre-realtion-task');
logger.info(`[incre-relation-task] incre-relation ${relations.length}`);
};