du-data.js 1.76 KB
const Nedb = require('nedb-promise');
const path = require('path');
const _ = require('lodash');
const dayjs = require('dayjs');

const db = new Nedb({
  filename: path.join(__dirname, 'db/data.json'),
  autoload: true
});

const ids = [20, 11172, 11538, 11653, 9536, 9431, 2358, 11621, 11408, 14546, 11746, 2355, 8815, 9670, 8708];

Promise.all(ids.map(id => {
  return db.cfind({productId: id}).sort({date: 1}).exec().then(result => {
    const firstbuys = _.first(result).lastSoldList.map(r => r.icon);
    const first = _.first(result);
    const last = _.last(result);
    const buys = new Set(_.flatten(_.map(result, r => r.lastSoldList.map(r => r.icon))));
    const intrBuys = Array.from(buys).filter(r => !_.some(firstbuys, f => f === r));

    const group = _.groupBy(result, 'soldNum');


    const dates = _.map(Object.keys(group).sort(), (k) => {
      const item = _.first(group[k]);

      return item;
    });

    for (let i = 0; i < dates.length; i++) {
      if (dates[i + 1]) {
        const day = dayjs(dates[i].date);
        const nextDay = dayjs(dates[i + 1].date);
      }
    }

    let intrMax = 0;

    for (let i = 0; i < result.length; i++) {
      if (i < result.length - 1) {
        const intr = result[i + 1].soldNum - result[i].soldNum;
        const day = dayjs(result[i].date);
        const nextDay = dayjs(result[i + 1].date);

        if (intr >= 1) {
          // console.log(result[i + 1].date, nextDay.format('YYYY-MM-DD HH:mm'), result[i].date, intr)
        }

        intrMax = intr > intrMax ? intr : intrMax;
      }
    }

    return `soldNum${last.soldNum - first.soldNum}, buysNum${intrBuys.length}, eq${last.soldNum - first.soldNum === intrBuys.length}, productId${id}, max${intrMax}`;
  });
})).then(result => {
  console.log(result);
});