const db = require('../db');
const xlsx = require('node-xlsx').default;
const fs = require('fs');
const _ = require('lodash');
const moment = require('moment');
async function task(argument) {
	
  const account = await db.getCollection('account');
  const dspRecognition = await db.getCollection('dspRecognition');
  const group = await db.getCollection('dspCallGroups');
  let group_arrs = await group.find({}).limit(1000000000000).toArray();
  let groupMap = {}

  let data = [['unikey','订单ID','tag1','tag2','tag3','tag4','tag5','tag6','tag7','反馈标签']];
  let _count = 0;
  let _arrs = await account.find({}).limit(1000000000000).toArray();
  let accountMap = {};
  _arrs.forEach(({email,company,_id}) => {
    accountMap[ _id + ''] = email + '[' + company + ']'
  })
  let arrs = await dspRecognition.find({createdDate:{'$gte':'20181008','$gte':'20181014'}}).limit(1000000000000).toArray();
  console.dir(arrs.length);
  let map = {};

  arrs.forEach( async ({ tags, _id, unikey, groupID, billID, remark, systemTags, calledInfo, accountID }) => {
    data.push([unikey,billID,tags[0],tags[1],tags[2],tags[3],tags[4],tags[5],tags[6],(systemTags && systemTags.length ? systemTags.join(',') : '')])
    console.dir((++_count) + '==' + arrs.length);
    if(_count === arrs.length) {
        console.dir('ii');
        var buffer = xlsx.build([{name: "mySheetName", data: data}]);
        fs.writeFile('./标签.xlsx', buffer, function(err) {
          console.dir(err + '====>')
              if (err) {
                  throw err;
              }
              console.log('Hello.');
        });
    }
  });

}


task();


