import fs from "fs";
import {MongoClient} from "mongodb";


async function start() {
    let mongodbUrl = "mongodb://blacklagoon:lffu2bD%5eGn2%5eE%2bE7@blacklagoon-mongo-secondary.pro.g123-cpp.com:3717,blacklagoon-mongo-primary.pro.g123-cpp.com:3717?replicaSet=mgset-351738825"
    //连接mongodb
    let client = await MongoClient.connect(mongodbUrl);

    console.log('链接成功');

    let user2email: { [uid: string]: { [title: string]: { prize: any, get_num: number, unget_num: number } } } = {};
    for (let i = 1; i < 49; i++) {
        let dbName = `blacklagoon_s${i}`
        console.log(dbName)

        let mongodb = client.db(dbName);
        let emails = await mongodb.collection("email").find({
            title: {$regex: "wzry"}
        }).toArray();

        for (let i = 0; i < emails.length; i++) {
            let email = emails[i];
            if (!email.prizeData?.prize) continue;
            if (!user2email[email.uid]) {
                user2email[email.uid] = {};
            }
            if (!user2email[email.uid][email.title]) {
                user2email[email.uid][email.title] = {
                    prize: email.prizeData.prize,
                    get_num: 0,
                    unget_num: 0,
                }
            }
            if (email.prizeData.isGet) {
                user2email[email.uid][email.title].get_num += 1;
            } else {
                user2email[email.uid][email.title].unget_num += 1;
            }
        }
    }
    fs.writeFileSync('email.json', JSON.stringify(user2email, null, 2))
}

start().then(() => {
    process.exit()
});
