price-180day-task.js 1.17 KB
const _ = require('lodash');
const dayjs = require('dayjs');
const {mysqlPool} = require('../../libs/mysql');
const {logger} = require('../../libs/logger');

module.exports = async() => {
  const insertTime = dayjs().startOf('day');
  const leastDay = await mysqlPool.query('SELECT MAX(`create_time`) AS create_time FROM `price_trend_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) {
      return;
    }
  }
  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-line-task] 180day insert ${affectedRows}`);
    }
  }
};