report.js
2.21 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
const _ = require('lodash')
const MysqlAdapter = require('./mysql')
const config = require('../config/index')
const logger = global.logger
const mysql = new MysqlAdapter(config.database.connect, config.database.database)
const sknCache = {}
const sendMessage = (tick) => {
if (!_sender) {
return;
}
try {
_sender.addMessage({
measurement: 'spider_ufo_success_report',
tags: {
type: 'ufo'
},
fields: {
tick
}
});
} catch (e) {
console.error(e);
}
};
module.exports = async (data) => {
let dataId = 0
const {productId, sizeId, price, time} = data
const cachekey = `p:${productId}_s:${sizeId}`
if (sknCache[cachekey]) {
dataId = sknCache[cachekey].id
}
if (!dataId) {
try {
const result = await mysql.query('SELECT * FROM `channel_sku_compare` WHERE `product_id` = :productId AND `size_id` = :sizeId', {
productId,
sizeId
})
if (result.length) {
dataId =_.first(result).id
} else {
logger.error(`channel_sku_compare表未找到对应的数据, productId: ${productId}, sizeId: ${sizeId}`)
// mysql.insert('INSERT `channel_sku_compare` (`product_id`, `sku`, `size_id`, `channel_price`, `update_time`) VALUES (:productId, 0, :sizeId, 0, 0)', {
// productId,
// sizeId
// })
return Promise.resolve()
}
} catch(error) {
logger.error(`channel_sku_compare表查询失败: ${error.message}`)
return Promise.resolve()
}
}
try {
const changedRows = await mysql.update('UPDATE `channel_sku_compare` SET `channel_price` = :channelPrice, `update_time` = :updateTime WHERE `id` = :dataId', {
channelPrice: price,
updateTime: time / 1000,
dataId
})
if (changedRows <= 0) {
logger.error(`更新channel_sku_compare表失败 dataId: ${dataId} ${JSON.stringify(data)}`)
} else {
console.log(`productId: ${productId}, sizeId: ${sizeId}, price: ${price}, changedRows: ${changedRows}`)
sendMessage(changedRows)
}
} catch (error) {
logger.error(`更新channel_sku_compare表错误: ${JSON.stringify(data)}, ${error.message}`)
}
return Promise.resolve()
}