spider-task.js 3.66 KB
const _ = require('lodash');

const spider = require('../libs/spider');
const report = require('../libs/report');
const sknsData = require('../data/skns.json');
const legoData = require('../data/lego.json');
const {mysqlPool} = require('../libs/mysql');
const {logger} = require('../libs/logger');
const {sendMessage} = require('../libs/influx-report');
const {getSpecialSizeProducts, getSize} = require('../utils');

// const {SknRecord} = require('../models');
// const md5 = require('yoho-md5');

// async function clearChannelSknCompare() {
//   const newSkns = sknsData.map(skn => skn.yhId);

//   const deleteRows = await mysqlPool.delete(`delete from \`channel_sku_compare\` where product_id not in (${newSkns.join(',')})`);

//   logger.warn(`[spider-task] 清理了 ${deleteRows} 条`);
// }

module.exports = async(time) => {
  const specialSizeProducts = await getSpecialSizeProducts();
  const sizeData = await mysqlPool.query('select `size_name`,min(`id`) as id from `size` where `size_name` <> \'-\' group by `size_name`');
  const sizeRelation = {};

  sizeData.forEach(size => {
    sizeRelation[_.trim(size.size_name)] = {
      sizeId: size.id
    };
  });

  // 另外有一个全量任务,这里不能删掉sknsData以外的skn
  // const record = await SknRecord.findOne();
  // const leastMd5 = md5(JSON.stringify(sknsData));

  // if (!record || record.md5 !== leastMd5) { // skns有更新
  //   logger.warn('[spider-task] 更新skns');
  //   clearChannelSknCompare();
  //   if (record) {
  //     SknRecord.removeById(record._id);
  //   }
  //   SknRecord.insert({
  //     md5: leastMd5
  //   });
  // }

  logger.info(`[spiderTask] time: ${time}, product_number: ${sknsData && sknsData.length}`);

  spider(sknsData.map(p => p.productId), '', {}, 1)
    .forEach(promise => promise.then(result => {
      if (result.status !== 200 && !_.has(result, 'data.detail')) {
        logger.warn(`[spider-task]爬取失败记录: ${JSON.stringify(result)}`);
        return;
      }

      const {data: {detail, sizeList}} = result;
      const yhProduct = sknsData.find(p => p.productId === detail.productId);
      const hrtime = process.hrtime();
      const timestamp = parseInt(Date.now() / 1000, 10) * 1000000000;

      const isSpecialSize = Boolean(specialSizeProducts[yhProduct.yhId]);

      sizeList.forEach(s => {
        if (_.isPlainObject(s.item) && s.item.price > 0) {

          if (sizeList.length === 1) {
            const isLego = legoData.some(lego => lego.productId === detail.productId);

            if (isLego) {
              s.size = 'F';
              console.log(`is lego size = F, ${detail.productId}`);
            }
          }
          const find = getSize(s.size, sizeRelation, isSpecialSize);

          if (!find) {
            return logger.error(`[spider-task]爬取尺码对应关系未找到: ${JSON.stringify(s)}`);
          }
          if (isSpecialSize) {
            console.log(`=================>spider-task yhId: ${yhProduct.yhId}, duId: ${detail.productId} size: ${s.size} => ${find.relationSize || s.size}, ${find.sizeId}`);
          }
          const highRate = 0.02;

          report({
            productId: yhProduct.yhId,
            sizeId: find.sizeId,
            price: s.item.price / 100,
            highRate,
            time
          }).then(() => {
            const reportTime = timestamp + process.hrtime(hrtime)[1];

            logger.info(`[spider-task]更新sku价格成功记录, duId:${detail.productId}, yhId: ${yhProduct.yhId}, sizeId: ${find.sizeId}, time: ${time}, highRate: ${highRate}`);
            sendMessage(1, reportTime);
          }).catch(error => {
            logger.error(error.message);
          });
        }
      });
    }));
};