price-180day-task.js
1.77 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
const _ = require('lodash');
const dayjs = require('dayjs');
const {mysqlPool} = require('../../libs/mysql');
const {sendMessage} = require('../../libs/influx-report');
const {logger} = require('../../libs/logger');
module.exports = async() => {
let insertTime = dayjs().startOf('day');
const least180Day = await mysqlPool.query('SELECT MAX(`create_time`) AS create_time FROM `price_trend_half_year`');
if (least180Day.length) {
const halfYearTime = _.first(least180Day).create_time;
if (halfYearTime && insertTime.diff(dayjs(halfYearTime * 1000), 'day') < 3) {
logger.info(`[price-180day-task] skip date: ${insertTime.unix()}`);
return;
} else {
insertTime = dayjs(halfYearTime * 1000).add(3, 'day');
}
}
const resultExists = await mysqlPool.query('SELECT 1 AS Num FROM `price_trend_half_year` WHERE `create_time` = :createTime limit 1', {
createTime: insertTime.unix()
});
if (resultExists.length > 0) {
logger.info(`[price-180day-task] exists createtime price: ${insertTime.unix()}`);
return;
}
const leastDay = await mysqlPool.query('SELECT MAX(`create_time`) AS create_time FROM `price_trend_day`');
if (leastDay.length) {
const dayTime = _.first(leastDay).create_time;
if (dayTime) {
const {affectedRows} = await mysqlPool.execute('INSERT INTO `price_trend_half_year` (`product_id`, `size_id`, `skn_price`, `sku_price`, `create_time`) SELECT `product_id`, `size_id`, `skn_price`, `sku_price`, :insertTime FROM `price_trend_day` WHERE `create_time` = :createTime', {
createTime: dayTime,
insertTime: insertTime.unix()
});
logger.info(`[price-180day-task] insert ${affectedRows}`);
sendMessage(affectedRows, new Date().getTime() * 1000000, 'price-180day-task-inserted');
}
}
};