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

const spider = require('../libs/spider');
// const sknsData = require('../data/skns.json');
const legoData = require('../data/lego.json');
const { logger } = require('../libs/logger');

const { getUfoSpecialSizeProducts, getsSizeData, handleChannelSkuCompare, queryAllChannelProductRelations } = require('../libs/ufo-product-api');
const { getSize } = require('../utils');

const ddAlert = require('../libs/dingding-alert');
const moment = require('moment');

module.exports = async(time) => {
  var beginTime =  moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
  ddAlert(`监控提醒 : 比价开始  ${beginTime}  `, 'bjSpider');
  let sizeData = {};
  let specialSizeProducts = {};
  let sizeRelation = {};
  let productRelationsArr = {};
  let sknsData = [];
  let n = 0; // 接口请求次数的初始值


  try {
    // 获取所有要比价的商品对应关系
    const productRelations = await queryAllChannelProductRelations();
    if (productRelations) {
      productRelationsArr = JSON.parse(productRelations).data;
    }
    // 获取特殊的商品
    const resultSpecialSizeProducts = await getUfoSpecialSizeProducts();
    if (resultSpecialSizeProducts) {
      specialSizeProducts = JSON.parse(resultSpecialSizeProducts).data;
    }
    // 获取尺码
    const resultSizeData = await getsSizeData();
    if (resultSizeData) {
      sizeData = JSON.parse(resultSizeData).data;
    }
  }catch(error) {
    ddAlert(`监控提醒 : pop 服务接口异常,请查看! @15101660386 `, 'bjSpider');
    return false;
  }
  //判断pop服务接口是否为空
  if(sizeData.length < 1 || specialSizeProducts.length < 1 || productRelationsArr.length < 1) {
    ddAlert(`监控提醒 : pop 接口获取数据为空,请查看! @15101660386 `, 'bjSpider');
    return false;
  }

  try {
    // 重新组织数据
    productRelationsArr.forEach(relations => {
      sknsData.push({'productId' : relations.channelProductId, 'yhId': relations.productId});
    });

    sizeData.forEach(size => {
      sizeRelation[_.trim(size.sizeName)] = {
        sizeId: size.id
      };
    });
  } catch (error) {
    ddAlert(`监控提醒 : pop 接口获取数据为空,请查看! @15101660386 `, 'bjSpider');
    return false;
  }

  logger.info(`[spiderTask] time: ${beginTime}, product_number: ${sknsData && sknsData.length}`);
  spider(sknsData.map(p => p.productId), '', {}, 3500)
    .forEach(promise => promise.then(result => {
      n++;
      if (n === sknsData.length) {
        const endTime =  moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
        ddAlert(`监控提醒 : 比价结束  ${endTime}  `, 'bjSpider');
      }
      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 isSpecialSize = specialSizeProducts.includes(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;

          handleChannelSkuCompare({
            productId: yhProduct.yhId,
            sizeId: find.sizeId,
            channelPrice: s.item.price / 100,
            highRate
          }).then((ret) => {
            const res = JSON.parse(ret);
            if( res.code === 200) {
              return logger.info(`[spider-task]更新sku价格成功记录, duId:${detail.productId}, yhId: ${yhProduct.yhId}, sizeId: ${find.sizeId}, highRate: ${highRate}`);
            }
            return logger.error(res.message);
          }).catch(error => {
            logger.error(error.message);
          });
        }
      });
    }));
};