export.js 1.55 KB
const fs = require('fs');
const readline = require('readline');
const dayjs = require('dayjs');
const _ = require('lodash');
const date = process.argv[2];



const fileName = date ? date.match(/--d=(.*)/)[1] : dayjs().format('YYYY-MM-DD');
const yesFileName = date ? dayjs(date.match(/--d=(.*)/)[1]).subtract(1, 'day').format('YYYY-MM-DD') : dayjs().subtract(1, 'day').format('YYYY-MM-DD');

const fr = fs.createReadStream(`/Data/logs/node/prices/${fileName}.log`);
const yesfr = fs.createReadStream(`/Data/logs/node/prices/${yesFileName}.log`);


const rl = readline.createInterface({
  input: fr
});
const yesrl = readline.createInterface({
  input: yesfr
});


const allProducts = [];
const yesallProducts = [];

let product300 = [];


const diffDay = (products, yesAllProducts) => {
  console.log('duid,sales,model,price');
  products.forEach(findP => {
    const yesFindP = yesAllProducts.find(p => p.productId === findP.productId);

    if (findP && yesFindP) {
      console.log(`${findP.productId},${findP.soldNum - yesFindP.soldNum},${findP.articleNumber},${findP.price}`);
    }
  });
};


rl.on('line', line => {
  allProducts.push(JSON.parse(line));
});

rl.on('close', async() => {
  product300 = _.take(_.orderBy(allProducts.filter(p => p.brandId !== 4981 && p.brandId !== 10065), 'soldNum', 'desc'), 300);

  if (yesallProducts.length) {
    diffDay(product300, yesallProducts);
  }
});

yesrl.on('line', line => {
  yesallProducts.push(JSON.parse(line));
});

yesrl.on('close', async() => {
  if (product300.length) {
    diffDay(product300, yesallProducts);
  }
});