temp.js 1.13 KB
const {ProductRelationModel} = require('./models');
const lockup = require('node-lockup');
const sjApi = require('./libs/sj-api');
const _ = require('lodash');

const task = async result => {
  if (result.third.du || result.third.sj.length) {
    let sj = '';
    const du = result.third.du ? `http://du.hupu.com/mdu/product/detail.html?id=${result.third.du.productId}&source=boutiqueRecommend` : '';

    if (result.third.sj.length) {
      const res = await sjApi(`source(__id:{in: ${JSON.stringify(result.third.sj)}}){data{}}`);

      if (res.code === 0) {
        sj = _.get(res, 'result.data', []).map(item => item.url).join('|');
      } else {
        console.log(res);
      }
    }
    console.log(`${result.productId},${result.productCode},${_.trim(result.productName.replace(',', ','))},${du},${sj}`);
    return true;
  }
  return false;
};

const locktask = lockup(task, 50);

let open = false;
let inx = 0;

ProductRelationModel.findAll().then((results) => {
  results.forEach(async result => {
    if (result.productId === 10000853) {
      open = true;
    }
    if (open && inx < 300) {
      locktask(result);
      inx++;
    }
  });
});