const models = require('../models');

const firstOpenCount = 2000; // 初始开放2000个内容


async function init() {
  let done = 0;
  let duration = 0;
  let hour, minute, second;
  let start = Date.now();
  let total = await models.Art.countDocuments();
  console.log('total:', total);

  models.Art
    .find()
    .sort({ lastMod: 1 })
    .cursor()
    .eachAsync(job)
    .catch(err => console.error(err.message))
    .then(() => require('process').exit(0))

  async function job(doc) {
    if (done >= firstOpenCount) doc.open = false;
    else doc.open = true;
    await doc.save();

    done++;
    duration = (Date.now() - start) / 1000;
    hour = (Math.floor(duration / 60 / 60)).toString().padStart(2, '0');
    minute = (Math.floor(duration / 60) % 60).toString().padStart(2, '0');
    second = (Math.floor(duration) % 60).toString().padStart(2, '0');

    console.log('progress: ' + Math.floor((100 * done / total)) + '% used time: ' + hour + ':' + minute + ':' + second);

  }
}

module.exports = { init }


if (require.main == module) {
  init();
}