import { md_redPoint_check } from '../api_s2c/gongyu/mingdao/ApiOpen';
import { Queue } from '../shared/public/queue';


export type CollectionWjjl = k_v<number>;

export class Wjjl {
    static queue = new Queue();
    static async setVal(uid: string, type: string, val: number, change = true) {
        this.queue.enqueue(async () => {
            if (change) {
                let d = await G.mongodb.collection('wjjl').findOne({ uid: uid });
                let des = 'up';
                if (type == 'jjc_rank') {
                    des = 'down';
                }
                if (!d?.[type] || (des == 'up' ? val > d[type] : val < d[type])) {
                    if (type == 'power') {
                        md_redPoint_check(G.server.uid_connections[uid], type);
                    }
                    await G.mongodb.collection('wjjl').updateOne({ uid: uid }, { $set: G.mongodb.createTreeObj({ key: type, val: val }) }, { upsert: true });
                }
            } else {
                await G.mongodb.collection('wjjl').updateOne({ uid: uid }, { $inc: G.mongodb.createTreeObj({ key: type, val: val }) }, { upsert: true });
            }
        });
    }
    static async getVal(uid: string, type: string) {
        return (await G.mongodb.collection('wjjl').findOne({ uid: uid }))?.[type] || 0;
    }
}