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 = {}
  group_arrs.forEach((data) => {
    if(data && data._id) {
     groupMap[data._id + ''] = data.dspgroup;
    }
  })
  console.dir(groupMap);

  let data = [['unikey','账单','客户','拨打次数','入库时间','用户标签1','用户标签2','用户标签3','用户标签4','用户标签5','标签','备注']];
  let _count = 0;
  let _arrs = await account.find({}).limit(1000000000000).toArray();
  let accountMap = {};
  _arrs.forEach(({email,company,_id}) => {
    accountMap[ _id + ''] = email + '[' + company + ']'
  })
  console.dir(accountMap);
  console.dir('begin ====>');
  let arrs = await dspRecognition.find({ billID: { '$in' : ['5bd2d6e0d3ee0a0007fd4722']} }).limit(1000000000000).toArray();
  console.dir(arrs.length);
  arrs.forEach( async ({ tags,createdDate, unikey, _id, groupID, billID, remark, systemTags, calledInfo, accountID }) => {
    let [ tag1,tag2,tag3,tag4,tag5] = tags;
    data.push([unikey, billID, accountMap[accountID], calledInfo && calledInfo.length ? calledInfo.length : 0, createdDate,tag1,tag2,tag3,tag4,tag5, (systemTags || []).join('、'), (remark || []).join('、')])
    console.dir((++_count) + '==' + arrs.length);
    if(_count === arrs.length) {
        console.dir('ii');
        console.dir(data.length);
        var buffer = xlsx.build([{name: "mySheetName", data: data}]);
        fs.writeFile('./dong3.xlsx', buffer, function(err) {
          console.dir(err + '====>')
              if (err) {
                  throw err;
              }
              console.log('Hello.');
        });
    }


  });

}


task();


