const db = require('../db');
const xlsx = require('node-xlsx').default;
const fs = require('fs');
const _ = require('lodash');
const url = require('url');
const moment = require('moment');
let max = 0;
async function task(argument) {
	
  const account = await db.getCollection('account');
  const bills = await db.getCollection('bills');
  const group = await db.getCollection('dspCallGroups');
  let group_arrs = await group.find({}).limit(1000000000000).toArray();
  let groupMap = {}
  let tagMap = {}
  let tagMap1 = {}
  let listMap = {}
  let cityMap = {}
  let appMap = {}
  let hostMap = {}
  let keyMap = {}
  let zhuMap = {}
   let zhuMap1 = {}
  group_arrs.forEach((data) => {
    if(data && data._id) {
     groupMap[data._id + ''] = data.dspgroup;
    }
    // max = Math.max(max,data['applist'].length);
    console.dir(data.dspgroup);
    listMap[data._id + ''] =  data['hostlist'] || [];
    zhuMap[data._id + ''] =  (data['hostlist'] || []).map( x => url.parse(x).host || x);
    zhuMap1[data._id + ''] =  zhuMap[data._id + ''].map( x => x.replace(/www./, ""));
    cityMap[data._id + ''] = (data['citylist'] || []).concat(data['provincelist'] || [])
    keyMap[data._id + ''] = data['interestlist']
    hostMap[data._id + ''] = data['hostlist']
    appMap[data._id + ''] = data['applist']
    for(var k in data) {
      if(k !== '_id' && k !== 'dspgroup') {

        if(k !== 'applist' && k !== 'hostlist' && k !== 'interestlist')
          tagMap[data._id + ''] = (tagMap[data._id + ''] || []).concat(Array.isArray(data[k]) ? data[k] : [data[k]])
        else 
          tagMap1[data._id + ''] = (tagMap1[data._id + ''] || []).concat(Array.isArray(data[k]) ? data[k] : [data[k]]) 
      }
    }
  })
  console.dir(groupMap);

  let data = [['id','公司','meta','提交时间','用户组','地区','关键词','域名','主域名','去www域名','app','预计','实际','状态']];
  //let data = [];
  let ids= [
 "5bf639b62e4fb2000782715c",
"5bf4fba52e4fb20007826ffb",
"5bf4d1d92e4fb20007826fb4",
"5bf4ce2a2e4fb20007826fa7",
"5bf38c8e2e4fb20007826da4",
"5bf3719c2e4fb20007826cdb",
"5bf36dc52e4fb20007826cb7",
"5bf368cb2e4fb20007826c91",
"5bf3666e2e4fb20007826c7c",
"5bf35c112e4fb20007826c3a",
"5bf25a1e2e4fb20007826b15",
"5bf22c502e4fb20007826a94",
"5bf2191a2e4fb20007826a28",
"5bee77a7e3ec3033c82d7ff2",
"5bee3dfe2e4fb200078266f1",
"5bee3c092e4fb200078266e4",
"5bee36e82e4fb200078266bd",
"5bee2e7e2e4fb200078266a4",
"5bee2e532e4fb200078266a0",
"5bee2777c5ed930007209d93",
"5bed5b85c5ed930007209d0a",
"5bed47f8c5ed930007209cf6",
"5bed22e8e17798293e8d883c",
"5bed1eedc5ed930007209c0c",
"5bece2f2537c8d0007db60b1",
"5becde87537c8d0007db60a1",
"5becdaf3537c8d0007db6085",
"5beaaf4c537c8d0007db5d94",
"5bea8eb8537c8d0007db5ce8",
"5bea3997232b830007e19cfc",
"5bea2af7232b830007e19c6c",
"5be95630232b830007e19bc7",
"5be93109232b830007e19b06",
"5be8e3bd232b830007e199bf"].map(x => db.ObjectID(x) );
  let _arrs = await account.find({ }).limit(1000000000000).toArray();
  let accountMap = {};
  _arrs.forEach(({email,company,_id}) => {
    accountMap[ _id + ''] = email + '[' + company + ']'
  })

  let _count = 0;

/*let metas = {
  meta: {

  }
}*/
  let arrs = await bills.find({type:{ $in:["dspbuynumber","dspbuynumber_ctcc"] }}).limit(1000000000000).toArray();
  let map = {};
  console.dir(arrs.length);
  arrs.forEach( async ({date, _id, groupID, meta, accountID, preNum, info, pre }) => {
/*    if(groupID) {
      let group = groupID + '';
      data.push([ _id+'', meta,  groupMap[group], tagMap[group] ])
    }*/
    let group = groupID + '';
    data.push([ _id+'',  accountMap[accountID + ''],  meta, date, groupMap[group], cityMap[group], keyMap[group], hostMap[group], zhuMap[group], zhuMap1[group],  appMap[group],  preNum/3, info ? info.qty : 0, pre ? '进行中' : '已关闭' ])

    // data.push([ _id+'',meta].concat(listMap[group]));



    ++_count;
    //console.dir((++_count) + '==' + arrs.length);
    if(_count === arrs.length) {
        console.dir('ii');
        var buffer = xlsx.build([{name: "mySheetName", data: data}]);
        fs.writeFile('./订单hanhan.xlsx', buffer, function(err) {
          console.dir(err + '====>')
              if (err) {
                  throw err;
              }
              console.log('Hello.');
        });
    }


  });

}


task();


