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 dspCallGroups = await db.getCollection('dspCallGroups');
  const dspRecognition = await db.getCollection('dspRecognition');

  let data = [['recogid','用户组','标签1','标签2','标签3','标签4','客户标签','客户备注']];
  let _count = 0;
  let arrs = await dspRecognition.find({ billID: '5bbef0e09fd6a700073e7928' }).limit(1000000000000).toArray();
  console.dir(arrs);
  arrs.forEach( async ({ tags, _id, groupID,remark = [], systemTags = [] }) => {
  /*  let areas =  provincelist.concat(citylist)
    data.push([dspgroup,areas.join(","),interestlist.join(",")]);
    console.dir(_count + '====>');*/
    let group = await dspCallGroups.findOne({_id:  db.ObjectID(groupID) });
     data.push([_id+'',(group ? group.dspgroup : ''), tags[0], tags[1], tags[2], tags[3], systemTags.join(','), remark.join(',')]);
    _count ++;
    if(_count === arrs.length) {
       //console.dir(JSON.stringify(data));
       var buffer = xlsx.build([{name: "mySheetName", data: data}]);
       fs.writeFile('./dongdong.xlsx', buffer, function(err) {
            if (err) {
                throw err;
            }
            console.log('Hello.');
        });
    }

  });
 
}



task();


