incre-relation-task.js 1.38 KB
/**
 * 每天定时增量更新,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}`);
};