import {patchInit} from "../patch";
// import {addGameLog, connGameLogDB} from "../gameLog";
import {MongoClient} from "mongodb";

class Path {

    async fun1(a: any) {

        let logDB = await connGameLogDB();

        console.log(11111,'开始查询')

        let ab = await logDB.collection('gameLog').aggregate([
            {$match: {"type": "kuangdong/YanShi", "req.hdid": {$lte: 21}}},
            {$group: {_id: "$uid", total: {$sum: 1}}}
        ]).toArray()

        let auids = ab.map(i => ({
            uid: i._id,
            type: 'yanshiNum',
            time: G.time,
            num: i.total,
            data: {
                ...i
            }
        }))

        console.log(auids)
        // @ts-ignore
        G.crossmongodb.collection('fupanLog').insertMany(auids)

        return auids
    }


    async run() {
        await this.fun1(1);
    }
}

async function connGameLogDB() {
    console.log('connect gamelog mongodb ......');
    let logDBUrl:string;
    if(G.config.isG123){
        logDBUrl = "mongodb://root:lffu2bD%5eGn2%5eE%2bE7@blacklagoon-mongo-log-primary.pro.g123-cpp.com:3717,blacklagoon-mongo-log-secondary.pro.g123-cpp.com:3717?replicaSet=mgset-351742307";
    }else{
        logDBUrl = "mongodb://root:lyMaple525458@10.0.1.20:27017/heijiao_gamelog?authSource=admin";
    }
    let client = await MongoClient.connect(logDBUrl,{
        maxPoolSize:10,
        maxIdleTimeMS: 5*60*1000
    });
    return client.db(`gameLog${G.config.serverId}`);
}


async function main() {
    await patchInit()
    let patch = new Path();
    await patch.run();
    console.log("逻辑执行完成，等待退出");
    setTimeout(function () {
        console.log('结束程序');
        process.exit();
    }, 3000);
}

main();
